X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/f139cded789908efce3ac2ed1be26b947df647db..refs/pull/5676/head:/tests/Entity/PageDraftTest.php diff --git a/tests/Entity/PageDraftTest.php b/tests/Entity/PageDraftTest.php index 9e2ceff51..e99ba9b81 100644 --- a/tests/Entity/PageDraftTest.php +++ b/tests/Entity/PageDraftTest.php @@ -2,7 +2,6 @@ namespace Tests\Entity; -use BookStack\Entities\Models\Book; use BookStack\Entities\Models\Page; use BookStack\Entities\Models\PageRevision; use BookStack\Entities\Repos\PageRepo; @@ -10,20 +9,13 @@ use Tests\TestCase; class PageDraftTest extends TestCase { - /** - * @var Page - */ - protected $page; - - /** - * @var PageRepo - */ - protected $pageRepo; + protected Page $page; + protected PageRepo $pageRepo; protected function setUp(): void { parent::setUp(); - $this->page = Page::query()->first(); + $this->page = $this->entities->page(); $this->pageRepo = app()->make(PageRepo::class); } @@ -31,27 +23,27 @@ class PageDraftTest extends TestCase { $addedContent = '
test message content
'; - $this->asAdmin()->get($this->page->getUrl('/edit')) - ->assertElementNotContains('[name="html"]', $addedContent); + $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); + $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent); $newContent = $this->page->html . $addedContent; $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]); - $this->asAdmin()->get($this->page->getUrl('/edit')) - ->assertElementContains('[name="html"]', $newContent); + $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); + $this->withHtml($resp)->assertElementContains('[name="html"]', $newContent); } public function test_draft_not_visible_by_others() { $addedContent = 'test message content
'; - $this->asAdmin()->get($this->page->getUrl('/edit')) - ->assertElementNotContains('[name="html"]', $addedContent); + $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); + $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent); $newContent = $this->page->html . $addedContent; - $newUser = $this->getEditor(); + $newUser = $this->users->editor(); $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]); - $this->actingAs($newUser)->get($this->page->getUrl('/edit')) - ->assertElementNotContains('[name="html"]', $newContent); + $resp = $this->actingAs($newUser)->get($this->page->getUrl('/edit')); + $this->withHtml($resp)->assertElementNotContains('[name="html"]', $newContent); } public function test_alert_message_shows_if_editing_draft() @@ -66,27 +58,26 @@ class PageDraftTest extends TestCase { $nonEditedPage = Page::query()->take(10)->get()->last(); $addedContent = 'test message content
'; - $this->asAdmin()->get($this->page->getUrl('/edit')) - ->assertElementNotContains('[name="html"]', $addedContent); + $resp = $this->asAdmin()->get($this->page->getUrl('/edit')); + $this->withHtml($resp)->assertElementNotContains('[name="html"]', $addedContent); $newContent = $this->page->html . $addedContent; - $newUser = $this->getEditor(); + $newUser = $this->users->editor(); $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]); $this->actingAs($newUser) ->get($this->page->getUrl('/edit')) ->assertSee('Admin has started editing this page'); $this->flushSession(); - $this->get($nonEditedPage->getUrl() . '/edit') - ->assertElementNotContains('.notification', 'Admin has started editing this page'); + $resp = $this->get($nonEditedPage->getUrl() . '/edit'); + $this->withHtml($resp)->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(); + $admin = $this->users->admin(); + $editor = $this->users->editor(); + $page = $this->entities->page(); $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [ 'name' => $page->name, @@ -118,10 +109,9 @@ class PageDraftTest extends TestCase 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(); + $admin = $this->users->admin(); + $editor = $this->users->editor(); + $page = $this->entities->page(); $this->actingAs($admin)->put('/ajax/page/' . $page->id . '/save-draft', [ 'name' => $page->name, @@ -140,42 +130,115 @@ class PageDraftTest extends TestCase public function test_draft_pages_show_on_homepage() { - /** @var Book $book */ - $book = Book::query()->first(); - $this->asAdmin()->get('/') - ->assertElementNotContains('#recent-drafts', 'New Page'); + $book = $this->entities->book(); + $resp = $this->asAdmin()->get('/'); + $this->withHtml($resp)->assertElementNotContains('#recent-drafts', 'New Page'); $this->get($book->getUrl() . '/create-page'); - $this->get('/')->assertElementContains('#recent-drafts', 'New Page'); + $this->withHtml($this->get('/'))->assertElementContains('#recent-drafts', 'New Page'); } public function test_draft_pages_not_visible_by_others() { - /** @var Book $book */ - $book = Book::query()->first(); + $book = $this->entities->book(); $chapter = $book->chapters->first(); - $newUser = $this->getEditor(); + $newUser = $this->users->editor(); $this->actingAs($newUser)->get($book->getUrl('/create-page')); $this->get($chapter->getUrl('/create-page')); - $this->get($book->getUrl()) - ->assertElementContains('.book-contents', 'New Page'); + $resp = $this->get($book->getUrl()); + $this->withHtml($resp)->assertElementContains('.book-contents', 'New Page'); - $this->asAdmin()->get($book->getUrl()) - ->assertElementNotContains('.book-contents', 'New Page'); - $this->get($chapter->getUrl()) - ->assertElementNotContains('.book-contents', 'New Page'); + $resp = $this->asAdmin()->get($book->getUrl()); + $this->withHtml($resp)->assertElementNotContains('.book-contents', 'New Page'); + $resp = $this->get($chapter->getUrl()); + $this->withHtml($resp)->assertElementNotContains('.book-contents', 'New Page'); } public function test_page_html_in_ajax_fetch_response() { $this->asAdmin(); - /** @var Page $page */ - $page = Page::query()->first(); + $page = $this->entities->page(); $this->getJson('/ajax/page/' . $page->id)->assertJson([ 'html' => $page->html, ]); } + + public function test_user_draft_removed_on_user_drafts_delete_call() + { + $editor = $this->users->editor(); + $page = $this->entities->page(); + + $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [ + 'name' => $page->name, + 'html' => 'updated draft again
', + ]); + + $revisionData = [ + 'type' => 'update_draft', + 'created_by' => $editor->id, + 'page_id' => $page->id, + ]; + + $this->assertDatabaseHas('page_revisions', $revisionData); + + $resp = $this->delete("/page-revisions/user-drafts/{$page->id}"); + + $resp->assertOk(); + $this->assertDatabaseMissing('page_revisions', $revisionData); + } + + public function test_updating_page_draft_with_markdown_retains_markdown_content() + { + $book = $this->entities->book(); + $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); + } + + public function test_slug_generated_on_draft_publish_to_page_when_no_name_change() + { + $book = $this->entities->book(); + $this->asEditor()->get($book->getUrl('/create-page')); + /** @var Page $draft */ + $draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail(); + + $this->put('/ajax/page/' . $draft->id . '/save-draft', [ + 'name' => 'My page', + 'markdown' => 'Update test', + ])->assertOk(); + + $draft->refresh(); + $this->assertEmpty($draft->slug); + + $this->post($draft->getUrl(), [ + 'name' => 'My page', + 'markdown' => '# My markdown page', + ]); + + $this->assertDatabaseHas('pages', [ + 'id' => $draft->id, + 'draft' => false, + 'slug' => 'my-page', + ]); + } }