]> BookStack Code Mirror - bookstack/blob - tests/ErrorTest.php
Sorting: Updated sort set command, Changed sort timestamp 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_404_page_does_not_non_visible_content()
27     {
28         $editor = $this->users->editor();
29         $book = $this->entities->book();
30
31         $this->actingAs($editor)->get($book->getUrl())->assertOk();
32
33         $this->permissions->disableEntityInheritedPermissions($book);
34
35         $this->actingAs($editor)->get($book->getUrl())->assertNotFound();
36     }
37
38     public function test_404_page_shows_visible_content_within_non_visible_parent()
39     {
40         $editor = $this->users->editor();
41         $book = $this->entities->book();
42         $page = $book->pages()->first();
43
44         $this->actingAs($editor)->get($page->getUrl())->assertOk();
45
46         $this->permissions->disableEntityInheritedPermissions($book);
47         $this->permissions->addEntityPermission($page, ['view'], $editor->roles()->first());
48
49         $resp = $this->actingAs($editor)->get($book->getUrl());
50         $resp->assertNotFound();
51         $resp->assertSee($page->name);
52         $resp->assertDontSee($book->name);
53     }
54
55     public function test_item_not_found_does_not_get_logged_to_file()
56     {
57         $this->actingAs($this->users->viewer());
58         $handler = $this->withTestLogger();
59         $book = $this->entities->book();
60
61         // Ensure we're seeing errors
62         Log::error('cat');
63         $this->assertTrue($handler->hasErrorThatContains('cat'));
64
65         $this->get('/books/arandomnotfouindbook');
66         $this->get($book->getUrl('/chapter/arandomnotfouindchapter'));
67         $this->get($book->getUrl('/chapter/arandomnotfouindpages'));
68
69         $this->assertCount(1, $handler->getRecords());
70     }
71
72     public function test_access_to_non_existing_image_location_provides_404_response()
73     {
74         $resp = $this->actingAs($this->users->viewer())->get('/uploads/images/gallery/2021-05/anonexistingimage.png');
75         $resp->assertStatus(404);
76         $resp->assertSeeText('Image Not Found');
77     }
78
79     public function test_posts_above_php_limit_shows_friendly_error()
80     {
81         // Fake super large JSON request
82         $resp = $this->asEditor()->call('GET', '/books', [], [], [], [
83             'CONTENT_LENGTH' => '10000000000',
84             'HTTP_ACCEPT' => 'application/json',
85         ]);
86
87         $resp->assertStatus(413);
88         $resp->assertJson(['error' => 'The server cannot receive the provided amount of data. Try again with less data or a smaller file.']);
89     }
90 }