3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Entities\Repos\PageRepo;
9 class SortTest extends TestCase
13 public function setUp()
16 $this->book = Book::first();
19 public function test_drafts_do_not_show_up()
22 $pageRepo = app(PageRepo::class);
23 $draft = $pageRepo->getDraftPage($this->book);
25 $resp = $this->get($this->book->getUrl());
26 $resp->assertSee($draft->name);
28 $resp = $this->get($this->book->getUrl() . '/sort');
29 $resp->assertDontSee($draft->name);
32 public function test_page_move()
34 $page = Page::first();
35 $currentBook = $page->book;
36 $newBook = Book::where('id', '!=', $currentBook->id)->first();
38 $resp = $this->asEditor()->get($page->getUrl('/move'));
39 $resp->assertSee('Move Page');
41 $movePageResp = $this->put($page->getUrl('/move'), [
42 'entity_selection' => 'book:' . $newBook->id
44 $page = Page::find($page->id);
46 $movePageResp->assertRedirect($page->getUrl());
47 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
49 $newBookResp = $this->get($newBook->getUrl());
50 $newBookResp->assertSee('moved page');
51 $newBookResp->assertSee($page->name);
54 public function test_page_move_requires_create_permissions_on_parent()
56 $page = Page::first();
57 $currentBook = $page->book;
58 $newBook = Book::where('id', '!=', $currentBook->id)->first();
59 $editor = $this->getEditor();
61 $this->setEntityRestrictions($newBook, ['view', 'edit', 'delete'], $editor->roles);
63 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
64 'entity_selection' => 'book:' . $newBook->id
66 $this->assertPermissionError($movePageResp);
68 $this->setEntityRestrictions($newBook, ['view', 'edit', 'delete', 'create'], $editor->roles);
69 $movePageResp = $this->put($page->getUrl('/move'), [
70 'entity_selection' => 'book:' . $newBook->id
73 $page = Page::find($page->id);
74 $movePageResp->assertRedirect($page->getUrl());
76 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
79 public function test_chapter_move()
81 $chapter = Chapter::first();
82 $currentBook = $chapter->book;
83 $pageToCheck = $chapter->pages->first();
84 $newBook = Book::where('id', '!=', $currentBook->id)->first();
86 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
87 $chapterMoveResp->assertSee('Move Chapter');
89 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
90 'entity_selection' => 'book:' . $newBook->id
93 $chapter = Chapter::find($chapter->id);
94 $moveChapterResp->assertRedirect($chapter->getUrl());
95 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
97 $newBookResp = $this->get($newBook->getUrl());
98 $newBookResp->assertSee('moved chapter');
99 $newBookResp->assertSee($chapter->name);
101 $pageToCheck = Page::find($pageToCheck->id);
102 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
103 $pageCheckResp = $this->get($pageToCheck->getUrl());
104 $pageCheckResp->assertSee($newBook->name);
107 public function test_book_sort()
109 $oldBook = Book::query()->first();
110 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
111 $newBook = $this->newBook(['name' => 'New sort book']);
112 $pagesToMove = Page::query()->take(5)->get();
114 // Create request data
117 'id' => $chapterToMove->id,
119 'parentChapter' => false,
121 'book' => $newBook->id
124 foreach ($pagesToMove as $index => $page) {
128 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
130 'book' => $newBook->id
134 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
135 $sortResp->assertRedirect($newBook->getUrl());
136 $sortResp->assertStatus(302);
137 $this->assertDatabaseHas('chapters', [
138 'id' => $chapterToMove->id,
139 'book_id' => $newBook->id,
142 $this->assertTrue($newBook->chapters()->count() === 1);
143 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
145 $checkPage = $pagesToMove[1];
146 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
147 $checkResp->assertSee($newBook->name);
150 public function test_page_copy()
152 $page = Page::first();
153 $currentBook = $page->book;
154 $newBook = Book::where('id', '!=', $currentBook->id)->first();
156 $resp = $this->asEditor()->get($page->getUrl('/copy'));
157 $resp->assertSee('Copy Page');
159 $movePageResp = $this->post($page->getUrl('/copy'), [
160 'entity_selection' => 'book:' . $newBook->id,
161 'name' => 'My copied test page'
164 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
166 $movePageResp->assertRedirect($pageCopy->getUrl());
167 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
170 public function test_page_copy_with_no_destination()
172 $page = Page::first();
173 $currentBook = $page->book;
175 $resp = $this->asEditor()->get($page->getUrl('/copy'));
176 $resp->assertSee('Copy Page');
178 $movePageResp = $this->post($page->getUrl('/copy'), [
179 'name' => 'My copied test page'
182 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
184 $movePageResp->assertRedirect($pageCopy->getUrl());
185 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
186 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');