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