]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageRevisionTest.php
Re-structured the app code to be feature based rather than code type based
[bookstack] / tests / Entity / PageRevisionTest.php
1 <?php namespace Entity;
2
3
4 use BookStack\Entities\Page;
5 use Tests\TestCase;
6
7 class PageRevisionTest extends TestCase
8 {
9
10     public function test_page_revision_count_increments_on_update()
11     {
12         $page = Page::first();
13         $startCount = $page->revision_count;
14         $resp = $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
15         $resp->assertStatus(302);
16
17         $this->assertTrue(Page::find($page->id)->revision_count === $startCount+1);
18     }
19
20     public function test_revision_count_shown_in_page_meta()
21     {
22         $page = Page::first();
23         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
24
25         $page = Page::find($page->id);
26         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
27
28         $page = Page::find($page->id);
29         $pageView = $this->get($page->getUrl());
30         $pageView->assertSee('Revision #' . $page->revision_count);
31     }
32
33     public function test_revision_deletion() {
34         $page = Page::first();
35         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
36
37         $page = Page::find($page->id);
38         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
39
40         $page = Page::find($page->id);
41         $beforeRevisionCount = $page->revisions->count();
42
43         // Delete the first revision
44         $revision = $page->revisions->get(1);
45         $resp = $this->asEditor()->delete($revision->getUrl('/delete/'));
46         $resp->assertStatus(200);
47
48         $page = Page::find($page->id);
49         $afterRevisionCount = $page->revisions->count();
50
51         $this->assertTrue($beforeRevisionCount === ($afterRevisionCount + 1));
52
53         // Try to delete the latest revision
54         $beforeRevisionCount = $page->revisions->count();
55         $currentRevision = $page->getCurrentRevision();
56         $resp = $this->asEditor()->delete($currentRevision->getUrl('/delete/'));
57         $resp->assertStatus(400);
58
59         $page = Page::find($page->id);
60         $afterRevisionCount = $page->revisions->count();
61         $this->assertTrue($beforeRevisionCount === $afterRevisionCount);
62     }
63
64     public function test_revision_limit_enforced()
65     {
66         config()->set('app.revision_limit', 2);
67         $page = Page::first();
68         $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']);
71         for ($i = 0; $i < 10; $i++) {
72             $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
73         }
74
75         $revisionCount = $page->revisions()->count();
76         $this->assertEquals(2, $revisionCount);
77     }
78
79     public function test_false_revision_limit_allows_many_revisions()
80     {
81         config()->set('app.revision_limit', false);
82         $page = Page::first();
83         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
84         $page = Page::find($page->id);
85         $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
86         for ($i = 0; $i < 10; $i++) {
87             $this->asEditor()->put($page->getUrl(), ['name' => 'Updated page', 'html' => 'new page html', 'summary' => 'Update a']);
88         }
89
90         $revisionCount = $page->revisions()->count();
91         $this->assertEquals(12, $revisionCount);
92     }
93 }