mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-10-30 07:32:39 +01:00
Updated book copy to copy shelf relations
Where permission to edit the shelf is allowed. For #3699
This commit is contained in:
parent
8f3430d386
commit
60171b3522
@ -19,6 +19,7 @@ use Illuminate\Support\Collection;
|
||||
* @property \Illuminate\Database\Eloquent\Collection $chapters
|
||||
* @property \Illuminate\Database\Eloquent\Collection $pages
|
||||
* @property \Illuminate\Database\Eloquent\Collection $directPages
|
||||
* @property \Illuminate\Database\Eloquent\Collection $shelves
|
||||
*/
|
||||
class Book extends Entity implements HasCoverImage
|
||||
{
|
||||
|
@ -4,6 +4,7 @@ namespace BookStack\Entities\Tools;
|
||||
|
||||
use BookStack\Actions\Tag;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Bookshelf;
|
||||
use BookStack\Entities\Models\Chapter;
|
||||
use BookStack\Entities\Models\Entity;
|
||||
use BookStack\Entities\Models\Page;
|
||||
@ -71,8 +72,10 @@ class Cloner
|
||||
$bookDetails = $this->entityToInputData($original);
|
||||
$bookDetails['name'] = $newName;
|
||||
|
||||
// Clone book
|
||||
$copyBook = $this->bookRepo->create($bookDetails);
|
||||
|
||||
// Clone contents
|
||||
$directChildren = $original->getDirectChildren();
|
||||
foreach ($directChildren as $child) {
|
||||
if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
|
||||
@ -84,6 +87,14 @@ class Cloner
|
||||
}
|
||||
}
|
||||
|
||||
// Clone bookshelf relationships
|
||||
/** @var Bookshelf $shelf */
|
||||
foreach ($original->shelves as $shelf) {
|
||||
if (userCan('bookshelf-update', $shelf)) {
|
||||
$shelf->appendBook($copyBook);
|
||||
}
|
||||
}
|
||||
|
||||
return $copyBook;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ namespace Tests\Entity;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\BookChild;
|
||||
use BookStack\Entities\Models\Bookshelf;
|
||||
use BookStack\Entities\Repos\BookRepo;
|
||||
use Tests\TestCase;
|
||||
use Tests\Uploads\UsesImages;
|
||||
@ -344,11 +345,34 @@ class BookTest extends TestCase
|
||||
$bookRepo->updateCoverImage($book, $coverImageFile);
|
||||
|
||||
$this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
|
||||
|
||||
/** @var Book $copy */
|
||||
$copy = Book::query()->where('name', '=', 'My copy book')->first();
|
||||
|
||||
$this->assertNotNull($copy->cover);
|
||||
$this->assertNotEquals($book->cover->id, $copy->cover->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();
|
||||
/** @var Book $book */
|
||||
$book = Book::query()->first();
|
||||
|
||||
$shelfA->appendBook($book);
|
||||
$shelfB->appendBook($book);
|
||||
|
||||
$viewer = $this->getViewer();
|
||||
$this->giveUserPermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
|
||||
$this->setEntityRestrictions($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());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user