| 
									
										
										
										
											2017-02-04 19:58:42 +08:00
										 |  |  | <?php namespace Tests; | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  | use Auth; | 
					
						
							|  |  |  | use BookStack\Auth\Permissions\PermissionService; | 
					
						
							|  |  |  | use BookStack\Auth\Permissions\RolePermission; | 
					
						
							|  |  |  | use BookStack\Auth\Role; | 
					
						
							|  |  |  | use BookStack\Auth\User; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-26 03:35:40 +08:00
										 |  |  | class PublicActionTest extends BrowserKitTest | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_app_not_public() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'false']); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $book = Book::orderBy('name', 'asc')->first(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $this->visit('/books')->seePageIs('/login'); | 
					
						
							|  |  |  |         $this->visit($book->getUrl())->seePageIs('/login'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $page = Page::first(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $this->visit($page->getUrl())->seePageIs('/login'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-05 23:01:16 +08:00
										 |  |  |     public function test_login_link_visible() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  |         $this->visit('/')->see(url('/login')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_register_link_visible_when_enabled() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->visit('/')->see(url('/login')); | 
					
						
							|  |  |  |         $this->visit('/')->dontSee(url('/register')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true', 'registration-enabled' => 'true']); | 
					
						
							|  |  |  |         $this->visit('/')->see(url('/login')); | 
					
						
							|  |  |  |         $this->visit('/')->see(url('/register')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |     public function test_books_viewable() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $books = Book::orderBy('name', 'asc')->take(10)->get(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $bookToVisit = $books[1]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check books index page is showing
 | 
					
						
							|  |  |  |         $this->visit('/books') | 
					
						
							|  |  |  |             ->seeStatusCode(200) | 
					
						
							|  |  |  |             ->see($books[0]->name) | 
					
						
							|  |  |  |             // Check individual book page is showing and it's child contents are visible.
 | 
					
						
							|  |  |  |             ->click($bookToVisit->name) | 
					
						
							|  |  |  |             ->seePageIs($bookToVisit->getUrl()) | 
					
						
							|  |  |  |             ->see($bookToVisit->name) | 
					
						
							|  |  |  |             ->see($bookToVisit->chapters()->first()->name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapters_viewable() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $chapterToVisit = Chapter::first(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $pageToVisit = $chapterToVisit->pages()->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check chapters index page is showing
 | 
					
						
							|  |  |  |         $this->visit($chapterToVisit->getUrl()) | 
					
						
							|  |  |  |             ->seeStatusCode(200) | 
					
						
							|  |  |  |             ->see($chapterToVisit->name) | 
					
						
							|  |  |  |             // Check individual chapter page is showing and it's child contents are visible.
 | 
					
						
							|  |  |  |             ->see($pageToVisit->name) | 
					
						
							|  |  |  |             ->click($pageToVisit->name) | 
					
						
							|  |  |  |             ->see($chapterToVisit->book->name) | 
					
						
							|  |  |  |             ->see($chapterToVisit->name) | 
					
						
							|  |  |  |             ->seePageIs($pageToVisit->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $this->app[PermissionService::class]->buildJointPermissionForRole($publicRole); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $chapter = Chapter::first(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $this->visit($chapter->book->getUrl()); | 
					
						
							|  |  |  |         $this->visit($chapter->getUrl()) | 
					
						
							|  |  |  |             ->click('New Page') | 
					
						
							| 
									
										
										
										
											2016-12-04 02:35:40 +08:00
										 |  |  |             ->see('New Page') | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |             ->seePageIs($chapter->getUrl('/create-page')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->submitForm('Continue', [ | 
					
						
							|  |  |  |             'name' => 'My guest page' | 
					
						
							|  |  |  |         ])->seePageIs($chapter->book->getUrl('/page/my-guest-page/edit')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $user = User::getDefault(); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         $this->seeInDatabase('pages', [ | 
					
						
							|  |  |  |             'name' => 'My guest page', | 
					
						
							|  |  |  |             'chapter_id' => $chapter->id, | 
					
						
							|  |  |  |             'created_by' => $user->id, | 
					
						
							|  |  |  |             'updated_by' => $user->id | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-06 05:19:29 +08:00
										 |  |  |     public function test_content_not_listed_on_404_for_public_users() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         $page = Page::first(); | 
					
						
							| 
									
										
										
										
											2017-02-06 05:19:29 +08:00
										 |  |  |         $this->asAdmin()->visit($page->getUrl()); | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |         Auth::logout(); | 
					
						
							| 
									
										
										
										
											2017-02-06 05:19:29 +08:00
										 |  |  |         view()->share('pageTitle', ''); | 
					
						
							|  |  |  |         $this->forceVisit('/cats/dogs/hippos'); | 
					
						
							|  |  |  |         $this->dontSee($page->name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-31 19:41:40 +08:00
										 |  |  |     public function test_robots_effected_by_public_status() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->visit('/robots.txt'); | 
					
						
							|  |  |  |         $this->seeText("User-agent: *\nDisallow: /"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  |         $this->visit('/robots.txt'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->seeText("User-agent: *\nDisallow:"); | 
					
						
							|  |  |  |         $this->dontSeeText("Disallow: /"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_robots_effected_by_setting() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->visit('/robots.txt'); | 
					
						
							|  |  |  |         $this->seeText("User-agent: *\nDisallow: /"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         config()->set('app.allow_robots', true); | 
					
						
							|  |  |  |         $this->visit('/robots.txt'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->seeText("User-agent: *\nDisallow:"); | 
					
						
							|  |  |  |         $this->dontSeeText("Disallow: /"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Check config overrides app-public setting
 | 
					
						
							|  |  |  |         config()->set('app.allow_robots', false); | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  |         $this->visit('/robots.txt'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->seeText("User-agent: *\nDisallow: /"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 02:29:31 +08:00
										 |  |  |     public function test_public_view_then_login_redirects_to_previous_content() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  |         $book = Book::query()->first(); | 
					
						
							|  |  |  |         $this->visit($book->getUrl()) | 
					
						
							|  |  |  |             ->see($book->name) | 
					
						
							|  |  |  |             ->visit('/login') | 
					
						
							|  |  |  |             ->type('admin@admin.com', '#email') | 
					
						
							|  |  |  |             ->type('password', '#password') | 
					
						
							|  |  |  |             ->press('Log In') | 
					
						
							|  |  |  |             ->seePageUrlIs($book->getUrl()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_access_hidden_content_then_login_redirects_to_intended_content() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->setSettings(['app-public' => 'true']); | 
					
						
							|  |  |  |         $book = Book::query()->first(); | 
					
						
							|  |  |  |         $this->setEntityRestrictions($book); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $this->visit($book->getUrl()); | 
					
						
							|  |  |  |         } catch (\Exception $exception) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->see('Book not found') | 
					
						
							|  |  |  |             ->dontSee($book->name) | 
					
						
							|  |  |  |             ->visit('/login') | 
					
						
							|  |  |  |             ->type('admin@admin.com', '#email') | 
					
						
							|  |  |  |             ->type('password', '#password') | 
					
						
							|  |  |  |             ->press('Log In') | 
					
						
							|  |  |  |             ->seePageUrlIs($book->getUrl()) | 
					
						
							|  |  |  |             ->see($book->name); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  | } |