3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Repos\PageRepo;
10 class PageDraftTest extends TestCase
22 public function setUp(): void
25 $this->page = Page::query()->first();
26 $this->pageRepo = app()->make(PageRepo::class);
29 public function test_draft_content_shows_if_available()
31 $addedContent = '<p>test message content</p>';
33 $this->asAdmin()->get($this->page->getUrl('/edit'))
34 ->assertElementNotContains('[name="html"]', $addedContent);
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);
42 public function test_draft_not_visible_by_others()
44 $addedContent = '<p>test message content</p>';
45 $this->asAdmin()->get($this->page->getUrl('/edit'))
46 ->assertElementNotContains('[name="html"]', $addedContent);
48 $newContent = $this->page->html . $addedContent;
49 $newUser = $this->getEditor();
50 $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
52 $this->actingAs($newUser)->get($this->page->getUrl('/edit'))
53 ->assertElementNotContains('[name="html"]', $newContent);
56 public function test_alert_message_shows_if_editing_draft()
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');
64 public function test_alert_message_shows_if_someone_else_editing()
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);
71 $newContent = $this->page->html . $addedContent;
72 $newUser = $this->getEditor();
73 $this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
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');
83 public function test_draft_pages_show_on_homepage()
85 /** @var Book $book */
86 $book = Book::query()->first();
87 $this->asAdmin()->get('/')
88 ->assertElementNotContains('#recent-drafts', 'New Page');
90 $this->get($book->getUrl() . '/create-page');
92 $this->get('/')->assertElementContains('#recent-drafts', 'New Page');
95 public function test_draft_pages_not_visible_by_others()
97 /** @var Book $book */
98 $book = Book::query()->first();
99 $chapter = $book->chapters->first();
100 $newUser = $this->getEditor();
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');
107 $this->asAdmin()->get($book->getUrl())
108 ->assertElementNotContains('.book-contents', 'New Page');
109 $this->get($chapter->getUrl())
110 ->assertElementNotContains('.book-contents', 'New Page');
113 public function test_page_html_in_ajax_fetch_response()
116 /** @var Page $page */
117 $page = Page::query()->first();
119 $this->getJson('/ajax/page/' . $page->id)->assertJson([
120 'html' => $page->html,