mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-30 04:23:11 +03:00
Reviewed and refactored additional editor draft save warnings
- Added testing to cover warning cases. - Refactored logic to be simpler and move much of the business out of the controller. - Added new message that's more suitable to the case this was handling. - For detecting an outdated draft, checked the draft created_at time instead of updated_at to better fit the scenario being checked. - Updated some method types to align with those potentially being used in the logic of the code. - Added a cache of shown messages on the front-end to prevent them re-showing on every save during the session, even if dismissed.
This commit is contained in:
@ -4,6 +4,7 @@ namespace Tests\Entity;
|
||||
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Page;
|
||||
use BookStack\Entities\Models\PageRevision;
|
||||
use BookStack\Entities\Repos\PageRepo;
|
||||
use Tests\TestCase;
|
||||
|
||||
@ -80,6 +81,65 @@ class PageDraftTest extends TestCase
|
||||
->assertElementNotContains('.notification', 'Admin has started editing this page');
|
||||
}
|
||||
|
||||
public function test_draft_save_shows_alert_if_draft_older_than_last_page_update()
|
||||
{
|
||||
$admin = $this->getAdmin();
|
||||
$editor = $this->getEditor();
|
||||
/** @var Page $page */
|
||||
$page = Page::query()->first();
|
||||
|
||||
$this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
|
||||
'name' => $page->name,
|
||||
'html' => '<p>updated draft</p>',
|
||||
]);
|
||||
|
||||
/** @var PageRevision $draft */
|
||||
$draft = $page->allRevisions()
|
||||
->where('type', '=', 'update_draft')
|
||||
->where('created_by', '=', $editor->id)
|
||||
->first();
|
||||
$draft->created_at = now()->subMinute(1);
|
||||
$draft->save();
|
||||
|
||||
$this->actingAs($admin)->put($page->refresh()->getUrl(), [
|
||||
'name' => $page->name,
|
||||
'html' => '<p>admin update</p>',
|
||||
]);
|
||||
|
||||
$resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
|
||||
'name' => $page->name,
|
||||
'html' => '<p>updated draft again</p>',
|
||||
]);
|
||||
|
||||
$resp->assertJson([
|
||||
'warning' => 'This page has been updated since this draft was created. It is recommended that you discard this draft or take care not to overwrite any page changes.',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_draft_save_shows_alert_if_draft_edit_started_by_someone_else()
|
||||
{
|
||||
$admin = $this->getAdmin();
|
||||
$editor = $this->getEditor();
|
||||
/** @var Page $page */
|
||||
$page = Page::query()->first();
|
||||
|
||||
$this->actingAs($admin)->put('/ajax/page/' . $page->id . '/save-draft', [
|
||||
'name' => $page->name,
|
||||
'html' => '<p>updated draft</p>',
|
||||
]);
|
||||
|
||||
$resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
|
||||
'name' => $page->name,
|
||||
'html' => '<p>updated draft again</p>',
|
||||
]);
|
||||
|
||||
$resp->assertJson([
|
||||
'warning' => 'Admin has started editing this page in the last 60 minutes. Take care not to overwrite each other\'s updates!',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function test_draft_pages_show_on_homepage()
|
||||
{
|
||||
/** @var Book $book */
|
||||
|
Reference in New Issue
Block a user