From 4daeb9daa6c980d97126293c972245d8e08711e8 Mon Sep 17 00:00:00 2001 From: jakob Date: Mon, 28 Oct 2019 15:33:28 +0100 Subject: [PATCH 1/3] Check if parent is a chapter. If so, move into Book and assing page to chapter. --- app/Entities/Repos/PageRepo.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index 0fc68f953..5d6fc8fa8 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -306,6 +306,13 @@ class PageRepo throw new PermissionsException('User does not have permission to create a page within the new parent'); } + if ($parent instanceof Chapter) { + $parentChapter = $parent; + $parent = $parent->book; + $page->chapter_id = $parentChapter->id; + $page->save(); + } + $page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id); $page->rebuildPermissions(); return $parent; From 7368ff3e6a00af5d9062d44172c6f45415277605 Mon Sep 17 00:00:00 2001 From: jakob Date: Mon, 28 Oct 2019 16:53:48 +0100 Subject: [PATCH 2/3] No need to save page --- app/Entities/Repos/PageRepo.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index 5d6fc8fa8..501b19c78 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -307,15 +307,13 @@ class PageRepo } if ($parent instanceof Chapter) { - $parentChapter = $parent; - $parent = $parent->book; - $page->chapter_id = $parentChapter->id; - $page->save(); + $page->chapter_id = $parent->id; } $page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id); $page->rebuildPermissions(); - return $parent; + + return ($parent instanceof Book ? $parent : $parent->book); } /** From d64c358c4f75533da5f66557c52e989a6d4ad1f0 Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 29 Oct 2019 22:33:09 +0000 Subject: [PATCH 3/3] Updated sort logic to handle chapter to book scenario - Extended tests out to cover --- app/Entities/Repos/PageRepo.php | 5 +---- tests/Entity/SortTest.php | 20 +++++++++++++++++++- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/Entities/Repos/PageRepo.php b/app/Entities/Repos/PageRepo.php index 501b19c78..e49eeb1ef 100644 --- a/app/Entities/Repos/PageRepo.php +++ b/app/Entities/Repos/PageRepo.php @@ -306,10 +306,7 @@ class PageRepo throw new PermissionsException('User does not have permission to create a page within the new parent'); } - if ($parent instanceof Chapter) { - $page->chapter_id = $parent->id; - } - + $page->chapter_id = ($parent instanceof Chapter) ? $parent->id : null; $page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id); $page->rebuildPermissions(); diff --git a/tests/Entity/SortTest.php b/tests/Entity/SortTest.php index d7c004dc8..7d67f05c6 100644 --- a/tests/Entity/SortTest.php +++ b/tests/Entity/SortTest.php @@ -66,10 +66,28 @@ class SortTest extends TestCase $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new chapter'); $newChapterResp = $this->get($newChapter->getUrl()); - $newChapterResp->assertSee('moved page'); $newChapterResp->assertSee($page->name); } + public function test_page_move_from_chapter_to_book() + { + $oldChapter = Chapter::first(); + $page = $oldChapter->pages()->first(); + $newBook = Book::where('id', '!=', $oldChapter->book_id)->first(); + + $movePageResp = $this->actingAs($this->getEditor())->put($page->getUrl('/move'), [ + 'entity_selection' => 'book:' . $newBook->id + ]); + $page = Page::find($page->id); + + $movePageResp->assertRedirect($page->getUrl()); + $this->assertTrue($page->book->id == $newBook->id, 'Page parent is now the new book'); + $this->assertTrue($page->chapter === null, 'Page has no parent chapter'); + + $newBookResp = $this->get($newBook->getUrl()); + $newBookResp->assertSee($page->name); + } + public function test_page_move_requires_create_permissions_on_parent() { $page = Page::first();