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