]> BookStack Code Mirror - bookstack/blob - tests/Entity/ConvertTest.php
Create additional test helper classes
[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         $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
46         $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
47     }
48
49     public function test_convert_chapter_to_book_requires_permissions()
50     {
51         $chapter = $this->entities->chapter();
52         $user = $this->users->viewer();
53
54         $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
55         $this->permissions->grantUserRolePermissions($user, $permissions);
56
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]);
62         }
63
64         $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
65         $this->assertNotPermissionError($resp);
66         $resp->assertRedirect();
67     }
68
69     public function test_book_edit_view_shows_convert_option()
70     {
71         $book = $this->entities->book();
72
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');
78     }
79
80     public function test_book_convert_to_shelf()
81     {
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();
93
94         // Run conversion
95         $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
96
97         /** @var Bookshelf $newShelf */
98         $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
99
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);
111
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]);
115
116         // Checks for nested page
117         $chapterChildPage->refresh();
118         $this->assertEquals(0, $chapterChildPage->chapter_id);
119         $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
120     }
121
122     public function test_book_convert_to_shelf_requires_permissions()
123     {
124         $book = $this->entities->book();
125         $user = $this->users->viewer();
126
127         $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
128         $this->permissions->grantUserRolePermissions($user, $permissions);
129
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]);
135         }
136
137         $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
138         $this->assertNotPermissionError($resp);
139         $resp->assertRedirect();
140     }
141 }