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

@@ -66,90 +66,7 @@ class ChapterTest extends TestCase
$this->assertNotificationContains($redirectReq, 'Chapter Successfully Deleted');
}
public function test_show_view_has_copy_button()
{
$chapter = $this->entities->chapter();
$resp = $this->asEditor()->get($chapter->getUrl());
$this->withHtml($resp)->assertElementContains("a[href$=\"{$chapter->getUrl('/copy')}\"]", 'Copy');
}
public function test_copy_view()
{
$chapter = $this->entities->chapter();
$resp = $this->asEditor()->get($chapter->getUrl('/copy'));
$resp->assertOk();
$resp->assertSee('Copy Chapter');
$this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$chapter->name}\"]");
$this->withHtml($resp)->assertElementExists('input[name="entity_selection"]');
}
public function test_copy()
{
/** @var Chapter $chapter */
$chapter = Chapter::query()->whereHas('pages')->first();
/** @var Book $otherBook */
$otherBook = Book::query()->where('id', '!=', $chapter->book_id)->first();
$resp = $this->asEditor()->post($chapter->getUrl('/copy'), [
'name' => 'My copied chapter',
'entity_selection' => 'book:' . $otherBook->id,
]);
/** @var Chapter $newChapter */
$newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
$resp->assertRedirect($newChapter->getUrl());
$this->assertEquals($otherBook->id, $newChapter->book_id);
$this->assertEquals($chapter->pages->count(), $newChapter->pages->count());
}
public function test_copy_does_not_copy_non_visible_pages()
{
$chapter = $this->entities->chapterHasPages();
// Hide pages to all non-admin roles
/** @var Page $page */
foreach ($chapter->pages as $page) {
$this->permissions->setEntityPermissions($page, [], []);
}
$this->asEditor()->post($chapter->getUrl('/copy'), [
'name' => 'My copied chapter',
]);
/** @var Chapter $newChapter */
$newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
$this->assertEquals(0, $newChapter->pages()->count());
}
public function test_copy_does_not_copy_pages_if_user_cant_page_create()
{
$chapter = $this->entities->chapterHasPages();
$viewer = $this->users->viewer();
$this->permissions->grantUserRolePermissions($viewer, ['chapter-create-all']);
// Lacking permission results in no copied pages
$this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
'name' => 'My copied chapter',
]);
/** @var Chapter $newChapter */
$newChapter = Chapter::query()->where('name', '=', 'My copied chapter')->first();
$this->assertEquals(0, $newChapter->pages()->count());
$this->permissions->grantUserRolePermissions($viewer, ['page-create-all']);
// Having permission rules in copied pages
$this->actingAs($viewer)->post($chapter->getUrl('/copy'), [
'name' => 'My copied again chapter',
]);
/** @var Chapter $newChapter2 */
$newChapter2 = Chapter::query()->where('name', '=', 'My copied again chapter')->first();
$this->assertEquals($chapter->pages()->count(), $newChapter2->pages()->count());
}
public function test_sort_book_action_visible_if_permissions_allow()
{