Skip to content

Commit

Permalink
Merge pull request #3262 from nextcloud/test/listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
provokateurin authored Oct 1, 2024
2 parents 17dbd61 + dedc5ec commit 6af52c0
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 16 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -113,6 +120,10 @@ jobs:
echo "SET GLOBAL sql_mode=(SELECT CONCAT(@@sql_mode,',ONLY_FULL_GROUP_BY'));" | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword
echo 'SELECT @@sql_mode;' | mysql -h 127.0.0.1 -P 4444 -u root -prootpassword
- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -131,6 +142,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=mysql --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-oci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -121,6 +128,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -139,6 +150,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=oci --database-name=XE --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=autotest --database-pass=owncloud --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-pgsql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -111,6 +118,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -129,6 +140,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=pgsql --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
12 changes: 12 additions & 0 deletions .github/workflows/phpunit-sqlite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ jobs:
repository: nextcloud/server
ref: ${{ matrix.server-versions }}

- name: Checkout Circles
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
repository: nextcloud/circles
ref: master
path: apps/circles

- name: Checkout app
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
Expand All @@ -100,6 +107,10 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Circles dependencies
working-directory: apps/circles
run: composer i

- name: Check composer file existence
id: check_composer
uses: andstor/file-existence-action@076e0072799f4942c8bc574a82233e1e4d13e9d6 # v3.0.0
Expand All @@ -118,6 +129,7 @@ jobs:
run: |
mkdir data
./occ maintenance:install --verbose --database=sqlite --database-name=nextcloud --database-host=127.0.0.1 --database-port=$DB_PORT --database-user=root --database-pass=rootpassword --admin-user admin --admin-pass admin
./occ app:enable --force circles
./occ app:enable --force ${{ env.APP_NAME }}
- name: Check PHPUnit script is defined
Expand Down
4 changes: 4 additions & 0 deletions lib/Listeners/LoadAdditionalScriptsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
class LoadAdditionalScriptsListener implements IEventListener {
public function handle(Event $event): void {
if (!$event instanceof LoadAdditionalScriptsEvent && !$event instanceof BeforeTemplateRenderedEvent) {
return;
}

\OCP\Util::addInitScript('groupfolders', 'groupfolders-init');
\OCP\Util::addScript('groupfolders', 'groupfolders-files');
}
Expand Down
42 changes: 27 additions & 15 deletions lib/Listeners/NodeRenamedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,36 @@ public function __construct(
}

public function handle(Event $event): void {
$source = $event->getSource();
if (!$event instanceof NodeRenamedEvent) {
return;
}

$target = $event->getTarget();
// Look at the parent because the node itself is not existing anymore
$sourceStorage = $source->getParent()->getStorage();
if (!$target instanceof Folder) {
return;
}

$targetStorage = $target->getStorage();
if (!$targetStorage->instanceOfStorage(GroupFolderStorage::class)) {
return;
}

$source = $event->getSource();
// Look at the parent because the node itself is not existing anymore
$sourceParent = $source->getParent();
$sourceParentStorage = $sourceParent->getStorage();
if (!$sourceParentStorage->instanceOfStorage(GroupFolderStorage::class)) {
return;
}

if (($target instanceof Folder) &&
$sourceStorage->instanceOfStorage(GroupFolderStorage::class) &&
$targetStorage->instanceOfStorage(GroupFolderStorage::class)) {
// Get internal path on parent to avoid NotFoundException
$sourcePath = $source->getParent()->getInternalPath();
if ($sourcePath !== '') {
$sourcePath .= '/';
}

$sourcePath .= $source->getName();
$targetPath = $target->getInternalPath();
$this->trashManager->updateTrashedChildren($sourceStorage->getFolderId(), $targetStorage->getFolderId(), $sourcePath, $targetPath);
// Get internal path on parent to avoid NotFoundException
$sourceParentPath = $sourceParent->getInternalPath();
if ($sourceParentPath !== '') {
$sourceParentPath .= '/';
}

$sourceParentPath .= $source->getName();
$targetPath = $target->getInternalPath();
$this->trashManager->updateTrashedChildren($sourceParentStorage->getFolderId(), $targetStorage->getFolderId(), $sourceParentPath, $targetPath);
}
}
63 changes: 63 additions & 0 deletions tests/Listeners/CircleDestroyedEventListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\GroupFolders\Tests\Listeners;

use OCA\Circles\Events\CircleDestroyedEvent;
use OCA\Circles\Model\Circle;
use OCA\GroupFolders\Folder\FolderManager;
use OCA\GroupFolders\Listeners\CircleDestroyedEventListener;
use OCP\EventDispatcher\Event;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class CircleDestroyedEventListenerTest extends TestCase {
private FolderManager&MockObject $folderManager;
private CircleDestroyedEventListener $listener;

protected function setUp(): void {
parent::setUp();

$this->folderManager = $this->createMock(FolderManager::class);

$this->listener = new CircleDestroyedEventListener($this->folderManager);
}

public function testHandleInvalid(): void {
$event = $this->createMock(Event::class);

$this->folderManager
->expects($this->never())
->method('deleteCircle');

/** @psalm-suppress InvalidArgument on purpose */
$this->listener->handle($event);
}

public function testHandle(): void {
$circle = $this->createMock(Circle::class);
$circle
->expects($this->once())
->method('getSingleId')
->willReturn('123');

$event = $this->createMock(CircleDestroyedEvent::class);
$event
->expects($this->once())
->method('getCircle')
->willReturn($circle);

$this->folderManager
->expects($this->once())
->method('deleteCircle')
->with('123');

$this->listener->handle($event);
}
}
51 changes: 51 additions & 0 deletions tests/Listeners/LoadAdditionalScriptsListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\GroupFolders\Tests\Listeners;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\GroupFolders\Listeners\LoadAdditionalScriptsListener;
use OCP\EventDispatcher\Event;
use OCP\Util;
use Test\TestCase;

class LoadAdditionalScriptsListenerTest extends TestCase {
private LoadAdditionalScriptsListener $listener;

protected function setUp(): void {
parent::setUp();

$this->listener = new LoadAdditionalScriptsListener();
}

public static function handleProvider(): array {
$expectedScripts = [
'groupfolders/l10n/en',
'groupfolders/js/groupfolders-init',
'groupfolders/js/groupfolders-files',
];
return [
[Event::class, []],
[LoadAdditionalScriptsEvent::class, $expectedScripts],
[BeforeTemplateRenderedEvent::class, $expectedScripts],
];
}

/**
* @dataProvider handleProvider
* @param class-string<LoadAdditionalScriptsEvent|BeforeTemplateRenderedEvent> $class
*/
public function testHandle(string $class, array $expectedScripts): void {
$event = $this->createMock($class);

$this->listener->handle($event);
$this->assertEquals($expectedScripts, array_values(Util::getScripts()));
}
}
Loading

0 comments on commit 6af52c0

Please sign in to comment.