preview.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. public function testIsPreviewDeleted() {
  11. $user = $this->initFS();
  12. $rootView = new \OC\Files\View('');
  13. $rootView->mkdir('/'.$user);
  14. $rootView->mkdir('/'.$user.'/files');
  15. $samplefile = '/'.$user.'/files/test.txt';
  16. $rootView->file_put_contents($samplefile, 'dummy file data');
  17. $x = 50;
  18. $y = 50;
  19. $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y);
  20. $preview->getPreview();
  21. $fileinfo = $rootView->getFileInfo($samplefile);
  22. $fileid = $fileinfo['fileid'];
  23. $thumbcachefile = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/' . $x . '-' . $y . '.png';
  24. $this->assertEquals($rootView->file_exists($thumbcachefile), true);
  25. $preview->deletePreview();
  26. $this->assertEquals($rootView->file_exists($thumbcachefile), false);
  27. }
  28. public function testAreAllPreviewsDeleted() {
  29. $user = $this->initFS();
  30. $rootView = new \OC\Files\View('');
  31. $rootView->mkdir('/'.$user);
  32. $rootView->mkdir('/'.$user.'/files');
  33. $samplefile = '/'.$user.'/files/test.txt';
  34. $rootView->file_put_contents($samplefile, 'dummy file data');
  35. $x = 50;
  36. $y = 50;
  37. $preview = new \OC\Preview($user, 'files/', 'test.txt', $x, $y);
  38. $preview->getPreview();
  39. $fileinfo = $rootView->getFileInfo($samplefile);
  40. $fileid = $fileinfo['fileid'];
  41. $thumbcachefolder = '/' . $user . '/' . \OC\Preview::THUMBNAILS_FOLDER . '/' . $fileid . '/';
  42. $this->assertEquals($rootView->is_dir($thumbcachefolder), true);
  43. $preview->deleteAllPreviews();
  44. $this->assertEquals($rootView->is_dir($thumbcachefolder), false);
  45. }
  46. public function testIsMaxSizeWorking() {
  47. $user = $this->initFS();
  48. $maxX = 250;
  49. $maxY = 250;
  50. \OC_Config::setValue('preview_max_x', $maxX);
  51. \OC_Config::setValue('preview_max_y', $maxY);
  52. $rootView = new \OC\Files\View('');
  53. $rootView->mkdir('/'.$user);
  54. $rootView->mkdir('/'.$user.'/files');
  55. $samplefile = '/'.$user.'/files/test.txt';
  56. $rootView->file_put_contents($samplefile, 'dummy file data');
  57. $preview = new \OC\Preview($user, 'files/', 'test.txt', 1000, 1000);
  58. $image = $preview->getPreview();
  59. $this->assertEquals($image->width(), $maxX);
  60. $this->assertEquals($image->height(), $maxY);
  61. }
  62. public function txtBlacklist() {
  63. $txt = 'random text file';
  64. $ics = file_get_contents(__DIR__ . '/../data/testcal.ics');
  65. $vcf = file_get_contents(__DIR__ . '/../data/testcontact.vcf');
  66. return array(
  67. array('txt', $txt, false),
  68. array('ics', $ics, true),
  69. array('vcf', $vcf, true),
  70. );
  71. }
  72. /**
  73. * @dataProvider txtBlacklist
  74. */
  75. public function testIsTransparent($extension, $data, $expectedResult) {
  76. $user = $this->initFS();
  77. $rootView = new \OC\Files\View('');
  78. $rootView->mkdir('/'.$user);
  79. $rootView->mkdir('/'.$user.'/files');
  80. $x = 32;
  81. $y = 32;
  82. $sample = '/'.$user.'/files/test.'.$extension;
  83. $rootView->file_put_contents($sample, $data);
  84. $preview = new \OC\Preview($user, 'files/', 'test.'.$extension, $x, $y);
  85. $image = $preview->getPreview();
  86. $resource = $image->resource();
  87. //http://stackoverflow.com/questions/5702953/imagecolorat-and-transparency
  88. $colorIndex = imagecolorat($resource, 1, 1);
  89. $colorInfo = imagecolorsforindex($resource, $colorIndex);
  90. $this->assertEquals(
  91. $expectedResult,
  92. $colorInfo['alpha'] === 127,
  93. 'Failed asserting that only previews for text files are transparent.'
  94. );
  95. }
  96. private function initFS() {
  97. // create a new user with his own filesystem view
  98. // this gets called by each test in this test class
  99. $user=uniqid();
  100. \OC_User::setUserId($user);
  101. \OC\Files\Filesystem::init($user, '/'.$user.'/files');
  102. \OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
  103. return $user;
  104. }
  105. }