]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
Apply fixes from StyleCI
[bookstack] / tests / Entity / SortTest.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 BookStack\Entities\Repos\PageRepo;
9 use Tests\TestCase;
10
11 class SortTest extends TestCase
12 {
13     protected $book;
14
15     public function setUp(): void
16     {
17         parent::setUp();
18         $this->book = Book::first();
19     }
20
21     public function test_drafts_do_not_show_up()
22     {
23         $this->asAdmin();
24         $pageRepo = app(PageRepo::class);
25         $draft = $pageRepo->getNewDraftPage($this->book);
26
27         $resp = $this->get($this->book->getUrl());
28         $resp->assertSee($draft->name);
29
30         $resp = $this->get($this->book->getUrl() . '/sort');
31         $resp->assertDontSee($draft->name);
32     }
33
34     public function test_page_move_into_book()
35     {
36         $page = Page::first();
37         $currentBook = $page->book;
38         $newBook = Book::where('id', '!=', $currentBook->id)->first();
39
40         $resp = $this->asEditor()->get($page->getUrl('/move'));
41         $resp->assertSee('Move Page');
42
43         $movePageResp = $this->put($page->getUrl('/move'), [
44             'entity_selection' => 'book:' . $newBook->id,
45         ]);
46         $page = Page::find($page->id);
47
48         $movePageResp->assertRedirect($page->getUrl());
49         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
50
51         $newBookResp = $this->get($newBook->getUrl());
52         $newBookResp->assertSee('moved page');
53         $newBookResp->assertSee($page->name);
54     }
55
56     public function test_page_move_into_chapter()
57     {
58         $page = Page::first();
59         $currentBook = $page->book;
60         $newBook = Book::where('id', '!=', $currentBook->id)->first();
61         $newChapter = $newBook->chapters()->first();
62
63         $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
64             'entity_selection' => 'chapter:' . $newChapter->id,
65         ]);
66         $page = Page::find($page->id);
67
68         $movePageResp->assertRedirect($page->getUrl());
69         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
70
71         $newChapterResp = $this->get($newChapter->getUrl());
72         $newChapterResp->assertSee($page->name);
73     }
74
75     public function test_page_move_from_chapter_to_book()
76     {
77         $oldChapter = Chapter::first();
78         $page = $oldChapter->pages()->first();
79         $newBook = Book::where('id', '!=', $oldChapter->book_id)->first();
80
81         $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [
82             'entity_selection' => 'book:' . $newBook->id,
83         ]);
84         $page->refresh();
85
86         $movePageResp->assertRedirect($page->getUrl());
87         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
88         $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
89
90         $newBookResp = $this->get($newBook->getUrl());
91         $newBookResp->assertSee($page->name);
92     }
93
94     public function test_page_move_requires_create_permissions_on_parent()
95     {
96         $page = Page::query()->first();
97         $currentBook = $page->book;
98         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
99         $editor = $this->getEditor();
100
101         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles->all());
102
103         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
104             'entity_selection' => 'book:' . $newBook->id,
105         ]);
106         $this->assertPermissionError($movePageResp);
107
108         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles->all());
109         $movePageResp = $this->put($page->getUrl('/move'), [
110             'entity_selection' => 'book:' . $newBook->id,
111         ]);
112
113         $page = Page::find($page->id);
114         $movePageResp->assertRedirect($page->getUrl());
115
116         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
117     }
118
119     public function test_page_move_requires_delete_permissions()
120     {
121         $page = Page::first();
122         $currentBook = $page->book;
123         $newBook = Book::where('id', '!=', $currentBook->id)->first();
124         $editor = $this->getEditor();
125
126         $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
127         $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles->all());
128
129         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
130             'entity_selection' => 'book:' . $newBook->id,
131         ]);
132         $this->assertPermissionError($movePageResp);
133         $pageView = $this->get($page->getUrl());
134         $pageView->assertDontSee($page->getUrl('/move'));
135
136         $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles->all());
137         $movePageResp = $this->put($page->getUrl('/move'), [
138             'entity_selection' => 'book:' . $newBook->id,
139         ]);
140
141         $page = Page::find($page->id);
142         $movePageResp->assertRedirect($page->getUrl());
143         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
144     }
145
146     public function test_chapter_move()
147     {
148         $chapter = Chapter::first();
149         $currentBook = $chapter->book;
150         $pageToCheck = $chapter->pages->first();
151         $newBook = Book::where('id', '!=', $currentBook->id)->first();
152
153         $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
154         $chapterMoveResp->assertSee('Move Chapter');
155
156         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
157             'entity_selection' => 'book:' . $newBook->id,
158         ]);
159
160         $chapter = Chapter::find($chapter->id);
161         $moveChapterResp->assertRedirect($chapter->getUrl());
162         $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
163
164         $newBookResp = $this->get($newBook->getUrl());
165         $newBookResp->assertSee('moved chapter');
166         $newBookResp->assertSee($chapter->name);
167
168         $pageToCheck = Page::find($pageToCheck->id);
169         $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
170         $pageCheckResp = $this->get($pageToCheck->getUrl());
171         $pageCheckResp->assertSee($newBook->name);
172     }
173
174     public function test_chapter_move_requires_delete_permissions()
175     {
176         $chapter = Chapter::first();
177         $currentBook = $chapter->book;
178         $newBook = Book::where('id', '!=', $currentBook->id)->first();
179         $editor = $this->getEditor();
180
181         $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
182         $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles->all());
183
184         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
185             'entity_selection' => 'book:' . $newBook->id,
186         ]);
187         $this->assertPermissionError($moveChapterResp);
188         $pageView = $this->get($chapter->getUrl());
189         $pageView->assertDontSee($chapter->getUrl('/move'));
190
191         $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles->all());
192         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
193             'entity_selection' => 'book:' . $newBook->id,
194         ]);
195
196         $chapter = Chapter::find($chapter->id);
197         $moveChapterResp->assertRedirect($chapter->getUrl());
198         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
199     }
200
201     public function test_chapter_move_changes_book_for_deleted_pages_within()
202     {
203         /** @var Chapter $chapter */
204         $chapter = Chapter::query()->whereHas('pages')->first();
205         $currentBook = $chapter->book;
206         $pageToCheck = $chapter->pages->first();
207         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
208
209         $pageToCheck->delete();
210
211         $this->asEditor()->put($chapter->getUrl('/move'), [
212             'entity_selection' => 'book:' . $newBook->id,
213         ]);
214
215         $pageToCheck->refresh();
216         $this->assertEquals($newBook->id, $pageToCheck->book_id);
217     }
218
219     public function test_book_sort()
220     {
221         $oldBook = Book::query()->first();
222         $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
223         $newBook = $this->newBook(['name' => 'New sort book']);
224         $pagesToMove = Page::query()->take(5)->get();
225
226         // Create request data
227         $reqData = [
228             [
229                 'id'            => $chapterToMove->id,
230                 'sort'          => 0,
231                 'parentChapter' => false,
232                 'type'          => 'chapter',
233                 'book'          => $newBook->id,
234             ],
235         ];
236         foreach ($pagesToMove as $index => $page) {
237             $reqData[] = [
238                 'id'            => $page->id,
239                 'sort'          => $index,
240                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
241                 'type'          => 'page',
242                 'book'          => $newBook->id,
243             ];
244         }
245
246         $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
247         $sortResp->assertRedirect($newBook->getUrl());
248         $sortResp->assertStatus(302);
249         $this->assertDatabaseHas('chapters', [
250             'id'       => $chapterToMove->id,
251             'book_id'  => $newBook->id,
252             'priority' => 0,
253         ]);
254         $this->assertTrue($newBook->chapters()->count() === 1);
255         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
256
257         $checkPage = $pagesToMove[1];
258         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
259         $checkResp->assertSee($newBook->name);
260     }
261 }