]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
Merge pull request #1646 from kostefun/patch-15
[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()
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_requires_create_permissions_on_parent()
54     {
55         $page = Page::first();
56         $currentBook = $page->book;
57         $newBook = Book::where('id', '!=', $currentBook->id)->first();
58         $editor = $this->getEditor();
59
60         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
61
62         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
63             'entity_selection' => 'book:' . $newBook->id
64         ]);
65         $this->assertPermissionError($movePageResp);
66
67         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles);
68         $movePageResp = $this->put($page->getUrl('/move'), [
69             'entity_selection' => 'book:' . $newBook->id
70         ]);
71
72         $page = Page::find($page->id);
73         $movePageResp->assertRedirect($page->getUrl());
74
75         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
76     }
77
78     public function test_page_move_requires_delete_permissions()
79     {
80         $page = Page::first();
81         $currentBook = $page->book;
82         $newBook = Book::where('id', '!=', $currentBook->id)->first();
83         $editor = $this->getEditor();
84
85         $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
86         $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
87
88         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
89             'entity_selection' => 'book:' . $newBook->id
90         ]);
91         $this->assertPermissionError($movePageResp);
92         $pageView = $this->get($page->getUrl());
93         $pageView->assertDontSee($page->getUrl('/move'));
94
95         $this->setEntityRestrictions($page, ['view', 'update', 'create', 'delete'], $editor->roles);
96         $movePageResp = $this->put($page->getUrl('/move'), [
97             'entity_selection' => 'book:' . $newBook->id
98         ]);
99
100         $page = Page::find($page->id);
101         $movePageResp->assertRedirect($page->getUrl());
102         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
103     }
104
105     public function test_chapter_move()
106     {
107         $chapter = Chapter::first();
108         $currentBook = $chapter->book;
109         $pageToCheck = $chapter->pages->first();
110         $newBook = Book::where('id', '!=', $currentBook->id)->first();
111
112         $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
113         $chapterMoveResp->assertSee('Move Chapter');
114
115         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
116             'entity_selection' => 'book:' . $newBook->id
117         ]);
118
119         $chapter = Chapter::find($chapter->id);
120         $moveChapterResp->assertRedirect($chapter->getUrl());
121         $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
122
123         $newBookResp = $this->get($newBook->getUrl());
124         $newBookResp->assertSee('moved chapter');
125         $newBookResp->assertSee($chapter->name);
126
127         $pageToCheck = Page::find($pageToCheck->id);
128         $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
129         $pageCheckResp = $this->get($pageToCheck->getUrl());
130         $pageCheckResp->assertSee($newBook->name);
131     }
132
133     public function test_chapter_move_requires_delete_permissions()
134     {
135         $chapter = Chapter::first();
136         $currentBook = $chapter->book;
137         $newBook = Book::where('id', '!=', $currentBook->id)->first();
138         $editor = $this->getEditor();
139
140         $this->setEntityRestrictions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles);
141         $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
142
143         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
144             'entity_selection' => 'book:' . $newBook->id
145         ]);
146         $this->assertPermissionError($moveChapterResp);
147         $pageView = $this->get($chapter->getUrl());
148         $pageView->assertDontSee($chapter->getUrl('/move'));
149
150         $this->setEntityRestrictions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles);
151         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
152             'entity_selection' => 'book:' . $newBook->id
153         ]);
154
155         $chapter = Chapter::find($chapter->id);
156         $moveChapterResp->assertRedirect($chapter->getUrl());
157         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
158     }
159
160     public function test_book_sort()
161     {
162         $oldBook = Book::query()->first();
163         $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
164         $newBook = $this->newBook(['name' => 'New sort book']);
165         $pagesToMove = Page::query()->take(5)->get();
166
167         // Create request data
168         $reqData = [
169             [
170                 'id' => $chapterToMove->id,
171                 'sort' => 0,
172                 'parentChapter' => false,
173                 'type' => 'chapter',
174                 'book' => $newBook->id
175             ]
176         ];
177         foreach ($pagesToMove as $index => $page) {
178             $reqData[] = [
179                 'id' => $page->id,
180                 'sort' => $index,
181                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
182                 'type' => 'page',
183                 'book' => $newBook->id
184             ];
185         }
186
187         $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
188         $sortResp->assertRedirect($newBook->getUrl());
189         $sortResp->assertStatus(302);
190         $this->assertDatabaseHas('chapters', [
191             'id' => $chapterToMove->id,
192             'book_id' => $newBook->id,
193             'priority' => 0
194         ]);
195         $this->assertTrue($newBook->chapters()->count() === 1);
196         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
197
198         $checkPage = $pagesToMove[1];
199         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
200         $checkResp->assertSee($newBook->name);
201     }
202
203     public function test_page_copy()
204     {
205         $page = Page::first();
206         $currentBook = $page->book;
207         $newBook = Book::where('id', '!=', $currentBook->id)->first();
208
209         $resp = $this->asEditor()->get($page->getUrl('/copy'));
210         $resp->assertSee('Copy Page');
211
212         $movePageResp = $this->post($page->getUrl('/copy'), [
213             'entity_selection' => 'book:' . $newBook->id,
214             'name' => 'My copied test page'
215         ]);
216         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
217
218         $movePageResp->assertRedirect($pageCopy->getUrl());
219         $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
220     }
221
222     public function test_page_copy_with_no_destination()
223     {
224         $page = Page::first();
225         $currentBook = $page->book;
226
227         $resp = $this->asEditor()->get($page->getUrl('/copy'));
228         $resp->assertSee('Copy Page');
229
230         $movePageResp = $this->post($page->getUrl('/copy'), [
231             'name' => 'My copied test page'
232         ]);
233
234         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
235
236         $movePageResp->assertRedirect($pageCopy->getUrl());
237         $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
238         $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
239     }
240
241     public function test_page_can_be_copied_without_edit_permission()
242     {
243         $page = Page::first();
244         $currentBook = $page->book;
245         $newBook = Book::where('id', '!=', $currentBook->id)->first();
246         $viewer = $this->getViewer();
247
248         $resp = $this->actingAs($viewer)->get($page->getUrl());
249         $resp->assertDontSee($page->getUrl('/copy'));
250
251         $newBook->created_by = $viewer->id;
252         $newBook->save();
253         $this->giveUserPermissions($viewer, ['page-create-own']);
254         $this->regenEntityPermissions($newBook);
255
256         $resp = $this->actingAs($viewer)->get($page->getUrl());
257         $resp->assertSee($page->getUrl('/copy'));
258
259         $movePageResp = $this->post($page->getUrl('/copy'), [
260             'entity_selection' => 'book:' . $newBook->id,
261             'name' => 'My copied test page'
262         ]);
263         $movePageResp->assertRedirect();
264
265         $this->assertDatabaseHas('pages', [
266             'name' => 'My copied test page',
267             'created_by' => $viewer->id,
268             'book_id' => $newBook->id,
269         ]);
270     }
271
272 }