-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathReferencesTest.php
More file actions
307 lines (250 loc) · 11 KB
/
ReferencesTest.php
File metadata and controls
307 lines (250 loc) · 11 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
namespace Tests\References;
use BookStack\App\Model;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\TrashCan;
use BookStack\References\Reference;
use Tests\TestCase;
class ReferencesTest extends TestCase
{
public function test_references_created_on_page_update()
{
$pageA = $this->entities->page();
$pageB = $this->entities->page();
$this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
$this->asEditor()->put($pageA->getUrl(), [
'name' => 'Reference test',
'html' => '<a href="' . $pageB->getUrl() . '">Testing</a>',
]);
$this->assertDatabaseHas('references', [
'from_id' => $pageA->id,
'from_type' => $pageA->getMorphClass(),
'to_id' => $pageB->id,
'to_type' => $pageB->getMorphClass(),
]);
}
public function test_references_created_on_book_chapter_bookshelf_update()
{
$entities = [$this->entities->book(), $this->entities->chapter(), $this->entities->shelf()];
$shelf = $this->entities->shelf();
foreach ($entities as $entity) {
$entity->refresh();
$this->assertDatabaseMissing('references', ['from_id' => $entity->id, 'from_type' => $entity->getMorphClass()]);
$this->asEditor()->put($entity->getUrl(), [
'name' => 'Reference test',
'description_html' => '<a href="' . $shelf->getUrl() . '">Testing</a>',
]);
$this->assertDatabaseHas('references', [
'from_id' => $entity->id,
'from_type' => $entity->getMorphClass(),
'to_id' => $shelf->id,
'to_type' => $shelf->getMorphClass(),
]);
}
}
public function test_references_deleted_on_page_delete()
{
$pageA = $this->entities->page();
$pageB = $this->entities->page();
$this->createReference($pageA, $pageB);
$this->createReference($pageB, $pageA);
$this->assertDatabaseHas('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
$this->assertDatabaseHas('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
app(PageRepo::class)->destroy($pageA);
app(TrashCan::class)->empty();
$this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
$this->assertDatabaseMissing('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
}
public function test_references_from_deleted_on_book_chapter_shelf_delete()
{
$entities = [$this->entities->chapter(), $this->entities->book(), $this->entities->shelf()];
$shelf = $this->entities->shelf();
foreach ($entities as $entity) {
$this->createReference($entity, $shelf);
$this->assertDatabaseHas('references', ['from_id' => $entity->id, 'from_type' => $entity->getMorphClass()]);
$this->asEditor()->delete($entity->getUrl());
app(TrashCan::class)->empty();
$this->assertDatabaseMissing('references', [
'from_id' => $entity->id,
'from_type' => $entity->getMorphClass()
]);
}
}
public function test_references_to_count_visible_on_entity_show_view()
{
$entities = $this->entities->all();
$otherPage = $this->entities->page();
$this->asEditor();
foreach ($entities as $entity) {
$this->createReference($entities['page'], $entity);
}
foreach ($entities as $entity) {
$resp = $this->get($entity->getUrl());
$resp->assertSee('Referenced by 1 item');
$resp->assertDontSee('Referenced by 1 items');
}
$this->createReference($otherPage, $entities['page']);
$resp = $this->get($entities['page']->getUrl());
$resp->assertSee('Referenced by 2 items');
}
public function test_references_to_visible_on_references_page()
{
$entities = $this->entities->all();
$this->asEditor();
foreach ($entities as $entity) {
$this->createReference($entities['page'], $entity);
}
foreach ($entities as $entity) {
$resp = $this->get($entity->getUrl('/references'));
$resp->assertSee('References');
$resp->assertSee($entities['page']->name);
$resp->assertDontSee('There are no tracked references');
}
}
public function test_reference_not_visible_if_view_permission_does_not_permit()
{
$page = $this->entities->page();
$pageB = $this->entities->page();
$this->createReference($pageB, $page);
$this->permissions->setEntityPermissions($pageB);
$this->asEditor()->get($page->getUrl('/references'))->assertDontSee($pageB->name);
$this->asAdmin()->get($page->getUrl('/references'))->assertSee($pageB->name);
}
public function test_reference_page_shows_empty_state_with_no_references()
{
$page = $this->entities->page();
$this->asEditor()
->get($page->getUrl('/references'))
->assertSee('There are no tracked references');
}
public function test_pages_leading_to_entity_updated_on_url_change()
{
$pageA = $this->entities->page();
$pageB = $this->entities->page();
$book = $this->entities->book();
foreach ([$pageA, $pageB] as $page) {
$page->html = '<a href="' . $book->getUrl() . '">Link</a>';
$page->save();
$this->createReference($page, $book);
}
$this->asEditor()->put($book->getUrl(), [
'name' => 'my updated book slugaroo',
]);
foreach ([$pageA, $pageB] as $page) {
$page->refresh();
$this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo"', $page->html);
$this->assertDatabaseHas('page_revisions', [
'page_id' => $page->id,
'summary' => 'System auto-update of internal links',
]);
}
}
public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
{
$bookPage = $this->entities->page();
$otherPage = $this->entities->page();
$book = $bookPage->book;
$otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
$otherPage->save();
$this->createReference($otherPage, $bookPage);
$this->asEditor()->put($book->getUrl(), [
'name' => 'my updated book slugaroo',
]);
$otherPage->refresh();
$this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/page/' . $bookPage->slug . '"', $otherPage->html);
$this->assertDatabaseHas('page_revisions', [
'page_id' => $otherPage->id,
'summary' => 'System auto-update of internal links',
]);
}
public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
{
$bookChapter = $this->entities->chapter();
$otherPage = $this->entities->page();
$book = $bookChapter->book;
$otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
$otherPage->save();
$this->createReference($otherPage, $bookChapter);
$this->asEditor()->put($book->getUrl(), [
'name' => 'my updated book slugaroo',
]);
$otherPage->refresh();
$this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/chapter/' . $bookChapter->slug . '"', $otherPage->html);
$this->assertDatabaseHas('page_revisions', [
'page_id' => $otherPage->id,
'summary' => 'System auto-update of internal links',
]);
}
public function test_markdown_links_leading_to_entity_updated_on_url_change()
{
$page = $this->entities->page();
$book = $this->entities->book();
$bookUrl = $book->getUrl();
$markdown = '
[An awesome link](' . $bookUrl . ')
[An awesome link with query & hash](' . $bookUrl . '?test=yes#cats)
[An awesome link with path](' . $bookUrl . '/an/extra/trail)
[An awesome link with title](' . $bookUrl . ' "title")
[ref]: ' . $bookUrl . '?test=yes#dogs
[ref_without_space]:' . $bookUrl . '
[ref_with_title]: ' . $bookUrl . ' "title"';
$page->markdown = $markdown;
$page->save();
$this->createReference($page, $book);
$this->asEditor()->put($book->getUrl(), [
'name' => 'my updated book slugadoo',
]);
$page->refresh();
$expected = str_replace($bookUrl, 'http://localhost/books/my-updated-book-slugadoo', $markdown);
$this->assertEquals($expected, $page->markdown);
}
public function test_description_links_from_book_chapter_shelf_updated_on_url_change()
{
$entities = [$this->entities->chapter(), $this->entities->book(), $this->entities->shelf()];
$shelf = $this->entities->shelf();
$this->asEditor();
foreach ($entities as $entity) {
$this->put($entity->getUrl(), [
'name' => 'Reference test',
'description_html' => '<a href="' . $shelf->getUrl() . '">Testing</a>',
]);
}
$oldUrl = $shelf->getUrl();
$this->put($shelf->getUrl(), ['name' => 'My updated shelf link'])->assertRedirect();
$shelf->refresh();
$this->assertNotEquals($oldUrl, $shelf->getUrl());
foreach ($entities as $entity) {
$oldHtml = $entity->description_html;
$entity->refresh();
$this->assertNotEquals($oldHtml, $entity->description_html);
$this->assertStringContainsString($shelf->getUrl(), $entity->description_html);
}
}
public function test_reference_from_deleted_item_does_not_count_or_show_in_references_page()
{
$page = $this->entities->page();
$referencingPageA = $this->entities->page();
$referencingPageB = $this->entities->page();
$this->asEditor();
$this->createReference($referencingPageA, $page);
$this->createReference($referencingPageB, $page);
$resp = $this->get($page->getUrl());
$resp->assertSee('Referenced by 2 items');
$this->delete($referencingPageA->getUrl());
$resp = $this->get($page->getUrl());
$resp->assertSee('Referenced by 1 item');
$resp = $this->get($page->getUrl('/references'));
$resp->assertOk();
$resp->assertSee($referencingPageB->getUrl());
$resp->assertDontSee($referencingPageA->getUrl());
}
protected function createReference(Model $from, Model $to): void
{
(new Reference())->forceFill([
'from_type' => $from->getMorphClass(),
'from_id' => $from->id,
'to_type' => $to->getMorphClass(),
'to_id' => $to->id,
])->save();
}
}