]> BookStack Code Mirror - bookstack/blob - tests/Entity/ConvertTest.php
Deps & Tests: Updated PHP deps, fixed test namespaces
[bookstack] / tests / Entity / ConvertTest.php
1 <?php
2
3 namespace Tests\Entity;
4
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;
11 use Tests\TestCase;
12
13 class ConvertTest extends TestCase
14 {
15     public function test_chapter_edit_view_shows_convert_option()
16     {
17         $chapter = $this->entities->chapter();
18
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');
23     }
24
25     public function test_convert_chapter_to_book()
26     {
27         $chapter = $this->entities->chapterHasPages();
28         $chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
29         /** @var Page $childPage */
30         $childPage = $chapter->pages()->first();
31
32         $resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
33         $resp->assertRedirectContains('/books/');
34
35         /** @var Book $newBook */
36         $newBook = Book::query()->orderBy('id', 'desc')->first();
37
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);
46
47         $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
48     }
49
50     public function test_convert_chapter_to_book_requires_permissions()
51     {
52         $chapter = $this->entities->chapter();
53         $user = $this->users->viewer();
54
55         $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
56         $this->permissions->grantUserRolePermissions($user, $permissions);
57
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]);
63         }
64
65         $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
66         $this->assertNotPermissionError($resp);
67         $resp->assertRedirect();
68     }
69
70     public function test_book_edit_view_shows_convert_option()
71     {
72         $book = $this->entities->book();
73
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');
79     }
80
81     public function test_book_convert_to_shelf()
82     {
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();
94
95         // Run conversion
96         $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
97
98         /** @var Bookshelf $newShelf */
99         $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
100
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);
113
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]);
117
118         // Checks for nested page
119         $chapterChildPage->refresh();
120         $this->assertEquals(0, $chapterChildPage->chapter_id);
121         $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
122     }
123
124     public function test_book_convert_to_shelf_requires_permissions()
125     {
126         $book = $this->entities->book();
127         $user = $this->users->viewer();
128
129         $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
130         $this->permissions->grantUserRolePermissions($user, $permissions);
131
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]);
137         }
138
139         $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
140         $this->assertNotPermissionError($resp);
141         $resp->assertRedirect();
142     }
143 }