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