]> BookStack Code Mirror - bookstack/blob - tests/Entity/ChapterTest.php
Input WYSIWYG: Updated tests, Added simple html limiting
[bookstack] / tests / Entity / ChapterTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use Tests\TestCase;
9
10 class ChapterTest extends TestCase
11 {
12     public function test_create()
13     {
14         $book = $this->entities->book();
15
16         $chapter = Chapter::factory()->make([
17             'name' => 'My First Chapter',
18         ]);
19
20         $resp = $this->asEditor()->get($book->getUrl());
21         $this->withHtml($resp)->assertElementContains('a[href="' . $book->getUrl('/create-chapter') . '"]', 'New Chapter');
22
23         $resp = $this->get($book->getUrl('/create-chapter'));
24         $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl('/create-chapter') . '"][method="POST"]', 'Save Chapter');
25
26         $resp = $this->post($book->getUrl('/create-chapter'), $chapter->only('name', 'description_html'));
27         $resp->assertRedirect($book->getUrl('/chapter/my-first-chapter'));
28
29         $resp = $this->get($book->getUrl('/chapter/my-first-chapter'));
30         $resp->assertSee($chapter->name);
31         $resp->assertSee($chapter->description_html, false);
32     }
33
34     public function test_delete()
35     {
36         $chapter = Chapter::query()->whereHas('pages')->first();
37         $this->assertNull($chapter->deleted_at);
38         $pageCount = $chapter->pages()->count();
39
40         $deleteViewReq = $this->asEditor()->get($chapter->getUrl('/delete'));
41         $deleteViewReq->assertSeeText('Are you sure you want to delete this chapter?');
42
43         $deleteReq = $this->delete($chapter->getUrl());
44         $deleteReq->assertRedirect($chapter->getParent()->getUrl());
45         $this->assertActivityExists('chapter_delete', $chapter);
46
47         $chapter->refresh();
48         $this->assertNotNull($chapter->deleted_at);
49
50         $this->assertTrue($chapter->pages()->count() === 0);
51         $this->assertTrue($chapter->pages()->withTrashed()->count() === $pageCount);
52         $this->assertTrue($chapter->deletions()->count() === 1);
53
54         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
55         $this->assertNotificationContains($redirectReq, 'Chapter Successfully Deleted');
56     }
57
58     public function test_show_view_has_copy_button()
59     {
60         $chapter = $this->entities->chapter();
61
62         $resp = $this->asEditor()->get($chapter->getUrl());
63         $this->withHtml($resp)->assertElementContains("a[href$=\"{$chapter->getUrl('/copy')}\"]", 'Copy');
64     }
65
66     public function test_copy_view()
67     {
68         $chapter = $this->entities->chapter();
69
70         $resp = $this->asEditor()->get($chapter->getUrl('/copy'));
71         $resp->assertOk();
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"]');
75     }
76
77     public function test_copy()
78     {
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();
83
84         $resp = $this->asEditor()->post($chapter->getUrl('/copy'), [
85             'name'             => 'My copied chapter',
86             'entity_selection' => 'book:' . $otherBook->id,
87         ]);
88
89         /** @var Chapter $newChapter */
90         $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
91
92         $resp->assertRedirect($newChapter->getUrl());
93         $this->assertEquals($otherBook->id, $newChapter->book_id);
94         $this->assertEquals($chapter->pages->count(), $newChapter->pages->count());
95     }
96
97     public function test_copy_does_not_copy_non_visible_pages()
98     {
99         $chapter = $this->entities->chapterHasPages();
100
101         // Hide pages to all non-admin roles
102         /** @var Page $page */
103         foreach ($chapter->pages as $page) {
104             $this->permissions->setEntityPermissions($page, [], []);
105         }
106
107         $this->asEditor()->post($chapter->getUrl('/copy'), [
108             'name' => 'My copied chapter',
109         ]);
110
111         /** @var Chapter $newChapter */
112         $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
113         $this->assertEquals(0, $newChapter->pages()->count());
114     }
115
116     public function test_copy_does_not_copy_pages_if_user_cant_page_create()
117     {
118         $chapter = $this->entities->chapterHasPages();
119         $viewer = $this->users->viewer();
120         $this->permissions->grantUserRolePermissions($viewer, ['chapter-create-all']);
121
122         // Lacking permission results in no copied pages
123         $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
124             'name' => 'My copied chapter',
125         ]);
126
127         /** @var Chapter $newChapter */
128         $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
129         $this->assertEquals(0, $newChapter->pages()->count());
130
131         $this->permissions->grantUserRolePermissions($viewer, ['page-create-all']);
132
133         // Having permission rules in copied pages
134         $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
135             'name' => 'My copied again chapter',
136         ]);
137
138         /** @var Chapter $newChapter2 */
139         $newChapter2 = Chapter::query()->where('name', '=', 'My copied again chapter')->first();
140         $this->assertEquals($chapter->pages()->count(), $newChapter2->pages()->count());
141     }
142
143     public function test_sort_book_action_visible_if_permissions_allow()
144     {
145         $chapter = $this->entities->chapter();
146
147         $resp = $this->actingAs($this->users->viewer())->get($chapter->getUrl());
148         $this->withHtml($resp)->assertLinkNotExists($chapter->book->getUrl('sort'));
149
150         $resp = $this->asEditor()->get($chapter->getUrl());
151         $this->withHtml($resp)->assertLinkExists($chapter->book->getUrl('sort'));
152     }
153 }