mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-28 17:02:04 +03:00
Added ability to copy/clone chapters
Builds upon page clone work. Takes permissions into account to decide if child pages should be copied.
This commit is contained in:
@ -4,6 +4,7 @@ namespace Tests\Entity;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ChapterTest extends TestCase
|
||||
@ -54,4 +55,95 @@ class ChapterTest extends TestCase
|
||||
$redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
|
||||
$redirectReq->assertNotificationContains('Chapter Successfully Deleted');
|
||||
}
|
||||
|
||||
public function test_show_view_has_copy_button()
|
||||
{
|
||||
/** @var Chapter $chapter */
|
||||
$chapter = Chapter::query()->first();
|
||||
|
||||
$resp = $this->asEditor()->get($chapter->getUrl());
|
||||
$resp->assertElementContains("a[href$=\"{$chapter->getUrl('/copy')}\"]", 'Copy');
|
||||
}
|
||||
|
||||
public function test_copy_view()
|
||||
{
|
||||
/** @var Chapter $chapter */
|
||||
$chapter = Chapter::query()->first();
|
||||
|
||||
$resp = $this->asEditor()->get($chapter->getUrl('/copy'));
|
||||
$resp->assertOk();
|
||||
$resp->assertSee('Copy Chapter');
|
||||
$resp->assertElementExists("input[name=\"name\"][value=\"{$chapter->name}\"]");
|
||||
$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()
|
||||
{
|
||||
/** @var Chapter $chapter */
|
||||
$chapter = Chapter::query()->whereHas('pages')->first();
|
||||
|
||||
// Hide pages to all non-admin roles
|
||||
/** @var Page $page */
|
||||
foreach ($chapter->pages as $page) {
|
||||
$page->restricted = true;
|
||||
$page->save();
|
||||
$this->regenEntityPermissions($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()
|
||||
{
|
||||
/** @var Chapter $chapter */
|
||||
$chapter = Chapter::query()->whereHas('pages')->first();
|
||||
$viewer = $this->getViewer();
|
||||
$this->giveUserPermissions($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->giveUserPermissions($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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user