]> BookStack Code Mirror - bookstack/blob - tests/Entity/SortTest.php
Added ability to copy a page
[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->asAdmin()->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_chapter_move()
54     {
55         $chapter = Chapter::first();
56         $currentBook = $chapter->book;
57         $pageToCheck = $chapter->pages->first();
58         $newBook = Book::where('id', '!=', $currentBook->id)->first();
59
60         $chapterMoveResp = $this->asAdmin()->get($chapter->getUrl() . '/move');
61         $chapterMoveResp->assertSee('Move Chapter');
62
63         $moveChapterResp = $this->put($chapter->getUrl() . '/move', [
64             'entity_selection' => 'book:' . $newBook->id
65         ]);
66
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');
70
71         $newBookResp = $this->get($newBook->getUrl());
72         $newBookResp->assertSee('moved chapter');
73         $newBookResp->assertSee($chapter->name);
74
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);
79     }
80
81     public function test_book_sort()
82     {
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();
87
88         // Create request data
89         $reqData = [
90             [
91                 'id' => $chapterToMove->id,
92                 'sort' => 0,
93                 'parentChapter' => false,
94                 'type' => 'chapter',
95                 'book' => $newBook->id
96             ]
97         ];
98         foreach ($pagesToMove as $index => $page) {
99             $reqData[] = [
100                 'id' => $page->id,
101                 'sort' => $index,
102                 'parentChapter' => $index === count($pagesToMove) - 1 ? $chapterToMove->id : false,
103                 'type' => 'page',
104                 'book' => $newBook->id
105             ];
106         }
107
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,
114             'priority' => 0
115         ]);
116         $this->assertTrue($newBook->chapters()->count() === 1);
117         $this->assertTrue($newBook->chapters()->first()->pages()->count() === 1);
118
119         $checkPage = $pagesToMove[1];
120         $checkResp = $this->get(Page::find($checkPage->id)->getUrl());
121         $checkResp->assertSee($newBook->name);
122     }
123
124     public function test_page_copy()
125     {
126         $page = Page::first();
127         $currentBook = $page->book;
128         $newBook = Book::where('id', '!=', $currentBook->id)->first();
129
130         $resp = $this->asEditor()->get($page->getUrl('/copy'));
131         $resp->assertSee('Copy Page');
132
133         $movePageResp = $this->post($page->getUrl('/copy'), [
134             'entity_selection' => 'book:' . $newBook->id,
135             'name' => 'My copied test page'
136         ]);
137
138         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
139
140         $movePageResp->assertRedirect($pageCopy->getUrl());
141         $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
142     }
143
144     public function test_page_copy_with_no_destination()
145     {
146         $page = Page::first();
147         $currentBook = $page->book;
148
149         $resp = $this->asEditor()->get($page->getUrl('/copy'));
150         $resp->assertSee('Copy Page');
151
152         $movePageResp = $this->post($page->getUrl('/copy'), [
153             'name' => 'My copied test page'
154         ]);
155
156         $pageCopy = Page::where('name', '=', 'My copied test page')->first();
157
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');
161     }
162
163 }