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 /** @var Chapter $chapter */
18 $chapter = Chapter::query()->first();
20 $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
21 $resp->assertSee('Convert to Book');
22 $resp->assertSee('Convert Chapter');
23 $resp->assertElementExists('form[action$="/convert-to-book"] button');
26 public function test_convert_chapter_to_book()
28 /** @var Chapter $chapter */
29 $chapter = Chapter::query()->whereHas('pages')->first();
30 $chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
31 /** @var Page $childPage */
32 $childPage = $chapter->pages()->first();
34 $resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
35 $resp->assertRedirectContains('/books/');
37 /** @var Book $newBook */
38 $newBook = Book::query()->orderBy('id', 'desc')->first();
40 $this->assertDatabaseMissing('chapters', ['id' => $chapter->id]);
41 $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $newBook->id, 'chapter_id' => 0]);
42 $this->assertCount(1, $newBook->tags);
43 $this->assertEquals('Category', $newBook->tags->first()->name);
44 $this->assertEquals('Penguins', $newBook->tags->first()->value);
45 $this->assertEquals($chapter->name, $newBook->name);
46 $this->assertEquals($chapter->description, $newBook->description);
48 $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
51 public function test_convert_chapter_to_book_requires_permissions()
53 /** @var Chapter $chapter */
54 $chapter = Chapter::query()->first();
55 $user = $this->getViewer();
57 $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
58 $this->giveUserPermissions($user, $permissions);
60 foreach ($permissions as $permission) {
61 $this->removePermissionFromUser($user, $permission);
62 $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
63 $this->assertPermissionError($resp);
64 $this->giveUserPermissions($user, [$permission]);
67 $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
68 $this->assertNotPermissionError($resp);
69 $resp->assertRedirect();
72 public function test_book_edit_view_shows_convert_option()
74 $book = Book::query()->first();
76 $resp = $this->asEditor()->get($book->getUrl('/edit'));
77 $resp->assertSee('Convert to Shelf');
78 $resp->assertSee('Convert Book');
79 $resp->assertSee('Note that permissions on shelves do not auto-cascade to content');
80 $resp->assertElementExists('form[action$="/convert-to-shelf"] button');
83 public function test_book_convert_to_shelf()
85 /** @var Book $book */
86 $book = Book::query()->whereHas('directPages')->whereHas('chapters')->firstOrFail();
87 $book->tags()->save(new Tag(['name' => 'Category', 'value' => 'Ducks']));
88 /** @var Page $childPage */
89 $childPage = $book->directPages()->first();
90 /** @var Chapter $childChapter */
91 $childChapter = $book->chapters()->whereHas('pages')->firstOrFail();
92 /** @var Page $chapterChildPage */
93 $chapterChildPage = $childChapter->pages()->firstOrFail();
94 $bookChapterCount = $book->chapters()->count();
95 $systemBookCount = Book::query()->count();
98 $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
100 /** @var Bookshelf $newShelf */
101 $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
103 // Checks for new shelf
104 $resp->assertRedirectContains('/shelves/');
105 $this->assertDatabaseMissing('chapters', ['id' => $childChapter->id]);
106 $this->assertCount(1, $newShelf->tags);
107 $this->assertEquals('Category', $newShelf->tags->first()->name);
108 $this->assertEquals('Ducks', $newShelf->tags->first()->value);
109 $this->assertEquals($book->name, $newShelf->name);
110 $this->assertEquals($book->description, $newShelf->description);
111 $this->assertEquals($newShelf->books()->count(), $bookChapterCount + 1);
112 $this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count());
113 $this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf);
115 // Checks for old book to contain child pages
116 $this->assertDatabaseHas('books', ['id' => $book->id, 'name' => $book->name . ' Pages']);
117 $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $book->id, 'chapter_id' => 0]);
119 // Checks for nested page
120 $chapterChildPage->refresh();
121 $this->assertEquals(0, $chapterChildPage->chapter_id);
122 $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
125 public function test_book_convert_to_shelf_requires_permissions()
127 /** @var Book $book */
128 $book = Book::query()->first();
129 $user = $this->getViewer();
131 $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
132 $this->giveUserPermissions($user, $permissions);
134 foreach ($permissions as $permission) {
135 $this->removePermissionFromUser($user, $permission);
136 $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
137 $this->assertPermissionError($resp);
138 $this->giveUserPermissions($user, [$permission]);
141 $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
142 $this->assertNotPermissionError($resp);
143 $resp->assertRedirect();