]> BookStack Code Mirror - bookstack/blob - tests/Sorting/MoveTest.php
Deps & Tests: Updated PHP deps, fixed test namespaces
[bookstack] / tests / Sorting / MoveTest.php
1 <?php
2
3 namespace Tests\Sorting;
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 MoveTest extends TestCase
11 {
12     public function test_page_move_into_book()
13     {
14         $page = $this->entities->page();
15         $currentBook = $page->book;
16         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
17
18         $resp = $this->asEditor()->get($page->getUrl('/move'));
19         $resp->assertSee('Move Page');
20
21         $movePageResp = $this->put($page->getUrl('/move'), [
22             'entity_selection' => 'book:' . $newBook->id,
23         ]);
24         $page->refresh();
25
26         $movePageResp->assertRedirect($page->getUrl());
27         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
28
29         $newBookResp = $this->get($newBook->getUrl());
30         $newBookResp->assertSee('moved page');
31         $newBookResp->assertSee($page->name);
32     }
33
34     public function test_page_move_into_chapter()
35     {
36         $page = $this->entities->page();
37         $currentBook = $page->book;
38         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
39         $newChapter = $newBook->chapters()->first();
40
41         $movePageResp = $this->actingAs($this->users->editor())->put($page->getUrl('/move'), [
42             'entity_selection' => 'chapter:' . $newChapter->id,
43         ]);
44         $page->refresh();
45
46         $movePageResp->assertRedirect($page->getUrl());
47         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter');
48
49         $newChapterResp = $this->get($newChapter->getUrl());
50         $newChapterResp->assertSee($page->name);
51     }
52
53     public function test_page_move_from_chapter_to_book()
54     {
55         $oldChapter = Chapter::query()->first();
56         $page = $oldChapter->pages()->first();
57         $newBook = Book::query()->where('id', '!=', $oldChapter->book_id)->first();
58
59         $movePageResp = $this->actingAs($this->users->editor())->put($page->getUrl('/move'), [
60             'entity_selection' => 'book:' . $newBook->id,
61         ]);
62         $page->refresh();
63
64         $movePageResp->assertRedirect($page->getUrl());
65         $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book');
66         $this->assertTrue($page->chapter === null, 'Page has no parent chapter');
67
68         $newBookResp = $this->get($newBook->getUrl());
69         $newBookResp->assertSee($page->name);
70     }
71
72     public function test_page_move_requires_create_permissions_on_parent()
73     {
74         $page = $this->entities->page();
75         $currentBook = $page->book;
76         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
77         $editor = $this->users->editor();
78
79         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete'], $editor->roles->all());
80
81         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
82             'entity_selection' => 'book:' . $newBook->id,
83         ]);
84         $this->assertPermissionError($movePageResp);
85
86         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete', 'create'], $editor->roles->all());
87         $movePageResp = $this->put($page->getUrl('/move'), [
88             'entity_selection' => 'book:' . $newBook->id,
89         ]);
90
91         $page->refresh();
92         $movePageResp->assertRedirect($page->getUrl());
93
94         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
95     }
96
97     public function test_page_move_requires_delete_permissions()
98     {
99         $page = $this->entities->page();
100         $currentBook = $page->book;
101         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
102         $editor = $this->users->editor();
103
104         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
105         $this->permissions->setEntityPermissions($page, ['view', 'update', 'create'], $editor->roles->all());
106
107         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
108             'entity_selection' => 'book:' . $newBook->id,
109         ]);
110         $this->assertPermissionError($movePageResp);
111         $pageView = $this->get($page->getUrl());
112         $pageView->assertDontSee($page->getUrl('/move'));
113
114         $this->permissions->setEntityPermissions($page, ['view', 'update', 'create', 'delete'], $editor->roles->all());
115         $movePageResp = $this->put($page->getUrl('/move'), [
116             'entity_selection' => 'book:' . $newBook->id,
117         ]);
118
119         $page->refresh();
120         $movePageResp->assertRedirect($page->getUrl());
121         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
122     }
123
124     public function test_chapter_move()
125     {
126         $chapter = $this->entities->chapter();
127         $currentBook = $chapter->book;
128         $pageToCheck = $chapter->pages->first();
129         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
130
131         $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
132         $chapterMoveResp->assertSee('Move Chapter');
133
134         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
135             'entity_selection' => 'book:' . $newBook->id,
136         ]);
137
138         $chapter = Chapter::query()->find($chapter->id);
139         $moveChapterResp->assertRedirect($chapter->getUrl());
140         $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
141
142         $newBookResp = $this->get($newBook->getUrl());
143         $newBookResp->assertSee('moved chapter');
144         $newBookResp->assertSee($chapter->name);
145
146         $pageToCheck = Page::query()->find($pageToCheck->id);
147         $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
148         $pageCheckResp = $this->get($pageToCheck->getUrl());
149         $pageCheckResp->assertSee($newBook->name);
150     }
151
152     public function test_chapter_move_requires_delete_permissions()
153     {
154         $chapter = $this->entities->chapter();
155         $currentBook = $chapter->book;
156         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
157         $editor = $this->users->editor();
158
159         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], $editor->roles->all());
160         $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create'], $editor->roles->all());
161
162         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
163             'entity_selection' => 'book:' . $newBook->id,
164         ]);
165         $this->assertPermissionError($moveChapterResp);
166         $pageView = $this->get($chapter->getUrl());
167         $pageView->assertDontSee($chapter->getUrl('/move'));
168
169         $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create', 'delete'], $editor->roles->all());
170         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
171             'entity_selection' => 'book:' . $newBook->id,
172         ]);
173
174         $chapter = Chapter::query()->find($chapter->id);
175         $moveChapterResp->assertRedirect($chapter->getUrl());
176         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
177     }
178
179     public function test_chapter_move_requires_create_permissions_in_new_book()
180     {
181         $chapter = $this->entities->chapter();
182         $currentBook = $chapter->book;
183         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
184         $editor = $this->users->editor();
185
186         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'delete'], [$editor->roles->first()]);
187         $this->permissions->setEntityPermissions($chapter, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
188
189         $moveChapterResp = $this->actingAs($editor)->put($chapter->getUrl('/move'), [
190             'entity_selection' => 'book:' . $newBook->id,
191         ]);
192         $this->assertPermissionError($moveChapterResp);
193
194         $this->permissions->setEntityPermissions($newBook, ['view', 'update', 'create', 'delete'], [$editor->roles->first()]);
195         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
196             'entity_selection' => 'book:' . $newBook->id,
197         ]);
198
199         $chapter = Chapter::query()->find($chapter->id);
200         $moveChapterResp->assertRedirect($chapter->getUrl());
201         $this->assertTrue($chapter->book->id == $newBook->id, 'Page book is now the new book');
202     }
203
204     public function test_chapter_move_changes_book_for_deleted_pages_within()
205     {
206         /** @var Chapter $chapter */
207         $chapter = Chapter::query()->whereHas('pages')->first();
208         $currentBook = $chapter->book;
209         $pageToCheck = $chapter->pages->first();
210         $newBook = Book::query()->where('id', '!=', $currentBook->id)->first();
211
212         $pageToCheck->delete();
213
214         $this->asEditor()->put($chapter->getUrl('/move'), [
215             'entity_selection' => 'book:' . $newBook->id,
216         ]);
217
218         $pageToCheck->refresh();
219         $this->assertEquals($newBook->id, $pageToCheck->book_id);
220     }
221 }