3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
10 class ChapterTest extends TestCase
12 public function test_create()
14 $book = $this->entities->book();
16 $chapter = Chapter::factory()->make([
17 'name' => 'My First Chapter',
20 $resp = $this->asEditor()->get($book->getUrl());
21 $this->withHtml($resp)->assertElementContains('a[href="' . $book->getUrl('/create-chapter') . '"]', 'New Chapter');
23 $resp = $this->get($book->getUrl('/create-chapter'));
24 $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl('/create-chapter') . '"][method="POST"]', 'Save Chapter');
26 $resp = $this->post($book->getUrl('/create-chapter'), $chapter->only('name', 'description_html'));
27 $resp->assertRedirect($book->getUrl('/chapter/my-first-chapter'));
29 $resp = $this->get($book->getUrl('/chapter/my-first-chapter'));
30 $resp->assertSee($chapter->name);
31 $resp->assertSee($chapter->description_html, false);
34 public function test_delete()
36 $chapter = Chapter::query()->whereHas('pages')->first();
37 $this->assertNull($chapter->deleted_at);
38 $pageCount = $chapter->pages()->count();
40 $deleteViewReq = $this->asEditor()->get($chapter->getUrl('/delete'));
41 $deleteViewReq->assertSeeText('Are you sure you want to delete this chapter?');
43 $deleteReq = $this->delete($chapter->getUrl());
44 $deleteReq->assertRedirect($chapter->getParent()->getUrl());
45 $this->assertActivityExists('chapter_delete', $chapter);
48 $this->assertNotNull($chapter->deleted_at);
50 $this->assertTrue($chapter->pages()->count() === 0);
51 $this->assertTrue($chapter->pages()->withTrashed()->count() === $pageCount);
52 $this->assertTrue($chapter->deletions()->count() === 1);
54 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
55 $this->assertNotificationContains($redirectReq, 'Chapter Successfully Deleted');
58 public function test_show_view_has_copy_button()
60 $chapter = $this->entities->chapter();
62 $resp = $this->asEditor()->get($chapter->getUrl());
63 $this->withHtml($resp)->assertElementContains("a[href$=\"{$chapter->getUrl('/copy')}\"]", 'Copy');
66 public function test_copy_view()
68 $chapter = $this->entities->chapter();
70 $resp = $this->asEditor()->get($chapter->getUrl('/copy'));
72 $resp->assertSee('Copy Chapter');
73 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$chapter->name}\"]");
74 $this->withHtml($resp)->assertElementExists('input[name="entity_selection"]');
77 public function test_copy()
79 /** @var Chapter $chapter */
80 $chapter = Chapter::query()->whereHas('pages')->first();
81 /** @var Book $otherBook */
82 $otherBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
84 $resp = $this->asEditor()->post($chapter->getUrl('/copy'), [
85 'name' => 'My copied chapter',
86 'entity_selection' => 'book:' . $otherBook->id,
89 /** @var Chapter $newChapter */
90 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
92 $resp->assertRedirect($newChapter->getUrl());
93 $this->assertEquals($otherBook->id, $newChapter->book_id);
94 $this->assertEquals($chapter->pages->count(), $newChapter->pages->count());
97 public function test_copy_does_not_copy_non_visible_pages()
99 $chapter = $this->entities->chapterHasPages();
101 // Hide pages to all non-admin roles
102 /** @var Page $page */
103 foreach ($chapter->pages as $page) {
104 $this->permissions->setEntityPermissions($page, [], []);
107 $this->asEditor()->post($chapter->getUrl('/copy'), [
108 'name' => 'My copied chapter',
111 /** @var Chapter $newChapter */
112 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
113 $this->assertEquals(0, $newChapter->pages()->count());
116 public function test_copy_does_not_copy_pages_if_user_cant_page_create()
118 $chapter = $this->entities->chapterHasPages();
119 $viewer = $this->users->viewer();
120 $this->permissions->grantUserRolePermissions($viewer, ['chapter-create-all']);
122 // Lacking permission results in no copied pages
123 $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
124 'name' => 'My copied chapter',
127 /** @var Chapter $newChapter */
128 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
129 $this->assertEquals(0, $newChapter->pages()->count());
131 $this->permissions->grantUserRolePermissions($viewer, ['page-create-all']);
133 // Having permission rules in copied pages
134 $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
135 'name' => 'My copied again chapter',
138 /** @var Chapter $newChapter2 */
139 $newChapter2 = Chapter::query()->where('name', '=', 'My copied again chapter')->first();
140 $this->assertEquals($chapter->pages()->count(), $newChapter2->pages()->count());
143 public function test_sort_book_action_visible_if_permissions_allow()
145 $chapter = $this->entities->chapter();
147 $resp = $this->actingAs($this->users->viewer())->get($chapter->getUrl());
148 $this->withHtml($resp)->assertLinkNotExists($chapter->book->getUrl('sort'));
150 $resp = $this->asEditor()->get($chapter->getUrl());
151 $this->withHtml($resp)->assertLinkExists($chapter->book->getUrl('sort'));