Added tests to cover convert functionality

Also updated cloner class with typed properties.
This commit is contained in:
Dan Brown 2022-06-19 16:57:33 +01:00
parent f145ffc930
commit 663f81a2b1
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 109 additions and 19 deletions

View File

@ -16,25 +16,10 @@ use Illuminate\Http\UploadedFile;
class Cloner class Cloner
{ {
/** protected PageRepo $pageRepo;
* @var PageRepo protected ChapterRepo $chapterRepo;
*/ protected BookRepo $bookRepo;
protected $pageRepo; protected ImageService $imageService;
/**
* @var ChapterRepo
*/
protected $chapterRepo;
/**
* @var BookRepo
*/
protected $bookRepo;
/**
* @var ImageService
*/
protected $imageService;
public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService) public function __construct(PageRepo $pageRepo, ChapterRepo $chapterRepo, BookRepo $bookRepo, ImageService $imageService)
{ {

View File

@ -0,0 +1,105 @@
<?php
namespace Tests\Entity;
use BookStack\Actions\ActivityType;
use BookStack\Actions\Tag;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Page;
use Tests\TestCase;
class ConvertTest extends TestCase
{
public function test_chapter_edit_view_shows_convert_option()
{
/** @var Chapter $chapter */
$chapter = Chapter::query()->first();
$resp = $this->asEditor()->get($chapter->getUrl('/edit'));
$resp->assertSee('Convert to Book');
$resp->assertSee('Convert Chapter');
$resp->assertElementExists('form[action$="/convert-to-book"] button');
}
public function test_convert_chapter_to_book()
{
/** @var Chapter $chapter */
$chapter = Chapter::query()->whereHas('pages')->first();
$chapter->tags()->save(new Tag(['name' => 'Category', 'value' => 'Penguins']));
/** @var Page $childPage */
$childPage = $chapter->pages()->first();
$resp = $this->asEditor()->post($chapter->getUrl('/convert-to-book'));
$resp->assertRedirectContains('/books/');
/** @var Book $newBook */
$newBook = Book::query()->orderBy('id', 'desc')->first();
$this->assertDatabaseMissing('chapters', ['id' => $chapter->id]);
$this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $newBook->id, 'chapter_id' => 0]);
$this->assertCount(1, $newBook->tags);
$this->assertEquals('Category', $newBook->tags->first()->name);
$this->assertEquals('Penguins', $newBook->tags->first()->value);
$this->assertEquals($chapter->name, $newBook->name);
$this->assertEquals($chapter->description, $newBook->description);
$this->assertActivityExists(ActivityType::BOOK_CREATE_FROM_CHAPTER, $newBook);
}
public function test_book_edit_view_shows_convert_option()
{
$book = Book::query()->first();
$resp = $this->asEditor()->get($book->getUrl('/edit'));
$resp->assertSee('Convert to Shelf');
$resp->assertSee('Convert Book');
$resp->assertSee('Note that permissions on shelves do not auto-cascade to content');
$resp->assertElementExists('form[action$="/convert-to-shelf"] button');
}
public function test_book_convert_to_shelf()
{
/** @var Book $book */
$book = Book::query()->whereHas('directPages')->whereHas('chapters')->firstOrFail();
$book->tags()->save(new Tag(['name' => 'Category', 'value' => 'Ducks']));
/** @var Page $childPage */
$childPage = $book->directPages()->first();
/** @var Chapter $childChapter */
$childChapter = $book->chapters()->whereHas('pages')->firstOrFail();
/** @var Page $chapterChildPage */
$chapterChildPage = $childChapter->pages()->firstOrFail();
$bookChapterCount = $book->chapters()->count();
$systemBookCount = Book::query()->count();
// Run conversion
$resp = $this->asEditor()->post($book->getUrl('/convert-to-shelf'));
/** @var Bookshelf $newShelf */
$newShelf = Bookshelf::query()->orderBy('id', 'desc')->first();
// Checks for new shelf
$resp->assertRedirectContains('/shelves/');
$this->assertDatabaseMissing('chapters', ['id' => $childChapter->id]);
$this->assertCount(1, $newShelf->tags);
$this->assertEquals('Category', $newShelf->tags->first()->name);
$this->assertEquals('Ducks', $newShelf->tags->first()->value);
$this->assertEquals($book->name, $newShelf->name);
$this->assertEquals($book->description, $newShelf->description);
$this->assertEquals($newShelf->books()->count(), $bookChapterCount + 1);
$this->assertEquals($systemBookCount + $bookChapterCount, Book::query()->count());
$this->assertActivityExists(ActivityType::BOOKSHELF_CREATE_FROM_BOOK, $newShelf);
// Checks for old book to contain child pages
$this->assertDatabaseHas('books', ['id' => $book->id, 'name' => $book->name . ' Pages']);
$this->assertDatabaseHas('pages', ['id' => $childPage->id, 'book_id' => $book->id, 'chapter_id' => 0]);
// Checks for nested page
$chapterChildPage->refresh();
$this->assertEquals(0, $chapterChildPage->chapter_id);
$this->assertEquals($childChapter->name, $chapterChildPage->book->name);
}
}