3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Repos\BookRepo;
13 class CopyTest extends TestCase
15 public function test_book_show_view_has_copy_button()
17 $book = $this->entities->book();
18 $resp = $this->asEditor()->get($book->getUrl());
20 $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
23 public function test_book_copy_view()
25 $book = $this->entities->book();
26 $resp = $this->asEditor()->get($book->getUrl('/copy'));
29 $resp->assertSee('Copy Book');
30 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
33 public function test_book_copy()
35 /** @var Book $book */
36 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
37 $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
39 /** @var Book $copy */
40 $copy = Book::query()->where('name', '=', 'My copy book')->first();
42 $resp->assertRedirect($copy->getUrl());
43 $this->assertEquals($book->getDirectVisibleChildren()->count(), $copy->getDirectVisibleChildren()->count());
45 $this->get($copy->getUrl())->assertSee($book->description_html, false);
48 public function test_book_copy_does_not_copy_non_visible_content()
50 /** @var Book $book */
51 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
54 /** @var BookChild $page */
55 foreach ($book->getDirectVisibleChildren() as $child) {
56 $this->permissions->setEntityPermissions($child, [], []);
59 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
60 /** @var Book $copy */
61 $copy = Book::query()->where('name', '=', 'My copy book')->first();
63 $this->assertEquals(0, $copy->getDirectVisibleChildren()->count());
66 public function test_book_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
68 /** @var Book $book */
69 $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
70 $viewer = $this->users->viewer();
71 $this->permissions->grantUserRolePermissions($viewer, ['book-create-all']);
73 $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
74 /** @var Book $copy */
75 $copy = Book::query()->where('name', '=', 'My copy book')->first();
77 $this->assertEquals(0, $copy->pages()->count());
78 $this->assertEquals(0, $copy->chapters()->count());
81 public function test_book_copy_clones_cover_image_if_existing()
83 $book = $this->entities->book();
84 $bookRepo = $this->app->make(BookRepo::class);
85 $coverImageFile = $this->files->uploadedImage('cover.png');
86 $bookRepo->updateCoverImage($book, $coverImageFile);
88 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book'])->assertRedirect();
89 /** @var Book $copy */
90 $copy = Book::query()->where('name', '=', 'My copy book')->first();
92 $this->assertNotNull($copy->coverInfo()->getImage());
93 $this->assertNotEquals($book->coverInfo()->getImage()->id, $copy->coverInfo()->getImage()->id);
96 public function test_book_copy_adds_book_to_shelves_if_edit_permissions_allows()
98 /** @var Bookshelf $shelfA */
99 /** @var Bookshelf $shelfB */
100 [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
101 $book = $this->entities->book();
103 $shelfA->appendBook($book);
104 $shelfB->appendBook($book);
106 $viewer = $this->users->viewer();
107 $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
108 $this->permissions->setEntityPermissions($shelfB);
111 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
112 /** @var Book $copy */
113 $copy = Book::query()->where('name', '=', 'My copy book')->first();
115 $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
116 $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());
119 public function test_chapter_show_view_has_copy_button()
121 $chapter = $this->entities->chapter();
123 $resp = $this->asEditor()->get($chapter->getUrl());
124 $this->withHtml($resp)->assertElementContains("a[href$=\"{$chapter->getUrl('/copy')}\"]", 'Copy');
127 public function test_chapter_copy_view()
129 $chapter = $this->entities->chapter();
131 $resp = $this->asEditor()->get($chapter->getUrl('/copy'));
133 $resp->assertSee('Copy Chapter');
134 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$chapter->name}\"]");
135 $this->withHtml($resp)->assertElementExists('input[name="entity_selection"]');
138 public function test_chapter_copy()
140 /** @var Chapter $chapter */
141 $chapter = Chapter::query()->whereHas('pages')->first();
142 /** @var Book $otherBook */
143 $otherBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
145 $resp = $this->asEditor()->post($chapter->getUrl('/copy'), [
146 'name' => 'My copied chapter',
147 'entity_selection' => 'book:' . $otherBook->id,
150 /** @var Chapter $newChapter */
151 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
153 $resp->assertRedirect($newChapter->getUrl());
154 $this->assertEquals($otherBook->id, $newChapter->book_id);
155 $this->assertEquals($chapter->pages->count(), $newChapter->pages->count());
158 public function test_chapter_copy_does_not_copy_non_visible_pages()
160 $chapter = $this->entities->chapterHasPages();
162 // Hide pages to all non-admin roles
163 /** @var Page $page */
164 foreach ($chapter->pages as $page) {
165 $this->permissions->setEntityPermissions($page, [], []);
168 $this->asEditor()->post($chapter->getUrl('/copy'), [
169 'name' => 'My copied chapter',
172 /** @var Chapter $newChapter */
173 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
174 $this->assertEquals(0, $newChapter->pages()->count());
177 public function test_chapter_copy_does_not_copy_pages_if_user_cant_page_create()
179 $chapter = $this->entities->chapterHasPages();
180 $viewer = $this->users->viewer();
181 $this->permissions->grantUserRolePermissions($viewer, ['chapter-create-all']);
183 // Lacking permission results in no copied pages
184 $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
185 'name' => 'My copied chapter',
188 /** @var Chapter $newChapter */
189 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
190 $this->assertEquals(0, $newChapter->pages()->count());
192 $this->permissions->grantUserRolePermissions($viewer, ['page-create-all']);
194 // Having permission rules in copied pages
195 $this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
196 'name' => 'My copied again chapter',
199 /** @var Chapter $newChapter2 */
200 $newChapter2 = Chapter::query()->where('name', '=', 'My copied again chapter')->first();
201 $this->assertEquals($chapter->pages()->count(), $newChapter2->pages()->count());
204 public function test_book_copy_updates_internal_references()
206 $book = $this->entities->bookHasChaptersAndPages();
207 /** @var Chapter $chapter */
208 $chapter = $book->chapters()->first();
209 /** @var Page $page */
210 $page = $chapter->pages()->first();
212 $this->entities->updatePage($page, [
213 'name' => 'reference test page',
214 'html' => '<p>This is a test <a href="' . $book->getUrl() . '">book link</a></p>',
217 // Quick pre-update to get stable slug
218 $this->put($book->getUrl(), ['name' => 'Internal ref test']);
222 $html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
223 $this->put($book->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]);
225 $this->post($book->getUrl('/copy'), ['name' => 'My copied book']);
227 $newBook = Book::query()->where('name', '=', 'My copied book')->first();
228 $newPage = $newBook->pages()->where('name', '=', 'reference test page')->first();
230 $this->assertStringContainsString($newBook->getUrl(), $newPage->html);
231 $this->assertStringContainsString($newPage->getUrl(), $newBook->description_html);
233 $this->assertStringNotContainsString($book->getUrl(), $newPage->html);
234 $this->assertStringNotContainsString($page->getUrl(), $newBook->description_html);
237 public function test_chapter_copy_updates_internal_references()
239 $chapter = $this->entities->chapterHasPages();
240 /** @var Page $page */
241 $page = $chapter->pages()->first();
243 $this->entities->updatePage($page, [
244 'name' => 'reference test page',
245 'html' => '<p>This is a test <a href="' . $chapter->getUrl() . '">chapter link</a></p>',
248 // Quick pre-update to get stable slug
249 $this->put($chapter->getUrl(), ['name' => 'Internal ref test']);
253 $html = '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>';
254 $this->put($chapter->getUrl(), ['name' => 'Internal ref test', 'description_html' => $html]);
256 $this->post($chapter->getUrl('/copy'), ['name' => 'My copied chapter']);
258 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
259 $newPage = $newChapter->pages()->where('name', '=', 'reference test page')->first();
261 $this->assertStringContainsString($newChapter->getUrl() . '"', $newPage->html);
262 $this->assertStringContainsString($newPage->getUrl() . '"', $newChapter->description_html);
264 $this->assertStringNotContainsString($chapter->getUrl() . '"', $newPage->html);
265 $this->assertStringNotContainsString($page->getUrl() . '"', $newChapter->description_html);
268 public function test_chapter_copy_updates_internal_permalink_references_in_its_description()
270 $chapter = $this->entities->chapterHasPages();
271 /** @var Page $page */
272 $page = $chapter->pages()->first();
274 $this->asEditor()->put($chapter->getUrl(), [
275 'name' => 'Internal ref test',
276 'description_html' => '<p>This is a test <a href="' . $page->getPermalink() . '">page link</a></p>',
280 $this->post($chapter->getUrl('/copy'), ['name' => 'My copied chapter']);
281 $newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
283 $this->assertStringContainsString('/link/', $newChapter->description_html);
284 $this->assertStringNotContainsString($page->getPermalink() . '"', $newChapter->description_html);
287 public function test_page_copy_updates_internal_self_references()
289 $page = $this->entities->page();
292 // Initial update to get stable slug
293 $this->entities->updatePage($page, ['name' => 'reference test page']);
296 $this->entities->updatePage($page, [
297 'name' => 'reference test page',
298 'html' => '<p>This is a test <a href="' . $page->getUrl() . '">page link</a></p>',
301 $this->post($page->getUrl('/copy'), ['name' => 'My copied page']);
302 $newPage = Page::query()->where('name', '=', 'My copied page')->first();
303 $this->assertNotNull($newPage);
305 $this->assertStringContainsString($newPage->getUrl(), $newPage->html);
306 $this->assertStringNotContainsString($page->getUrl(), $newPage->html);
309 public function test_page_copy()
311 $page = $this->entities->page();
312 $page->html = '<p>This is some test content</p>';
315 $currentBook = $page->book;
316 $newBook = Book::where('id', '!=', $currentBook->id)->first();
318 $resp = $this->asEditor()->get($page->getUrl('/copy'));
319 $resp->assertSee('Copy Page');
321 $movePageResp = $this->post($page->getUrl('/copy'), [
322 'entity_selection' => 'book:' . $newBook->id,
323 'name' => 'My copied test page',
325 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
327 $movePageResp->assertRedirect($pageCopy->getUrl());
328 $this->assertTrue($pageCopy->book->id == $newBook->id, 'Page was copied to correct book');
329 $this->assertStringContainsString('This is some test content', $pageCopy->html);
332 public function test_page_copy_with_markdown_has_both_html_and_markdown()
334 $page = $this->entities->page();
335 $page->html = '<h1>This is some test content</h1>';
336 $page->markdown = '# This is some test content';
338 $newBook = Book::where('id', '!=', $page->book->id)->first();
340 $this->asEditor()->post($page->getUrl('/copy'), [
341 'entity_selection' => 'book:' . $newBook->id,
342 'name' => 'My copied test page',
344 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
346 $this->assertStringContainsString('This is some test content', $pageCopy->html);
347 $this->assertEquals('# This is some test content', $pageCopy->markdown);
350 public function test_page_copy_with_no_destination()
352 $page = $this->entities->page();
353 $currentBook = $page->book;
355 $resp = $this->asEditor()->get($page->getUrl('/copy'));
356 $resp->assertSee('Copy Page');
358 $movePageResp = $this->post($page->getUrl('/copy'), [
359 'name' => 'My copied test page',
362 $pageCopy = Page::where('name', '=', 'My copied test page')->first();
364 $movePageResp->assertRedirect($pageCopy->getUrl());
365 $this->assertTrue($pageCopy->book->id == $currentBook->id, 'Page was copied to correct book');
366 $this->assertTrue($pageCopy->id !== $page->id, 'Page copy is not the same instance');
369 public function test_page_can_be_copied_without_edit_permission()
371 $page = $this->entities->page();
372 $currentBook = $page->book;
373 $newBook = Book::where('id', '!=', $currentBook->id)->first();
374 $viewer = $this->users->viewer();
376 $resp = $this->actingAs($viewer)->get($page->getUrl());
377 $resp->assertDontSee($page->getUrl('/copy'));
379 $newBook->owned_by = $viewer->id;
381 $this->permissions->grantUserRolePermissions($viewer, ['page-create-own']);
382 $this->permissions->regenerateForEntity($newBook);
384 $resp = $this->actingAs($viewer)->get($page->getUrl());
385 $resp->assertSee($page->getUrl('/copy'));
387 $movePageResp = $this->post($page->getUrl('/copy'), [
388 'entity_selection' => 'book:' . $newBook->id,
389 'name' => 'My copied test page',
391 $movePageResp->assertRedirect();
393 $this->assertDatabaseHasEntityData('page', [
394 'name' => 'My copied test page',
395 'created_by' => $viewer->id,
396 'book_id' => $newBook->id,