preview.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Georg Ehrke <georg@ownCloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. class Preview extends TestCase {
  10. /**
  11. * @var string
  12. */
  13. private $user;
  14. /**
  15. * @var \OC\Files\View
  16. */
  17. private $rootView;
  18. /** @var \OC\Files\Storage\Storage */
  19. private $originalStorage;
  20. protected function setUp() {
  21. parent::setUp();
  22. // FIXME: use proper tearDown with $this->loginAsUser() and $this->logout()
  23. // (would currently break the tests for some reason)
  24. $this->originalStorage = \OC\Files\Filesystem::getStorage('/');
  25. // create a new user with his own filesystem view
  26. // this gets called by each test in this test class
  27. $this->user = $this->getUniqueID();
  28. \OC_User::setUserId($this->user);
  29. \OC\Files\Filesystem::init($this->user, '/' . $this->user . '/files');
  30. \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
  31. $this->rootView = new \OC\Files\View('');
  32. $this->rootView->mkdir('/'.$this->user);
  33. $this->rootView->mkdir('/'.$this->user.'/files');
  34. }
  35. protected function tearDown() {
  36. \OC\Files\Filesystem::clearMounts();
  37. \OC\Files\Filesystem::mount($this->originalStorage, array(), '/');
  38. parent::tearDown();
  39. }
  40. public function testIsMaxSizeWorking() {
  41. // Max size from config
  42. $maxX = 1024;
  43. $maxY = 1024;
  44. \OC::$server->getConfig()->setSystemValue('preview_max_x', $maxX);
  45. \OC::$server->getConfig()->setSystemValue('preview_max_y', $maxY);
  46. // Sample is 1680x1050 JPEG
  47. $sampleFile = '/' . $this->user . '/files/testimage.jpg';
  48. $this->rootView->file_put_contents($sampleFile, file_get_contents(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
  49. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  50. $fileId = $fileInfo['fileid'];
  51. $largeX = 1920;
  52. $largeY = 1080;
  53. $preview = new \OC\Preview($this->user, 'files/', 'testimage.jpg', $largeX, $largeY);
  54. $this->assertEquals($preview->isFileValid(), true);
  55. // There should be no cached copy
  56. $isCached = $preview->isCached($fileId);
  57. $this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '-max.png', $isCached);
  58. $this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
  59. $this->assertNotEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $largeX . '-' . $largeY . '.png', $isCached);
  60. // The returned preview should be of max size
  61. $image = $preview->getPreview();
  62. $this->assertEquals($image->width(), $maxX);
  63. $this->assertEquals($image->height(), $maxY);
  64. // The max thumbnail should be created
  65. $maxThumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '-max.png';
  66. $this->assertEquals($this->rootView->file_exists($maxThumbCacheFile), true);
  67. // A preview of the asked size should not have been created
  68. $thumbCacheFile = \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $largeX . '-' . $largeY . '.png';
  69. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
  70. // 2nd request should indicate that we have a cached copy of max dimension
  71. $isCached = $preview->isCached($fileId);
  72. $this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
  73. // Smaller previews should be based on the cached max preview
  74. $smallX = 50;
  75. $smallY = 50;
  76. $preview = new \OC\Preview($this->user, 'files/', 'testimage.jpg', $smallX, $smallY);
  77. $isCached = $preview->isCached($fileId);
  78. $this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $maxX . '-' . $maxY . '.png', $isCached);
  79. // A small preview should be created
  80. $image = $preview->getPreview();
  81. $this->assertEquals($image->width(), $smallX);
  82. $this->assertEquals($image->height(), $smallY);
  83. // The cache should contain the small preview
  84. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $smallX . '-' . $smallY . '.png';
  85. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  86. // 2nd request should indicate that we have a cached copy of the exact dimension
  87. $isCached = $preview->isCached($fileId);
  88. $this->assertEquals(\OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $smallX . '-' . $smallY . '.png', $isCached);
  89. }
  90. public function testIsPreviewDeleted() {
  91. $sampleFile = '/'.$this->user.'/files/test.txt';
  92. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  93. $x = 50;
  94. $y = 50;
  95. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  96. $preview->getPreview();
  97. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  98. $fileId = $fileInfo['fileid'];
  99. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
  100. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  101. $preview->deletePreview();
  102. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
  103. }
  104. public function testAreAllPreviewsDeleted() {
  105. $sampleFile = '/'.$this->user.'/files/test.txt';
  106. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  107. $x = 50;
  108. $y = 50;
  109. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  110. $preview->getPreview();
  111. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  112. $fileId = $fileInfo['fileid'];
  113. $thumbCacheFolder = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/';
  114. $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), true);
  115. $preview->deleteAllPreviews();
  116. $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false);
  117. }
  118. public function txtBlacklist() {
  119. $txt = 'random text file';
  120. return array(
  121. array('txt', $txt, false),
  122. );
  123. }
  124. /**
  125. * @dataProvider txtBlacklist
  126. */
  127. public function testIsTransparent($extension, $data, $expectedResult) {
  128. $x = 32;
  129. $y = 32;
  130. $sample = '/'.$this->user.'/files/test.'.$extension;
  131. $this->rootView->file_put_contents($sample, $data);
  132. $preview = new \OC\Preview($this->user, 'files/', 'test.'.$extension, $x, $y);
  133. $image = $preview->getPreview();
  134. $resource = $image->resource();
  135. //http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
  136. $colorIndex = imagecolorat($resource, 1, 1);
  137. $colorInfo = imagecolorsforindex($resource, $colorIndex);
  138. $this->assertEquals(
  139. $expectedResult,
  140. $colorInfo['alpha'] === 127,
  141. 'Failed asserting that only previews for text files are transparent.'
  142. );
  143. }
  144. public function testCreationFromCached() {
  145. $sampleFile = '/'.$this->user.'/files/test.txt';
  146. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  147. // create base preview
  148. $x = 150;
  149. $y = 150;
  150. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  151. $preview->getPreview();
  152. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  153. $fileId = $fileInfo['fileid'];
  154. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
  155. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  156. // create smaller previews
  157. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 50, 50);
  158. $isCached = $preview->isCached($fileId);
  159. $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
  160. }
  161. /*
  162. public function testScalingUp() {
  163. $sampleFile = '/'.$this->user.'/files/test.txt';
  164. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  165. // create base preview
  166. $x = 150;
  167. $y = 150;
  168. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  169. $preview->getPreview();
  170. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  171. $fileId = $fileInfo['fileid'];
  172. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
  173. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  174. // create bigger previews - with scale up
  175. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 250, 250);
  176. $isCached = $preview->isCached($fileId);
  177. $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
  178. }
  179. */
  180. }