]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageRevisionTest.php
Merge pull request #2 from BookStackApp/master
[bookstack] / tests / Entity / PageRevisionTest.php
1 <?php namespace Tests\Entity;
2
3 use BookStack\Entities\Page;
4 use BookStack\Entities\Repos\PageRepo;
5 use Tests\TestCase;
6
7 class PageRevisionTest extends TestCase
8 {
9     public function test_page_revision_views_viewable()
10     {
11         $this->asEditor();
12
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();
17
18         $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id);
19         $revisionView->assertStatus(200);
20         $revisionView->assertSee('new content');
21
22         $revisionView = $this->get($page->getUrl() . '/revisions/' . $pageRevision->id . '/changes');
23         $revisionView->assertStatus(200);
24         $revisionView->assertSee('new content');
25     }
26
27     public function test_page_revision_restore_updates_content()
28     {
29         $this->asEditor();
30
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);
36
37
38         $pageView = $this->get($page->getUrl());
39         $pageView->assertDontSee('abc123');
40         $pageView->assertDontSee('def456');
41
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);
45
46         $restoreReq->assertStatus(302);
47         $restoreReq->assertRedirect($page->getUrl());
48
49         $pageView = $this->get($page->getUrl());
50         $pageView->assertSee('abc123');
51         $pageView->assertSee('def456');
52     }
53
54     public function test_page_revision_count_increments_on_update()
55     {
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);
60
61         $this->assertTrue(Page::find($page->id)->revision_count === $startCount+1);
62     }
63
64     public function test_revision_count_shown_in_page_meta()
65     {
66         $page = Page::first();
67         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
68
69         $page = Page::find($page->id);
70         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
71
72         $page = Page::find($page->id);
73         $pageView = $this->get($page->getUrl());
74         $pageView->assertSee('Revision #' . $page->revision_count);
75     }
76
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']);
80
81         $page = Page::find($page->id);
82         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
83
84         $page = Page::find($page->id);
85         $beforeRevisionCount = $page->revisions->count();
86
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'));
91
92         $page = Page::find($page->id);
93         $afterRevisionCount = $page->revisions->count();
94
95         $this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
96
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'));
102
103         $page = Page::find($page->id);
104         $afterRevisionCount = $page->revisions->count();
105         $this->assertTrue($beforeRevisionCount === $afterRevisionCount);
106     }
107
108     public function test_revision_limit_enforced()
109     {
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']);
117         }
118
119         $revisionCount = $page->revisions()->count();
120         $this->assertEquals(2, $revisionCount);
121     }
122
123     public function test_false_revision_limit_allows_many_revisions()
124     {
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']);
132         }
133
134         $revisionCount = $page->revisions()->count();
135         $this->assertEquals(12, $revisionCount);
136     }
137 }