3 namespace Tests\Entity;
5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\Models\Tag;
7 use BookStack\Entities\Models\Book;
8 use BookStack\Entities\Models\Bookshelf;
9 use BookStack\Entities\Models\Chapter;
10 use BookStack\Entities\Models\Page;
13 class ConvertTest extends TestCase
15 public function test_chapter_edit_view_shows_convert_option()
17 $chapter = $this->entities->chapter();
19 $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
20 $resp->assertSee('Convert to Book');
21 $resp->assertSee('Convert Chapter');
22 $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-book"] button');
25 public function test_convert_chapter_to_book()
27 $chapter = $this->entities->chapterHasPages();
28 $chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
29 /** @var Page $childPage */
30 $childPage = $chapter->pages()->first();
32 $resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
33 $resp->assertRedirectContains('/books/');
35 /** @var Book $newBook */
36 $newBook = Book::query()->orderBy('id', 'desc')->first();
38 $this->assertDatabaseMissing('chapters', ['id' => $chapter->id]);
39 $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $newBook->id, 'chapter_id' => 0]);
40 $this->assertCount(1, $newBook->tags);
41 $this->assertEquals('Category', $newBook->tags->first()->name);
42 $this->assertEquals('Penguins', $newBook->tags->first()->value);
43 $this->assertEquals($chapter->name, $newBook->name);
44 $this->assertEquals($chapter->description, $newBook->description);
45 $this->assertEquals($chapter->description_html, $newBook->description_html);
47 $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
50 public function test_convert_chapter_to_book_requires_permissions()
52 $chapter = $this->entities->chapter();
53 $user = $this->users->viewer();
55 $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
56 $this->permissions->grantUserRolePermissions($user, $permissions);
58 foreach ($permissions as $permission) {
59 $this->permissions->removeUserRolePermissions($user, [$permission]);
60 $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
61 $this->assertPermissionError($resp);
62 $this->permissions->grantUserRolePermissions($user, [$permission]);
65 $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
66 $this->assertNotPermissionError($resp);
67 $resp->assertRedirect();
70 public function test_book_edit_view_shows_convert_option()
72 $book = $this->entities->book();
74 $resp = $this->asEditor()->get($book->getUrl('/edit'));
75 $resp->assertSee('Convert to Shelf');
76 $resp->assertSee('Convert Book');
77 $resp->assertSee('Note that permissions on shelves do not auto-cascade to content');
78 $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-shelf"] button');
81 public function test_book_convert_to_shelf()
83 /** @var Book $book */
84 $book = Book::query()->whereHas('directPages')->whereHas('chapters')->firstOrFail();
85 $book->tags()->save(new Tag(['name' => 'Category', 'value' => 'Ducks']));
86 /** @var Page $childPage */
87 $childPage = $book->directPages()->first();
88 /** @var Chapter $childChapter */
89 $childChapter = $book->chapters()->whereHas('pages')->firstOrFail();
90 /** @var Page $chapterChildPage */
91 $chapterChildPage = $childChapter->pages()->firstOrFail();
92 $bookChapterCount = $book->chapters()->count();
93 $systemBookCount = Book::query()->count();
96 $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
98 /** @var Bookshelf $newShelf */
99 $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
101 // Checks for new shelf
102 $resp->assertRedirectContains('/shelves/');
103 $this->assertDatabaseMissing('chapters', ['id' => $childChapter->id]);
104 $this->assertCount(1, $newShelf->tags);
105 $this->assertEquals('Category', $newShelf->tags->first()->name);
106 $this->assertEquals('Ducks', $newShelf->tags->first()->value);
107 $this->assertEquals($book->name, $newShelf->name);
108 $this->assertEquals($book->description, $newShelf->description);
109 $this->assertEquals($book->description_html, $newShelf->description_html);
110 $this->assertEquals($newShelf->books()->count(), $bookChapterCount + 1);
111 $this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count());
112 $this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf);
114 // Checks for old book to contain child pages
115 $this->assertDatabaseHas('books', ['id' => $book->id, 'name' => $book->name . ' Pages']);
116 $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $book->id, 'chapter_id' => 0]);
118 // Checks for nested page
119 $chapterChildPage->refresh();
120 $this->assertEquals(0, $chapterChildPage->chapter_id);
121 $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
124 public function test_book_convert_to_shelf_requires_permissions()
126 $book = $this->entities->book();
127 $user = $this->users->viewer();
129 $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
130 $this->permissions->grantUserRolePermissions($user, $permissions);
132 foreach ($permissions as $permission) {
133 $this->permissions->removeUserRolePermissions($user, [$permission]);
134 $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
135 $this->assertPermissionError($resp);
136 $this->permissions->grantUserRolePermissions($user, [$permission]);
139 $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
140 $this->assertNotPermissionError($resp);
141 $resp->assertRedirect();