preview.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 \PHPUnit_Framework_TestCase {
  10. /**
  11. * @var string
  12. */
  13. private $user;
  14. /**
  15. * @var \OC\Files\View
  16. */
  17. private $rootView;
  18. public function setUp() {
  19. $this->user = $this->initFS();
  20. $this->rootView = new \OC\Files\View('');
  21. $this->rootView->mkdir('/'.$this->user);
  22. $this->rootView->mkdir('/'.$this->user.'/files');
  23. }
  24. public function testIsPreviewDeleted() {
  25. $sampleFile = '/'.$this->user.'/files/test.txt';
  26. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  27. $x = 50;
  28. $y = 50;
  29. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  30. $preview->getPreview();
  31. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  32. $fileId = $fileInfo['fileid'];
  33. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
  34. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  35. $preview->deletePreview();
  36. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), false);
  37. }
  38. public function testAreAllPreviewsDeleted() {
  39. $sampleFile = '/'.$this->user.'/files/test.txt';
  40. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  41. $x = 50;
  42. $y = 50;
  43. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  44. $preview->getPreview();
  45. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  46. $fileId = $fileInfo['fileid'];
  47. $thumbCacheFolder = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/';
  48. $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), true);
  49. $preview->deleteAllPreviews();
  50. $this->assertEquals($this->rootView->is_dir($thumbCacheFolder), false);
  51. }
  52. public function testIsMaxSizeWorking() {
  53. $maxX = 250;
  54. $maxY = 250;
  55. \OC_Config::setValue('preview_max_x', $maxX);
  56. \OC_Config::setValue('preview_max_y', $maxY);
  57. $sampleFile = '/'.$this->user.'/files/test.txt';
  58. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  59. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 1000, 1000);
  60. $image = $preview->getPreview();
  61. $this->assertEquals($image->width(), $maxX);
  62. $this->assertEquals($image->height(), $maxY);
  63. }
  64. public function txtBlacklist() {
  65. $txt = 'random text file';
  66. $ics = file_get_contents(__DIR__ . '/../data/testcal.ics');
  67. $vcf = file_get_contents(__DIR__ . '/../data/testcontact.vcf');
  68. return array(
  69. array('txt', $txt, false),
  70. array('ics', $ics, true),
  71. array('vcf', $vcf, true),
  72. );
  73. }
  74. /**
  75. * @dataProvider txtBlacklist
  76. */
  77. public function testIsTransparent($extension, $data, $expectedResult) {
  78. $x = 32;
  79. $y = 32;
  80. $sample = '/'.$this->user.'/files/test.'.$extension;
  81. $this->rootView->file_put_contents($sample, $data);
  82. $preview = new \OC\Preview($this->user, 'files/', 'test.'.$extension, $x, $y);
  83. $image = $preview->getPreview();
  84. $resource = $image->resource();
  85. //http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
  86. $colorIndex = imagecolorat($resource, 1, 1);
  87. $colorInfo = imagecolorsforindex($resource, $colorIndex);
  88. $this->assertEquals(
  89. $expectedResult,
  90. $colorInfo['alpha'] === 127,
  91. 'Failed asserting that only previews for text files are transparent.'
  92. );
  93. }
  94. public function testCreationFromCached() {
  95. $sampleFile = '/'.$this->user.'/files/test.txt';
  96. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  97. // create base preview
  98. $x = 150;
  99. $y = 150;
  100. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  101. $preview->getPreview();
  102. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  103. $fileId = $fileInfo['fileid'];
  104. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
  105. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  106. // create smaller previews
  107. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 50, 50);
  108. $isCached = $preview->isCached($fileId);
  109. $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
  110. }
  111. /*
  112. public function testScalingUp() {
  113. $sampleFile = '/'.$this->user.'/files/test.txt';
  114. $this->rootView->file_put_contents($sampleFile, 'dummy file data');
  115. // create base preview
  116. $x = 150;
  117. $y = 150;
  118. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', $x, $y);
  119. $preview->getPreview();
  120. $fileInfo = $this->rootView->getFileInfo($sampleFile);
  121. $fileId = $fileInfo['fileid'];
  122. $thumbCacheFile = '/' . $this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/' . $x . '-' . $y . '.png';
  123. $this->assertEquals($this->rootView->file_exists($thumbCacheFile), true);
  124. // create bigger previews - with scale up
  125. $preview = new \OC\Preview($this->user, 'files/', 'test.txt', 250, 250);
  126. $isCached = $preview->isCached($fileId);
  127. $this->assertEquals($this->user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileId . '/150-150.png', $isCached);
  128. }
  129. */
  130. private function initFS() {
  131. // create a new user with his own filesystem view
  132. // this gets called by each test in this test class
  133. $user=uniqid();
  134. \OC_User::setUserId($user);
  135. \OC\Files\Filesystem::init($user, '/'.$user.'/files');
  136. \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
  137. return $user;
  138. }
  139. }