]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
Merge pull request #846 from moucho/master
[bookstack] / tests / Entity / SortTest.php
1 <?php namespace Tests;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Page;
6 use BookStack\Repos\EntityRepo;
7
8 class SortTest extends TestCase
9 {
10     protected $book;
11
12     public function setUp()
13     {
14         parent::setUp();
15         $this->book = Book::first();
16     }
17
18     public function test_drafts_do_not_show_up()
19     {
20         $this->asAdmin();
21         $entityRepo = app(EntityRepo::class);
22         $draft = $entityRepo->getDraftPage($this->book);
23
24         $resp = $this->get($this->book->getUrl());
25         $resp->assertSee($draft->name);
26
27         $resp = $this->get($this->book->getUrl() . '/sort');
28         $resp->assertDontSee($draft->name);
29     }
30
31     public function test_page_move()
32     {
33         $page = Page::first();
34         $currentBook = $page->book;
35         $newBook = Book::where('id', '!=', $currentBook->id)->first();
36
37         $resp = $this->asEditor()->get($page->getUrl('/move'));
38         $resp->assertSee('Move Page');
39
40         $movePageResp = $this->put($page->getUrl('/move'), [
41             'entity_selection' => 'book:' . $newBook->id
42         ]);
43         $page = Page::find($page->id);
44
45         $movePageResp->assertRedirect($page->getUrl());
46         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
47
48         $newBookResp = $this->get($newBook->getUrl());
49         $newBookResp->assertSee('moved page');
50         $newBookResp->assertSee($page->name);
51     }
52
53     public function test_page_move_requires_create_permissions_on_parent()
54     {
55         $page = Page::first();
56         $currentBook = $page->book;
57         $newBook = Book::where('id', '!=', $currentBook->id)->first();
58         $editor = $this->getEditor();
59
60         $this->setEntityRestrictions($newBook, ['view', 'edit', 'delete'], $editor->roles);
61
62         $movePageResp = $this->actingAs($editor)->put($page->getUrl('/move'), [
63             'entity_selection' => 'book:' . $newBook->id
64         ]);
65         $this->assertPermissionError($movePageResp);
66
67         $this->setEntityRestrictions($newBook, ['view', 'edit', 'delete', 'create'], $editor->roles);
68         $movePageResp = $this->put($page->getUrl('/move'), [
69             'entity_selection' => 'book:' . $newBook->id
70         ]);
71
72         $page = Page::find($page->id);
73         $movePageResp->assertRedirect($page->getUrl());
74
75         $this->assertTrue($page->book->id == $newBook->id, 'Page book is now the new book');
76     }
77
78     public function test_chapter_move()
79     {
80         $chapter = Chapter::first();
81         $currentBook = $chapter->book;
82         $pageToCheck = $chapter->pages->first();
83         $newBook = Book::where('id', '!=', $currentBook->id)->first();
84
85         $chapterMoveResp = $this->asEditor()->get($chapter->getUrl('/move'));
86         $chapterMoveResp->assertSee('Move Chapter');
87
88         $moveChapterResp = $this->put($chapter->getUrl('/move'), [
89             'entity_selection' => 'book:' . $newBook->id
90         ]);
91
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');
95
96         $newBookResp = $this->get($newBook->getUrl());
97         $newBookResp->assertSee('moved chapter');
98         $newBookResp->assertSee($chapter->name);
99
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);
104     }
105
106     public function test_book_sort()
107     {
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();
112
113         // Create request data
114         $reqData = [
115             [
116                 'id' => $chapterToMove->id,
117                 'sort' => 0,
118                 'parentChapter' => false,
119                 'type' => 'chapter',
120                 'book' => $newBook->id
121             ]
122         ];
123         foreach ($pagesToMove as $index => $page) {
124             $reqData[] = [
125                 'id' => $page->id,
126                 'sort' => $index,
127                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
128                 'type' => 'page',
129                 'book' => $newBook->id
130             ];
131         }
132
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,
139             'priority' => 0
140         ]);
141         $this->assertTrue($newBook->chapters()->count() === 1);
142         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
143
144         $checkPage = $pagesToMove[1];
145         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
146         $checkResp->assertSee($newBook->name);
147     }
148
149     public function test_page_copy()
150     {
151         $page = Page::first();
152         $currentBook = $page->book;
153         $newBook = Book::where('id', '!=', $currentBook->id)->first();
154
155         $resp = $this->asEditor()->get($page->getUrl('/copy'));
156         $resp->assertSee('Copy Page');
157
158         $movePageResp = $this->post($page->getUrl('/copy'), [
159             'entity_selection' => 'book:' . $newBook->id,
160             'name' => 'My copied test page'
161         ]);
162
163         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
164
165         $movePageResp->assertRedirect($pageCopy->getUrl());
166         $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
167     }
168
169     public function test_page_copy_with_no_destination()
170     {
171         $page = Page::first();
172         $currentBook = $page->book;
173
174         $resp = $this->asEditor()->get($page->getUrl('/copy'));
175         $resp->assertSee('Copy Page');
176
177         $movePageResp = $this->post($page->getUrl('/copy'), [
178             'name' => 'My copied test page'
179         ]);
180
181         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
182
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');
186     }
187
188 }