| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Tests\Entity; | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2020-11-22 08:17:45 +08:00
										 |  |  | use BookStack\Entities\Models\Chapter; | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | use BookStack\Entities\Models\Page; | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  | use Tests\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ChapterTest extends TestCase | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |     public function test_create() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $book = $this->entities->book(); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-31 04:29:59 +08:00
										 |  |  |         $chapter = Chapter::factory()->make([ | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  |             'name' => 'My First Chapter', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get($book->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('a[href="' . $book->getUrl('/create-chapter') . '"]', 'New Chapter'); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($book->getUrl('/create-chapter')); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl('/create-chapter') . '"][method="POST"]', 'Save Chapter'); | 
					
						
							| 
									
										
										
										
											2021-09-18 04:29:16 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->post($book->getUrl('/create-chapter'), $chapter->only('name', 'description')); | 
					
						
							|  |  |  |         $resp->assertRedirect($book->getUrl('/chapter/my-first-chapter')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->get($book->getUrl('/chapter/my-first-chapter')); | 
					
						
							|  |  |  |         $resp->assertSee($chapter->name); | 
					
						
							|  |  |  |         $resp->assertSee($chapter->description); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_delete() | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $chapter = Chapter::query()->whereHas('pages')->first(); | 
					
						
							|  |  |  |         $this->assertNull($chapter->deleted_at); | 
					
						
							|  |  |  |         $pageCount = $chapter->pages()->count(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $deleteViewReq = $this->asEditor()->get($chapter->getUrl('/delete')); | 
					
						
							|  |  |  |         $deleteViewReq->assertSeeText('Are you sure you want to delete this chapter?'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $deleteReq = $this->delete($chapter->getUrl()); | 
					
						
							|  |  |  |         $deleteReq->assertRedirect($chapter->getParent()->getUrl()); | 
					
						
							|  |  |  |         $this->assertActivityExists('chapter_delete', $chapter); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $chapter->refresh(); | 
					
						
							|  |  |  |         $this->assertNotNull($chapter->deleted_at); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->assertTrue($chapter->pages()->count() === 0); | 
					
						
							|  |  |  |         $this->assertTrue($chapter->pages()->withTrashed()->count() === $pageCount); | 
					
						
							|  |  |  |         $this->assertTrue($chapter->deletions()->count() === 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location')); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->assertNotificationContains($redirectReq, 'Chapter Successfully Deleted'); | 
					
						
							| 
									
										
										
										
											2020-11-06 20:54:39 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_show_view_has_copy_button() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get($chapter->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementContains("a[href$=\"{$chapter->getUrl('/copy')}\"]", 'Copy'); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_copy_view() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get($chapter->getUrl('/copy')); | 
					
						
							|  |  |  |         $resp->assertOk(); | 
					
						
							|  |  |  |         $resp->assertSee('Copy Chapter'); | 
					
						
							| 
									
										
										
										
											2022-07-23 22:10:18 +08:00
										 |  |  |         $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$chapter->name}\"]"); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertElementExists('input[name="entity_selection"]'); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_copy() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var Chapter $chapter */ | 
					
						
							|  |  |  |         $chapter = Chapter::query()->whereHas('pages')->first(); | 
					
						
							|  |  |  |         /** @var Book $otherBook */ | 
					
						
							|  |  |  |         $otherBook = Book::query()->where('id', '!=', $chapter->book_id)->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->post($chapter->getUrl('/copy'), [ | 
					
						
							| 
									
										
										
										
											2021-12-21 01:40:27 +08:00
										 |  |  |             'name'             => 'My copied chapter', | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  |             'entity_selection' => 'book:' . $otherBook->id, | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Chapter $newChapter */ | 
					
						
							|  |  |  |         $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp->assertRedirect($newChapter->getUrl()); | 
					
						
							|  |  |  |         $this->assertEquals($otherBook->id, $newChapter->book_id); | 
					
						
							|  |  |  |         $this->assertEquals($chapter->pages->count(), $newChapter->pages->count()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_copy_does_not_copy_non_visible_pages() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapterHasPages(); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Hide pages to all non-admin roles
 | 
					
						
							|  |  |  |         /** @var Page $page */ | 
					
						
							|  |  |  |         foreach ($chapter->pages as $page) { | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |             $this->permissions->setEntityPermissions($page, [], []); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->asEditor()->post($chapter->getUrl('/copy'), [ | 
					
						
							|  |  |  |             'name' => 'My copied chapter', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Chapter $newChapter */ | 
					
						
							|  |  |  |         $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first(); | 
					
						
							|  |  |  |         $this->assertEquals(0, $newChapter->pages()->count()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function test_copy_does_not_copy_pages_if_user_cant_page_create() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 05:11:16 +08:00
										 |  |  |         $chapter = $this->entities->chapterHasPages(); | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $viewer = $this->users->viewer(); | 
					
						
							|  |  |  |         $this->permissions->grantUserRolePermissions($viewer, ['chapter-create-all']); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Lacking permission results in no copied pages
 | 
					
						
							|  |  |  |         $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [ | 
					
						
							|  |  |  |             'name' => 'My copied chapter', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Chapter $newChapter */ | 
					
						
							|  |  |  |         $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first(); | 
					
						
							|  |  |  |         $this->assertEquals(0, $newChapter->pages()->count()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $this->permissions->grantUserRolePermissions($viewer, ['page-create-all']); | 
					
						
							| 
									
										
										
										
											2021-12-19 23:40:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // Having permission rules in copied pages
 | 
					
						
							|  |  |  |         $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [ | 
					
						
							|  |  |  |             'name' => 'My copied again chapter', | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @var Chapter $newChapter2 */ | 
					
						
							|  |  |  |         $newChapter2 = Chapter::query()->where('name', '=', 'My copied again chapter')->first(); | 
					
						
							|  |  |  |         $this->assertEquals($chapter->pages()->count(), $newChapter2->pages()->count()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-26 19:35:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function test_sort_book_action_visible_if_permissions_allow() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-09-30 00:31:38 +08:00
										 |  |  |         $chapter = $this->entities->chapter(); | 
					
						
							| 
									
										
										
										
											2022-07-26 19:35:50 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-21 19:08:34 +08:00
										 |  |  |         $resp = $this->actingAs($this->users->viewer())->get($chapter->getUrl()); | 
					
						
							| 
									
										
										
										
											2022-07-26 19:35:50 +08:00
										 |  |  |         $this->withHtml($resp)->assertLinkNotExists($chapter->book->getUrl('sort')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $resp = $this->asEditor()->get($chapter->getUrl()); | 
					
						
							|  |  |  |         $this->withHtml($resp)->assertLinkExists($chapter->book->getUrl('sort')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | } |