]> BookStack Code Mirror - bookstack/blob - tests/Entity/ConvertTest.php
Updated tests to use ssddanbrown/asserthtml package
[bookstack] / tests / Entity / ConvertTest.php
1 <?php
2
3 namespace Tests\Entity;
4
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;
11 use Tests\TestCase;
12
13 class ConvertTest extends TestCase
14 {
15     public function test_chapter_edit_view_shows_convert_option()
16     {
17         /** @var Chapter $chapter */
18         $chapter = Chapter::query()->first();
19
20         $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
21         $resp->assertSee('Convert to Book');
22         $resp->assertSee('Convert Chapter');
23         $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-book"] button');
24     }
25
26     public function test_convert_chapter_to_book()
27     {
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();
33
34         $resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
35         $resp->assertRedirectContains('/books/');
36
37         /** @var Book $newBook */
38         $newBook = Book::query()->orderBy('id', 'desc')->first();
39
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);
47
48         $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
49     }
50
51     public function test_convert_chapter_to_book_requires_permissions()
52     {
53         /** @var Chapter $chapter */
54         $chapter = Chapter::query()->first();
55         $user = $this->getViewer();
56
57         $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
58         $this->giveUserPermissions($user, $permissions);
59
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]);
65         }
66
67         $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
68         $this->assertNotPermissionError($resp);
69         $resp->assertRedirect();
70     }
71
72     public function test_book_edit_view_shows_convert_option()
73     {
74         $book = Book::query()->first();
75
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         $this->withHtml($resp)->assertElementExists('form[action$="/convert-to-shelf"] button');
81     }
82
83     public function test_book_convert_to_shelf()
84     {
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();
96
97         // Run conversion
98         $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
99
100         /** @var Bookshelf $newShelf */
101         $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
102
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);
114
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]);
118
119         // Checks for nested page
120         $chapterChildPage->refresh();
121         $this->assertEquals(0, $chapterChildPage->chapter_id);
122         $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
123     }
124
125     public function test_book_convert_to_shelf_requires_permissions()
126     {
127         /** @var Book $book */
128         $book = Book::query()->first();
129         $user = $this->getViewer();
130
131         $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
132         $this->giveUserPermissions($user, $permissions);
133
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]);
139         }
140
141         $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
142         $this->assertNotPermissionError($resp);
143         $resp->assertRedirect();
144     }
145 }