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