AttachmentThumbnailTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace App\Tests\Entity;
  19. use App\Core\DB\DB;
  20. use App\Core\Event;
  21. use App\Entity\AttachmentThumbnail;
  22. use App\Util\Exception\ClientException;
  23. use App\Util\Exception\NotStoredLocallyException;
  24. use App\Util\GNUsocialTestCase;
  25. use Functional as F;
  26. use Jchook\AssertThrows\AssertThrows;
  27. class AttachmentThumbnailTest extends GNUsocialTestCase
  28. {
  29. use AssertThrows;
  30. public function testAttachmentThumbnailLifecycle()
  31. {
  32. parent::bootKernel();
  33. // Data fixture already loaded this file, but we need to get its hash to find it
  34. $file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/attachment-lifecycle-target.jpg');
  35. Event::handle('HashFile', [$file->getPathname(), &$hash]);
  36. $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
  37. $thumbs = [
  38. AttachmentThumbnail::getOrCreate($attachment, width: 1, height: 1, crop: false),
  39. AttachmentThumbnail::getOrCreate($attachment, width: 2, height: 2, crop: false),
  40. AttachmentThumbnail::getOrCreate($attachment, width: 3, height: 3, crop: false),
  41. $thumb = AttachmentThumbnail::getOrCreate($attachment, width: 4, height: 4, crop: false),
  42. ];
  43. static::assertSame($attachment, $thumb->getAttachment());
  44. $thumb->setAttachment(null);
  45. static::assertSame($attachment, $thumb->getAttachment());
  46. $sort = fn ($l, $r) => [$l->getWidth(), $l->getHeight()] <=> [$r->getWidth(), $r->getHeight()];
  47. $at_thumbs = F\sort($attachment->getThumbnails(), $sort);
  48. static::assertSame($thumbs, $at_thumbs);
  49. array_pop($thumbs);
  50. $thumb->delete(flush: true);
  51. $at_thumbs = F\sort($attachment->getThumbnails(), $sort);
  52. static::assertSame($thumbs, $at_thumbs);
  53. $attachment->deleteStorage();
  54. // This was deleted earlier, and the backed storage as well, so we can't generate another thumbnail
  55. static::assertThrows(NotStoredLocallyException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, width: 4, height: 4, crop: false));
  56. $attachment->kill();
  57. }
  58. public function testInvalidThumbnail()
  59. {
  60. parent::bootKernel();
  61. $file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/spreadsheet.ods');
  62. Event::handle('HashFile', [$file->getPathname(), &$hash]);
  63. $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
  64. static::assertThrows(ClientException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, width: 1, height: 1, crop: false));
  65. }
  66. public function testPredictScalingValues()
  67. {
  68. // Test without cropping
  69. static::assertSame([100, 50], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 100, requested_height: 100, crop: false));
  70. static::assertSame([200, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 200, requested_height: 200, crop: false));
  71. static::assertSame([300, 150], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 300, requested_height: 300, crop: false));
  72. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 400, requested_height: 400, crop: false));
  73. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 600, requested_height: 600, crop: false));
  74. // Test with cropping
  75. static::assertSame([100, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 100, requested_height: 100, crop: true));
  76. static::assertSame([200, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 200, requested_height: 200, crop: true));
  77. static::assertSame([300, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 300, requested_height: 300, crop: true));
  78. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 400, requested_height: 400, crop: true));
  79. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 600, requested_height: 600, crop: true));
  80. }
  81. public function testGetHTMLAttributes()
  82. {
  83. parent::bootKernel();
  84. $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
  85. $w = $attachment->getWidth();
  86. $h = $attachment->getHeight();
  87. $thumb = AttachmentThumbnail::getOrCreate($attachment, width: $w, height: $h, crop: false);
  88. $id = $attachment->getId();
  89. $url = "/attachment/{$id}/thumbnail?w={$w}&h={$h}";
  90. static::assertSame($url, $thumb->getUrl());
  91. static::assertSame(['height' => $h, 'width' => $w, 'src' => $url], $thumb->getHTMLAttributes());
  92. }
  93. }