diff options
Diffstat (limited to 'tests/lib/Preview')
-rw-r--r-- | tests/lib/Preview/BackgroundCleanupJobTest.php | 133 | ||||
-rw-r--r-- | tests/lib/Preview/BitmapTest.php | 25 | ||||
-rw-r--r-- | tests/lib/Preview/GeneratorTest.php | 192 | ||||
-rw-r--r-- | tests/lib/Preview/HEICTest.php | 26 | ||||
-rw-r--r-- | tests/lib/Preview/ImageTest.php | 25 | ||||
-rw-r--r-- | tests/lib/Preview/MP3Test.php | 25 | ||||
-rw-r--r-- | tests/lib/Preview/MovieBrokenStuckFfmpegTest.php | 20 | ||||
-rw-r--r-- | tests/lib/Preview/MovieTest.php | 48 | ||||
-rw-r--r-- | tests/lib/Preview/OfficeTest.php | 34 | ||||
-rw-r--r-- | tests/lib/Preview/Provider.php | 81 | ||||
-rw-r--r-- | tests/lib/Preview/SVGTest.php | 55 | ||||
-rw-r--r-- | tests/lib/Preview/TXTTest.php | 25 |
12 files changed, 278 insertions, 411 deletions
diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php index c1c225bd179..ab904f2b499 100644 --- a/tests/lib/Preview/BackgroundCleanupJobTest.php +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -1,36 +1,27 @@ <?php + /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Files\Storage\Temporary; use OC\Preview\BackgroundCleanupJob; use OC\Preview\Storage\Root; use OC\PreviewManager; +use OC\SystemConfig; +use OCP\App\IAppManager; use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Files\AppData\IAppDataFactory; use OCP\Files\File; use OCP\Files\IMimeTypeLoader; use OCP\Files\IRootFolder; use OCP\Files\NotFoundException; use OCP\IDBConnection; +use OCP\IPreview; +use OCP\Server; use Test\Traits\MountProviderTrait; use Test\Traits\UserTrait; @@ -44,54 +35,41 @@ use Test\Traits\UserTrait; class BackgroundCleanupJobTest extends \Test\TestCase { use MountProviderTrait; use UserTrait; - - /** @var string */ - private $userId; - - /** @var bool */ - private $trashEnabled; - - /** @var IDBConnection */ - private $connection; - - /** @var PreviewManager */ - private $previewManager; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var IMimeTypeLoader */ - private $mimeTypeLoader; - + private string $userId; + private bool $trashEnabled; + private IDBConnection $connection; + private PreviewManager $previewManager; + private IRootFolder $rootFolder; + private IMimeTypeLoader $mimeTypeLoader; private ITimeFactory $timeFactory; protected function setUp(): void { parent::setUp(); $this->userId = $this->getUniqueID(); - $this->createUser($this->userId, $this->userId); + $user = $this->createUser($this->userId, $this->userId); - $storage = new \OC\Files\Storage\Temporary([]); + $storage = new Temporary([]); $this->registerMount($this->userId, $storage, ''); $this->loginAsUser($this->userId); $this->logout(); $this->loginAsUser($this->userId); - $appManager = \OC::$server->getAppManager(); - $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $this->userId); + $appManager = Server::get(IAppManager::class); + $this->trashEnabled = $appManager->isEnabledForUser('files_trashbin', $user); $appManager->disableApp('files_trashbin'); - $this->connection = \OC::$server->getDatabaseConnection(); - $this->previewManager = \OC::$server->getPreviewManager(); - $this->rootFolder = \OC::$server->getRootFolder(); - $this->mimeTypeLoader = \OC::$server->getMimeTypeLoader(); - $this->timeFactory = \OCP\Server::get(ITimeFactory::class); + $this->connection = Server::get(IDBConnection::class); + $this->previewManager = Server::get(IPreview::class); + $this->rootFolder = Server::get(IRootFolder::class); + $this->mimeTypeLoader = Server::get(IMimeTypeLoader::class); + $this->timeFactory = Server::get(ITimeFactory::class); } protected function tearDown(): void { if ($this->trashEnabled) { - $appManager = \OC::$server->getAppManager(); + $appManager = Server::get(IAppManager::class); $appManager->enableApp('files_trashbin'); } @@ -102,8 +80,8 @@ class BackgroundCleanupJobTest extends \Test\TestCase { private function getRoot(): Root { return new Root( - \OC::$server->getRootFolder(), - \OC::$server->getSystemConfig() + Server::get(IRootFolder::class), + Server::get(SystemConfig::class) ); } @@ -112,7 +90,7 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $files = []; for ($i = 0; $i < 11; $i++) { - $file = $userFolder->newFile($i.'.txt'); + $file = $userFolder->newFile($i . '.txt'); $file->putContent('hello world!'); $this->previewManager->getPreview($file); $files[] = $file; @@ -137,7 +115,7 @@ class BackgroundCleanupJobTest extends \Test\TestCase { return $i; } - public function testCleanupSystemCron() { + public function testCleanupSystemCron(): void { $files = $this->setup11Previews(); $fileIds = array_map(function (File $f) { return $f->getId(); @@ -161,7 +139,11 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $this->assertSame(0, $this->countPreviews($root, $fileIds)); } - public function testCleanupAjax() { + public function testCleanupAjax(): void { + if ($this->connection->getShardDefinition('filecache')) { + $this->markTestSkipped('ajax cron is not supported for sharded setups'); + return; + } $files = $this->setup11Previews(); $fileIds = array_map(function (File $f) { return $f->getId(); @@ -189,21 +171,58 @@ class BackgroundCleanupJobTest extends \Test\TestCase { $this->assertSame(0, $this->countPreviews($root, $fileIds)); } - public function testOldPreviews() { - $appdata = \OC::$server->getAppDataDir('preview'); + public function testOldPreviews(): void { + if ($this->connection->getShardDefinition('filecache')) { + $this->markTestSkipped('old previews are not supported for sharded setups'); + return; + } + $appdata = Server::get(IAppDataFactory::class)->get('preview'); $f1 = $appdata->newFolder('123456781'); $f1->newFile('foo.jpg', 'foo'); $f2 = $appdata->newFolder('123456782'); $f2->newFile('foo.jpg', 'foo'); + $f2 = $appdata->newFolder((string)PHP_INT_MAX - 1); + $f2->newFile('foo.jpg', 'foo'); - $appdata = \OC::$server->getAppDataDir('preview'); - $this->assertSame(2, count($appdata->getDirectoryListing())); + /* + * Cleanup of OldPreviewLocations should only remove numeric folders on AppData level, + * therefore these files should stay untouched. + */ + $appdata->getFolder('/')->newFile('not-a-directory', 'foo'); + $appdata->getFolder('/')->newFile('133742', 'bar'); + + $appdata = Server::get(IAppDataFactory::class)->get('preview'); + // AppData::getDirectoryListing filters all non-folders + $this->assertSame(3, count($appdata->getDirectoryListing())); + try { + $appdata->getFolder('/')->getFile('not-a-directory'); + } catch (NotFoundException) { + $this->fail('Could not find file \'not-a-directory\''); + } + try { + $appdata->getFolder('/')->getFile('133742'); + } catch (NotFoundException) { + $this->fail('Could not find file \'133742\''); + } $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true); $job->run([]); - $appdata = \OC::$server->getAppDataDir('preview'); + $appdata = Server::get(IAppDataFactory::class)->get('preview'); + + // Check if the files created above are still present + // Remember: AppData::getDirectoryListing filters all non-folders $this->assertSame(0, count($appdata->getDirectoryListing())); + try { + $appdata->getFolder('/')->getFile('not-a-directory'); + } catch (NotFoundException) { + $this->fail('Could not find file \'not-a-directory\''); + } + try { + $appdata->getFolder('/')->getFile('133742'); + } catch (NotFoundException) { + $this->fail('Could not find file \'133742\''); + } } } diff --git a/tests/lib/Preview/BitmapTest.php b/tests/lib/Preview/BitmapTest.php index 554e499ee84..36e768010a9 100644 --- a/tests/lib/Preview/BitmapTest.php +++ b/tests/lib/Preview/BitmapTest.php @@ -1,26 +1,15 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\Postscript; + /** * Class BitmapTest * @@ -36,6 +25,6 @@ class BitmapTest extends Provider { $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); $this->width = 2400; $this->height = 1707; - $this->provider = new \OC\Preview\Postscript; + $this->provider = new Postscript; } } diff --git a/tests/lib/Preview/GeneratorTest.php b/tests/lib/Preview/GeneratorTest.php index 1f6f43dce1e..edf5418da6e 100644 --- a/tests/lib/Preview/GeneratorTest.php +++ b/tests/lib/Preview/GeneratorTest.php @@ -1,24 +1,8 @@ <?php + /** - * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Preview; @@ -32,34 +16,33 @@ use OCP\Files\NotFoundException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; use OCP\IConfig; +use OCP\IImage; use OCP\IPreview; use OCP\Preview\BeforePreviewFetchedEvent; use OCP\Preview\IProviderV2; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; -use Symfony\Component\EventDispatcher\GenericEvent; +use Psr\Log\LoggerInterface; class GeneratorTest extends \Test\TestCase { - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig&\PHPUnit\Framework\MockObject\MockObject */ private $config; - /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IPreview&\PHPUnit\Framework\MockObject\MockObject */ private $previewManager; - /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IAppData&\PHPUnit\Framework\MockObject\MockObject */ private $appData; - /** @var GeneratorHelper|\PHPUnit\Framework\MockObject\MockObject */ + /** @var GeneratorHelper&\PHPUnit\Framework\MockObject\MockObject */ private $helper; - /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IEventDispatcher&\PHPUnit\Framework\MockObject\MockObject */ private $eventDispatcher; - /** @var EventDispatcherInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $legacyEventDispatcher; - /** @var Generator */ private $generator; + private LoggerInterface&\PHPUnit\Framework\MockObject\MockObject $logger; + protected function setUp(): void { parent::setUp(); @@ -68,19 +51,19 @@ class GeneratorTest extends \Test\TestCase { $this->appData = $this->createMock(IAppData::class); $this->helper = $this->createMock(GeneratorHelper::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); - $this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class); + $this->logger = $this->createMock(LoggerInterface::class); $this->generator = new Generator( $this->config, $this->previewManager, $this->appData, $this->helper, - $this->legacyEventDispatcher, - $this->eventDispatcher + $this->eventDispatcher, + $this->logger, ); } - public function testGetCachedPreview() { + public function testGetCachedPreview(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -105,36 +88,22 @@ class GeneratorTest extends \Test\TestCase { $maxPreview->method('getMimeType') ->willReturn('image/png'); - $previewFolder->method('getDirectoryListing') - ->willReturn([$maxPreview]); - $previewFile = $this->createMock(ISimpleFile::class); $previewFile->method('getSize')->willReturn(1000); + $previewFile->method('getName')->willReturn('256-256.png'); - $previewFolder->method('getFile') - ->with($this->equalTo('256-256.png')) - ->willReturn($previewFile); - - $this->legacyEventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 100 && - $event->getArgument('height') === 100; - }) - ); + $previewFolder->method('getDirectoryListing') + ->willReturn([$maxPreview, $previewFile]); $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); } - public function testGetNewPreview() { + public function testGetNewPreview(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -199,7 +168,7 @@ class GeneratorTest extends \Test\TestCase { $this->fail('Unexpected provider requested'); }); - $image = $this->createMock(\OC_Image::class); + $image = $this->createMock(IImage::class); $image->method('width')->willReturn(2048); $image->method('height')->willReturn(2048); $image->method('valid')->willReturn(true); @@ -228,18 +197,10 @@ class GeneratorTest extends \Test\TestCase { $previewFolder->method('getDirectoryListing') ->willReturn([]); $previewFolder->method('newFile') - ->willReturnCallback(function ($filename) use ($maxPreview, $previewFile) { - if ($filename === '2048-2048-max.png') { - return $maxPreview; - } elseif ($filename === '256-256.png') { - return $previewFile; - } - $this->fail('Unexpected file'); - }); - - $maxPreview->expects($this->once()) - ->method('putContent') - ->with($this->equalTo('my data')); + ->willReturnMap([ + ['2048-2048-max.png', 'my data', $maxPreview], + ['256-256.png', 'my resized data', $previewFile], + ]); $previewFolder->method('getFile') ->with($this->equalTo('256-256.png')) @@ -250,30 +211,15 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo($maxPreview)) ->willReturn($image); - $previewFile->expects($this->once()) - ->method('putContent') - ->with('my resized data'); - - $this->legacyEventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 100 && - $event->getArgument('height') === 100; - }) - ); - $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $result = $this->generator->getPreview($file, 100, 100); $this->assertSame($previewFile, $result); } - public function testInvalidMimeType() { + public function testInvalidMimeType(): void { $this->expectException(NotFoundException::class); $file = $this->createMock(File::class); @@ -304,27 +250,14 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo('1024-512-crop.png')) ->willThrowException(new NotFoundException()); - $this->legacyEventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 1024 && - $event->getArgument('height') === 512 && - $event->getArgument('crop') === true && - $event->getArgument('mode') === IPreview::MODE_COVER; - }) - ); - $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType')); $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); } - public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype() { + public function testReturnCachedPreviewsWithoutCheckingSupportedMimetype(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -344,40 +277,25 @@ class GeneratorTest extends \Test\TestCase { $maxPreview->method('getMimeType') ->willReturn('image/png'); - $previewFolder->method('getDirectoryListing') - ->willReturn([$maxPreview]); - $preview = $this->createMock(ISimpleFile::class); $preview->method('getSize')->willReturn(1000); - $previewFolder->method('getFile') - ->with($this->equalTo('1024-512-crop.png')) - ->willReturn($preview); + $preview->method('getName')->willReturn('1024-512-crop.png'); + + $previewFolder->method('getDirectoryListing') + ->willReturn([$maxPreview, $preview]); $this->previewManager->expects($this->never()) ->method('isMimeSupported'); - $this->legacyEventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 1024 && - $event->getArgument('height') === 512 && - $event->getArgument('crop') === true && - $event->getArgument('mode') === IPreview::MODE_COVER; - }) - ); - $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType')); $result = $this->generator->getPreview($file, 1024, 512, true, IPreview::MODE_COVER, 'invalidType'); $this->assertSame($preview, $result); } - public function testNoProvider() { + public function testNoProvider(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -397,27 +315,16 @@ class GeneratorTest extends \Test\TestCase { $this->previewManager->method('getProviders') ->willReturn([]); - $this->legacyEventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file) { - return $event->getSubject() === $file && - $event->getArgument('width') === 100 && - $event->getArgument('height') === 100; - }) - ); - $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, 100, 100, false, IPreview::MODE_FILL, null)); $this->expectException(NotFoundException::class); $this->generator->getPreview($file, 100, 100); } private function getMockImage($width, $height, $data = null) { - $image = $this->createMock(\OC_Image::class); + $image = $this->createMock(IImage::class); $image->method('height')->willReturn($width); $image->method('width')->willReturn($height); $image->method('valid')->willReturn(true); @@ -437,7 +344,7 @@ class GeneratorTest extends \Test\TestCase { return $image; } - public function dataSize() { + public static function dataSize(): array { return [ [1024, 2048, 512, 512, false, IPreview::MODE_FILL, 256, 512], [1024, 2048, 512, 512, false, IPreview::MODE_COVER, 512, 1024], @@ -474,7 +381,6 @@ class GeneratorTest extends \Test\TestCase { } /** - * @dataProvider dataSize * * @param int $maxX * @param int $maxY @@ -485,7 +391,8 @@ class GeneratorTest extends \Test\TestCase { * @param int $expectedX * @param int $expectedY */ - public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY) { + #[\PHPUnit\Framework\Attributes\DataProvider('dataSize')] + public function testCorrectSize($maxX, $maxY, $reqX, $reqY, $crop, $mode, $expectedX, $expectedY): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(true); @@ -533,22 +440,9 @@ class GeneratorTest extends \Test\TestCase { ->with($this->equalTo($filename)) ->willReturn($preview); - $this->legacyEventDispatcher->expects($this->once()) - ->method('dispatch') - ->with( - $this->equalTo(IPreview::EVENT), - $this->callback(function (GenericEvent $event) use ($file, $reqX, $reqY, $crop, $mode) { - return $event->getSubject() === $file && - $event->getArgument('width') === $reqX && - $event->getArgument('height') === $reqY && - $event->getArgument('crop') === $crop && - $event->getArgument('mode') === $mode; - }) - ); - $this->eventDispatcher->expects($this->once()) ->method('dispatchTyped') - ->with(new BeforePreviewFetchedEvent($file)); + ->with(new BeforePreviewFetchedEvent($file, $reqX, $reqY, $crop, $mode, null)); $result = $this->generator->getPreview($file, $reqX, $reqY, $crop, $mode); if ($expectedX === $maxX && $expectedY === $maxY) { @@ -558,7 +452,7 @@ class GeneratorTest extends \Test\TestCase { } } - public function testUnreadbleFile() { + public function testUnreadbleFile(): void { $file = $this->createMock(File::class); $file->method('isReadable') ->willReturn(false); diff --git a/tests/lib/Preview/HEICTest.php b/tests/lib/Preview/HEICTest.php index 18571dc52ea..def113fbf08 100644 --- a/tests/lib/Preview/HEICTest.php +++ b/tests/lib/Preview/HEICTest.php @@ -1,26 +1,14 @@ <?php + /** - * @copyright Copyright (c) 2018, Sebastian Steinmetz (me@sebastiansteinmetz.ch) - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Preview; +use OC\Preview\HEIC; + /** * Class BitmapTest * @@ -30,7 +18,7 @@ namespace Test\Preview; */ class HEICTest extends Provider { protected function setUp(): void { - if (!in_array("HEIC", \Imagick::queryFormats("HEI*"))) { + if (!in_array('HEIC', \Imagick::queryFormats('HEI*'))) { $this->markTestSkipped('ImageMagick is not HEIC aware. Skipping tests'); } else { parent::setUp(); @@ -39,7 +27,7 @@ class HEICTest extends Provider { $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); $this->width = 1680; $this->height = 1050; - $this->provider = new \OC\Preview\HEIC; + $this->provider = new HEIC; } } } diff --git a/tests/lib/Preview/ImageTest.php b/tests/lib/Preview/ImageTest.php index d58e4ca91ba..88c3090557f 100644 --- a/tests/lib/Preview/ImageTest.php +++ b/tests/lib/Preview/ImageTest.php @@ -1,26 +1,15 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\JPEG; + /** * Class ImageTest * @@ -36,6 +25,6 @@ class ImageTest extends Provider { $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); $this->width = 1680; $this->height = 1050; - $this->provider = new \OC\Preview\JPEG(); + $this->provider = new JPEG(); } } diff --git a/tests/lib/Preview/MP3Test.php b/tests/lib/Preview/MP3Test.php index 6e841e29ec4..faa06fe42a1 100644 --- a/tests/lib/Preview/MP3Test.php +++ b/tests/lib/Preview/MP3Test.php @@ -1,26 +1,15 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\MP3; + /** * Class MP3Test * @@ -36,6 +25,6 @@ class MP3Test extends Provider { $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); $this->width = 200; $this->height = 200; - $this->provider = new \OC\Preview\MP3; + $this->provider = new MP3; } } diff --git a/tests/lib/Preview/MovieBrokenStuckFfmpegTest.php b/tests/lib/Preview/MovieBrokenStuckFfmpegTest.php new file mode 100644 index 00000000000..e66d5e64649 --- /dev/null +++ b/tests/lib/Preview/MovieBrokenStuckFfmpegTest.php @@ -0,0 +1,20 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace Test\Preview; + +/** + * Class MovieTest + * + * @group DB + * + * @package Test\Preview + */ +class MovieBrokenStuckFfmpegTest extends MovieTest { + protected string $fileName = 'broken-video.webm'; +} diff --git a/tests/lib/Preview/MovieTest.php b/tests/lib/Preview/MovieTest.php index 767ce130ae4..c85a9f99748 100644 --- a/tests/lib/Preview/MovieTest.php +++ b/tests/lib/Preview/MovieTest.php @@ -1,26 +1,17 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\Movie; +use OCP\IBinaryFinder; +use OCP\Server; + /** * Class MovieTest * @@ -29,21 +20,22 @@ namespace Test\Preview; * @package Test\Preview */ class MovieTest extends Provider { + protected string $fileName = 'testimage.mp4'; + protected int $width = 560; + protected int $height = 320; + protected function setUp(): void { - $avconvBinary = \OC_Helper::findBinaryPath('avconv'); - $ffmpegBinary = ($avconvBinary) ? null : \OC_Helper::findBinaryPath('ffmpeg'); + $binaryFinder = Server::get(IBinaryFinder::class); + $movieBinary = $binaryFinder->findBinaryPath('avconv'); + if (!is_string($movieBinary)) { + $movieBinary = $binaryFinder->findBinaryPath('ffmpeg'); + } - if ($avconvBinary || $ffmpegBinary) { + if (is_string($movieBinary)) { parent::setUp(); - \OC\Preview\Movie::$avconvBinary = $avconvBinary; - \OC\Preview\Movie::$ffmpegBinary = $ffmpegBinary; - - $fileName = 'testimage.mp4'; - $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); - $this->width = 560; - $this->height = 320; - $this->provider = new \OC\Preview\Movie; + $this->imgPath = $this->prepareTestFile($this->fileName, \OC::$SERVERROOT . '/tests/data/' . $this->fileName); + $this->provider = new Movie(['movieBinary' => $movieBinary]); } else { $this->markTestSkipped('No Movie provider present'); } diff --git a/tests/lib/Preview/OfficeTest.php b/tests/lib/Preview/OfficeTest.php index c37e5b95a75..62a4767fd2e 100644 --- a/tests/lib/Preview/OfficeTest.php +++ b/tests/lib/Preview/OfficeTest.php @@ -1,26 +1,17 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\OpenDocument; +use OCP\IBinaryFinder; +use OCP\Server; + /** * Class OfficeTest * @@ -30,17 +21,18 @@ namespace Test\Preview; */ class OfficeTest extends Provider { protected function setUp(): void { - $libreofficeBinary = \OC_Helper::findBinaryPath('libreoffice'); - $openofficeBinary = ($libreofficeBinary) ? null : \OC_Helper::findBinaryPath('openoffice'); + $binaryFinder = Server::get(IBinaryFinder::class); + $libreofficeBinary = $binaryFinder->findBinaryPath('libreoffice'); + $openofficeBinary = $libreofficeBinary === false ? $binaryFinder->findBinaryPath('openoffice') : false; - if ($libreofficeBinary || $openofficeBinary) { + if ($libreofficeBinary !== false || $openofficeBinary !== false) { parent::setUp(); $fileName = 'testimage.odt'; $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); $this->width = 595; $this->height = 842; - $this->provider = new \OC\Preview\OpenDocument; + $this->provider = new OpenDocument; } else { $this->markTestSkipped('No Office provider present'); } diff --git a/tests/lib/Preview/Provider.php b/tests/lib/Preview/Provider.php index a2e5170c82c..d8f10c430e4 100644 --- a/tests/lib/Preview/Provider.php +++ b/tests/lib/Preview/Provider.php @@ -1,54 +1,41 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Files\Filesystem; use OC\Files\Node\File; +use OC\Files\Storage\Storage; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use OC\Preview\TXT; +use OCP\Files\IRootFolder; +use OCP\IImage; +use OCP\IUserManager; +use OCP\Server; abstract class Provider extends \Test\TestCase { - /** @var string */ - protected $imgPath; - /** @var int */ - protected $width; - /** @var int */ - protected $height; - /** @var \OC\Preview\Provider */ + protected string $imgPath; + protected int $width; + protected int $height; + /** @var \OC\Preview\Provider|mixed $provider */ protected $provider; - /** @var int */ - protected $maxWidth = 1024; - /** @var int */ - protected $maxHeight = 1024; - /** @var bool */ - protected $scalingUp = false; - /** @var int */ - protected $userId; - /** @var \OC\Files\View */ - protected $rootView; - /** @var \OC\Files\Storage\Storage */ - protected $storage; + protected int $maxWidth = 1024; + protected int $maxHeight = 1024; + protected bool $scalingUp = false; + protected string $userId; + protected View $rootView; + protected Storage $storage; protected function setUp(): void { parent::setUp(); - $userManager = \OC::$server->getUserManager(); + $userManager = Server::get(IUserManager::class); $userManager->clearBackends(); $backend = new \Test\Util\User\Dummy(); $userManager->registerBackend($backend); @@ -57,10 +44,10 @@ abstract class Provider extends \Test\TestCase { $backend->createUser($userId, $userId); $this->loginAsUser($userId); - $this->storage = new \OC\Files\Storage\Temporary([]); - \OC\Files\Filesystem::mount($this->storage, [], '/' . $userId . '/'); + $this->storage = new Temporary([]); + Filesystem::mount($this->storage, [], '/' . $userId . '/'); - $this->rootView = new \OC\Files\View(''); + $this->rootView = new View(''); $this->rootView->mkdir('/' . $userId); $this->rootView->mkdir('/' . $userId . '/files'); @@ -85,13 +72,13 @@ abstract class Provider extends \Test\TestCase { /** * Launches all the tests we have * - * @dataProvider dimensionsDataProvider * @requires extension imagick * * @param int $widthAdjustment * @param int $heightAdjustment */ - public function testGetThumbnail($widthAdjustment, $heightAdjustment) { + #[\PHPUnit\Framework\Attributes\DataProvider('dimensionsDataProvider')] + public function testGetThumbnail($widthAdjustment, $heightAdjustment): void { $ratio = round($this->width / $this->height, 2); $this->maxWidth = $this->width - $widthAdjustment; $this->maxHeight = $this->height - $heightAdjustment; @@ -104,7 +91,7 @@ abstract class Provider extends \Test\TestCase { $preview = $this->getPreview($this->provider); // The TXT provider uses the max dimensions to create its canvas, // so the ratio will always be the one of the max dimension canvas - if (!$this->provider instanceof \OC\Preview\TXT) { + if (!$this->provider instanceof TXT) { $this->doesRatioMatch($preview, $ratio); } $this->doesPreviewFit($preview); @@ -134,10 +121,10 @@ abstract class Provider extends \Test\TestCase { * * @param \OC\Preview\Provider $provider * - * @return bool|\OCP\IImage + * @return bool|IImage */ private function getPreview($provider) { - $file = new File(\OC::$server->getRootFolder(), $this->rootView, $this->imgPath); + $file = new File(Server::get(IRootFolder::class), $this->rootView, $this->imgPath); $preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp); if (get_class($this) === BitmapTest::class && $preview === null) { @@ -153,7 +140,7 @@ abstract class Provider extends \Test\TestCase { /** * Checks if the preview ratio matches the original ratio * - * @param \OCP\IImage $preview + * @param IImage $preview * @param int $ratio */ private function doesRatioMatch($preview, $ratio) { @@ -164,7 +151,7 @@ abstract class Provider extends \Test\TestCase { /** * Tests if a max size preview of smaller dimensions can be created * - * @param \OCP\IImage $preview + * @param IImage $preview */ private function doesPreviewFit($preview) { $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2); diff --git a/tests/lib/Preview/SVGTest.php b/tests/lib/Preview/SVGTest.php index e48018a301b..8c6d9bb6691 100644 --- a/tests/lib/Preview/SVGTest.php +++ b/tests/lib/Preview/SVGTest.php @@ -1,26 +1,16 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\SVG; +use OCP\Files\File; + /** * Class SVGTest * @@ -38,9 +28,38 @@ class SVGTest extends Provider { $this->imgPath = $this->prepareTestFile($fileName, \OC::$SERVERROOT . '/tests/data/' . $fileName); $this->width = 3000; $this->height = 2000; - $this->provider = new \OC\Preview\SVG; + $this->provider = new SVG; } else { $this->markTestSkipped('No SVG provider present'); } } + + public static function dataGetThumbnailSVGHref(): array { + return [ + ['href'], + [' href'], + ["\nhref"], + ['xlink:href'], + [' xlink:href'], + ["\nxlink:href"], + ]; + } + + /** + * @requires extension imagick + */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataGetThumbnailSVGHref')] + public function testGetThumbnailSVGHref(string $content): void { + $handle = fopen('php://temp', 'w+'); + fwrite($handle, '<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> + <image x="0" y="0"' . $content . '="fxlogo.png" height="100" width="100" /> +</svg>'); + rewind($handle); + + $file = $this->createMock(File::class); + $file->method('fopen') + ->willReturn($handle); + + self::assertNull($this->provider->getThumbnail($file, 512, 512)); + } } diff --git a/tests/lib/Preview/TXTTest.php b/tests/lib/Preview/TXTTest.php index 785fe8c2526..d722495136c 100644 --- a/tests/lib/Preview/TXTTest.php +++ b/tests/lib/Preview/TXTTest.php @@ -1,26 +1,15 @@ <?php + /** - * @author Olivier Paroz <owncloud@interfasys.ch> - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace Test\Preview; +use OC\Preview\TXT; + /** * Class TXTTest * @@ -37,6 +26,6 @@ class TXTTest extends Provider { // Arbitrary width and length which won't be used to calculate the ratio $this->width = 500; $this->height = 200; - $this->provider = new \OC\Preview\TXT; + $this->provider = new TXT; } } |