5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\PageRepo;
8 use BookStack\Sorting\SortRule;
11 class BookSortTest extends TestCase
13 public function test_book_sort_page_shows()
15 $bookToSort = $this->entities->book();
17 $resp = $this->asAdmin()->get($bookToSort->getUrl());
18 $this->withHtml($resp)->assertElementExists('a[href="' . $bookToSort->getUrl('/sort') . '"]');
20 $resp = $this->get($bookToSort->getUrl('/sort'));
21 $resp->assertStatus(200);
22 $resp->assertSee($bookToSort->name);
25 public function test_drafts_do_not_show_up()
28 $pageRepo = app(PageRepo::class);
29 $book = $this->entities->book();
30 $draft = $pageRepo->getNewDraftPage($book);
32 $resp = $this->get($book->getUrl());
33 $resp->assertSee($draft->name);
35 $resp = $this->get($book->getUrl('/sort'));
36 $resp->assertDontSee($draft->name);
39 public function test_book_sort()
41 $oldBook = $this->entities->book();
42 $chapterToMove = $this->entities->newChapter(['name' => 'chapter to move'], $oldBook);
43 $newBook = $this->entities->newBook(['name' => 'New sort book']);
44 $pagesToMove = Page::query()->take(5)->get();
46 // Create request data
49 'id' => $chapterToMove->id,
51 'parentChapter' => false,
53 'book' => $newBook->id,
56 foreach ($pagesToMove as $index => $page) {
60 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
62 'book' => $newBook->id,
66 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
67 $sortResp->assertRedirect($newBook->getUrl());
68 $sortResp->assertStatus(302);
69 $this->assertDatabaseHas('chapters', [
70 'id' => $chapterToMove->id,
71 'book_id' => $newBook->id,
74 $this->assertTrue($newBook->chapters()->count() === 1);
75 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
77 $checkPage = $pagesToMove[1];
78 $checkResp = $this->get($checkPage->refresh()->getUrl());
79 $checkResp->assertSee($newBook->name);
82 public function test_book_sort_makes_no_changes_if_new_chapter_does_not_align_with_new_book()
84 $page = $this->entities->pageWithinChapter();
85 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
90 'parentChapter' => $otherChapter->id,
92 'book' => $page->book_id,
94 $this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
96 $this->assertDatabaseHas('pages', [
97 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
101 public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_chapter()
103 $page = $this->entities->pageWithinChapter();
104 /** @var Chapter $otherChapter */
105 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
106 $this->permissions->setEntityPermissions($otherChapter);
111 'parentChapter' => $otherChapter->id,
113 'book' => $otherChapter->book_id,
115 $this->asEditor()->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
117 $this->assertDatabaseHas('pages', [
118 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
122 public function test_book_sort_makes_no_changes_if_no_view_permissions_on_new_book()
124 $page = $this->entities->pageWithinChapter();
125 /** @var Chapter $otherChapter */
126 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
127 $editor = $this->users->editor();
128 $this->permissions->setEntityPermissions($otherChapter->book, ['update', 'delete'], [$editor->roles()->first()]);
133 'parentChapter' => $otherChapter->id,
135 'book' => $otherChapter->book_id,
137 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
139 $this->assertDatabaseHas('pages', [
140 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
144 public function test_book_sort_makes_no_changes_if_no_update_or_create_permissions_on_new_chapter()
146 $page = $this->entities->pageWithinChapter();
147 /** @var Chapter $otherChapter */
148 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
149 $editor = $this->users->editor();
150 $this->permissions->setEntityPermissions($otherChapter, ['view', 'delete'], [$editor->roles()->first()]);
155 'parentChapter' => $otherChapter->id,
157 'book' => $otherChapter->book_id,
159 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
161 $this->assertDatabaseHas('pages', [
162 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
166 public function test_book_sort_makes_no_changes_if_no_update_permissions_on_moved_item()
168 $page = $this->entities->pageWithinChapter();
169 /** @var Chapter $otherChapter */
170 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
171 $editor = $this->users->editor();
172 $this->permissions->setEntityPermissions($page, ['view', 'delete'], [$editor->roles()->first()]);
177 'parentChapter' => $otherChapter->id,
179 'book' => $otherChapter->book_id,
181 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
183 $this->assertDatabaseHas('pages', [
184 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
188 public function test_book_sort_makes_no_changes_if_no_delete_permissions_on_moved_item()
190 $page = $this->entities->pageWithinChapter();
191 /** @var Chapter $otherChapter */
192 $otherChapter = Chapter::query()->where('book_id', '!=', $page->book_id)->first();
193 $editor = $this->users->editor();
194 $this->permissions->setEntityPermissions($page, ['view', 'update'], [$editor->roles()->first()]);
199 'parentChapter' => $otherChapter->id,
201 'book' => $otherChapter->book_id,
203 $this->actingAs($editor)->put($page->book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
205 $this->assertDatabaseHas('pages', [
206 'id' => $page->id, 'chapter_id' => $page->chapter_id, 'book_id' => $page->book_id,
210 public function test_book_sort_does_not_change_timestamps_on_just_order_changes()
212 $book = $this->entities->bookHasChaptersAndPages();
213 $chapter = $book->chapters()->first();
214 \DB::table('chapters')->where('id', '=', $chapter->id)->update([
216 'updated_at' => \Carbon\Carbon::now()->subYear(5),
220 $oldUpdatedAt = $chapter->updated_at->unix();
223 'id' => $chapter->id,
225 'parentChapter' => false,
229 $this->asEditor()->put($book->getUrl('/sort'), ['sort-tree' => json_encode([$sortData])])->assertRedirect();
232 $this->assertNotEquals(10001, $chapter->priority);
233 $this->assertEquals($oldUpdatedAt, $chapter->updated_at->unix());
236 public function test_book_sort_item_returns_book_content()
238 $bookToSort = $this->entities->book();
239 $firstPage = $bookToSort->pages[0];
240 $firstChapter = $bookToSort->chapters[0];
242 $resp = $this->asAdmin()->get($bookToSort->getUrl('/sort-item'));
244 // Ensure book details are returned
245 $resp->assertSee($bookToSort->name);
246 $resp->assertSee($firstPage->name);
247 $resp->assertSee($firstChapter->name);
250 public function test_book_sort_item_shows_auto_sort_status()
252 $sort = SortRule::factory()->create(['name' => 'My sort']);
253 $book = $this->entities->book();
255 $resp = $this->asAdmin()->get($book->getUrl('/sort-item'));
256 $this->withHtml($resp)->assertElementNotExists("span[title='Auto Sort Active: My sort']");
258 $book->sort_rule_id = $sort->id;
261 $resp = $this->asAdmin()->get($book->getUrl('/sort-item'));
262 $this->withHtml($resp)->assertElementExists("span[title='Auto Sort Active: My sort']");
265 public function test_auto_sort_options_shown_on_sort_page()
267 $sort = SortRule::factory()->create();
268 $book = $this->entities->book();
269 $resp = $this->asAdmin()->get($book->getUrl('/sort'));
271 $this->withHtml($resp)->assertElementExists('select[name="auto-sort"] option[value="' . $sort->id . '"]');
274 public function test_auto_sort_option_submit_saves_to_book()
276 $sort = SortRule::factory()->create();
277 $book = $this->entities->book();
278 $bookPage = $book->pages()->first();
279 $bookPage->priority = 10000;
282 $resp = $this->asAdmin()->put($book->getUrl('/sort'), [
283 'auto-sort' => $sort->id,
286 $resp->assertRedirect($book->getUrl());
288 $bookPage->refresh();
290 $this->assertEquals($sort->id, $book->sort_rule_id);
291 $this->assertNotEquals(10000, $bookPage->priority);
293 $resp = $this->get($book->getUrl('/sort'));
294 $this->withHtml($resp)->assertElementExists('select[name="auto-sort"] option[value="' . $sort->id . '"][selected]');
297 public function test_pages_in_book_show_sorted_by_priority()
299 $book = $this->entities->bookHasChaptersAndPages();
300 $book->chapters()->forceDelete();
301 /** @var Page[] $pages */
302 $pages = $book->pages()->where('chapter_id', '=', 0)->take(2)->get();
303 $book->pages()->whereNotIn('id', $pages->pluck('id'))->delete();
305 $resp = $this->asEditor()->get($book->getUrl());
306 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[0]->name);
307 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[1]->name);
309 $pages[0]->forceFill(['priority' => 10])->save();
310 $pages[1]->forceFill(['priority' => 5])->save();
312 $resp = $this->asEditor()->get($book->getUrl());
313 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(1)', $pages[1]->name);
314 $this->withHtml($resp)->assertElementContains('.content-wrap a.page:nth-child(2)', $pages[0]->name);