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