AttachmentTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Controller;
  20. use App\Core\DB\DB;
  21. use App\Util\GNUsocialTestCase;
  22. class AttachmentTest extends GNUsocialTestCase
  23. {
  24. public function testNoAttachmentID()
  25. {
  26. // This calls static::bootKernel(), and creates a "client" that is acting as the browser
  27. $client = static::createClient();
  28. $client->request('GET', '/attachment');
  29. $this->assertResponseStatusCodeSame(404);
  30. $client->request('GET', '/attachment/-1');
  31. $this->assertResponseStatusCodeSame(404);
  32. $client->request('GET', '/attachment/asd');
  33. $this->assertResponseStatusCodeSame(404);
  34. $client->request('GET', '/attachment/0');
  35. // In the meantime, throwing ClientException doesn't actually result in the reaching the UI, as it's intercepted
  36. // by the helpful framework that displays the stack traces and such. This should be easily fixable when we have
  37. // our own error pages
  38. $this->assertSelectorTextContains('.stacktrace', 'ClientException');
  39. }
  40. private function testAttachment(string $suffix = '')
  41. {
  42. $client = static::createClient();
  43. $id = DB::findOneBy('attachment', ['filehash' => '5d8ee7ead51a28803b4ee5cb2306a0b90b6ba570f1e5bcc2209926f6ab08e7ea'])->getId();
  44. $crawler = $client->request('GET', "/attachment/{$id}{$suffix}");
  45. }
  46. public function testAttachmentShow()
  47. {
  48. $this->testAttachment();
  49. $this->assertResponseIsSuccessful();
  50. $this->assertSelectorTextContains('figure figcaption', '5d8ee7ead51a28803b4ee5cb2306a0b90b6ba570f1e5bcc2209926f6ab08e7ea');
  51. }
  52. public function testAttachmentView()
  53. {
  54. $this->testAttachment('/view');
  55. $this->assertResponseIsSuccessful();
  56. }
  57. public function testAttachmentViewNotStored()
  58. {
  59. $client = static::createClient();
  60. $last_attachment = DB::findBy('attachment', [], order_by: ['id' => 'DESC'], limit: 1)[0];
  61. $id = $last_attachment->getId() + 1;
  62. $crawler = $client->request('GET', "/attachment/{$id}/view");
  63. $this->assertResponseStatusCodeSame(500); // TODO (exception page) 404
  64. $this->assertSelectorTextContains('.stacktrace', 'ClientException');
  65. }
  66. public function testAttachmentDownload()
  67. {
  68. $this->testAttachment('/download');
  69. $this->assertResponseIsSuccessful();
  70. }
  71. public function testAttachmentThumbnailSmall()
  72. {
  73. $this->testAttachment('/thumbnail/small');
  74. $this->assertResponseIsSuccessful();
  75. }
  76. public function testAttachmentThumbnailMedium()
  77. {
  78. $this->testAttachment('/thumbnail/medium');
  79. $this->assertResponseIsSuccessful();
  80. }
  81. public function testAttachmentThumbnailBig()
  82. {
  83. $this->testAttachment('/thumbnail/big');
  84. $this->assertResponseIsSuccessful();
  85. }
  86. }