@@ -22,138 +22,138 @@ discard block |
||
| 22 | 22 | use OCP\Server; |
| 23 | 23 | |
| 24 | 24 | abstract class Provider extends \Test\TestCase { |
| 25 | - protected string $imgPath; |
|
| 26 | - protected int $width; |
|
| 27 | - protected int $height; |
|
| 28 | - protected IProviderV2 $provider; |
|
| 29 | - protected int $maxWidth = 1024; |
|
| 30 | - protected int $maxHeight = 1024; |
|
| 31 | - protected bool $scalingUp = false; |
|
| 32 | - protected string $userId; |
|
| 33 | - protected View $rootView; |
|
| 34 | - protected Storage $storage; |
|
| 35 | - |
|
| 36 | - protected function setUp(): void { |
|
| 37 | - parent::setUp(); |
|
| 38 | - |
|
| 39 | - $userManager = Server::get(IUserManager::class); |
|
| 40 | - $userManager->clearBackends(); |
|
| 41 | - $backend = new \Test\Util\User\Dummy(); |
|
| 42 | - $userManager->registerBackend($backend); |
|
| 43 | - |
|
| 44 | - $userId = $this->getUniqueID(); |
|
| 45 | - $backend->createUser($userId, $userId); |
|
| 46 | - $this->loginAsUser($userId); |
|
| 47 | - |
|
| 48 | - $this->storage = new Temporary([]); |
|
| 49 | - Filesystem::mount($this->storage, [], '/' . $userId . '/'); |
|
| 50 | - |
|
| 51 | - $this->rootView = new View(''); |
|
| 52 | - $this->rootView->mkdir('/' . $userId); |
|
| 53 | - $this->rootView->mkdir('/' . $userId . '/files'); |
|
| 54 | - |
|
| 55 | - $this->userId = $userId; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - protected function tearDown(): void { |
|
| 59 | - $this->logout(); |
|
| 60 | - |
|
| 61 | - parent::tearDown(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public static function dimensionsDataProvider() { |
|
| 65 | - return [ |
|
| 66 | - [-rand(5, 100), -rand(5, 100)], |
|
| 67 | - [rand(5, 100), rand(5, 100)], |
|
| 68 | - [-rand(5, 100), rand(5, 100)], |
|
| 69 | - [rand(5, 100), -rand(5, 100)], |
|
| 70 | - ]; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Launches all the tests we have |
|
| 75 | - * |
|
| 76 | - * |
|
| 77 | - * @param int $widthAdjustment |
|
| 78 | - * @param int $heightAdjustment |
|
| 79 | - */ |
|
| 80 | - #[\PHPUnit\Framework\Attributes\DataProvider('dimensionsDataProvider')] |
|
| 81 | - #[\PHPUnit\Framework\Attributes\RequiresPhpExtension('imagick')] |
|
| 82 | - public function testGetThumbnail($widthAdjustment, $heightAdjustment): void { |
|
| 83 | - $ratio = round($this->width / $this->height, 2); |
|
| 84 | - $this->maxWidth = $this->width - $widthAdjustment; |
|
| 85 | - $this->maxHeight = $this->height - $heightAdjustment; |
|
| 86 | - |
|
| 87 | - // Testing code |
|
| 88 | - /*print_r("w $this->width "); |
|
| 25 | + protected string $imgPath; |
|
| 26 | + protected int $width; |
|
| 27 | + protected int $height; |
|
| 28 | + protected IProviderV2 $provider; |
|
| 29 | + protected int $maxWidth = 1024; |
|
| 30 | + protected int $maxHeight = 1024; |
|
| 31 | + protected bool $scalingUp = false; |
|
| 32 | + protected string $userId; |
|
| 33 | + protected View $rootView; |
|
| 34 | + protected Storage $storage; |
|
| 35 | + |
|
| 36 | + protected function setUp(): void { |
|
| 37 | + parent::setUp(); |
|
| 38 | + |
|
| 39 | + $userManager = Server::get(IUserManager::class); |
|
| 40 | + $userManager->clearBackends(); |
|
| 41 | + $backend = new \Test\Util\User\Dummy(); |
|
| 42 | + $userManager->registerBackend($backend); |
|
| 43 | + |
|
| 44 | + $userId = $this->getUniqueID(); |
|
| 45 | + $backend->createUser($userId, $userId); |
|
| 46 | + $this->loginAsUser($userId); |
|
| 47 | + |
|
| 48 | + $this->storage = new Temporary([]); |
|
| 49 | + Filesystem::mount($this->storage, [], '/' . $userId . '/'); |
|
| 50 | + |
|
| 51 | + $this->rootView = new View(''); |
|
| 52 | + $this->rootView->mkdir('/' . $userId); |
|
| 53 | + $this->rootView->mkdir('/' . $userId . '/files'); |
|
| 54 | + |
|
| 55 | + $this->userId = $userId; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + protected function tearDown(): void { |
|
| 59 | + $this->logout(); |
|
| 60 | + |
|
| 61 | + parent::tearDown(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public static function dimensionsDataProvider() { |
|
| 65 | + return [ |
|
| 66 | + [-rand(5, 100), -rand(5, 100)], |
|
| 67 | + [rand(5, 100), rand(5, 100)], |
|
| 68 | + [-rand(5, 100), rand(5, 100)], |
|
| 69 | + [rand(5, 100), -rand(5, 100)], |
|
| 70 | + ]; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Launches all the tests we have |
|
| 75 | + * |
|
| 76 | + * |
|
| 77 | + * @param int $widthAdjustment |
|
| 78 | + * @param int $heightAdjustment |
|
| 79 | + */ |
|
| 80 | + #[\PHPUnit\Framework\Attributes\DataProvider('dimensionsDataProvider')] |
|
| 81 | + #[\PHPUnit\Framework\Attributes\RequiresPhpExtension('imagick')] |
|
| 82 | + public function testGetThumbnail($widthAdjustment, $heightAdjustment): void { |
|
| 83 | + $ratio = round($this->width / $this->height, 2); |
|
| 84 | + $this->maxWidth = $this->width - $widthAdjustment; |
|
| 85 | + $this->maxHeight = $this->height - $heightAdjustment; |
|
| 86 | + |
|
| 87 | + // Testing code |
|
| 88 | + /*print_r("w $this->width "); |
|
| 89 | 89 | print_r("h $this->height "); |
| 90 | 90 | print_r("r $ratio ");*/ |
| 91 | 91 | |
| 92 | - $preview = $this->getPreview($this->provider); |
|
| 93 | - // The TXT provider uses the max dimensions to create its canvas, |
|
| 94 | - // so the ratio will always be the one of the max dimension canvas |
|
| 95 | - if (!$this->provider instanceof TXT) { |
|
| 96 | - $this->doesRatioMatch($preview, $ratio); |
|
| 97 | - } |
|
| 98 | - $this->doesPreviewFit($preview); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Adds the test file to the filesystem |
|
| 103 | - * |
|
| 104 | - * @param string $fileName name of the file to create |
|
| 105 | - * @param string $fileContent path to file to use for test |
|
| 106 | - * |
|
| 107 | - * @return string |
|
| 108 | - */ |
|
| 109 | - protected function prepareTestFile($fileName, $fileContent) { |
|
| 110 | - $imgData = file_get_contents($fileContent); |
|
| 111 | - $imgPath = '/' . $this->userId . '/files/' . $fileName; |
|
| 112 | - $this->rootView->file_put_contents($imgPath, $imgData); |
|
| 113 | - |
|
| 114 | - $scanner = $this->storage->getScanner(); |
|
| 115 | - $scanner->scan(''); |
|
| 116 | - |
|
| 117 | - return $imgPath; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Retrieves a max size thumbnail can be created |
|
| 122 | - * |
|
| 123 | - * @return bool|IImage |
|
| 124 | - */ |
|
| 125 | - private function getPreview(ProviderV2 $provider) { |
|
| 126 | - $file = new File(Server::get(IRootFolder::class), $this->rootView, $this->imgPath); |
|
| 127 | - $preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp); |
|
| 128 | - |
|
| 129 | - $this->assertNotEquals(false, $preview); |
|
| 130 | - $this->assertEquals(true, $preview->valid()); |
|
| 131 | - |
|
| 132 | - return $preview; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Checks if the preview ratio matches the original ratio |
|
| 137 | - * |
|
| 138 | - * @param IImage $preview |
|
| 139 | - * @param int $ratio |
|
| 140 | - */ |
|
| 141 | - private function doesRatioMatch($preview, $ratio) { |
|
| 142 | - $previewRatio = round($preview->width() / $preview->height(), 2); |
|
| 143 | - $this->assertEquals($ratio, $previewRatio); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Tests if a max size preview of smaller dimensions can be created |
|
| 148 | - * |
|
| 149 | - * @param IImage $preview |
|
| 150 | - */ |
|
| 151 | - private function doesPreviewFit($preview) { |
|
| 152 | - $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2); |
|
| 153 | - $previewRatio = round($preview->width() / $preview->height(), 2); |
|
| 154 | - |
|
| 155 | - // Testing code |
|
| 156 | - /*print_r("mw $this->maxWidth "); |
|
| 92 | + $preview = $this->getPreview($this->provider); |
|
| 93 | + // The TXT provider uses the max dimensions to create its canvas, |
|
| 94 | + // so the ratio will always be the one of the max dimension canvas |
|
| 95 | + if (!$this->provider instanceof TXT) { |
|
| 96 | + $this->doesRatioMatch($preview, $ratio); |
|
| 97 | + } |
|
| 98 | + $this->doesPreviewFit($preview); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Adds the test file to the filesystem |
|
| 103 | + * |
|
| 104 | + * @param string $fileName name of the file to create |
|
| 105 | + * @param string $fileContent path to file to use for test |
|
| 106 | + * |
|
| 107 | + * @return string |
|
| 108 | + */ |
|
| 109 | + protected function prepareTestFile($fileName, $fileContent) { |
|
| 110 | + $imgData = file_get_contents($fileContent); |
|
| 111 | + $imgPath = '/' . $this->userId . '/files/' . $fileName; |
|
| 112 | + $this->rootView->file_put_contents($imgPath, $imgData); |
|
| 113 | + |
|
| 114 | + $scanner = $this->storage->getScanner(); |
|
| 115 | + $scanner->scan(''); |
|
| 116 | + |
|
| 117 | + return $imgPath; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Retrieves a max size thumbnail can be created |
|
| 122 | + * |
|
| 123 | + * @return bool|IImage |
|
| 124 | + */ |
|
| 125 | + private function getPreview(ProviderV2 $provider) { |
|
| 126 | + $file = new File(Server::get(IRootFolder::class), $this->rootView, $this->imgPath); |
|
| 127 | + $preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp); |
|
| 128 | + |
|
| 129 | + $this->assertNotEquals(false, $preview); |
|
| 130 | + $this->assertEquals(true, $preview->valid()); |
|
| 131 | + |
|
| 132 | + return $preview; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Checks if the preview ratio matches the original ratio |
|
| 137 | + * |
|
| 138 | + * @param IImage $preview |
|
| 139 | + * @param int $ratio |
|
| 140 | + */ |
|
| 141 | + private function doesRatioMatch($preview, $ratio) { |
|
| 142 | + $previewRatio = round($preview->width() / $preview->height(), 2); |
|
| 143 | + $this->assertEquals($ratio, $previewRatio); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Tests if a max size preview of smaller dimensions can be created |
|
| 148 | + * |
|
| 149 | + * @param IImage $preview |
|
| 150 | + */ |
|
| 151 | + private function doesPreviewFit($preview) { |
|
| 152 | + $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2); |
|
| 153 | + $previewRatio = round($preview->width() / $preview->height(), 2); |
|
| 154 | + |
|
| 155 | + // Testing code |
|
| 156 | + /*print_r("mw $this->maxWidth "); |
|
| 157 | 157 | print_r("mh $this->maxHeight "); |
| 158 | 158 | print_r("mr $maxDimRatio "); |
| 159 | 159 | $pw = $preview->width(); |
@@ -162,15 +162,15 @@ discard block |
||
| 162 | 162 | print_r("ph $ph "); |
| 163 | 163 | print_r("pr $previewRatio ");*/ |
| 164 | 164 | |
| 165 | - if ($maxDimRatio < $previewRatio) { |
|
| 166 | - $this->assertLessThanOrEqual($this->maxWidth, $preview->width()); |
|
| 167 | - $this->assertLessThan($this->maxHeight, $preview->height()); |
|
| 168 | - } elseif ($maxDimRatio > $previewRatio) { |
|
| 169 | - $this->assertLessThan($this->maxWidth, $preview->width()); |
|
| 170 | - $this->assertLessThanOrEqual($this->maxHeight, $preview->height()); |
|
| 171 | - } else { // Original had to be resized |
|
| 172 | - $this->assertLessThanOrEqual($this->maxWidth, $preview->width()); |
|
| 173 | - $this->assertLessThanOrEqual($this->maxHeight, $preview->height()); |
|
| 174 | - } |
|
| 175 | - } |
|
| 165 | + if ($maxDimRatio < $previewRatio) { |
|
| 166 | + $this->assertLessThanOrEqual($this->maxWidth, $preview->width()); |
|
| 167 | + $this->assertLessThan($this->maxHeight, $preview->height()); |
|
| 168 | + } elseif ($maxDimRatio > $previewRatio) { |
|
| 169 | + $this->assertLessThan($this->maxWidth, $preview->width()); |
|
| 170 | + $this->assertLessThanOrEqual($this->maxHeight, $preview->height()); |
|
| 171 | + } else { // Original had to be resized |
|
| 172 | + $this->assertLessThanOrEqual($this->maxWidth, $preview->width()); |
|
| 173 | + $this->assertLessThanOrEqual($this->maxHeight, $preview->height()); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | 176 | } |
@@ -9,17 +9,17 @@ |
||
| 9 | 9 | |
| 10 | 10 | //.eps |
| 11 | 11 | class Postscript extends Bitmap { |
| 12 | - /** |
|
| 13 | - * {@inheritDoc} |
|
| 14 | - */ |
|
| 15 | - public function getMimeType(): string { |
|
| 16 | - return '/application\/postscript/'; |
|
| 17 | - } |
|
| 12 | + /** |
|
| 13 | + * {@inheritDoc} |
|
| 14 | + */ |
|
| 15 | + public function getMimeType(): string { |
|
| 16 | + return '/application\/postscript/'; |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * {@inheritDoc} |
|
| 21 | - */ |
|
| 22 | - protected function getAllowedMimeTypes(): string { |
|
| 23 | - return '/(application\/postscript|image\/x-eps)/'; |
|
| 24 | - } |
|
| 19 | + /** |
|
| 20 | + * {@inheritDoc} |
|
| 21 | + */ |
|
| 22 | + protected function getAllowedMimeTypes(): string { |
|
| 23 | + return '/(application\/postscript|image\/x-eps)/'; |
|
| 24 | + } |
|
| 25 | 25 | } |