]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
#47 - Fixes the issues with the test case.
[bookstack] / tests / Entity / SortTest.php
1 <?php namespace Tests;
2
3 use BookStack\Book;
4 use BookStack\Page;
5 use BookStack\Repos\EntityRepo;
6
7 class SortTest extends TestCase
8 {
9     protected $book;
10
11     public function setUp()
12     {
13         parent::setUp();
14         $this->book = \BookStack\Book::first();
15     }
16
17     public function test_drafts_do_not_show_up()
18     {
19         $this->asAdmin();
20         $entityRepo = app(EntityRepo::class);
21         $draft = $entityRepo->getDraftPage($this->book);
22
23         $resp = $this->get($this->book->getUrl());
24         $resp->assertSee($draft->name);
25
26         $resp = $this->get($this->book->getUrl() . '/sort');
27         $resp->assertDontSee($draft->name);
28     }
29
30     public function test_page_move()
31     {
32         $page = \BookStack\Page::first();
33         $currentBook = $page->book;
34         $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
35
36         $resp = $this->asAdmin()->get($page->getUrl() . '/move');
37         $resp->assertSee('Move Page');
38
39         $movePageResp = $this->put($page->getUrl() . '/move', [
40             'entity_selection' => 'book:' . $newBook->id
41         ]);
42         $page = \BookStack\Page::find($page->id);
43
44         $movePageResp->assertRedirect($page->getUrl());
45         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
46
47         $newBookResp = $this->get($newBook->getUrl());
48         $newBookResp->assertSee('moved page');
49         $newBookResp->assertSee($page->name);
50     }
51
52     public function test_chapter_move()
53     {
54         $chapter = \BookStack\Chapter::first();
55         $currentBook = $chapter->book;
56         $pageToCheck = $chapter->pages->first();
57         $newBook = \BookStack\Book::where('id', '!=', $currentBook->id)->first();
58
59         $chapterMoveResp = $this->asAdmin()->get($chapter->getUrl() . '/move');
60         $chapterMoveResp->assertSee('Move Chapter');
61
62         $moveChapterResp = $this->put($chapter->getUrl() . '/move', [
63             'entity_selection' => 'book:' . $newBook->id
64         ]);
65
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');
69
70         $newBookResp = $this->get($newBook->getUrl());
71         $newBookResp->assertSee('moved chapter');
72         $newBookResp->assertSee($chapter->name);
73
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);
78     }
79
80     public function test_book_sort()
81     {
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();
86
87         // Create request data
88         $reqData = [
89             [
90                 'id' => $chapterToMove->id,
91                 'sort' => 0,
92                 'parentChapter' => false,
93                 'type' => 'chapter',
94                 'book' => $newBook->id
95             ]
96         ];
97         foreach ($pagesToMove as $index => $page) {
98             $reqData[] = [
99                 'id' => $page->id,
100                 'sort' => $index,
101                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
102                 'type' => 'page',
103                 'book' => $newBook->id
104             ];
105         }
106
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,
113             'priority' => 0
114         ]);
115         $this->assertTrue($newBook->chapters()->count() === 1);
116         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
117
118         $checkPage = $pagesToMove[1];
119         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
120         $checkResp->assertSee($newBook->name);
121     }
122
123 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.