]> BookStack Code Mirror - bookstack/blob - tests/PublicActionTest.php
Started new permission-caching/querying model
[bookstack] / tests / PublicActionTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Auth\Permissions\JointPermissionBuilder;
6 use BookStack\Auth\Permissions\RolePermission;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\User;
9 use BookStack\Entities\Models\Book;
10 use BookStack\Entities\Models\Chapter;
11 use Illuminate\Support\Facades\Auth;
12 use Illuminate\Support\Facades\View;
13
14 class PublicActionTest extends TestCase
15 {
16     public function test_app_not_public()
17     {
18         $this->setSettings(['app-public' => 'false']);
19         $book = $this->entities->book();
20         $this->get('/books')->assertRedirect('/login');
21         $this->get($book->getUrl())->assertRedirect('/login');
22
23         $page = $this->entities->page();
24         $this->get($page->getUrl())->assertRedirect('/login');
25     }
26
27     public function test_login_link_visible()
28     {
29         $this->setSettings(['app-public' => 'true']);
30         $resp = $this->get('/');
31         $this->withHtml($resp)->assertElementExists('a[href="' . url('/login') . '"]');
32     }
33
34     public function test_register_link_visible_when_enabled()
35     {
36         $this->setSettings(['app-public' => 'true']);
37         $home = $this->get('/');
38         $home->assertSee(url('/login'));
39         $home->assertDontSee(url('/register'));
40
41         $this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']);
42         $home = $this->get('/');
43         $home->assertSee(url('/login'));
44         $home->assertSee(url('/register'));
45     }
46
47     public function test_books_viewable()
48     {
49         $this->setSettings(['app-public' => 'true']);
50         $books = Book::query()->orderBy('name', 'asc')->take(10)->get();
51         $bookToVisit = $books[1];
52
53         // Check books index page is showing
54         $resp = $this->get('/books');
55         $resp->assertStatus(200);
56         $resp->assertSee($books[0]->name);
57
58         // Check individual book page is showing and it's child contents are visible.
59         $resp = $this->get($bookToVisit->getUrl());
60         $resp->assertSee($bookToVisit->name);
61         $resp->assertSee($bookToVisit->chapters()->first()->name);
62     }
63
64     public function test_chapters_viewable()
65     {
66         $this->setSettings(['app-public' => 'true']);
67         /** @var Chapter $chapterToVisit */
68         $chapterToVisit = Chapter::query()->first();
69         $pageToVisit = $chapterToVisit->pages()->first();
70
71         // Check chapters index page is showing
72         $resp = $this->get($chapterToVisit->getUrl());
73         $resp->assertStatus(200);
74         $resp->assertSee($chapterToVisit->name);
75         // Check individual chapter page is showing and it's child contents are visible.
76         $resp->assertSee($pageToVisit->name);
77         $resp = $this->get($pageToVisit->getUrl());
78         $resp->assertStatus(200);
79         $resp->assertSee($chapterToVisit->book->name);
80         $resp->assertSee($chapterToVisit->name);
81     }
82
83     public function test_public_page_creation()
84     {
85         $this->setSettings(['app-public' => 'true']);
86         $publicRole = Role::getSystemRole('public');
87         // Grant all permissions to public
88         $publicRole->permissions()->detach();
89         foreach (RolePermission::all() as $perm) {
90             $publicRole->attachPermission($perm);
91         }
92         user()->clearPermissionCache();
93
94         $chapter = $this->entities->chapter();
95         $resp = $this->get($chapter->getUrl());
96         $resp->assertSee('New Page');
97         $this->withHtml($resp)->assertElementExists('a[href="' . $chapter->getUrl('/create-page') . '"]');
98
99         $resp = $this->get($chapter->getUrl('/create-page'));
100         $resp->assertSee('Continue');
101         $resp->assertSee('Page Name');
102         $this->withHtml($resp)->assertElementExists('form[action="' . $chapter->getUrl('/create-guest-page') . '"]');
103
104         $resp = $this->post($chapter->getUrl('/create-guest-page'), ['name' => 'My guest page']);
105         $resp->assertRedirect($chapter->book->getUrl('/page/my-guest-page/edit'));
106
107         $user = User::getDefault();
108         $this->assertDatabaseHas('pages', [
109             'name'       => 'My guest page',
110             'chapter_id' => $chapter->id,
111             'created_by' => $user->id,
112             'updated_by' => $user->id,
113         ]);
114     }
115
116     public function test_content_not_listed_on_404_for_public_users()
117     {
118         $page = $this->entities->page();
119         $page->fill(['name' => 'my testing random unique page name'])->save();
120         $this->asAdmin()->get($page->getUrl()); // Fake visit to show on recents
121         $resp = $this->get('/cats/dogs/hippos');
122         $resp->assertStatus(404);
123         $resp->assertSee($page->name);
124         View::share('pageTitle', '');
125
126         Auth::logout();
127         $resp = $this->get('/cats/dogs/hippos');
128         $resp->assertStatus(404);
129         $resp->assertDontSee($page->name);
130     }
131
132     public function test_robots_effected_by_public_status()
133     {
134         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
135
136         $this->setSettings(['app-public' => 'true']);
137
138         $resp = $this->get('/robots.txt');
139         $resp->assertSee("User-agent: *\nDisallow:");
140         $resp->assertDontSee('Disallow: /');
141     }
142
143     public function test_robots_effected_by_setting()
144     {
145         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
146
147         config()->set('app.allow_robots', true);
148
149         $resp = $this->get('/robots.txt');
150         $resp->assertSee("User-agent: *\nDisallow:");
151         $resp->assertDontSee('Disallow: /');
152
153         // Check config overrides app-public setting
154         config()->set('app.allow_robots', false);
155         $this->setSettings(['app-public' => 'true']);
156         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /");
157     }
158
159     public function test_public_view_then_login_redirects_to_previous_content()
160     {
161         $this->setSettings(['app-public' => 'true']);
162         $book = $this->entities->book();
163         $resp = $this->get($book->getUrl());
164         $resp->assertSee($book->name);
165
166         $this->get('/login');
167         $resp = $this->post('/login', ['email' => '[email protected]', 'password' => 'password']);
168         $resp->assertRedirect($book->getUrl());
169     }
170
171     public function test_access_hidden_content_then_login_redirects_to_intended_content()
172     {
173         $this->setSettings(['app-public' => 'true']);
174         $book = $this->entities->book();
175         $this->permissions->setEntityPermissions($book);
176
177         $resp = $this->get($book->getUrl());
178         $resp->assertSee('Book not found');
179
180         $this->get('/login');
181         $resp = $this->post('/login', ['email' => '[email protected]', 'password' => 'password']);
182         $resp->assertRedirect($book->getUrl());
183         $this->followRedirects($resp)->assertSee($book->name);
184     }
185 }