3 namespace Tests\Entity;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\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);
46 $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
49 public function test_convert_chapter_to_book_requires_permissions()
51 $chapter = $this->entities->chapter();
52 $user = $this->users->viewer();
54 $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
55 $this->permissions->grantUserRolePermissions($user, $permissions);
57 foreach ($permissions as $permission) {
58 $this->permissions->removeUserRolePermissions($user, [$permission]);
59 $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
60 $this->assertPermissionError($resp);
61 $this->permissions->grantUserRolePermissions($user, [$permission]);
64 $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
65 $this->assertNotPermissionError($resp);
66 $resp->assertRedirect();
69 public function test_book_edit_view_shows_convert_option()
71 $book = $this->entities->book();
73 $resp = $this->asEditor()->get($book->getUrl('/edit'));
74 $resp->assertSee('Convert to Shelf');
75 $resp->assertSee('Convert Book');
76 $resp->assertSee('Note that permissions on shelves do not auto-cascade to content');
77 $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-shelf"] button');
80 public function test_book_convert_to_shelf()
82 /** @var Book $book */
83 $book = Book::query()->whereHas('directPages')->whereHas('chapters')->firstOrFail();
84 $book->tags()->save(new Tag(['name' => 'Category', 'value' => 'Ducks']));
85 /** @var Page $childPage */
86 $childPage = $book->directPages()->first();
87 /** @var Chapter $childChapter */
88 $childChapter = $book->chapters()->whereHas('pages')->firstOrFail();
89 /** @var Page $chapterChildPage */
90 $chapterChildPage = $childChapter->pages()->firstOrFail();
91 $bookChapterCount = $book->chapters()->count();
92 $systemBookCount = Book::query()->count();
95 $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
97 /** @var Bookshelf $newShelf */
98 $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
100 // Checks for new shelf
101 $resp->assertRedirectContains('/shelves/');
102 $this->assertDatabaseMissing('chapters', ['id' => $childChapter->id]);
103 $this->assertCount(1, $newShelf->tags);
104 $this->assertEquals('Category', $newShelf->tags->first()->name);
105 $this->assertEquals('Ducks', $newShelf->tags->first()->value);
106 $this->assertEquals($book->name, $newShelf->name);
107 $this->assertEquals($book->description, $newShelf->description);
108 $this->assertEquals($newShelf->books()->count(), $bookChapterCount + 1);
109 $this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count());
110 $this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf);
112 // Checks for old book to contain child pages
113 $this->assertDatabaseHas('books', ['id' => $book->id, 'name' => $book->name . ' Pages']);
114 $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $book->id, 'chapter_id' => 0]);
116 // Checks for nested page
117 $chapterChildPage->refresh();
118 $this->assertEquals(0, $chapterChildPage->chapter_id);
119 $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
122 public function test_book_convert_to_shelf_requires_permissions()
124 $book = $this->entities->book();
125 $user = $this->users->viewer();
127 $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
128 $this->permissions->grantUserRolePermissions($user, $permissions);
130 foreach ($permissions as $permission) {
131 $this->permissions->removeUserRolePermissions($user, [$permission]);
132 $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
133 $this->assertPermissionError($resp);
134 $this->permissions->grantUserRolePermissions($user, [$permission]);
137 $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
138 $this->assertNotPermissionError($resp);
139 $resp->assertRedirect();