]> BookStack Code Mirror - bookstack/blob - tests/ErrorTest.php
Uploads: Added user-facing message for Laravel post limit handling
[bookstack] / tests / ErrorTest.php
1 <?php
2
3 namespace Tests;
4
5 use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
6 use Illuminate\Support\Facades\Log;
7
8 class ErrorTest extends TestCase
9 {
10     public function test_404_page_does_not_show_login()
11     {
12         // Due to middleware being handled differently this will not fail
13         // if our custom, middleware-loaded handler fails but this is here
14         // as a reminder and as a general check in the event of other issues.
15         $editor = $this->users->editor();
16         $editor->name = 'tester';
17         $editor->save();
18
19         $this->actingAs($editor);
20         $notFound = $this->get('/fgfdngldfnotfound');
21         $notFound->assertStatus(404);
22         $notFound->assertDontSeeText('Log in');
23         $notFound->assertSeeText('tester');
24     }
25
26     public function test_item_not_found_does_not_get_logged_to_file()
27     {
28         $this->actingAs($this->users->viewer());
29         $handler = $this->withTestLogger();
30         $book = $this->entities->book();
31
32         // Ensure we're seeing errors
33         Log::error('cat');
34         $this->assertTrue($handler->hasErrorThatContains('cat'));
35
36         $this->get('/books/arandomnotfouindbook');
37         $this->get($book->getUrl('/chapter/arandomnotfouindchapter'));
38         $this->get($book->getUrl('/chapter/arandomnotfouindpages'));
39
40         $this->assertCount(1, $handler->getRecords());
41     }
42
43     public function test_access_to_non_existing_image_location_provides_404_response()
44     {
45         $resp = $this->actingAs($this->users->viewer())->get('/uploads/images/gallery/2021-05/anonexistingimage.png');
46         $resp->assertStatus(404);
47         $resp->assertSeeText('Image Not Found');
48     }
49
50     public function test_posts_above_php_limit_shows_friendly_error()
51     {
52         // Fake super large JSON request
53         $resp = $this->asEditor()->call('GET', '/books', [], [], [], [
54             'CONTENT_LENGTH' => '10000000000',
55             'HTTP_ACCEPT' => 'application/json',
56         ]);
57
58         $resp->assertStatus(413);
59         $resp->assertJson(['error' => 'The server cannot receive the provided amount of data. Try again with less data or a smaller file.']);
60     }
61 }