forked from BookStackApp/BookStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageDraftTest.php
More file actions
207 lines (170 loc) · 7.09 KB
/
Copy pathPageDraftTest.php
File metadata and controls
207 lines (170 loc) · 7.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace Tests\Entity;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Models\PageRevision;
use BookStack\Entities\Repos\PageRepo;
use Tests\TestCase;
class PageDraftTest extends TestCase
{
/**
* @var Page
*/
protected $page;
/**
* @var PageRepo
*/
protected $pageRepo;
protected function setUp(): void
{
parent::setUp();
$this->page = Page::query()->first();
$this->pageRepo = app()->make(PageRepo::class);
}
public function test_draft_content_shows_if_available()
{
$addedContent = '<p>test message content</p>';
$this->asAdmin()->get($this->page->getUrl('/edit'))
->assertElementNotContains('[name="html"]', $addedContent);
$newContent = $this->page->html . $addedContent;
$this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
$this->asAdmin()->get($this->page->getUrl('/edit'))
->assertElementContains('[name="html"]', $newContent);
}
public function test_draft_not_visible_by_others()
{
$addedContent = '<p>test message content</p>';
$this->asAdmin()->get($this->page->getUrl('/edit'))
->assertElementNotContains('[name="html"]', $addedContent);
$newContent = $this->page->html . $addedContent;
$newUser = $this->getEditor();
$this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
$this->actingAs($newUser)->get($this->page->getUrl('/edit'))
->assertElementNotContains('[name="html"]', $newContent);
}
public function test_alert_message_shows_if_editing_draft()
{
$this->asAdmin();
$this->pageRepo->updatePageDraft($this->page, ['html' => 'test content']);
$this->asAdmin()->get($this->page->getUrl('/edit'))
->assertSee('You are currently editing a draft');
}
public function test_alert_message_shows_if_someone_else_editing()
{
$nonEditedPage = Page::query()->take(10)->get()->last();
$addedContent = '<p>test message content</p>';
$this->asAdmin()->get($this->page->getUrl('/edit'))
->assertElementNotContains('[name="html"]', $addedContent);
$newContent = $this->page->html . $addedContent;
$newUser = $this->getEditor();
$this->pageRepo->updatePageDraft($this->page, ['html' => $newContent]);
$this->actingAs($newUser)
->get($this->page->getUrl('/edit'))
->assertSee('Admin has started editing this page');
$this->flushSession();
$this->get($nonEditedPage->getUrl() . '/edit')
->assertElementNotContains('.notification', 'Admin has started editing this page');
}
public function test_draft_save_shows_alert_if_draft_older_than_last_page_update()
{
$admin = $this->getAdmin();
$editor = $this->getEditor();
/** @var Page $page */
$page = Page::query()->first();
$this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
'name' => $page->name,
'html' => '<p>updated draft</p>',
]);
/** @var PageRevision $draft */
$draft = $page->allRevisions()
->where('type', '=', 'update_draft')
->where('created_by', '=', $editor->id)
->first();
$draft->created_at = now()->subMinute(1);
$draft->save();
$this->actingAs($admin)->put($page->refresh()->getUrl(), [
'name' => $page->name,
'html' => '<p>admin update</p>',
]);
$resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
'name' => $page->name,
'html' => '<p>updated draft again</p>',
]);
$resp->assertJson([
'warning' => 'This page has been updated since this draft was created. It is recommended that you discard this draft or take care not to overwrite any page changes.',
]);
}
public function test_draft_save_shows_alert_if_draft_edit_started_by_someone_else()
{
$admin = $this->getAdmin();
$editor = $this->getEditor();
/** @var Page $page */
$page = Page::query()->first();
$this->actingAs($admin)->put('/ajax/page/' . $page->id . '/save-draft', [
'name' => $page->name,
'html' => '<p>updated draft</p>',
]);
$resp = $this->actingAs($editor)->put('/ajax/page/' . $page->id . '/save-draft', [
'name' => $page->name,
'html' => '<p>updated draft again</p>',
]);
$resp->assertJson([
'warning' => 'Admin has started editing this page in the last 60 minutes. Take care not to overwrite each other\'s updates!',
]);
}
public function test_draft_pages_show_on_homepage()
{
/** @var Book $book */
$book = Book::query()->first();
$this->asAdmin()->get('/')
->assertElementNotContains('#recent-drafts', 'New Page');
$this->get($book->getUrl() . '/create-page');
$this->get('/')->assertElementContains('#recent-drafts', 'New Page');
}
public function test_draft_pages_not_visible_by_others()
{
/** @var Book $book */
$book = Book::query()->first();
$chapter = $book->chapters->first();
$newUser = $this->getEditor();
$this->actingAs($newUser)->get($book->getUrl('/create-page'));
$this->get($chapter->getUrl('/create-page'));
$this->get($book->getUrl())
->assertElementContains('.book-contents', 'New Page');
$this->asAdmin()->get($book->getUrl())
->assertElementNotContains('.book-contents', 'New Page');
$this->get($chapter->getUrl())
->assertElementNotContains('.book-contents', 'New Page');
}
public function test_page_html_in_ajax_fetch_response()
{
$this->asAdmin();
/** @var Page $page */
$page = Page::query()->first();
$this->getJson('/ajax/page/' . $page->id)->assertJson([
'html' => $page->html,
]);
}
public function test_updating_page_draft_with_markdown_retains_markdown_content()
{
/** @var Book $book */
$book = Book::query()->first();
$this->asEditor()->get($book->getUrl('/create-page'));
/** @var Page $draft */
$draft = Page::query()->where('draft', '=', true)->where('book_id', '=', $book->id)->firstOrFail();
$resp = $this->put('/ajax/page/' . $draft->id . '/save-draft', [
'name' => 'My updated draft',
'markdown' => "# My markdown page\n\n[A link](https://example.com)",
'html' => '<p>checking markdown takes priority over this</p>',
]);
$resp->assertOk();
$this->assertDatabaseHas('pages', [
'id' => $draft->id,
'draft' => true,
'name' => 'My updated draft',
'markdown' => "# My markdown page\n\n[A link](https://example.com)",
]);
$draft->refresh();
$this->assertStringContainsString('href="https://example.com"', $draft->html);
}
}