5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Permissions\Models\RolePermission;
8 use BookStack\Users\Models\Role;
9 use BookStack\Users\Models\User;
10 use Illuminate\Support\Facades\Auth;
11 use Illuminate\Support\Facades\View;
13 class PublicActionTest extends TestCase
15 public function test_app_not_public()
17 $this->setSettings(['app-public' => 'false']);
18 $book = $this->entities->book();
19 $this->get('/books')->assertRedirect('/login');
20 $this->get($book->getUrl())->assertRedirect('/login');
22 $page = $this->entities->page();
23 $this->get($page->getUrl())->assertRedirect('/login');
26 public function test_login_link_visible()
28 $this->setSettings(['app-public' => 'true']);
29 $resp = $this->get('/');
30 $this->withHtml($resp)->assertElementExists('a[href="' . url('/login') . '"]');
33 public function test_register_link_visible_when_enabled()
35 $this->setSettings(['app-public' => 'true']);
36 $home = $this->get('/');
37 $home->assertSee(url('/login'));
38 $home->assertDontSee(url('/register'));
40 $this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']);
41 $home = $this->get('/');
42 $home->assertSee(url('/login'));
43 $home->assertSee(url('/register'));
46 public function test_books_viewable()
48 $this->setSettings(['app-public' => 'true']);
49 $books = Book::query()->orderBy('name', 'asc')->take(10)->get();
50 $bookToVisit = $books[1];
52 // Check books index page is showing
53 $resp = $this->get('/books');
54 $resp->assertStatus(200);
55 $resp->assertSee($books[0]->name);
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);
63 public function test_chapters_viewable()
65 $this->setSettings(['app-public' => 'true']);
66 /** @var Chapter $chapterToVisit */
67 $chapterToVisit = Chapter::query()->first();
68 $pageToVisit = $chapterToVisit->pages()->first();
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);
82 public function test_public_page_creation()
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);
91 user()->clearPermissionCache();
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') . '"]');
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') . '"]');
103 $resp = $this->post($chapter->getUrl('/create-guest-page'), ['name' => 'My guest page']);
104 $resp->assertRedirect($chapter->book->getUrl('/page/my-guest-page/edit'));
106 $user = $this->users->guest();
107 $this->assertDatabaseHas('pages', [
108 'name' => 'My guest page',
109 'chapter_id' => $chapter->id,
110 'created_by' => $user->id,
111 'updated_by' => $user->id,
115 public function test_content_not_listed_on_404_for_public_users()
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', '');
126 $resp = $this->get('/cats/dogs/hippos');
127 $resp->assertStatus(404);
128 $resp->assertDontSee($page->name);
131 public function test_default_favicon_file_created_upon_access()
133 $faviconPath = public_path('favicon.ico');
134 if (file_exists($faviconPath)) {
135 unlink($faviconPath);
138 $this->assertFileDoesNotExist($faviconPath);
139 $this->get('/favicon.ico');
140 $this->assertFileExists($faviconPath);
143 public function test_public_view_then_login_redirects_to_previous_content()
145 $this->setSettings(['app-public' => 'true']);
146 $book = $this->entities->book();
147 $resp = $this->get($book->getUrl());
148 $resp->assertSee($book->name);
150 $this->get('/login');
152 $resp->assertRedirect($book->getUrl());
155 public function test_access_hidden_content_then_login_redirects_to_intended_content()
157 $this->setSettings(['app-public' => 'true']);
158 $book = $this->entities->book();
159 $this->permissions->setEntityPermissions($book);
161 $resp = $this->get($book->getUrl());
162 $resp->assertSee('Book not found');
164 $this->get('/login');
166 $resp->assertRedirect($book->getUrl());
167 $this->followRedirects($resp)->assertSee($book->name);
170 public function test_public_view_can_take_on_other_roles()
172 $this->setSettings(['app-public' => 'true']);
173 $newRole = $this->users->attachNewRole($this->users->guest(), []);
174 $page = $this->entities->page();
175 $this->permissions->disableEntityInheritedPermissions($page);
176 $this->permissions->addEntityPermission($page, ['view', 'update'], $newRole);
178 $resp = $this->get($page->getUrl());
181 $this->withHtml($resp)->assertLinkExists($page->getUrl('/edit'));
184 public function test_public_user_cannot_view_or_update_their_profile()
186 $this->setSettings(['app-public' => 'true']);
187 $guest = $this->users->guest();
189 $resp = $this->get($guest->getEditUrl());
190 $this->assertPermissionError($resp);
192 $resp = $this->put($guest->getEditUrl(), ['name' => 'My new guest name']);
193 $this->assertPermissionError($resp);