| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | namespace Tests\Permissions; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Users\Models\Role; | 
					
						
							|  |  |  | use BookStack\Users\Models\User; | 
					
						
							| 
									
										
										
										
											2022-11-10 21:48:17 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2020-12-19 05:42:43 +08:00
										 |  |  | use Illuminate\Support\Str; | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  | class EntityPermissionsTest extends TestCase | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |     protected User $user; | 
					
						
							|  |  |  |     protected User $viewer; | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-31 04:29:59 +08:00
										 |  |  |     protected function setUp(): void | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         parent::setUp(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->user = $this->users->editor(); | 
					
						
							|  |  |  |         $this->viewer = $this->users->viewer(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |     protected function setRestrictionsForTestRoles(Entity $entity, array $actions = []) | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-04-15 01:47:13 +08:00
										 |  |  |         $roles = [ | 
					
						
							|  |  |  |             $this->user->roles->first(), | 
					
						
							|  |  |  |             $this->viewer->roles->first(), | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->setEntityPermissions($entity, $actions, $roles); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     public function test_bookshelf_view_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $shelf = $this->entities->shelf(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($shelf->getUrl()) | 
					
						
							|  |  |  |             ->assertStatus(200); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, []); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->followingRedirects()->get($shelf->getUrl()) | 
					
						
							| 
									
										
										
										
											2022-09-02 21:47:44 +08:00
										 |  |  |             ->assertSee('Shelf not found'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl()) | 
					
						
							|  |  |  |             ->assertSee($shelf->name); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_bookshelf_update_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $shelf = $this->entities->shelf(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($shelf->getUrl('/edit')) | 
					
						
							| 
									
										
										
										
											2022-09-02 21:47:44 +08:00
										 |  |  |             ->assertSee('Edit Shelf'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $resp = $this->get($shelf->getUrl('/edit')) | 
					
						
							|  |  |  |             ->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->followRedirects($resp)->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl('/edit')) | 
					
						
							|  |  |  |             ->assertOk(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_bookshelf_delete_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $shelf = $this->entities->shelf(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($shelf->getUrl('/delete')) | 
					
						
							| 
									
										
										
										
											2022-09-02 21:47:44 +08:00
										 |  |  |             ->assertSee('Delete Shelf'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl('/delete'))->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl('/delete')) | 
					
						
							|  |  |  |             ->assertOk() | 
					
						
							| 
									
										
										
										
											2022-09-02 21:47:44 +08:00
										 |  |  |             ->assertSee('Delete Shelf'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     public function test_book_view_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $bookPage = $book->pages->first(); | 
					
						
							|  |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($bookUrl) | 
					
						
							|  |  |  |             ->assertOk(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, []); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->followingRedirects()->get($bookUrl) | 
					
						
							|  |  |  |             ->assertSee('Book not found'); | 
					
						
							|  |  |  |         $this->followingRedirects()->get($bookPage->getUrl()) | 
					
						
							|  |  |  |             ->assertSee('Page not found'); | 
					
						
							|  |  |  |         $this->followingRedirects()->get($bookChapter->getUrl()) | 
					
						
							|  |  |  |             ->assertSee('Chapter not found'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl) | 
					
						
							|  |  |  |             ->assertSee($book->name); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl()) | 
					
						
							|  |  |  |             ->assertSee($bookPage->name); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl()) | 
					
						
							|  |  |  |             ->assertSee($bookChapter->name); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_book_create_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->viewer)->get($bookUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementNotContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->user)->get($bookUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/create-chapter')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get($bookUrl . '/create-page')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get($bookUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementNotContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'create']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $resp = $this->post($book->getUrl('/create-chapter'), [ | 
					
						
							| 
									
										
										
										
											2021-09-19 04:21:44 +08:00
										 |  |  |             'name'        => 'test chapter', | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             'description' => 'desc', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl('/chapter/test-chapter')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get($book->getUrl('/create-page')); | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         $page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first(); | 
					
						
							|  |  |  |         $resp = $this->post($page->getUrl(), [ | 
					
						
							|  |  |  |             'name' => 'test page', | 
					
						
							|  |  |  |             'html' => 'test content', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl('/page/test-page')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get($bookUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_book_update_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $bookPage = $book->pages->first(); | 
					
						
							|  |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($bookUrl . '/edit') | 
					
						
							|  |  |  |             ->assertSee('Edit Book'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/edit')->assertOk(); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/edit')->assertOk(); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_book_delete_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $bookPage = $book->pages->first(); | 
					
						
							|  |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($bookUrl . '/delete') | 
					
						
							|  |  |  |             ->assertSee('Delete Book'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book'); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl('/delete'))->assertOk()->assertSee('Delete Page'); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl('/delete'))->assertSee('Delete Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapter_view_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $chapterPage = $chapter->pages->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $chapterUrl = $chapter->getUrl(); | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($chapterUrl)->assertOk(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, []); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->followingRedirects()->get($chapterUrl)->assertSee('Chapter not found'); | 
					
						
							|  |  |  |         $this->followingRedirects()->get($chapterPage->getUrl())->assertSee('Page not found'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapterUrl)->assertSee($chapter->name); | 
					
						
							|  |  |  |         $this->get($chapterPage->getUrl())->assertSee($chapterPage->name); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapter_create_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $chapterUrl = $chapter->getUrl(); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->user)->get($chapterUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('.actions', 'New Page'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view', 'delete', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapterUrl . '/create-page')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($this->get($chapterUrl))->assertElementNotContains('.actions', 'New Page'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view', 'create']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapter->getUrl('/create-page')); | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         $page = Page::query()->where('draft', '=', true)->orderBy('id', 'desc')->first(); | 
					
						
							|  |  |  |         $resp = $this->post($page->getUrl(), [ | 
					
						
							|  |  |  |             'name' => 'test page', | 
					
						
							|  |  |  |             'html' => 'test content', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($chapter->book->getUrl('/page/test-page')); | 
					
						
							| 
									
										
										
										
											2017-04-30 05:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($this->get($chapterUrl))->assertElementContains('.actions', 'New Page'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapter_update_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $chapterPage = $chapter->pages->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $chapterUrl = $chapter->getUrl(); | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($chapterUrl . '/edit') | 
					
						
							|  |  |  |             ->assertSee('Edit Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapterUrl . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($chapterPage->getUrl() . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapterUrl . '/edit')->assertOk()->assertSee('Edit Chapter'); | 
					
						
							|  |  |  |         $this->get($chapterPage->getUrl() . '/edit')->assertOk(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapter_delete_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $chapterPage = $chapter->pages->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $chapterUrl = $chapter->getUrl(); | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($chapterUrl . '/delete') | 
					
						
							|  |  |  |             ->assertSee('Delete Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapterUrl . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($chapterPage->getUrl() . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($chapter, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($chapterUrl . '/delete')->assertOk()->assertSee('Delete Chapter'); | 
					
						
							|  |  |  |         $this->get($chapterPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_page_view_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $pageUrl = $page->getUrl(); | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($pageUrl)->assertOk(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, ['update', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($pageUrl)->assertSee('Page not found'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, ['view']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($pageUrl)->assertSee($page->name); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_page_update_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $pageUrl = $page->getUrl(); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->user) | 
					
						
							|  |  |  |             ->get($pageUrl . '/edit'); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($pageUrl . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get($pageUrl . '/edit') | 
					
						
							|  |  |  |             ->assertOk(); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('input[name="name"][value="' . $page->name . '"]'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_page_delete_restriction() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $page = $this->entities->page(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $pageUrl = $page->getUrl(); | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($pageUrl . '/delete') | 
					
						
							|  |  |  |             ->assertSee('Delete Page'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($pageUrl . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($pageUrl . '/delete')->assertOk()->assertSee('Delete Page'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected function entityRestrictionFormTest(string $model, string $title, string $permission, string $roleId) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Entity $modelInstance */ | 
					
						
							|  |  |  |         $modelInstance = $model::query()->first(); | 
					
						
							|  |  |  |         $this->asAdmin()->get($modelInstance->getUrl('/permissions')) | 
					
						
							|  |  |  |             ->assertSee($title); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->put($modelInstance->getUrl('/permissions'), [ | 
					
						
							| 
									
										
										
										
											2022-10-12 19:12:36 +08:00
										 |  |  |             'permissions' => [ | 
					
						
							| 
									
										
										
										
											2023-01-21 21:03:47 +08:00
										 |  |  |                 $roleId => [ | 
					
						
							|  |  |  |                     $permission => 'true', | 
					
						
							| 
									
										
										
										
											2021-09-19 04:21:44 +08:00
										 |  |  |                 ], | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ], | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertDatabaseHas('entity_permissions', [ | 
					
						
							| 
									
										
										
										
											2023-01-21 21:03:47 +08:00
										 |  |  |             'entity_id'      => $modelInstance->id, | 
					
						
							|  |  |  |             'entity_type'    => $modelInstance->getMorphClass(), | 
					
						
							|  |  |  |             'role_id'        => $roleId, | 
					
						
							|  |  |  |             $permission => true, | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     public function test_bookshelf_restriction_form() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-02 21:47:44 +08:00
										 |  |  |         $this->entityRestrictionFormTest(Bookshelf::class, 'Shelf Permissions', 'view', '2'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     public function test_book_restriction_form() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->entityRestrictionFormTest(Book::class, 'Book Permissions', 'view', '2'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_chapter_restriction_form() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->entityRestrictionFormTest(Chapter::class, 'Chapter Permissions', 'update', '2'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_page_restriction_form() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->entityRestrictionFormTest(Page::class, 'Page Permissions', 'delete', '2'); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-07-13 05:04:05 +08:00
										 |  |  |     public function test_shelf_create_permission_visible_with_notice() | 
					
						
							| 
									
										
										
										
											2023-06-26 06:22:49 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $shelf = $this->entities->shelf(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions')); | 
					
						
							|  |  |  |         $html = $this->withHtml($resp); | 
					
						
							| 
									
										
										
										
											2023-07-13 05:04:05 +08:00
										 |  |  |         $html->assertElementExists('input[name$="[create]"]'); | 
					
						
							|  |  |  |         $resp->assertSee('Shelf create permissions are only used for copying permissions to child books using the action below.'); | 
					
						
							| 
									
										
										
										
											2023-06-26 06:22:49 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     public function test_restricted_pages_not_visible_in_book_navigation_on_pages() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $page = $chapter->pages->first(); | 
					
						
							|  |  |  |         $page2 = $chapter->pages[2]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, []); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->user)->get($page2->getUrl()); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_restricted_pages_not_visible_in_book_navigation_on_chapters() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $page = $chapter->pages->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, []); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->user)->get($chapter->getUrl()); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('.sidebar-page-list', $page->name); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_restricted_pages_not_visible_on_chapter_pages() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |         $page = $chapter->pages->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($page, []); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($chapter->getUrl()) | 
					
						
							|  |  |  |             ->assertDontSee($page->name); | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  |     public function test_restricted_chapter_pages_not_visible_on_book_page() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($chapter->book->getUrl()) | 
					
						
							|  |  |  |             ->assertSee($chapter->pages->first()->name); | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($chapter->pages as $page) { | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |             $this->setRestrictionsForTestRoles($page, []); | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->user) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($chapter->book->getUrl()) | 
					
						
							|  |  |  |             ->assertDontSee($chapter->pages->first()->name); | 
					
						
							| 
									
										
										
										
											2020-12-18 01:31:18 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     public function test_bookshelf_update_restriction_override() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $shelf = $this->entities->shelf(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->viewer) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($shelf->getUrl('/edit')) | 
					
						
							|  |  |  |             ->assertDontSee('Edit Book'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl('/edit'))->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl('/edit'))->assertOk(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_bookshelf_delete_restriction_override() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $shelf = $this->entities->shelf(); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->viewer) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($shelf->getUrl('/delete')) | 
					
						
							|  |  |  |             ->assertDontSee('Delete Book'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($shelf->getUrl('/delete'))->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($shelf, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-02 21:47:44 +08:00
										 |  |  |         $this->get($shelf->getUrl('/delete'))->assertOk()->assertSee('Delete Shelf'); | 
					
						
							| 
									
										
										
										
											2018-09-21 22:15:16 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  |     public function test_book_create_restriction_override() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->actingAs($this->viewer)->get($bookUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementNotContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'delete', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/create-chapter')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookUrl . '/create-page')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:11:06 +08:00
										 |  |  |         $resp = $this->get($bookUrl); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementNotContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementNotContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'create']); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $resp = $this->post($book->getUrl('/create-chapter'), [ | 
					
						
							| 
									
										
										
										
											2021-09-19 04:21:44 +08:00
										 |  |  |             'name'        => 'test chapter', | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             'description' => 'test desc', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl('/chapter/test-chapter')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->get($book->getUrl('/create-page')); | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         $page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first(); | 
					
						
							|  |  |  |         $resp = $this->post($page->getUrl(), [ | 
					
						
							|  |  |  |             'name' => 'test page', | 
					
						
							|  |  |  |             'html' => 'test desc', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl('/page/test-page')); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $resp = $this->get($bookUrl); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementContains('.actions', 'New Page') | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->assertElementContains('.actions', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_book_update_restriction_override() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  |         $bookPage = $book->pages->first(); | 
					
						
							|  |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->viewer)->get($bookUrl . '/edit') | 
					
						
							|  |  |  |             ->assertDontSee('Edit Book'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/edit')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/edit')->assertOk(); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/edit')->assertOk(); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/edit')->assertSee('Edit Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_book_delete_restriction_override() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  |         $bookPage = $book->pages->first(); | 
					
						
							|  |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $bookUrl = $book->getUrl(); | 
					
						
							|  |  |  |         $this->actingAs($this->viewer) | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |             ->get($bookUrl . '/delete') | 
					
						
							|  |  |  |             ->assertDontSee('Delete Book'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'update']); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/delete')->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, ['view', 'delete']); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookUrl . '/delete')->assertOk()->assertSee('Delete Book'); | 
					
						
							|  |  |  |         $this->get($bookPage->getUrl() . '/delete')->assertOk()->assertSee('Delete Page'); | 
					
						
							|  |  |  |         $this->get($bookChapter->getUrl() . '/delete')->assertSee('Delete Chapter'); | 
					
						
							| 
									
										
										
										
											2016-03-31 03:15:44 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-22 20:39:34 +08:00
										 |  |  |     public function test_page_visible_if_has_permissions_when_book_not_visible() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							|  |  |  |         $bookPage = $bookChapter->pages->first(); | 
					
						
							| 
									
										
										
										
											2020-12-19 05:42:43 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ([$book, $bookChapter, $bookPage] as $entity) { | 
					
						
							|  |  |  |             $entity->name = Str::random(24); | 
					
						
							|  |  |  |             $entity->save(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, []); | 
					
						
							|  |  |  |         $this->setRestrictionsForTestRoles($bookPage, ['view']); | 
					
						
							| 
									
										
										
										
											2017-04-22 20:39:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->actingAs($this->viewer); | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $resp = $this->get($bookPage->getUrl()); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $resp->assertSee($bookPage->name); | 
					
						
							|  |  |  |         $resp->assertDontSee(substr($book->name, 0, 15)); | 
					
						
							|  |  |  |         $resp->assertDontSee(substr($bookChapter->name, 0, 15)); | 
					
						
							| 
									
										
										
										
											2017-04-22 20:39:34 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-31 22:47:08 +08:00
										 |  |  |     public function test_book_sort_view_permission() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         /** @var Book $firstBook */ | 
					
						
							|  |  |  |         $firstBook = Book::query()->first(); | 
					
						
							|  |  |  |         /** @var Book $secondBook */ | 
					
						
							|  |  |  |         $secondBook = Book::query()->find(2); | 
					
						
							| 
									
										
										
										
											2017-12-31 22:47:08 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($firstBook, ['view', 'update']); | 
					
						
							|  |  |  |         $this->setRestrictionsForTestRoles($secondBook, ['view']); | 
					
						
							| 
									
										
										
										
											2017-12-31 22:47:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Test sort page visibility
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($secondBook->getUrl('/sort'))->assertRedirect('/'); | 
					
						
							|  |  |  |         $this->get('/')->assertSee('You do not have permission'); | 
					
						
							| 
									
										
										
										
											2017-12-31 22:47:08 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Check sort page on first book
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($firstBook->getUrl('/sort')); | 
					
						
							| 
									
										
										
										
											2017-12-31 22:47:08 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-14 21:12:29 +08:00
										 |  |  |     public function test_can_create_page_if_chapter_has_permissions_when_book_not_visible() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($book, []); | 
					
						
							| 
									
										
										
										
											2018-07-14 21:12:29 +08:00
										 |  |  |         $bookChapter = $book->chapters->first(); | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($bookChapter, ['view']); | 
					
						
							| 
									
										
										
										
											2018-07-14 21:12:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->actingAs($this->user)->get($bookChapter->getUrl()) | 
					
						
							|  |  |  |             ->assertDontSee('New Page'); | 
					
						
							| 
									
										
										
										
											2018-07-14 21:12:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-04 06:11:00 +08:00
										 |  |  |         $this->setRestrictionsForTestRoles($bookChapter, ['view', 'create']); | 
					
						
							| 
									
										
										
										
											2018-07-14 21:12:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 05:35:28 +08:00
										 |  |  |         $this->get($bookChapter->getUrl('/create-page')); | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         $page = Page::query()->where('draft', '=', true)->orderByDesc('id')->first(); | 
					
						
							|  |  |  |         $resp = $this->post($page->getUrl(), [ | 
					
						
							|  |  |  |             'name' => 'test page', | 
					
						
							|  |  |  |             'html' => 'test content', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl('/page/test-page')); | 
					
						
							| 
									
										
										
										
											2018-07-14 21:12:29 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-11-10 21:48:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |     public function test_access_to_item_prevented_if_inheritance_active_but_permission_prevented_via_role() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = $this->users->viewer(); | 
					
						
							|  |  |  |         $viewerRole = $user->roles->first(); | 
					
						
							|  |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							|  |  |  |         $book = $chapter->book; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 03:04:32 +08:00
										 |  |  |         $this->permissions->setEntityPermissions($book, ['update'], [$viewerRole], false); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->setEntityPermissions($chapter, [], [$viewerRole], true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertFalse(userCan('chapter-update', $chapter)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_access_to_item_allowed_if_inheritance_active_and_permission_prevented_via_role_but_allowed_via_parent() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = $this->users->viewer(); | 
					
						
							|  |  |  |         $viewerRole = $user->roles->first(); | 
					
						
							|  |  |  |         $editorRole = Role::getRole('Editor'); | 
					
						
							|  |  |  |         $user->attachRole($editorRole); | 
					
						
							|  |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							|  |  |  |         $book = $chapter->book; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 03:04:32 +08:00
										 |  |  |         $this->permissions->setEntityPermissions($book, ['update'], [$editorRole], false); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->setEntityPermissions($chapter, [], [$viewerRole], true); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 03:04:32 +08:00
										 |  |  |         $this->actingAs($user); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->assertTrue(userCan('chapter-update', $chapter)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-10 21:48:17 +08:00
										 |  |  |     public function test_book_permissions_can_be_generated_without_error_if_child_chapter_is_in_recycle_bin() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $book = $this->entities->bookHasChaptersAndPages(); | 
					
						
							|  |  |  |         /** @var Chapter $chapter */ | 
					
						
							|  |  |  |         $chapter = $book->chapters()->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->asAdmin()->delete($chapter->getUrl()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $error = null; | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |             $this->permissions->setEntityPermissions($book, ['view'], []); | 
					
						
							| 
									
										
										
										
											2022-11-10 21:48:17 +08:00
										 |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             $error = $e; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertNull($error); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-03-06 02:09:21 +08:00
										 |  |  | } |