1
0

Provider.php 5.0 KB

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