AttachmentThumbnailTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. $hash = null;
  36. Event::handle('HashFile', [$file->getPathname(), &$hash]);
  37. $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
  38. $thumbs = [
  39. AttachmentThumbnail::getOrCreate($attachment, width: 1, height: 1, crop: false),
  40. AttachmentThumbnail::getOrCreate($attachment, width: 2, height: 2, crop: false),
  41. AttachmentThumbnail::getOrCreate($attachment, width: 3, height: 3, crop: false),
  42. $thumb = AttachmentThumbnail::getOrCreate($attachment, width: 4, height: 4, crop: false),
  43. ];
  44. static::assertSame($attachment, $thumb->getAttachment());
  45. $thumb->setAttachment(null);
  46. static::assertSame($attachment, $thumb->getAttachment());
  47. $sort = fn ($l, $r) => [$l->getWidth(), $l->getHeight()] <=> [$r->getWidth(), $r->getHeight()];
  48. $at_thumbs = F\sort($attachment->getThumbnails(), $sort);
  49. static::assertSame($thumbs, $at_thumbs);
  50. array_pop($thumbs);
  51. $thumb->delete(flush: true);
  52. $at_thumbs = F\sort($attachment->getThumbnails(), $sort);
  53. static::assertSame($thumbs, $at_thumbs);
  54. $attachment->deleteStorage();
  55. // This was deleted earlier, and the backed storage as well, so we can't generate another thumbnail
  56. static::assertThrows(NotStoredLocallyException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, width: 4, height: 4, crop: false));
  57. $attachment->kill();
  58. }
  59. public function testInvalidThumbnail()
  60. {
  61. parent::bootKernel();
  62. $file = new \SplFileInfo(INSTALLDIR . '/tests/sample-uploads/spreadsheet.ods');
  63. $hash = null;
  64. Event::handle('HashFile', [$file->getPathname(), &$hash]);
  65. $attachment = DB::findOneBy('attachment', ['filehash' => $hash]);
  66. static::assertThrows(ClientException::class, fn () => AttachmentThumbnail::getOrCreate($attachment, width: 1, height: 1, crop: false));
  67. }
  68. public function testPredictScalingValues()
  69. {
  70. // Test without cropping
  71. static::assertSame([100, 50], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 100, requested_height: 100, crop: false));
  72. static::assertSame([200, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 200, requested_height: 200, crop: false));
  73. static::assertSame([300, 150], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 300, requested_height: 300, crop: false));
  74. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 400, requested_height: 400, crop: false));
  75. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 600, requested_height: 600, crop: false));
  76. // Test with cropping
  77. static::assertSame([100, 100], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 100, requested_height: 100, crop: true));
  78. static::assertSame([200, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 200, requested_height: 200, crop: true));
  79. static::assertSame([300, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 300, requested_height: 300, crop: true));
  80. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 400, requested_height: 400, crop: true));
  81. static::assertSame([400, 200], AttachmentThumbnail::predictScalingValues(existing_width: 400, existing_height: 200, requested_width: 600, requested_height: 600, crop: true));
  82. }
  83. public function testGetHTMLAttributes()
  84. {
  85. parent::bootKernel();
  86. $attachment = DB::findBy('attachment', ['mimetype' => 'image/png'], limit: 1)[0];
  87. $w = $attachment->getWidth();
  88. $h = $attachment->getHeight();
  89. $thumb = AttachmentThumbnail::getOrCreate($attachment, width: $w, height: $h, crop: false);
  90. $id = $attachment->getId();
  91. $url = "/attachment/{$id}/thumbnail?w={$w}&h={$h}";
  92. static::assertSame($url, $thumb->getUrl());
  93. static::assertSame(['height' => $h, 'width' => $w, 'src' => $url], $thumb->getHTMLAttributes());
  94. }
  95. }