6 use BookStack\Repos\EntityRepo;
8 class SortTest extends TestCase
12 public function setUp()
15 $this->book = Book::first();
18 public function test_drafts_do_not_show_up()
21 $entityRepo = app(EntityRepo::class);
22 $draft = $entityRepo->getDraftPage($this->book);
24 $resp = $this->get($this->book->getUrl());
25 $resp->assertSee($draft->name);
27 $resp = $this->get($this->book->getUrl() . '/sort');
28 $resp->assertDontSee($draft->name);
31 public function test_page_move()
33 $page = Page::first();
34 $currentBook = $page->book;
35 $newBook = Book::where('id', '!=', $currentBook->id)->first();
37 $resp = $this->asEditor()->get($page->getUrl('/move'));
38 $resp->assertSee('Move Page');
40 $movePageResp = $this->put($page->getUrl('/move'), [
41 'entity_selection' => 'book:' . $newBook->id
43 $page = Page::find($page->id);
45 $movePageResp->assertRedirect($page->getUrl());
46 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
48 $newBookResp = $this->get($newBook->getUrl());
49 $newBookResp->assertSee('moved page');
50 $newBookResp->assertSee($page->name);
53 public function test_page_move_requires_create_permissions_on_parent()
55 $page = Page::first();
56 $currentBook = $page->book;
57 $newBook = Book::where('id', '!=', $currentBook->id)->first();
58 $editor = $this->getEditor();
60 $this->setEntityRestrictions($newBook, ['view', 'edit', 'delete'], $editor->roles);
62 $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
63 'entity_selection' => 'book:' . $newBook->id
65 $this->assertPermissionError($movePageResp);
67 $this->setEntityRestrictions($newBook, ['view', 'edit', 'delete', 'create'], $editor->roles);
68 $movePageResp = $this->put($page->getUrl('/move'), [
69 'entity_selection' => 'book:' . $newBook->id
72 $page = Page::find($page->id);
73 $movePageResp->assertRedirect($page->getUrl());
75 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
78 public function test_chapter_move()
80 $chapter = Chapter::first();
81 $currentBook = $chapter->book;
82 $pageToCheck = $chapter->pages->first();
83 $newBook = Book::where('id', '!=', $currentBook->id)->first();
85 $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
86 $chapterMoveResp->assertSee('Move Chapter');
88 $moveChapterResp = $this->put($chapter->getUrl('/move'), [
89 'entity_selection' => 'book:' . $newBook->id
92 $chapter = Chapter::find($chapter->id);
93 $moveChapterResp->assertRedirect($chapter->getUrl());
94 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
96 $newBookResp = $this->get($newBook->getUrl());
97 $newBookResp->assertSee('moved chapter');
98 $newBookResp->assertSee($chapter->name);
100 $pageToCheck = Page::find($pageToCheck->id);
101 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
102 $pageCheckResp = $this->get($pageToCheck->getUrl());
103 $pageCheckResp->assertSee($newBook->name);
106 public function test_book_sort()
108 $oldBook = Book::query()->first();
109 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
110 $newBook = $this->newBook(['name' => 'New sort book']);
111 $pagesToMove = Page::query()->take(5)->get();
113 // Create request data
116 'id' => $chapterToMove->id,
118 'parentChapter' => false,
120 'book' => $newBook->id
123 foreach ($pagesToMove as $index => $page) {
127 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
129 'book' => $newBook->id
133 $sortResp = $this->asEditor()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
134 $sortResp->assertRedirect($newBook->getUrl());
135 $sortResp->assertStatus(302);
136 $this->assertDatabaseHas('chapters', [
137 'id' => $chapterToMove->id,
138 'book_id' => $newBook->id,
141 $this->assertTrue($newBook->chapters()->count() === 1);
142 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
144 $checkPage = $pagesToMove[1];
145 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
146 $checkResp->assertSee($newBook->name);
149 public function test_page_copy()
151 $page = Page::first();
152 $currentBook = $page->book;
153 $newBook = Book::where('id', '!=', $currentBook->id)->first();
155 $resp = $this->asEditor()->get($page->getUrl('/copy'));
156 $resp->assertSee('Copy Page');
158 $movePageResp = $this->post($page->getUrl('/copy'), [
159 'entity_selection' => 'book:' . $newBook->id,
160 'name' => 'My copied test page'
163 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
165 $movePageResp->assertRedirect($pageCopy->getUrl());
166 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
169 public function test_page_copy_with_no_destination()
171 $page = Page::first();
172 $currentBook = $page->book;
174 $resp = $this->asEditor()->get($page->getUrl('/copy'));
175 $resp->assertSee('Copy Page');
177 $movePageResp = $this->post($page->getUrl('/copy'), [
178 'name' => 'My copied test page'
181 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
183 $movePageResp->assertRedirect($pageCopy->getUrl());
184 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
185 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');