Added test for markdown page revision restore

Also added md change detection in revision saving.
This commit is contained in:
Dan Brown 2021-02-06 13:51:05 +00:00
parent 61a911dd39
commit 37de4e2e0a
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
2 changed files with 35 additions and 6 deletions

View File

@ -177,17 +177,13 @@ class PageRepo
// Hold the old details to compare later
$oldHtml = $page->html;
$oldName = $page->name;
$oldMarkdown = $page->markdown;
$this->updateTemplateStatusAndContentFromInput($page, $input);
$this->baseRepo->update($page, $input);
// Update with new details
$page->revision_count++;
if (setting('app-editor') !== 'markdown') {
$page->markdown = '';
}
$page->save();
// Remove all update drafts for this user & page.
@ -195,7 +191,10 @@ class PageRepo
// Save a revision after updating
$summary = $input['summary'] ?? null;
if ($oldHtml !== $input['html'] || $oldName !== $input['name'] || $summary !== null) {
$htmlChanged = isset($input['html']) && $input['html'] !== $oldHtml;
$nameChanged = isset($input['name']) && $input['name'] !== $oldName;
$markdownChanged = isset($input['markdown']) && $input['markdown'] !== $oldMarkdown;
if ($htmlChanged || $nameChanged || $markdownChanged || $summary !== null) {
$this->savePageRevision($page, $summary);
}

View File

@ -66,6 +66,36 @@ class PageRevisionTest extends TestCase
$pageView->assertSee('def456');
}
public function test_page_revision_restore_with_markdown_retains_markdown_content()
{
$this->asEditor();
$pageRepo = app(PageRepo::class);
$page = Page::first();
$pageRepo->update($page, ['name' => 'updated page abc123', 'markdown' => '## New Content def456', 'summary' => 'initial page revision testing']);
$pageRepo->update($page, ['name' => 'updated page again', 'markdown' => '## New Content Updated', 'summary' => 'page revision testing']);
$page = Page::find($page->id);
$pageView = $this->get($page->getUrl());
$pageView->assertDontSee('abc123');
$pageView->assertDontSee('def456');
$revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
$restoreReq = $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
$page = Page::find($page->id);
$restoreReq->assertStatus(302);
$restoreReq->assertRedirect($page->getUrl());
$pageView = $this->get($page->getUrl());
$this->assertDatabaseHas('pages', [
'id' => $page->id,
'markdown' => '## New Content Updated',
]);
$pageView->assertSee('abc123');
$pageView->assertSee('def456');
}
public function test_page_revision_restore_sets_new_revision_with_summary()
{
$this->asEditor();