]> BookStack Code Mirror - bookstack/blob - tests/Entity/ConvertTest.php
Added tests to cover convert functionality
[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
16     public function test_chapter_edit_view_shows_convert_option()
17     {
18         /** @var Chapter $chapter */
19         $chapter = Chapter::query()->first();
20
21         $resp = $this->asEditor()->get($chapter->getUrl('/edit'));
22         $resp->assertSee('Convert to Book');
23         $resp->assertSee('Convert Chapter');
24         $resp->assertElementExists('form[action$="/convert-to-book"] button');
25     }
26
27     public function test_convert_chapter_to_book()
28     {
29         /** @var Chapter $chapter */
30         $chapter = Chapter::query()->whereHas('pages')->first();
31         $chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
32         /** @var Page $childPage */
33         $childPage = $chapter->pages()->first();
34
35         $resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
36         $resp->assertRedirectContains('/books/');
37
38         /** @var Book $newBook */
39         $newBook = Book::query()->orderBy('id', 'desc')->first();
40
41         $this->assertDatabaseMissing('chapters', ['id' => $chapter->id]);
42         $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $newBook->id, 'chapter_id' => 0]);
43         $this->assertCount(1, $newBook->tags);
44         $this->assertEquals('Category', $newBook->tags->first()->name);
45         $this->assertEquals('Penguins', $newBook->tags->first()->value);
46         $this->assertEquals($chapter->name, $newBook->name);
47         $this->assertEquals($chapter->description, $newBook->description);
48
49         $this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
50     }
51
52     public function test_book_edit_view_shows_convert_option()
53     {
54         $book = Book::query()->first();
55
56         $resp = $this->asEditor()->get($book->getUrl('/edit'));
57         $resp->assertSee('Convert to Shelf');
58         $resp->assertSee('Convert Book');
59         $resp->assertSee('Note that permissions on shelves do not auto-cascade to content');
60         $resp->assertElementExists('form[action$="/convert-to-shelf"] button');
61     }
62
63     public function test_book_convert_to_shelf()
64     {
65         /** @var Book $book */
66         $book = Book::query()->whereHas('directPages')->whereHas('chapters')->firstOrFail();
67         $book->tags()->save(new Tag(['name' => 'Category', 'value' => 'Ducks']));
68         /** @var Page $childPage */
69         $childPage = $book->directPages()->first();
70         /** @var Chapter $childChapter */
71         $childChapter = $book->chapters()->whereHas('pages')->firstOrFail();
72         /** @var Page $chapterChildPage */
73         $chapterChildPage = $childChapter->pages()->firstOrFail();
74         $bookChapterCount = $book->chapters()->count();
75         $systemBookCount = Book::query()->count();
76
77         // Run conversion
78         $resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
79
80         /** @var Bookshelf $newShelf */
81         $newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
82
83         // Checks for new shelf
84         $resp->assertRedirectContains('/shelves/');
85         $this->assertDatabaseMissing('chapters', ['id' => $childChapter->id]);
86         $this->assertCount(1, $newShelf->tags);
87         $this->assertEquals('Category', $newShelf->tags->first()->name);
88         $this->assertEquals('Ducks', $newShelf->tags->first()->value);
89         $this->assertEquals($book->name, $newShelf->name);
90         $this->assertEquals($book->description, $newShelf->description);
91         $this->assertEquals($newShelf->books()->count(), $bookChapterCount + 1);
92         $this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count());
93         $this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf);
94
95         // Checks for old book to contain child pages
96         $this->assertDatabaseHas('books', ['id' => $book->id, 'name' => $book->name . ' Pages']);
97         $this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $book->id, 'chapter_id' => 0]);
98
99         // Checks for nested page
100         $chapterChildPage->refresh();
101         $this->assertEquals(0, $chapterChildPage->chapter_id);
102         $this->assertEquals($childChapter->name, $chapterChildPage->book->name);
103     }
104
105 }