| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests; | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Permissions\Models\RolePermission; | 
					
						
							|  |  |  | use BookStack\Users\Models\Role; | 
					
						
							|  |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2021-09-26 22:48:22 +08:00
										 |  |  | use Illuminate\Support\Facades\Auth; | 
					
						
							| 
									
										
										
										
											2021-04-21 04:59:17 +08:00
										 |  |  | use Illuminate\Support\Facades\View; | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  | class PublicActionTest extends TestCase | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     public function test_app_not_public() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'false']); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->get('/books')->assertRedirect('/login'); | 
					
						
							|  |  |  |         $this->get($book->getUrl())->assertRedirect('/login'); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->get($page->getUrl())->assertRedirect('/login'); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 23:01:16 +08:00
										 |  |  |     public function test_login_link_visible() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get('/'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('a[href="' . url('/login') . '"]'); | 
					
						
							| 
									
										
										
										
											2019-01-05 23:01:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_register_link_visible_when_enabled() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $home = $this->get('/'); | 
					
						
							|  |  |  |         $home->assertSee(url('/login')); | 
					
						
							|  |  |  |         $home->assertDontSee(url('/register')); | 
					
						
							| 
									
										
										
										
											2019-01-05 23:01:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $home = $this->get('/'); | 
					
						
							|  |  |  |         $home->assertSee(url('/login')); | 
					
						
							|  |  |  |         $home->assertSee(url('/register')); | 
					
						
							| 
									
										
										
										
											2019-01-05 23:01:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |     public function test_books_viewable() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $books = Book::query()->orderBy('name', 'asc')->take(10)->get(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $bookToVisit = $books[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check books index page is showing
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get('/books'); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertSee($books[0]->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check individual book page is showing and it's child contents are visible.
 | 
					
						
							|  |  |  |         $resp = $this->get($bookToVisit->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSee($bookToVisit->name); | 
					
						
							|  |  |  |         $resp->assertSee($bookToVisit->chapters()->first()->name); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapters_viewable() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         /** @var Chapter $chapterToVisit */ | 
					
						
							|  |  |  |         $chapterToVisit = Chapter::query()->first(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $pageToVisit = $chapterToVisit->pages()->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check chapters index page is showing
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get($chapterToVisit->getUrl()); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertSee($chapterToVisit->name); | 
					
						
							|  |  |  |         // Check individual chapter page is showing and it's child contents are visible.
 | 
					
						
							|  |  |  |         $resp->assertSee($pageToVisit->name); | 
					
						
							|  |  |  |         $resp = $this->get($pageToVisit->getUrl()); | 
					
						
							|  |  |  |         $resp->assertStatus(200); | 
					
						
							|  |  |  |         $resp->assertSee($chapterToVisit->book->name); | 
					
						
							|  |  |  |         $resp->assertSee($chapterToVisit->name); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_public_page_creation() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $publicRole = Role::getSystemRole('public'); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         // Grant all permissions to public
 | 
					
						
							|  |  |  |         $publicRole->permissions()->detach(); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         foreach (RolePermission::all() as $perm) { | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |             $publicRole->attachPermission($perm); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-07-17 04:50:42 +08:00
										 |  |  |         user()->clearPermissionCache(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get($chapter->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSee('New Page'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementExists('a[href="' . $chapter->getUrl('/create-page') . '"]'); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get($chapter->getUrl('/create-page')); | 
					
						
							|  |  |  |         $resp->assertSee('Continue'); | 
					
						
							|  |  |  |         $resp->assertSee('Page Name'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementExists('form[action="' . $chapter->getUrl('/create-guest-page') . '"]'); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->post($chapter->getUrl('/create-guest-page'), ['name' => 'My guest page']); | 
					
						
							|  |  |  |         $resp->assertRedirect($chapter->book->getUrl('/page/my-guest-page/edit')); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-16 20:18:35 +08:00
										 |  |  |         $user = $this->users->guest(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->assertDatabaseHas('pages', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'name'       => 'My guest page', | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |             'chapter_id' => $chapter->id, | 
					
						
							|  |  |  |             'created_by' => $user->id, | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'updated_by' => $user->id, | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 05:19:29 +08:00
										 |  |  |     public function test_content_not_listed_on_404_for_public_users() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2021-04-21 04:59:17 +08:00
										 |  |  |         $page->fill(['name' => 'my testing random unique page name'])->save(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->asAdmin()->get($page->getUrl()); // Fake visit to show on recents
 | 
					
						
							|  |  |  |         $resp = $this->get('/cats/dogs/hippos'); | 
					
						
							|  |  |  |         $resp->assertStatus(404); | 
					
						
							|  |  |  |         $resp->assertSee($page->name); | 
					
						
							| 
									
										
										
										
											2021-04-21 04:59:17 +08:00
										 |  |  |         View::share('pageTitle', ''); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         Auth::logout(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get('/cats/dogs/hippos'); | 
					
						
							|  |  |  |         $resp->assertStatus(404); | 
					
						
							|  |  |  |         $resp->assertDontSee($page->name); | 
					
						
							| 
									
										
										
										
											2017-02-06 05:19:29 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  |     public function test_robots_effected_by_public_status() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get('/robots.txt'); | 
					
						
							|  |  |  |         $resp->assertSee("User-agent: *\nDisallow:"); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $resp->assertDontSee('Disallow: /'); | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_robots_effected_by_setting() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         config()->set('app.allow_robots', true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get('/robots.txt'); | 
					
						
							|  |  |  |         $resp->assertSee("User-agent: *\nDisallow:"); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |         $resp->assertDontSee('Disallow: /'); | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Check config overrides app-public setting
 | 
					
						
							|  |  |  |         config()->set('app.allow_robots', false); | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->get('/robots.txt')->assertSee("User-agent: *\nDisallow: /"); | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-10 05:16:27 +08:00
										 |  |  |     public function test_default_favicon_file_created_upon_access() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $faviconPath = public_path('favicon.ico'); | 
					
						
							|  |  |  |         if (file_exists($faviconPath)) { | 
					
						
							|  |  |  |             unlink($faviconPath); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertFileDoesNotExist($faviconPath); | 
					
						
							|  |  |  |         $this->get('/favicon.ico'); | 
					
						
							|  |  |  |         $this->assertFileExists($faviconPath); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |     public function test_public_view_then_login_redirects_to_previous_content() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get($book->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSee($book->name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get('/login'); | 
					
						
							|  |  |  |         $resp = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl()); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_access_hidden_content_then_login_redirects_to_intended_content() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->setEntityPermissions($book); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $resp = $this->get($book->getUrl()); | 
					
						
							|  |  |  |         $resp->assertSee('Book not found'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get('/login'); | 
					
						
							|  |  |  |         $resp = $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl()); | 
					
						
							|  |  |  |         $this->followRedirects($resp)->assertSee($book->name); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-06-10 18:37:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_public_view_can_take_on_other_roles() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2023-09-16 20:18:35 +08:00
										 |  |  |         $newRole = $this->users->attachNewRole($this->users->guest(), []); | 
					
						
							| 
									
										
										
										
											2023-06-10 18:37:01 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							|  |  |  |         $this->permissions->disableEntityInheritedPermissions($page); | 
					
						
							|  |  |  |         $this->permissions->addEntityPermission($page, ['view', 'update'], $newRole); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($page->getUrl()); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertLinkExists($page->getUrl('/edit')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-08-26 21:07:48 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_public_user_cannot_view_or_update_their_profile() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  |         $guest = $this->users->guest(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($guest->getEditUrl()); | 
					
						
							|  |  |  |         $this->assertPermissionError($resp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->put($guest->getEditUrl(), ['name' => 'My new guest name']); | 
					
						
							|  |  |  |         $this->assertPermissionError($resp); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |