3 namespace Tests\Entity;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Entities\Models\Page;
9 class PageRevisionTest extends TestCase
11 public function test_revision_links_visible_to_viewer()
13 /** @var Page $page */
14 $page = Page::query()->first();
16 $html = $this->withHtml($this->asViewer()->get($page->getUrl()));
17 $html->assertLinkExists($page->getUrl('/revisions'));
18 $html->assertElementContains('a', 'Revisions');
19 $html->assertElementContains('a', 'Revision #1');
22 public function test_page_revision_views_viewable()
25 $page = Page::first();
26 $this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>new content</p>']);
27 $pageRevision = $page->revisions->last();
29 $resp = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
30 $resp->assertStatus(200);
31 $resp->assertSee('new content');
33 $resp = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id . '/changes');
34 $resp->assertStatus(200);
35 $resp->assertSee('new content');
38 public function test_page_revision_preview_shows_content_of_revision()
41 $page = Page::first();
42 $this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>new revision content</p>']);
43 $pageRevision = $page->revisions->last();
44 $this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>Updated content</p>']);
46 $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
47 $revisionView->assertStatus(200);
48 $revisionView->assertSee('new revision content');
51 public function test_page_revision_restore_updates_content()
54 $page = Page::first();
55 $this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>']);
56 $this->createRevisions($page, 1, ['name' => 'updated page again', 'html' => '<p>new content</p>']);
57 $page = Page::find($page->id);
59 $pageView = $this->get($page->getUrl());
60 $pageView->assertDontSee('abc123');
61 $pageView->assertDontSee('def456');
63 $revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
64 $restoreReq = $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
65 $page = Page::find($page->id);
67 $restoreReq->assertStatus(302);
68 $restoreReq->assertRedirect($page->getUrl());
70 $pageView = $this->get($page->getUrl());
71 $pageView->assertSee('abc123');
72 $pageView->assertSee('def456');
75 public function test_page_revision_restore_with_markdown_retains_markdown_content()
78 $page = Page::first();
79 $this->createRevisions($page, 1, ['name' => 'updated page abc123', 'markdown' => '## New Content def456']);
80 $this->createRevisions($page, 1, ['name' => 'updated page again', 'markdown' => '## New Content Updated']);
81 $page = Page::find($page->id);
83 $pageView = $this->get($page->getUrl());
84 $pageView->assertDontSee('abc123');
85 $pageView->assertDontSee('def456');
87 $revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
88 $restoreReq = $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
89 $page = Page::find($page->id);
91 $restoreReq->assertStatus(302);
92 $restoreReq->assertRedirect($page->getUrl());
94 $pageView = $this->get($page->getUrl());
95 $this->assertDatabaseHas('pages', [
97 'markdown' => '## New Content def456',
99 $pageView->assertSee('abc123');
100 $pageView->assertSee('def456');
103 public function test_page_revision_restore_sets_new_revision_with_summary()
106 $page = Page::first();
107 $this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'summary' => 'My first update']);
108 $this->createRevisions($page, 1, ['html' => '<p>new content</p>']);
111 $revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
112 $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
115 $this->assertDatabaseHas('page_revisions', [
116 'page_id' => $page->id,
117 'text' => 'new contente def456',
119 'summary' => "Restored from #{$revToRestore->id}; My first update",
122 $detail = "Revision #{$revToRestore->revision_number} (ID: {$revToRestore->id}) for page ID {$revToRestore->page_id}";
123 $this->assertActivityExists(ActivityType::REVISION_RESTORE, null, $detail);
126 public function test_page_revision_count_increments_on_update()
128 $page = Page::first();
129 $startCount = $page->revision_count;
130 $this->createRevisions($page, 1);
132 $this->assertTrue(Page::find($page->id)->revision_count === $startCount + 1);
135 public function test_revision_count_shown_in_page_meta()
137 $page = Page::first();
138 $this->createRevisions($page, 2);
140 $pageView = $this->get($page->getUrl());
141 $pageView->assertSee('Revision #' . $page->revision_count);
144 public function test_revision_deletion()
146 /** @var Page $page */
147 $page = Page::query()->first();
148 $this->createRevisions($page, 2);
149 $beforeRevisionCount = $page->revisions->count();
151 // Delete the first revision
152 $revision = $page->revisions->get(1);
153 $resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
154 $resp->assertRedirect($page->getUrl('/revisions'));
157 $afterRevisionCount = $page->revisions->count();
159 $this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
161 $detail = "Revision #{$revision->revision_number} (ID: {$revision->id}) for page ID {$revision->page_id}";
162 $this->assertActivityExists(ActivityType::REVISION_DELETE, null, $detail);
164 // Try to delete the latest revision
165 $beforeRevisionCount = $page->revisions->count();
166 $resp = $this->asEditor()->delete($page->currentRevision->getUrl('/delete/'));
167 $resp->assertRedirect($page->getUrl('/revisions'));
170 $afterRevisionCount = $page->revisions->count();
171 $this->assertTrue($beforeRevisionCount === $afterRevisionCount);
174 public function test_revision_limit_enforced()
176 config()->set('app.revision_limit', 2);
177 $page = Page::first();
178 $this->createRevisions($page, 12);
180 $revisionCount = $page->revisions()->count();
181 $this->assertEquals(2, $revisionCount);
184 public function test_false_revision_limit_allows_many_revisions()
186 config()->set('app.revision_limit', false);
187 $page = Page::first();
188 $this->createRevisions($page, 12);
190 $revisionCount = $page->revisions()->count();
191 $this->assertEquals(12, $revisionCount);
194 public function test_revision_list_shows_editor_type()
196 /** @var Page $page */
197 $page = Page::first();
198 $this->createRevisions($page, 1, ['html' => 'new page html']);
200 $resp = $this->asAdmin()->get($page->refresh()->getUrl('/revisions'));
201 $this->withHtml($resp)->assertElementContains('td', '(WYSIWYG)');
202 $this->withHtml($resp)->assertElementNotContains('td', '(Markdown)');
204 $this->createRevisions($page, 1, ['markdown' => '# Some markdown content']);
205 $resp = $this->get($page->refresh()->getUrl('/revisions'));
206 $this->withHtml($resp)->assertElementContains('td', '(Markdown)');
209 public function test_revision_restore_action_only_visible_with_permission()
211 /** @var Page $page */
212 $page = Page::query()->first();
213 $this->createRevisions($page, 2);
215 $viewer = $this->getViewer();
216 $this->actingAs($viewer);
217 $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
218 $respHtml->assertElementNotContains('.actions a', 'Restore');
219 $respHtml->assertElementNotExists('form[action$="/restore"]');
221 $this->giveUserPermissions($viewer, ['page-update-all']);
223 $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
224 $respHtml->assertElementContains('.actions a', 'Restore');
225 $respHtml->assertElementExists('form[action$="/restore"]');
228 public function test_revision_delete_action_only_visible_with_permission()
230 /** @var Page $page */
231 $page = Page::query()->first();
232 $this->createRevisions($page, 2);
234 $viewer = $this->getViewer();
235 $this->actingAs($viewer);
236 $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
237 $respHtml->assertElementNotContains('.actions a', 'Delete');
238 $respHtml->assertElementNotExists('form[action$="/delete"]');
240 $this->giveUserPermissions($viewer, ['page-delete-all']);
242 $respHtml = $this->withHtml($this->get($page->getUrl('/revisions')));
243 $respHtml->assertElementContains('.actions a', 'Delete');
244 $respHtml->assertElementExists('form[action$="/delete"]');
247 protected function createRevisions(Page $page, int $times, array $attrs = [])
251 for ($i = 0; $i < $times; $i++) {
252 $data = ['name' => 'Page update' . $i, 'summary' => 'Update entry' . $i];
253 if (!isset($attrs['markdown'])) {
254 $data['html'] = '<p>My update page</p>';
256 $this->asAdmin()->put($page->getUrl(), array_merge($data, $attrs));
260 $this->actingAs($user);