Provider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @author Olivier Paroz <owncloud@interfasys.ch>
  4. *
  5. * @copyright Copyright (c) 2015, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Preview;
  22. use OC\Files\Node\File;
  23. use OCP\Files\IRootFolder;
  24. abstract class Provider extends \Test\TestCase {
  25. /** @var string */
  26. protected $imgPath;
  27. /** @var int */
  28. protected $width;
  29. /** @var int */
  30. protected $height;
  31. /** @var \OC\Preview\Provider */
  32. protected $provider;
  33. /** @var int */
  34. protected $maxWidth = 1024;
  35. /** @var int */
  36. protected $maxHeight = 1024;
  37. /** @var bool */
  38. protected $scalingUp = false;
  39. /** @var int */
  40. protected $userId;
  41. /** @var \OC\Files\View */
  42. protected $rootView;
  43. /** @var \OC\Files\Storage\Storage */
  44. protected $storage;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $userManager = \OC::$server->getUserManager();
  48. $userManager->clearBackends();
  49. $backend = new \Test\Util\User\Dummy();
  50. $userManager->registerBackend($backend);
  51. $userId = $this->getUniqueID();
  52. $backend->createUser($userId, $userId);
  53. $this->loginAsUser($userId);
  54. $this->storage = new \OC\Files\Storage\Temporary([]);
  55. \OC\Files\Filesystem::mount($this->storage, [], '/' . $userId . '/');
  56. $this->rootView = new \OC\Files\View('');
  57. $this->rootView->mkdir('/' . $userId);
  58. $this->rootView->mkdir('/' . $userId . '/files');
  59. $this->userId = $userId;
  60. }
  61. protected function tearDown(): void {
  62. $this->logout();
  63. parent::tearDown();
  64. }
  65. public static function dimensionsDataProvider() {
  66. return [
  67. [-rand(5, 100), -rand(5, 100)],
  68. [rand(5, 100), rand(5, 100)],
  69. [-rand(5, 100), rand(5, 100)],
  70. [rand(5, 100), -rand(5, 100)],
  71. ];
  72. }
  73. /**
  74. * Launches all the tests we have
  75. *
  76. * @dataProvider dimensionsDataProvider
  77. * @requires extension imagick
  78. *
  79. * @param int $widthAdjustment
  80. * @param int $heightAdjustment
  81. */
  82. public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
  83. $ratio = round($this->width / $this->height, 2);
  84. $this->maxWidth = $this->width - $widthAdjustment;
  85. $this->maxHeight = $this->height - $heightAdjustment;
  86. // Testing code
  87. /*print_r("w $this->width ");
  88. print_r("h $this->height ");
  89. print_r("r $ratio ");*/
  90. $preview = $this->getPreview($this->provider);
  91. // The TXT provider uses the max dimensions to create its canvas,
  92. // so the ratio will always be the one of the max dimension canvas
  93. if (!$this->provider instanceof \OC\Preview\TXT) {
  94. $this->doesRatioMatch($preview, $ratio);
  95. }
  96. $this->doesPreviewFit($preview);
  97. }
  98. /**
  99. * Adds the test file to the filesystem
  100. *
  101. * @param string $fileName name of the file to create
  102. * @param string $fileContent path to file to use for test
  103. *
  104. * @return string
  105. */
  106. protected function prepareTestFile($fileName, $fileContent) {
  107. $imgData = file_get_contents($fileContent);
  108. $imgPath = '/' . $this->userId . '/files/' . $fileName;
  109. $this->rootView->file_put_contents($imgPath, $imgData);
  110. $scanner = $this->storage->getScanner();
  111. $scanner->scan('');
  112. return $imgPath;
  113. }
  114. /**
  115. * Retrieves a max size thumbnail can be created
  116. *
  117. * @param \OC\Preview\Provider $provider
  118. *
  119. * @return bool|\OCP\IImage
  120. */
  121. private function getPreview($provider) {
  122. $file = new File(\OC::$server->get(IRootFolder::class), $this->rootView, $this->imgPath);
  123. $preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
  124. if (get_class($this) === BitmapTest::class && $preview === null) {
  125. $this->markTestSkipped('An error occured while operating with Imagick.');
  126. }
  127. $this->assertNotEquals(false, $preview);
  128. $this->assertEquals(true, $preview->valid());
  129. return $preview;
  130. }
  131. /**
  132. * Checks if the preview ratio matches the original ratio
  133. *
  134. * @param \OCP\IImage $preview
  135. * @param int $ratio
  136. */
  137. private function doesRatioMatch($preview, $ratio) {
  138. $previewRatio = round($preview->width() / $preview->height(), 2);
  139. $this->assertEquals($ratio, $previewRatio);
  140. }
  141. /**
  142. * Tests if a max size preview of smaller dimensions can be created
  143. *
  144. * @param \OCP\IImage $preview
  145. */
  146. private function doesPreviewFit($preview) {
  147. $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2);
  148. $previewRatio = round($preview->width() / $preview->height(), 2);
  149. // Testing code
  150. /*print_r("mw $this->maxWidth ");
  151. print_r("mh $this->maxHeight ");
  152. print_r("mr $maxDimRatio ");
  153. $pw = $preview->width();
  154. $ph = $preview->height();
  155. print_r("pw $pw ");
  156. print_r("ph $ph ");
  157. print_r("pr $previewRatio ");*/
  158. if ($maxDimRatio < $previewRatio) {
  159. $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
  160. $this->assertLessThan($this->maxHeight, $preview->height());
  161. } elseif ($maxDimRatio > $previewRatio) {
  162. $this->assertLessThan($this->maxWidth, $preview->width());
  163. $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
  164. } else { // Original had to be resized
  165. $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
  166. $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
  167. }
  168. }
  169. }