1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-12-14 19:42:14 +03:00

Testing: Extracted copy tests to their own class

This commit is contained in:
Dan Brown
2025-11-25 12:36:33 +00:00
parent 0f40aeb0d3
commit f073994bc3
4 changed files with 294 additions and 278 deletions

View File

@@ -264,108 +264,4 @@ class BookTest extends TestCase
$resp = $this->asEditor()->get($book->getUrl());
$resp->assertSee("<p>My great<br>\ndescription<br>\n<br>\nwith newlines</p>", false);
}
public function test_show_view_has_copy_button()
{
$book = $this->entities->book();
$resp = $this->asEditor()->get($book->getUrl());
$this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
}
public function test_copy_view()
{
$book = $this->entities->book();
$resp = $this->asEditor()->get($book->getUrl('/copy'));
$resp->assertOk();
$resp->assertSee('Copy Book');
$this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
}
public function test_copy()
{
/** @var Book $book */
$book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
$resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
/** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first();
$resp->assertRedirect($copy->getUrl());
$this->assertEquals($book->getDirectVisibleChildren()->count(), $copy->getDirectVisibleChildren()->count());
$this->get($copy->getUrl())->assertSee($book->description_html, false);
}
public function test_copy_does_not_copy_non_visible_content()
{
/** @var Book $book */
$book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
// Hide child content
/** @var BookChild $page */
foreach ($book->getDirectVisibleChildren() as $child) {
$this->permissions->setEntityPermissions($child, [], []);
}
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
/** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first();
$this->assertEquals(0, $copy->getDirectVisibleChildren()->count());
}
public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
{
/** @var Book $book */
$book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
$viewer = $this->users->viewer();
$this->permissions->grantUserRolePermissions($viewer, ['book-create-all']);
$this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
/** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first();
$this->assertEquals(0, $copy->pages()->count());
$this->assertEquals(0, $copy->chapters()->count());
}
public function test_copy_clones_cover_image_if_existing()
{
$book = $this->entities->book();
$bookRepo = $this->app->make(BookRepo::class);
$coverImageFile = $this->files->uploadedImage('cover.png');
$bookRepo->updateCoverImage($book, $coverImageFile);
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book'])->assertRedirect();
/** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first();
$this->assertNotNull($copy->coverInfo()->getImage());
$this->assertNotEquals($book->coverInfo()->getImage()->id, $copy->coverInfo()->getImage()->id);
}
public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
{
/** @var Bookshelf $shelfA */
/** @var Bookshelf $shelfB */
[$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
$book = $this->entities->book();
$shelfA->appendBook($book);
$shelfB->appendBook($book);
$viewer = $this->users->viewer();
$this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
$this->permissions->setEntityPermissions($shelfB);
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
/** @var Book $copy */
$copy = Book::query()->where('name', '=', 'My copy book')->first();
$this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
$this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());
}
}