X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/756b55bbffedb6e5fde91f9fbc61f5a382f20705..refs/pull/3364/head:/tests/Entity/PageDraftTest.php diff --git a/tests/Entity/PageDraftTest.php b/tests/Entity/PageDraftTest.php index b2fa4bb31..cac1babea 100644 --- a/tests/Entity/PageDraftTest.php +++ b/tests/Entity/PageDraftTest.php @@ -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; @@ -19,7 +20,7 @@ class PageDraftTest extends TestCase */ protected $pageRepo; - public function setUp(): void + protected function setUp(): void { parent::setUp(); $this->page = Page::query()->first(); @@ -80,6 +81,63 @@ 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' => '
updated draft
', + ]); + + /** @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' => 'admin update
', + ]); + + $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [ + 'name' => $page->name, + 'html' => 'updated draft again
', + ]); + + $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' => 'updated draft
', + ]); + + $resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [ + 'name' => $page->name, + 'html' => 'updated draft again
', + ]); + + $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 */ @@ -120,4 +178,30 @@ class PageDraftTest extends TestCase 'html' => $page->html, ]); } + + public function test_updating_page_draft_with_markdown_retains_markdown_content() + { + /** @var Book $book */ + $book = Book::query()->first(); + $this->asEditor()->get($book->getUrl('/create-page')); + /** @var Page $draft */ + $draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail(); + + $resp = $this->put('/ajax/page/' . $draft->id . '/save-draft', [ + 'name' => 'My updated draft', + 'markdown' => "# My markdown page\n\n[A link](https://example.com)", + 'html' => 'checking markdown takes priority over this
', + ]); + $resp->assertOk(); + + $this->assertDatabaseHas('pages', [ + 'id' => $draft->id, + 'draft' => true, + 'name' => 'My updated draft', + 'markdown' => "# My markdown page\n\n[A link](https://example.com)", + ]); + + $draft->refresh(); + $this->assertStringContainsString('href="https://example.com"', $draft->html); + } }