5 use BookStack\Repos\EntityRepo;
7 class SortTest extends TestCase
11 public function setUp()
14 $this->book = \BookStack\Book::first();
17 public function test_drafts_do_not_show_up()
20 $entityRepo = app(EntityRepo::class);
21 $draft = $entityRepo->getDraftPage($this->book);
23 $resp = $this->get($this->book->getUrl());
24 $resp->assertSee($draft->name);
26 $resp = $this->get($this->book->getUrl() . '/sort');
27 $resp->assertDontSee($draft->name);
30 public function test_page_move()
32 $page = \BookStack\Page::first();
33 $currentBook = $page->book;
34 $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
36 $resp = $this->asAdmin()->get($page->getUrl() . '/move');
37 $resp->assertSee('Move Page');
39 $movePageResp = $this->put($page->getUrl() . '/move', [
40 'entity_selection' => 'book:' . $newBook->id
42 $page = \BookStack\Page::find($page->id);
44 $movePageResp->assertRedirect($page->getUrl());
45 $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
47 $newBookResp = $this->get($newBook->getUrl());
48 $newBookResp->assertSee('moved page');
49 $newBookResp->assertSee($page->name);
52 public function test_chapter_move()
54 $chapter = \BookStack\Chapter::first();
55 $currentBook = $chapter->book;
56 $pageToCheck = $chapter->pages->first();
57 $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
59 $chapterMoveResp = $this->asAdmin()->get($chapter->getUrl() . '/move');
60 $chapterMoveResp->assertSee('Move Chapter');
62 $moveChapterResp = $this->put($chapter->getUrl() . '/move', [
63 'entity_selection' => 'book:' . $newBook->id
66 $chapter = \BookStack\Chapter::find($chapter->id);
67 $moveChapterResp->assertRedirect($chapter->getUrl());
68 $this->assertTrue($chapter->book->id === $newBook->id, 'Chapter Book is now the new book');
70 $newBookResp = $this->get($newBook->getUrl());
71 $newBookResp->assertSee('moved chapter');
72 $newBookResp->assertSee($chapter->name);
74 $pageToCheck = \BookStack\Page::find($pageToCheck->id);
75 $this->assertTrue($pageToCheck->book_id === $newBook->id, 'Chapter child page\'s book id has changed to the new book');
76 $pageCheckResp = $this->get($pageToCheck->getUrl());
77 $pageCheckResp->assertSee($newBook->name);
80 public function test_book_sort()
82 $oldBook = Book::query()->first();
83 $chapterToMove = $this->newChapter(['name' => 'chapter to move'], $oldBook);
84 $newBook = $this->newBook(['name' => 'New sort book']);
85 $pagesToMove = Page::query()->take(5)->get();
87 // Create request data
90 'id' => $chapterToMove->id,
92 'parentChapter' => false,
94 'book' => $newBook->id
97 foreach ($pagesToMove as $index => $page) {
101 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
103 'book' => $newBook->id
107 $sortResp = $this->asAdmin()->put($newBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]);
108 $sortResp->assertRedirect($newBook->getUrl());
109 $sortResp->assertStatus(302);
110 $this->assertDatabaseHas('chapters', [
111 'id' => $chapterToMove->id,
112 'book_id' => $newBook->id,
115 $this->assertTrue($newBook->chapters()->count() === 1);
116 $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
118 $checkPage = $pagesToMove[1];
119 $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
120 $checkResp->assertSee($newBook->name);