]> BookStack Code Mirror - bookstack/blob - tests/Entity/ConvertTest.php
Migrated much test entity usage via find/replace
[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         /** @var Chapter $chapter */
28         $chapter = Chapter::query()->whereHas('pages')->first();
29         $chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
30         /** @var Page $childPage */
31         $childPage = $chapter->pages()->first();
32
33         $resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
34         $resp->assertRedirectContains('/books/');
35
36         /** @var Book $newBook */
37         $newBook = Book::query()->orderBy('id', 'desc')->first();
38
39         $this->assertDatabaseMissing('chapters', ['id' => $chapter->id]);
40         $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $newBook->id, 'chapter_id' => 0]);
41         $this->assertCount(1, $newBook->tags);
42         $this->assertEquals('Category', $newBook->tags->first()->name);
43         $this->assertEquals('Penguins', $newBook->tags->first()->value);
44         $this->assertEquals($chapter->name, $newBook->name);
45         $this->assertEquals($chapter->description, $newBook->description);
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->getViewer();
54
55         $permissions = ['chapter-delete-all', 'book-create-all', 'chapter-update-all'];
56         $this->giveUserPermissions($user, $permissions);
57
58         foreach ($permissions as $permission) {
59             $this->removePermissionFromUser($user, $permission);
60             $resp = $this->actingAs($user)->post($chapter->getUrl('/convert-to-book'));
61             $this->assertPermissionError($resp);
62             $this->giveUserPermissions($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($newShelf->books()->count(), $bookChapterCount + 1);
110         $this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count());
111         $this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf);
112
113         // Checks for old book to contain child pages
114         $this->assertDatabaseHas('books', ['id' => $book->id, 'name' => $book->name . ' Pages']);
115         $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $book->id, 'chapter_id' => 0]);
116
117         // Checks for nested page
118         $chapterChildPage->refresh();
119         $this->assertEquals(0, $chapterChildPage->chapter_id);
120         $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
121     }
122
123     public function test_book_convert_to_shelf_requires_permissions()
124     {
125         $book = $this->entities->book();
126         $user = $this->getViewer();
127
128         $permissions = ['book-delete-all', 'bookshelf-create-all', 'book-update-all', 'book-create-all'];
129         $this->giveUserPermissions($user, $permissions);
130
131         foreach ($permissions as $permission) {
132             $this->removePermissionFromUser($user, $permission);
133             $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
134             $this->assertPermissionError($resp);
135             $this->giveUserPermissions($user, [$permission]);
136         }
137
138         $resp = $this->actingAs($user)->post($book->getUrl('/convert-to-shelf'));
139         $this->assertNotPermissionError($resp);
140         $resp->assertRedirect();
141     }
142 }