Provider.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. abstract class Provider extends \Test\TestCase {
  24. /** @var string */
  25. protected $imgPath;
  26. /** @var int */
  27. protected $width;
  28. /** @var int */
  29. protected $height;
  30. /** @var \OC\Preview\Provider */
  31. protected $provider;
  32. /** @var int */
  33. protected $maxWidth = 1024;
  34. /** @var int */
  35. protected $maxHeight = 1024;
  36. /** @var bool */
  37. protected $scalingUp = false;
  38. /** @var int */
  39. protected $userId;
  40. /** @var \OC\Files\View */
  41. protected $rootView;
  42. /** @var \OC\Files\Storage\Storage */
  43. protected $storage;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $userManager = \OC::$server->getUserManager();
  47. $userManager->clearBackends();
  48. $backend = new \Test\Util\User\Dummy();
  49. $userManager->registerBackend($backend);
  50. $userId = $this->getUniqueID();
  51. $backend->createUser($userId, $userId);
  52. $this->loginAsUser($userId);
  53. $this->storage = new \OC\Files\Storage\Temporary([]);
  54. \OC\Files\Filesystem::mount($this->storage, [], '/' . $userId . '/');
  55. $this->rootView = new \OC\Files\View('');
  56. $this->rootView->mkdir('/' . $userId);
  57. $this->rootView->mkdir('/' . $userId . '/files');
  58. $this->userId = $userId;
  59. }
  60. protected function tearDown(): void {
  61. $this->logout();
  62. parent::tearDown();
  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. * Launches all the tests we have
  74. *
  75. * @dataProvider dimensionsDataProvider
  76. * @requires extension imagick
  77. *
  78. * @param int $widthAdjustment
  79. * @param int $heightAdjustment
  80. */
  81. public function testGetThumbnail($widthAdjustment, $heightAdjustment) {
  82. $ratio = round($this->width / $this->height, 2);
  83. $this->maxWidth = $this->width - $widthAdjustment;
  84. $this->maxHeight = $this->height - $heightAdjustment;
  85. // Testing code
  86. /*print_r("w $this->width ");
  87. print_r("h $this->height ");
  88. print_r("r $ratio ");*/
  89. $preview = $this->getPreview($this->provider);
  90. // The TXT provider uses the max dimensions to create its canvas,
  91. // so the ratio will always be the one of the max dimension canvas
  92. if (!$this->provider instanceof \OC\Preview\TXT) {
  93. $this->doesRatioMatch($preview, $ratio);
  94. }
  95. $this->doesPreviewFit($preview);
  96. }
  97. /**
  98. * Adds the test file to the filesystem
  99. *
  100. * @param string $fileName name of the file to create
  101. * @param string $fileContent path to file to use for test
  102. *
  103. * @return string
  104. */
  105. protected function prepareTestFile($fileName, $fileContent) {
  106. $imgData = file_get_contents($fileContent);
  107. $imgPath = '/' . $this->userId . '/files/' . $fileName;
  108. $this->rootView->file_put_contents($imgPath, $imgData);
  109. $scanner = $this->storage->getScanner();
  110. $scanner->scan('');
  111. return $imgPath;
  112. }
  113. /**
  114. * Retrieves a max size thumbnail can be created
  115. *
  116. * @param \OC\Preview\Provider $provider
  117. *
  118. * @return bool|\OCP\IImage
  119. */
  120. private function getPreview($provider) {
  121. $file = new File(\OC::$server->getRootFolder(), $this->rootView, $this->imgPath);
  122. $preview = $provider->getThumbnail($file, $this->maxWidth, $this->maxHeight, $this->scalingUp);
  123. if (get_class($this) === BitmapTest::class && $preview === null) {
  124. $this->markTestSkipped('An error occured while operating with Imagick.');
  125. }
  126. $this->assertNotEquals(false, $preview);
  127. $this->assertEquals(true, $preview->valid());
  128. return $preview;
  129. }
  130. /**
  131. * Checks if the preview ratio matches the original ratio
  132. *
  133. * @param \OCP\IImage $preview
  134. * @param int $ratio
  135. */
  136. private function doesRatioMatch($preview, $ratio) {
  137. $previewRatio = round($preview->width() / $preview->height(), 2);
  138. $this->assertEquals($ratio, $previewRatio);
  139. }
  140. /**
  141. * Tests if a max size preview of smaller dimensions can be created
  142. *
  143. * @param \OCP\IImage $preview
  144. */
  145. private function doesPreviewFit($preview) {
  146. $maxDimRatio = round($this->maxWidth / $this->maxHeight, 2);
  147. $previewRatio = round($preview->width() / $preview->height(), 2);
  148. // Testing code
  149. /*print_r("mw $this->maxWidth ");
  150. print_r("mh $this->maxHeight ");
  151. print_r("mr $maxDimRatio ");
  152. $pw = $preview->width();
  153. $ph = $preview->height();
  154. print_r("pw $pw ");
  155. print_r("ph $ph ");
  156. print_r("pr $previewRatio ");*/
  157. if ($maxDimRatio < $previewRatio) {
  158. $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
  159. $this->assertLessThan($this->maxHeight, $preview->height());
  160. } elseif ($maxDimRatio > $previewRatio) {
  161. $this->assertLessThan($this->maxWidth, $preview->width());
  162. $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
  163. } else { // Original had to be resized
  164. $this->assertLessThanOrEqual($this->maxWidth, $preview->width());
  165. $this->assertLessThanOrEqual($this->maxHeight, $preview->height());
  166. }
  167. }
  168. }