]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageDraftTest.php
Fix coding style
[bookstack] / tests / Entity / PageDraftTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\PageRepo;
8 use Tests\TestCase;
9
10 class PageDraftTest extends TestCase
11 {
12     /**
13      * @var Page
14      */
15     protected $page;
16
17     /**
18      * @var PageRepo
19      */
20     protected $pageRepo;
21
22     public function setUp(): void
23     {
24         parent::setUp();
25         $this->page = Page::query()->first();
26         $this->pageRepo = app()->make(PageRepo::class);
27     }
28
29     public function test_draft_content_shows_if_available()
30     {
31         $addedContent = '<p>test message content</p>';
32
33         $this->asAdmin()->get($this->page->getUrl('/edit'))
34             ->assertElementNotContains('[name="html"]', $addedContent);
35
36         $newContent = $this->page->html . $addedContent;
37         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
38         $this->asAdmin()->get($this->page->getUrl('/edit'))
39             ->assertElementContains('[name="html"]', $newContent);
40     }
41
42     public function test_draft_not_visible_by_others()
43     {
44         $addedContent = '<p>test message content</p>';
45         $this->asAdmin()->get($this->page->getUrl('/edit'))
46             ->assertElementNotContains('[name="html"]', $addedContent);
47
48         $newContent = $this->page->html . $addedContent;
49         $newUser = $this->getEditor();
50         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
51
52         $this->actingAs($newUser)->get($this->page->getUrl('/edit'))
53             ->assertElementNotContains('[name="html"]', $newContent);
54     }
55
56     public function test_alert_message_shows_if_editing_draft()
57     {
58         $this->asAdmin();
59         $this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']);
60         $this->asAdmin()->get($this->page->getUrl('/edit'))
61             ->assertSee('You are currently editing a draft');
62     }
63
64     public function test_alert_message_shows_if_someone_else_editing()
65     {
66         $nonEditedPage = Page::query()->take(10)->get()->last();
67         $addedContent = '<p>test message content</p>';
68         $this->asAdmin()->get($this->page->getUrl('/edit'))
69             ->assertElementNotContains('[name="html"]', $addedContent);
70
71         $newContent = $this->page->html . $addedContent;
72         $newUser = $this->getEditor();
73         $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
74
75         $this->actingAs($newUser)
76             ->get($this->page->getUrl('/edit'))
77             ->assertSee('Admin has started editing this page');
78         $this->flushSession();
79         $this->get($nonEditedPage->getUrl() . '/edit')
80             ->assertElementNotContains('.notification', 'Admin has started editing this page');
81     }
82
83     public function test_draft_pages_show_on_homepage()
84     {
85         /** @var Book $book */
86         $book = Book::query()->first();
87         $this->asAdmin()->get('/')
88             ->assertElementNotContains('#recent-drafts', 'New Page');
89
90         $this->get($book->getUrl() . '/create-page');
91
92         $this->get('/')->assertElementContains('#recent-drafts', 'New Page');
93     }
94
95     public function test_draft_pages_not_visible_by_others()
96     {
97         /** @var Book $book */
98         $book = Book::query()->first();
99         $chapter = $book->chapters->first();
100         $newUser = $this->getEditor();
101
102         $this->actingAs($newUser)->get($book->getUrl('/create-page'));
103         $this->get($chapter->getUrl('/create-page'));
104         $this->get($book->getUrl())
105             ->assertElementContains('.book-contents', 'New Page');
106
107         $this->asAdmin()->get($book->getUrl())
108             ->assertElementNotContains('.book-contents', 'New Page');
109         $this->get($chapter->getUrl())
110             ->assertElementNotContains('.book-contents', 'New Page');
111     }
112
113     public function test_page_html_in_ajax_fetch_response()
114     {
115         $this->asAdmin();
116         /** @var Page $page */
117         $page = Page::query()->first();
118
119         $this->getJson('/ajax/page/' . $page->id)->assertJson([
120             'html' => $page->html,
121         ]);
122     }
123 }