]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/PageDraftTest.php
Fixes padding issues of the sidebar's items
[bookstack] / tests / Entity / PageDraftTest.php
index 233f300ee339a7fed34fb6eeb64c587c2006ee65..4fb7d7ab6e5226d01910ef57ac4cda633057a00d 100644 (file)
 <?php
 
+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;
 
 class PageDraftTest extends TestCase
 {
+    /**
+     * @var Page
+     */
     protected $page;
-    protected $entityRepo;
 
-    public function setUp()
+    /**
+     * @var PageRepo
+     */
+    protected $pageRepo;
+
+    public function setUp(): void
     {
         parent::setUp();
-        $this->page = \BookStack\Page::first();
-        $this->entityRepo = app('\BookStack\Repos\EntityRepo');
+        $this->page = Page::query()->first();
+        $this->pageRepo = app()->make(PageRepo::class);
     }
 
     public function test_draft_content_shows_if_available()
     {
         $addedContent = '<p>test message content</p>';
-        $this->asAdmin()->visit($this->page->getUrl() . '/edit')
-            ->dontSeeInField('html', $addedContent);
+
+        $this->asAdmin()->get($this->page->getUrl('/edit'))
+            ->assertElementNotContains('[name="html"]', $addedContent);
 
         $newContent = $this->page->html . $addedContent;
-        $this->entityRepo->updatePageDraft($this->page, ['html' => $newContent]);
-        $this->asAdmin()->visit($this->page->getUrl() . '/edit')
-            ->seeInField('html', $newContent);
+        $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
+        $this->asAdmin()->get($this->page->getUrl('/edit'))
+            ->assertElementContains('[name="html"]', $newContent);
     }
 
     public function test_draft_not_visible_by_others()
     {
         $addedContent = '<p>test message content</p>';
-        $this->asAdmin()->visit($this->page->getUrl() . '/edit')
-            ->dontSeeInField('html', $addedContent);
+        $this->asAdmin()->get($this->page->getUrl('/edit'))
+            ->assertElementNotContains('[name="html"]', $addedContent);
 
         $newContent = $this->page->html . $addedContent;
         $newUser = $this->getEditor();
-        $this->entityRepo->updatePageDraft($this->page, ['html' => $newContent]);
-        $this->actingAs($newUser)->visit($this->page->getUrl() . '/edit')
-            ->dontSeeInField('html', $newContent);
+        $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
+
+        $this->actingAs($newUser)->get($this->page->getUrl('/edit'))
+            ->assertElementNotContains('[name="html"]', $newContent);
     }
 
     public function test_alert_message_shows_if_editing_draft()
     {
         $this->asAdmin();
-        $this->entityRepo->updatePageDraft($this->page, ['html' => 'test content']);
-        $this->asAdmin()->visit($this->page->getUrl() . '/edit')
-            ->see('You are currently editing a draft');
+        $this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']);
+        $this->asAdmin()->get($this->page->getUrl('/edit'))
+            ->assertSee('You are currently editing a draft');
     }
 
     public function test_alert_message_shows_if_someone_else_editing()
     {
-        $nonEditedPage = \BookStack\Page::take(10)->get()->last();
+        $nonEditedPage = Page::query()->take(10)->get()->last();
         $addedContent = '<p>test message content</p>';
-        $this->asAdmin()->visit($this->page->getUrl() . '/edit')
-            ->dontSeeInField('html', $addedContent);
+        $this->asAdmin()->get($this->page->getUrl('/edit'))
+            ->assertElementNotContains('[name="html"]', $addedContent);
 
         $newContent = $this->page->html . $addedContent;
         $newUser = $this->getEditor();
-        $this->entityRepo->updatePageDraft($this->page, ['html' => $newContent]);
+        $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
 
         $this->actingAs($newUser)
-            ->visit($this->page->getUrl() . '/edit')
-            ->see('Admin has started editing this page');
-            $this->flushSession();
-        $this->visit($nonEditedPage->getUrl() . '/edit')
-            ->dontSeeInElement('.notification', 'Admin has started editing this page');
+            ->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');
+    }
+
+    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()
     {
-        $book = \BookStack\Book::first();
-        $this->asAdmin()->visit('/')
-            ->dontSeeInElement('#recent-drafts', 'New Page')
-            ->visit($book->getUrl() . '/page/create')
-            ->visit('/')
-            ->seeInElement('#recent-drafts', 'New Page');
+        /** @var Book $book */
+        $book = Book::query()->first();
+        $this->asAdmin()->get('/')
+            ->assertElementNotContains('#recent-drafts', 'New Page');
+
+        $this->get($book->getUrl() . '/create-page');
+
+        $this->get('/')->assertElementContains('#recent-drafts', 'New Page');
     }
 
     public function test_draft_pages_not_visible_by_others()
     {
-        $book = \BookStack\Book::first();
+        /** @var Book $book */
+        $book = Book::query()->first();
         $chapter = $book->chapters->first();
         $newUser = $this->getEditor();
 
-        $this->actingAs($newUser)->visit('/')
-            ->visit($book->getUrl() . '/page/create')
-            ->visit($chapter->getUrl() . '/create-page')
-            ->visit($book->getUrl())
-            ->seeInElement('.page-list', 'New Page');
-        
-        $this->asAdmin()
-            ->visit($book->getUrl())
-            ->dontSeeInElement('.page-list', 'New Page')
-            ->visit($chapter->getUrl())
-            ->dontSeeInElement('.page-list', 'New Page');
+        $this->actingAs($newUser)->get($book->getUrl('/create-page'));
+        $this->get($chapter->getUrl('/create-page'));
+        $this->get($book->getUrl())
+            ->assertElementContains('.book-contents', 'New Page');
+
+        $this->asAdmin()->get($book->getUrl())
+            ->assertElementNotContains('.book-contents', 'New Page');
+        $this->get($chapter->getUrl())
+            ->assertElementNotContains('.book-contents', 'New Page');
     }
 
+    public function test_page_html_in_ajax_fetch_response()
+    {
+        $this->asAdmin();
+        /** @var Page $page */
+        $page = Page::query()->first();
+
+        $this->getJson('/ajax/page/' . $page->id)->assertJson([
+            'html' => $page->html,
+        ]);
+    }
 }