]> BookStack Code Mirror - bookstack/blobdiff - tests/Entity/PageRevisionTest.php
Revisions: Hid changes link for oldest revision
[bookstack] / tests / Entity / PageRevisionTest.php
index 2ed7d3b411e86b8990cc0b000740ec2a79a26bbd..9040254f78fdaaa3add672531032548edb8e7e1c 100644 (file)
@@ -2,39 +2,45 @@
 
 namespace Tests\Entity;
 
+use BookStack\Activity\ActivityType;
 use BookStack\Entities\Models\Page;
-use BookStack\Entities\Repos\PageRepo;
 use Tests\TestCase;
 
 class PageRevisionTest extends TestCase
 {
+    public function test_revision_links_visible_to_viewer()
+    {
+        $page = $this->entities->page();
+
+        $html = $this->withHtml($this->asViewer()->get($page->getUrl()));
+        $html->assertLinkExists($page->getUrl('/revisions'));
+        $html->assertElementContains('a', 'Revisions');
+        $html->assertElementContains('a', 'Revision #1');
+    }
+
     public function test_page_revision_views_viewable()
     {
         $this->asEditor();
-
-        $pageRepo = app(PageRepo::class);
-        $page = Page::first();
-        $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
+        $page = $this->entities->page();
+        $this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>new content</p>']);
         $pageRevision = $page->revisions->last();
 
-        $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
-        $revisionView->assertStatus(200);
-        $revisionView->assertSee('new content');
+        $resp = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
+        $resp->assertStatus(200);
+        $resp->assertSee('new content');
 
-        $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id . '/changes');
-        $revisionView->assertStatus(200);
-        $revisionView->assertSee('new content');
+        $resp = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id . '/changes');
+        $resp->assertStatus(200);
+        $resp->assertSee('new content');
     }
 
     public function test_page_revision_preview_shows_content_of_revision()
     {
         $this->asEditor();
-
-        $pageRepo = app(PageRepo::class);
-        $page = Page::first();
-        $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new revision content</p>', 'summary' => 'page revision testing']);
+        $page = $this->entities->page();
+        $this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>new revision content</p>']);
         $pageRevision = $page->revisions->last();
-        $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>Updated content</p>', 'summary' => 'page revision testing 2']);
+        $this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>Updated content</p>']);
 
         $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
         $revisionView->assertStatus(200);
@@ -44,11 +50,9 @@ class PageRevisionTest extends TestCase
     public function test_page_revision_restore_updates_content()
     {
         $this->asEditor();
-
-        $pageRepo = app(PageRepo::class);
-        $page = Page::first();
-        $pageRepo->update($page, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'summary' => 'initial page revision testing']);
-        $pageRepo->update($page, ['name' => 'updated page again', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
+        $page = $this->entities->page();
+        $this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>']);
+        $this->createRevisions($page, 1, ['name' => 'updated page again', 'html' => '<p>new content</p>']);
         $page = Page::find($page->id);
 
         $pageView = $this->get($page->getUrl());
@@ -70,11 +74,9 @@ class PageRevisionTest extends TestCase
     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 = $this->entities->page();
+        $this->createRevisions($page, 1, ['name' => 'updated page abc123', 'markdown' => '## New Content def456']);
+        $this->createRevisions($page, 1, ['name' => 'updated page again', 'markdown' => '## New Content Updated']);
         $page = Page::find($page->id);
 
         $pageView = $this->get($page->getUrl());
@@ -100,11 +102,9 @@ class PageRevisionTest extends TestCase
     public function test_page_revision_restore_sets_new_revision_with_summary()
     {
         $this->asEditor();
-
-        $pageRepo = app(PageRepo::class);
-        $page = Page::first();
-        $pageRepo->update($page, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'summary' => 'My first update']);
-        $pageRepo->update($page, ['name' => 'updated page again', 'html' => '<p>new content</p>', 'summary' => '']);
+        $page = $this->entities->page();
+        $this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'summary' => 'My first update']);
+        $this->createRevisions($page, 1, ['html' => '<p>new content</p>']);
         $page->refresh();
 
         $revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
@@ -117,40 +117,33 @@ class PageRevisionTest extends TestCase
             'type'    => 'version',
             'summary' => "Restored from #{$revToRestore->id}; My first update",
         ]);
+
+        $detail = "Revision #{$revToRestore->revision_number} (ID: {$revToRestore->id}) for page ID {$revToRestore->page_id}";
+        $this->assertActivityExists(ActivityType::REVISION_RESTORE, null, $detail);
     }
 
     public function test_page_revision_count_increments_on_update()
     {
-        $page = Page::first();
+        $page = $this->entities->page();
         $startCount = $page->revision_count;
-        $resp = $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        $resp->assertStatus(302);
+        $this->createRevisions($page, 1);
 
         $this->assertTrue(Page::find($page->id)->revision_count === $startCount + 1);
     }
 
     public function test_revision_count_shown_in_page_meta()
     {
-        $page = Page::first();
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-
-        $page = Page::find($page->id);
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
+        $page = $this->entities->page();
+        $this->createRevisions($page, 2);
 
-        $page = Page::find($page->id);
-        $pageView = $this->get($page->getUrl());
+        $pageView = $this->asViewer()->get($page->getUrl());
         $pageView->assertSee('Revision #' . $page->revision_count);
     }
 
     public function test_revision_deletion()
     {
-        $page = Page::first();
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-
-        $page = Page::find($page->id);
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-
-        $page = Page::find($page->id);
+        $page = $this->entities->page();
+        $this->createRevisions($page, 2);
         $beforeRevisionCount = $page->revisions->count();
 
         // Delete the first revision
@@ -158,18 +151,20 @@ class PageRevisionTest extends TestCase
         $resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
         $resp->assertRedirect($page->getUrl('/revisions'));
 
-        $page = Page::find($page->id);
+        $page->refresh();
         $afterRevisionCount = $page->revisions->count();
 
         $this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
 
+        $detail = "Revision #{$revision->revision_number} (ID: {$revision->id}) for page ID {$revision->page_id}";
+        $this->assertActivityExists(ActivityType::REVISION_DELETE, null, $detail);
+
         // Try to delete the latest revision
         $beforeRevisionCount = $page->revisions->count();
-        $currentRevision = $page->getCurrentRevision();
-        $resp = $this->asEditor()->delete($currentRevision->getUrl('/delete/'));
+        $resp = $this->asEditor()->delete($page->currentRevision->getUrl('/delete/'));
         $resp->assertRedirect($page->getUrl('/revisions'));
 
-        $page = Page::find($page->id);
+        $page->refresh();
         $afterRevisionCount = $page->revisions->count();
         $this->assertTrue($beforeRevisionCount === $afterRevisionCount);
     }
@@ -177,13 +172,8 @@ class PageRevisionTest extends TestCase
     public function test_revision_limit_enforced()
     {
         config()->set('app.revision_limit', 2);
-        $page = Page::first();
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        $page = Page::find($page->id);
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        for ($i = 0; $i < 10; $i++) {
-            $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        }
+        $page = $this->entities->page();
+        $this->createRevisions($page, 12);
 
         $revisionCount = $page->revisions()->count();
         $this->assertEquals(2, $revisionCount);
@@ -192,15 +182,88 @@ class PageRevisionTest extends TestCase
     public function test_false_revision_limit_allows_many_revisions()
     {
         config()->set('app.revision_limit', false);
-        $page = Page::first();
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        $page = Page::find($page->id);
-        $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        for ($i = 0; $i < 10; $i++) {
-            $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
-        }
+        $page = $this->entities->page();
+        $this->createRevisions($page, 12);
 
         $revisionCount = $page->revisions()->count();
         $this->assertEquals(12, $revisionCount);
     }
+
+    public function test_revision_list_shows_editor_type()
+    {
+        $page = $this->entities->page();
+        $this->createRevisions($page, 1, ['html' => 'new page html']);
+
+        $resp = $this->asAdmin()->get($page->refresh()->getUrl('/revisions'));
+        $this->withHtml($resp)->assertElementContains('.item-list-row > div:nth-child(2)', 'WYSIWYG)');
+        $this->withHtml($resp)->assertElementNotContains('.item-list-row > div:nth-child(2)', 'Markdown)');
+
+        $this->createRevisions($page, 1, ['markdown' => '# Some markdown content']);
+        $resp = $this->get($page->refresh()->getUrl('/revisions'));
+        $this->withHtml($resp)->assertElementContains('.item-list-row > div:nth-child(2)', 'Markdown)');
+    }
+
+    public function test_revision_changes_link_not_shown_for_oldest_revision()
+    {
+        $page = $this->entities->page();
+        $this->createRevisions($page, 3, ['html' => 'new page html']);
+
+        $resp = $this->asAdmin()->get($page->refresh()->getUrl('/revisions'));
+        $html = $this->withHtml($resp);
+
+        $html->assertElementNotExists('.item-list > .item-list-row:last-child a[href*="/changes"]');
+        $html->assertElementContains('.item-list > .item-list-row:nth-child(2)', 'Changes');
+    }
+
+    public function test_revision_restore_action_only_visible_with_permission()
+    {
+        $page = $this->entities->page();
+        $this->createRevisions($page, 2);
+
+        $viewer = $this->users->viewer();
+        $this->actingAs($viewer);
+        $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
+        $respHtml->assertElementNotContains('.actions a', 'Restore');
+        $respHtml->assertElementNotExists('form[action$="/restore"]');
+
+        $this->permissions->grantUserRolePermissions($viewer, ['page-update-all']);
+
+        $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
+        $respHtml->assertElementContains('.actions a', 'Restore');
+        $respHtml->assertElementExists('form[action$="/restore"]');
+    }
+
+    public function test_revision_delete_action_only_visible_with_permission()
+    {
+        $page = $this->entities->page();
+        $this->createRevisions($page, 2);
+
+        $viewer = $this->users->viewer();
+        $this->actingAs($viewer);
+        $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
+        $respHtml->assertElementNotContains('.actions a', 'Delete');
+        $respHtml->assertElementNotExists('form[action$="/delete"]');
+
+        $this->permissions->grantUserRolePermissions($viewer, ['page-delete-all']);
+
+        $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
+        $respHtml->assertElementContains('.actions a', 'Delete');
+        $respHtml->assertElementExists('form[action$="/delete"]');
+    }
+
+    protected function createRevisions(Page $page, int $times, array $attrs = [])
+    {
+        $user = user();
+
+        for ($i = 0; $i < $times; $i++) {
+            $data = ['name' => 'Page update' . $i, 'summary' => 'Update entry' . $i];
+            if (!isset($attrs['markdown'])) {
+                $data['html'] = '<p>My update page</p>';
+            }
+            $this->asAdmin()->put($page->getUrl(), array_merge($data, $attrs));
+            $page->refresh();
+        }
+
+        $this->actingAs($user);
+    }
 }