1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Page;
4 use BookStack\Entities\Repos\PageRepo;
7 class PageRevisionTest extends TestCase
9 public function test_page_revision_views_viewable()
13 $pageRepo = app(PageRepo::class);
14 $page = Page::first();
15 $pageRepo->update($page, ['name' => 'updated page', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
16 $pageRevision = $page->revisions->last();
18 $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
19 $revisionView->assertStatus(200);
20 $revisionView->assertSee('new content');
22 $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id . '/changes');
23 $revisionView->assertStatus(200);
24 $revisionView->assertSee('new content');
27 public function test_page_revision_restore_updates_content()
31 $pageRepo = app(PageRepo::class);
32 $page = Page::first();
33 $pageRepo->update($page, ['name' => 'updated page abc123', 'html' => '<p>new contente def456</p>', 'summary' => 'initial page revision testing']);
34 $pageRepo->update($page, ['name' => 'updated page again', 'html' => '<p>new content</p>', 'summary' => 'page revision testing']);
35 $page = Page::find($page->id);
38 $pageView = $this->get($page->getUrl());
39 $pageView->assertDontSee('abc123');
40 $pageView->assertDontSee('def456');
42 $revToRestore = $page->revisions()->where('name', 'like', '%abc123')->first();
43 $restoreReq = $this->put($page->getUrl() . '/revisions/' . $revToRestore->id . '/restore');
44 $page = Page::find($page->id);
46 $restoreReq->assertStatus(302);
47 $restoreReq->assertRedirect($page->getUrl());
49 $pageView = $this->get($page->getUrl());
50 $pageView->assertSee('abc123');
51 $pageView->assertSee('def456');
54 public function test_page_revision_count_increments_on_update()
56 $page = Page::first();
57 $startCount = $page->revision_count;
58 $resp = $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
59 $resp->assertStatus(302);
61 $this->assertTrue(Page::find($page->id)->revision_count === $startCount+1);
64 public function test_revision_count_shown_in_page_meta()
66 $page = Page::first();
67 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
69 $page = Page::find($page->id);
70 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
72 $page = Page::find($page->id);
73 $pageView = $this->get($page->getUrl());
74 $pageView->assertSee('Revision #' . $page->revision_count);
77 public function test_revision_deletion() {
78 $page = Page::first();
79 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
81 $page = Page::find($page->id);
82 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
84 $page = Page::find($page->id);
85 $beforeRevisionCount = $page->revisions->count();
87 // Delete the first revision
88 $revision = $page->revisions->get(1);
89 $resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
90 $resp->assertRedirect($page->getUrl('/revisions'));
92 $page = Page::find($page->id);
93 $afterRevisionCount = $page->revisions->count();
95 $this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
97 // Try to delete the latest revision
98 $beforeRevisionCount = $page->revisions->count();
99 $currentRevision = $page->getCurrentRevision();
100 $resp = $this->asEditor()->delete($currentRevision->getUrl('/delete/'));
101 $resp->assertRedirect($page->getUrl('/revisions'));
103 $page = Page::find($page->id);
104 $afterRevisionCount = $page->revisions->count();
105 $this->assertTrue($beforeRevisionCount === $afterRevisionCount);
108 public function test_revision_limit_enforced()
110 config()->set('app.revision_limit', 2);
111 $page = Page::first();
112 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
113 $page = Page::find($page->id);
114 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
115 for ($i = 0; $i < 10; $i++) {
116 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
119 $revisionCount = $page->revisions()->count();
120 $this->assertEquals(2, $revisionCount);
123 public function test_false_revision_limit_allows_many_revisions()
125 config()->set('app.revision_limit', false);
126 $page = Page::first();
127 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
128 $page = Page::find($page->id);
129 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
130 for ($i = 0; $i < 10; $i++) {
131 $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
134 $revisionCount = $page->revisions()->count();
135 $this->assertEquals(12, $revisionCount);