]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
fix image delete confirm text
[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::first();
95         $currentBook = $page->book;
96         $newBook = Book::where('id', '!=', $currentBook->id)->first();
97         $editor = $this->getEditor();
98
99         $this->setEntityRestrictions($newBook, ['view', 'update', 'delete'], $editor->roles);
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);
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);
125         $this->setEntityRestrictions($page, ['view', 'update', 'create'], $editor->roles);
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);
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);
180         $this->setEntityRestrictions($chapter, ['view', 'update', 'create'], $editor->roles);
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);
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_book_sort()
200     {
201         $oldBook = Book::query()->first();
202         $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
203         $newBook = $this->newBook(['name' => 'New sort book']);
204         $pagesToMove = Page::query()->take(5)->get();
205
206         // Create request data
207         $reqData = [
208             [
209                 'id' => $chapterToMove->id,
210                 'sort' => 0,
211                 'parentChapter' => false,
212                 'type' => 'chapter',
213                 'book' => $newBook->id
214             ]
215         ];
216         foreach ($pagesToMove as $index => $page) {
217             $reqData[] = [
218                 'id' => $page->id,
219                 'sort' => $index,
220                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
221                 'type' => 'page',
222                 'book' => $newBook->id
223             ];
224         }
225
226         $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
227         $sortResp->assertRedirect($newBook->getUrl());
228         $sortResp->assertStatus(302);
229         $this->assertDatabaseHas('chapters', [
230             'id' => $chapterToMove->id,
231             'book_id' => $newBook->id,
232             'priority' => 0
233         ]);
234         $this->assertTrue($newBook->chapters()->count() === 1);
235         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
236
237         $checkPage = $pagesToMove[1];
238         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
239         $checkResp->assertSee($newBook->name);
240     }
241
242 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.