From 08b967607f14c48d7a1b9f5bf43b872b2494a3f0 Mon Sep 17 00:00:00 2001 From: Abijeet Date: Sun, 16 Sep 2018 20:44:09 +0530 Subject: [PATCH] Changes as per code review, and fixes failing test cases. Signed-off-by: Abijeet --- app/Http/Controllers/PageController.php | 6 +++--- app/Page.php | 5 +---- tests/Entity/PageRevisionTest.php | 14 ++++++++++---- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 505fb7e5a..e3079047c 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -459,7 +459,7 @@ class PageController extends Controller * Deletes a revision using the id of the specified revision. * @param string $bookSlug * @param string $pageSlug - * @param int $revisionId + * @param int $revId * @throws NotFoundException * @throws BadRequestException * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector @@ -467,7 +467,7 @@ class PageController extends Controller public function destroyRevision($bookSlug, $pageSlug, $revId) { $page = $this->entityRepo->getBySlug('page', $pageSlug, $bookSlug); - $this->checkOwnablePermission('page-update', $page); + $this->checkOwnablePermission('page-delete', $page); $revision = $page->revisions()->where('id', '=', $revId)->first(); if ($revision === null) { @@ -480,7 +480,7 @@ class PageController extends Controller // Check if its the latest revision, cannot delete latest revision. if (intval($currentRevision->id) === intval($revId)) { session()->flash('error', trans('entities.revision_cannot_delete_latest')); - return view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page]); + return response()->view('pages/revisions', ['page' => $page, 'book' => $page->book, 'current' => $page], 400); } $revision->delete(); diff --git a/app/Page.php b/app/Page.php index db6996c23..5c03e7d66 100644 --- a/app/Page.php +++ b/app/Page.php @@ -119,9 +119,6 @@ class Page extends Entity */ public function getCurrentRevision() { - if ($id = PageRevision::where('page_id', '=', $this->id)->max('id')) { - return PageRevision::find($id); - } - return null; + return $this->revisions()->first(); } } diff --git a/tests/Entity/PageRevisionTest.php b/tests/Entity/PageRevisionTest.php index cd7aa766e..08b379107 100644 --- a/tests/Entity/PageRevisionTest.php +++ b/tests/Entity/PageRevisionTest.php @@ -21,9 +21,11 @@ class PageRevisionTest extends TestCase { $page = Page::first(); $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']); - $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']); - $page = Page::find($page->id); + $page = Page::find($page->id); + $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']); + + $page = Page::find($page->id); $pageView = $this->get($page->getUrl()); $pageView->assertSee('Revision #' . $page->revision_count); } @@ -31,12 +33,15 @@ class PageRevisionTest extends TestCase public function test_revision_deletion() { $page = Page::first(); $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']); + + $page = Page::find($page->id); $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']); + $page = Page::find($page->id); $beforeRevisionCount = $page->revisions->count(); // Delete the first revision - $revision = $page->revisions->get(0); + $revision = $page->revisions->get(1); $resp = $this->asEditor()->delete($revision->getUrl('/delete/')); $resp->assertStatus(200); @@ -48,7 +53,8 @@ class PageRevisionTest extends TestCase // Try to delete the latest revision $beforeRevisionCount = $page->revisions->count(); $currentRevision = $page->getCurrentRevision(); - $this->asEditor()->delete($currentRevision->getUrl('/delete/')); + $resp = $this->asEditor()->delete($currentRevision->getUrl('/delete/')); + $resp->assertStatus(400); $page = Page::find($page->id); $afterRevisionCount = $page->revisions->count();