2020-04-04 00:16:05 +00:00
|
|
|
<?php namespace Tests\Entity;
|
2017-04-20 19:58:54 +00:00
|
|
|
|
2020-11-22 00:17:45 +00:00
|
|
|
use BookStack\Entities\Models\Page;
|
2019-04-20 12:25:16 +00:00
|
|
|
use BookStack\Entities\Repos\PageRepo;
|
2017-04-20 19:58:54 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class PageRevisionTest extends TestCase
|
|
|
|
{
|
2019-04-20 12:25:16 +00:00
|
|
|
public function test_page_revision_views_viewable()
|
|
|
|
{
|
|
|
|
$this->asEditor();
|
|
|
|
|
|
|
|
$pageRepo = app(PageRepo::class);
|
|
|
|
$page = Page::first();
|
2019-10-05 11:55:01 +00:00
|
|
|
$pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
|
2019-04-20 12:25:16 +00:00
|
|
|
$pageRevision = $page->revisions->last();
|
|
|
|
|
|
|
|
$revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
|
|
|
|
$revisionView->assertStatus(200);
|
|
|
|
$revisionView->assertSee('new content');
|
|
|
|
|
|
|
|
$revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id . '/changes');
|
|
|
|
$revisionView->assertStatus(200);
|
|
|
|
$revisionView->assertSee('new content');
|
|
|
|
}
|
|
|
|
|
2020-05-23 11:28:14 +00:00
|
|
|
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']);
|
|
|
|
$pageRevision = $page->revisions->last();
|
|
|
|
$pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>Updated content</p>', 'summary' => 'page revision testing 2']);
|
|
|
|
|
|
|
|
$revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
|
|
|
|
$revisionView->assertStatus(200);
|
|
|
|
$revisionView->assertSee('new revision content');
|
|
|
|
}
|
|
|
|
|
2019-04-20 12:25:16 +00:00
|
|
|
public function test_page_revision_restore_updates_content()
|
|
|
|
{
|
|
|
|
$this->asEditor();
|
|
|
|
|
|
|
|
$pageRepo = app(PageRepo::class);
|
|
|
|
$page = Page::first();
|
2019-10-05 11:55:01 +00:00
|
|
|
$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']);
|
2019-04-20 12:25:16 +00:00
|
|
|
$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');
|
|
|
|
}
|
2017-04-20 19:58:54 +00:00
|
|
|
|
2021-02-06 13:51:05 +00:00
|
|
|
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 = 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->assertDatabaseHas('pages', [
|
|
|
|
'id' => $page->id,
|
|
|
|
'markdown' => '## New Content Updated',
|
|
|
|
]);
|
|
|
|
$pageView->assertSee('abc123');
|
|
|
|
$pageView->assertSee('def456');
|
|
|
|
}
|
|
|
|
|
2021-01-02 16:42:05 +00:00
|
|
|
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->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",
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-04-20 19:58:54 +00:00
|
|
|
public function test_page_revision_count_increments_on_update()
|
|
|
|
{
|
|
|
|
$page = Page::first();
|
|
|
|
$startCount = $page->revision_count;
|
|
|
|
$resp = $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
|
|
|
|
$resp->assertStatus(302);
|
|
|
|
|
|
|
|
$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']);
|
2018-09-16 15:14:09 +00:00
|
|
|
|
2017-04-20 19:58:54 +00:00
|
|
|
$page = Page::find($page->id);
|
2018-09-16 15:14:09 +00:00
|
|
|
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
|
2017-04-20 19:58:54 +00:00
|
|
|
|
2018-09-16 15:14:09 +00:00
|
|
|
$page = Page::find($page->id);
|
2017-04-20 19:58:54 +00:00
|
|
|
$pageView = $this->get($page->getUrl());
|
|
|
|
$pageView->assertSee('Revision #' . $page->revision_count);
|
|
|
|
}
|
|
|
|
|
2018-09-15 15:35:51 +00:00
|
|
|
public function test_revision_deletion() {
|
|
|
|
$page = Page::first();
|
|
|
|
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
|
2018-09-16 15:14:09 +00:00
|
|
|
|
|
|
|
$page = Page::find($page->id);
|
2018-09-15 15:35:51 +00:00
|
|
|
$this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
|
2018-09-16 15:14:09 +00:00
|
|
|
|
2018-09-15 15:35:51 +00:00
|
|
|
$page = Page::find($page->id);
|
|
|
|
$beforeRevisionCount = $page->revisions->count();
|
|
|
|
|
|
|
|
// Delete the first revision
|
2018-09-16 15:14:09 +00:00
|
|
|
$revision = $page->revisions->get(1);
|
2018-09-15 15:35:51 +00:00
|
|
|
$resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
|
2019-08-25 16:21:25 +00:00
|
|
|
$resp->assertRedirect($page->getUrl('/revisions'));
|
2018-09-15 15:35:51 +00:00
|
|
|
|
|
|
|
$page = Page::find($page->id);
|
|
|
|
$afterRevisionCount = $page->revisions->count();
|
|
|
|
|
|
|
|
$this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
|
|
|
|
|
|
|
|
// Try to delete the latest revision
|
2018-09-15 19:42:36 +00:00
|
|
|
$beforeRevisionCount = $page->revisions->count();
|
|
|
|
$currentRevision = $page->getCurrentRevision();
|
2018-09-16 15:14:09 +00:00
|
|
|
$resp = $this->asEditor()->delete($currentRevision->getUrl('/delete/'));
|
2019-10-05 11:55:01 +00:00
|
|
|
$resp->assertRedirect($page->getUrl('/revisions'));
|
2018-09-15 19:42:36 +00:00
|
|
|
|
|
|
|
$page = Page::find($page->id);
|
|
|
|
$afterRevisionCount = $page->revisions->count();
|
|
|
|
$this->assertTrue($beforeRevisionCount === $afterRevisionCount);
|
2018-09-15 15:35:51 +00:00
|
|
|
}
|
2018-09-22 16:30:42 +00:00
|
|
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$revisionCount = $page->revisions()->count();
|
|
|
|
$this->assertEquals(2, $revisionCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
$revisionCount = $page->revisions()->count();
|
|
|
|
$this->assertEquals(12, $revisionCount);
|
|
|
|
}
|
2017-04-20 19:58:54 +00:00
|
|
|
}
|