Skip to content

Commit

Permalink
fix: Fallback to old eventdispatcher on older releases
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliushaertl authored and max-nextcloud committed Aug 17, 2023
1 parent 9e08520 commit 8314ab7
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Versions/CollectiveVersionsExpireManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\Files\NotPermittedException as FilesNotPermittedException;
use OCP\IDBConnection;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class CollectiveVersionsExpireManager extends BasicEmitter {
private CollectiveFolderManager $folderManager;
Expand Down Expand Up @@ -54,6 +55,13 @@ public function __construct(ContainerInterface $appContainer,
$this->collectiveMapper = $collectiveMapper;
$this->timeFactory = $timeFactory;
$this->dispatcher = $dispatcher;

[$major] = \OCP\Util::getVersion();
if ($major < 28) {
// Use Symfony event dispatcher on older Nextcloud releases
$this->dispatcher = \OCP\Server::get(EventDispatcherInterface::class);
}

try {
$this->versionsBackend = $appContainer->get(VersionsBackend::class);
} catch (QueryException $e) {
Expand Down
74 changes: 74 additions & 0 deletions tests/Unit/BackgroundJob/CollectiveVersionsExpireManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* @copyright Copyright (c) 2023 Julius Härtl <[email protected]>
*
* @author Julius Härtl <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace BackgroundJob;

use OC\Files\FileInfo;
use OC\User\User;
use OCA\Collectives\Db\CollectiveMapper;
use OCA\Collectives\Mount\CollectiveFolderManager;
use OCA\Collectives\Versions\CollectiveVersionsExpireManager;
use OCA\Collectives\Versions\ExpireManager;
use OCA\Collectives\Versions\VersionsBackend;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Container\ContainerInterface;
use Test\TestCase;

class CollectiveVersionsExpireManagerTest extends TestCase {
private VersionsBackend|MockObject $versionsBackend;

public function setUp(): void {
$this->versionsBackend = $this->createMock(VersionsBackend::class);
}

public function testEventDispatcherFallback() {
$appContainer = $this->createMock(ContainerInterface::class);
$appContainer->method('get')
->with(VersionsBackend::class)
->willReturn($this->versionsBackend);

$file = $this->createMock(FileInfo::class);
$this->versionsBackend->method('getAllVersionedFiles')
->willReturn([$file]);
$this->versionsBackend->expects($this->once())
->method('getVersionsForFile')
->willReturnCallback(function ($user, $passedFile) use ($file) {
self::assertEquals($file, $passedFile);
self::assertInstanceOf(User::class, $user);
return [];
});

$manager = new CollectiveVersionsExpireManager(
$appContainer,
$this->createMock(CollectiveFolderManager::class),
$this->createMock(ExpireManager::class),
$this->createMock(IDBConnection::class),
$this->createMock(CollectiveMapper::class),
$this->createMock(ITimeFactory::class),
$this->createMock(IEventDispatcher::class),
);
$manager->expireFolder(['id' => 123]);
}
}

0 comments on commit 8314ab7

Please sign in to comment.