AttachmentTest.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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\Core;
  19. use App\Core\DB\DB;
  20. use App\Util\GNUsocialTestCase;
  21. class AttachmentTest extends GNUsocialTestCase
  22. {
  23. public function testNoAttachmentID()
  24. {
  25. // This calls static::bootKernel(), and creates a "client" that is acting as the browser
  26. $client = static::createClient();
  27. $client->request('GET', '/attachment');
  28. $this->assertResponseStatusCodeSame(404);
  29. $client->request('GET', '/attachment/-1');
  30. $this->assertResponseStatusCodeSame(404);
  31. $client->request('GET', '/attachment/asd');
  32. $this->assertResponseStatusCodeSame(404);
  33. $client->request('GET', '/attachment/0');
  34. // In the meantime, throwing ClientException doesn't actually result in the reaching the UI, as it's intercepted
  35. // by the helpful framework that displays the stack traces and such. This should be easily fixable when we have
  36. // our own error pages
  37. $this->assertSelectorTextContains('.stacktrace', 'ClientException');
  38. }
  39. private function testAttachment(string $suffix = '')
  40. {
  41. $client = static::createClient();
  42. $id = DB::findOneBy('attachment', ['filehash' => '5d8ee7ead51a28803b4ee5cb2306a0b90b6ba570f1e5bcc2209926f6ab08e7ea'])->getId();
  43. $crawler = $client->request('GET', "/attachment/{$id}{$suffix}");
  44. }
  45. public function testAttachmentShow()
  46. {
  47. $this->testAttachment();
  48. $this->assertResponseIsSuccessful();
  49. $this->assertSelectorTextContains('figure figcaption', '5d8ee7ead51a28803b4ee5cb2306a0b90b6ba570f1e5bcc2209926f6ab08e7ea');
  50. }
  51. public function testAttachmentView()
  52. {
  53. $this->testAttachment('/view');
  54. $this->assertResponseIsSuccessful();
  55. }
  56. public function testAttachmentViewNotStored()
  57. {
  58. $client = static::createClient();
  59. $last_attachment = DB::findBy('attachment', [], orderBy: ['id' => 'DESC'], limit: 1)[0];
  60. $id = $last_attachment->getId() + 1;
  61. $crawler = $client->request('GET', "/attachment/{$id}/view");
  62. $this->assertResponseStatusCodeSame(500); // TODO (exception page) 404
  63. $this->assertSelectorTextContains('.stacktrace', 'ClientException');
  64. }
  65. public function testAttachmentDownload()
  66. {
  67. $this->testAttachment('/download');
  68. $this->assertResponseIsSuccessful();
  69. }
  70. public function testAttachmentThumbnail()
  71. {
  72. $this->testAttachment('/thumbnail');
  73. $this->assertResponseIsSuccessful();
  74. }
  75. public function testAttachmentThumbnailWrongSize()
  76. {
  77. $this->testAttachment('/thumbnail?w=1&h=1');
  78. $this->assertSelectorTextContains('.stacktrace', 'ClientException');
  79. // $this->assertResponseStatusCodeSame(400);
  80. }
  81. }