-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathPageRevisionTest.php
More file actions
340 lines (274 loc) · 14.1 KB
/
Copy pathPageRevisionTest.php
File metadata and controls
340 lines (274 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
namespace Tests\Entity;
use BookStack\Activity\ActivityType;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\PageRevision;
use BookStack\Permissions\Permission;
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();
$page = $this->entities->page();
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>new content</p>']);
$pageRevision = $page->revisions->last();
$resp = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
$resp->assertStatus(200);
$resp->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();
$page = $this->entities->page();
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>new revision content</p>']);
$pageRevision = $page->revisions->last();
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>Updated content</p>']);
$revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
$revisionView->assertStatus(200);
$revisionView->assertSee('new revision content');
}
public function test_page_revision_preview_filters_html_content()
{
$this->asEditor();
$page = $this->entities->page();
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<script>dontwantthishere</script><style>dontwantthishere</style><p>expectthisthough</p>']);
$pageRevision = $page->revisions->last();
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p>Updated content</p>']);
$revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
$revisionView->assertStatus(200);
$revisionView->assertSee('expectthisthough');
$revisionView->assertDontSee('dontwantthishere');
}
public function test_page_revision_restore_updates_content()
{
$this->asEditor();
$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());
$pageView->assertDontSee('abc123');
$pageView->assertDontSee('def456');
$revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
$restoreReq = $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
$page = Page::find($page->id);
$restoreReq->assertStatus(302);
$restoreReq->assertRedirect($page->getUrl());
$pageView = $this->get($page->getUrl());
$pageView->assertSee('abc123');
$pageView->assertSee('def456');
}
public function test_page_revision_restore_with_markdown_retains_markdown_content()
{
$this->asEditor();
$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());
$pageView->assertDontSee('abc123');
$pageView->assertDontSee('def456');
$revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
$restoreReq = $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
$page = Page::find($page->id);
$restoreReq->assertStatus(302);
$restoreReq->assertRedirect($page->getUrl());
$pageView = $this->get($page->getUrl());
$this->assertDatabaseHasEntityData('page', [
'id' => $page->id,
'markdown' => '## New Content def456',
]);
$pageView->assertSee('abc123');
$pageView->assertSee('def456');
}
public function test_page_revision_restore_sets_new_revision_with_summary()
{
$this->asEditor();
$page = $this->entities->page();
$this->createRevisions($page, 1, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'changelog' => 'My first update']);
$this->createRevisions($page, 1, ['html' => '<p>new content</p>']);
$page->refresh();
$revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
$this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
$page->refresh();
$this->assertDatabaseHas('page_revisions', [
'page_id' => $page->id,
'text' => 'new contente def456',
'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 = $this->entities->page();
$startCount = $page->revision_count;
$this->createRevisions($page, 1);
$this->assertTrue(Page::find($page->id)->revision_count === $startCount + 1);
}
public function test_revision_count_shown_in_page_meta()
{
$page = $this->entities->page();
$this->createRevisions($page, 2);
$pageView = $this->asViewer()->get($page->getUrl());
$pageView->assertSee('Revision #' . $page->revision_count);
}
public function test_revision_deletion()
{
$page = $this->entities->page();
$this->createRevisions($page, 2);
$beforeRevisionCount = $page->revisions->count();
// Delete the first revision
$revision = $page->revisions->get(1);
$resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
$resp->assertRedirect($page->getUrl('/revisions'));
$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();
$resp = $this->asEditor()->delete($page->currentRevision->getUrl('/delete/'));
$resp->assertRedirect($page->getUrl('/revisions'));
$page->refresh();
$afterRevisionCount = $page->revisions->count();
$this->assertTrue($beforeRevisionCount === $afterRevisionCount);
}
public function test_revision_limit_enforced()
{
config()->set('app.revision_limit', 2);
$page = $this->entities->page();
$this->createRevisions($page, 12);
$revisionCount = $page->revisions()->count();
$this->assertEquals(2, $revisionCount);
}
public function test_false_revision_limit_allows_many_revisions()
{
config()->set('app.revision_limit', false);
$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_changes_view_shows_diff()
{
$this->asEditor();
$page = $this->entities->page();
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p id="bkmrk-hello">Hello there dog</p>']);
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => '<p id="bkmrk-hello">Hello there cat</p>']);
$pageRevision = $page->revisions()->orderBy('id', 'desc')->first();
$revisionView = $this->get("{$page->getUrl()}/revisions/{$pageRevision->id}/changes");
$revisionView->assertStatus(200);
$revisionView->assertSee('<p id="bkmrk-hello">Hello there <del class="diffmod">dog</del><ins class="diffmod">cat</ins></p>', false);
}
public function test_revision_changes_view_filters_html_content()
{
$this->asEditor();
$page = $this->entities->page();
$html = '<script>dontwantthishere</script><style>dontwantthishere</style><p>expectthisthough</p>';
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => $html]);
$this->createRevisions($page, 1, ['name' => 'updated page', 'html' => $html]);
$pageRevision = $page->revisions()->orderBy('id', 'desc')->first();
$revisionView = $this->get("{$page->getUrl()}/revisions/{$pageRevision->id}/changes");
$revisionView->assertStatus(200);
$revisionView->assertSee('expectthisthough');
$revisionView->assertDontSee('dontwantthishere');
}
public function test_access_to_revision_operation_requires_revision_view_all_permission()
{
$editor = $this->users->editor();
$this->actingAs($editor);
$page = $this->entities->page();
$this->createRevisions($page, 3);
/** @var PageRevision $revision */
$revision = $page->revisions()->orderBy('id', 'desc')->first();
$this->get($page->getUrl())->assertSee($page->getUrl('/revisions'), false);
$this->get($page->getUrl('/revisions'))->assertOk();
$this->get($revision->getUrl())->assertOk();
$this->get($revision->getUrl('/changes'))->assertOk();
$this->put($revision->getUrl('/restore'))->assertRedirect($page->getUrl());
$this->delete($revision->getUrl('/delete'))->assertRedirect($page->getUrl('/revisions'));
$this->permissions->removeUserRolePermissions($editor, [Permission::RevisionViewAll]);
$this->get($page->getUrl())->assertDontSee($page->getUrl('/revisions'), false);
$this->assertPermissionError($this->get($page->getUrl('/revisions')));
$this->assertPermissionError($this->get($revision->getUrl()));
$this->assertPermissionError($this->get($revision->getUrl('/changes')));
$this->assertPermissionError($this->put($revision->getUrl('/restore')));
$this->assertPermissionError($this->delete($revision->getUrl('/delete')));
}
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, 'changelog' => '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);
}
}