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->asAdmin()->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_chapter_move()
55 $chapter = Chapter::first();
56 $currentBook = $chapter->book;
57 $pageToCheck = $chapter->pages->first();
58 $newBook = Book::where('id', '!=', $currentBook->id)->first();
60 $chapterMoveResp = $this->asAdmin()->get($chapter->getUrl() . '/move');
61 $chapterMoveResp->assertSee('Move Chapter');
63 $moveChapterResp = $this->put($chapter->getUrl() . '/move', [
64 'entity_selection' => 'book:' . $newBook->id
67 $chapter = Chapter::find($chapter->id);
68 $moveChapterResp->assertRedirect($chapter->getUrl());
69 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
71 $newBookResp = $this->get($newBook->getUrl());
72 $newBookResp->assertSee('moved chapter');
73 $newBookResp->assertSee($chapter->name);
75 $pageToCheck = Page::find($pageToCheck->id);
76 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
77 $pageCheckResp = $this->get($pageToCheck->getUrl());
78 $pageCheckResp->assertSee($newBook->name);
81 public function test_book_sort()
83 $oldBook = Book::query()->first();
84 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
85 $newBook = $this->newBook(['name' => 'New sort book']);
86 $pagesToMove = Page::query()->take(5)->get();
88 // Create request data
91 'id' => $chapterToMove->id,
93 'parentChapter' => false,
95 'book' => $newBook->id
98 foreach ($pagesToMove as $index => $page) {
102 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
104 'book' => $newBook->id
108 $sortResp = $this->asAdmin()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
109 $sortResp->assertRedirect($newBook->getUrl());
110 $sortResp->assertStatus(302);
111 $this->assertDatabaseHas('chapters', [
112 'id' => $chapterToMove->id,
113 'book_id' => $newBook->id,
116 $this->assertTrue($newBook->chapters()->count() === 1);
117 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
119 $checkPage = $pagesToMove[1];
120 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
121 $checkResp->assertSee($newBook->name);
124 public function test_page_copy()
126 $page = Page::first();
127 $currentBook = $page->book;
128 $newBook = Book::where('id', '!=', $currentBook->id)->first();
130 $resp = $this->asEditor()->get($page->getUrl('/copy'));
131 $resp->assertSee('Copy Page');
133 $movePageResp = $this->post($page->getUrl('/copy'), [
134 'entity_selection' => 'book:' . $newBook->id,
135 'name' => 'My copied test page'
138 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
140 $movePageResp->assertRedirect($pageCopy->getUrl());
141 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
144 public function test_page_copy_with_no_destination()
146 $page = Page::first();
147 $currentBook = $page->book;
149 $resp = $this->asEditor()->get($page->getUrl('/copy'));
150 $resp->assertSee('Copy Page');
152 $movePageResp = $this->post($page->getUrl('/copy'), [
153 'name' => 'My copied test page'
156 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
158 $movePageResp->assertRedirect($pageCopy->getUrl());
159 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
160 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');