]> BookStack Code Mirror - bookstack/blob - tests/References/ReferencesTest.php
Cleaned testing service provider usage
[bookstack] / tests / References / ReferencesTest.php
1 <?php
2
3 namespace Tests\References;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Repos\PageRepo;
9 use BookStack\Entities\Tools\TrashCan;
10 use BookStack\Model;
11 use BookStack\References\Reference;
12 use Tests\TestCase;
13
14 class ReferencesTest extends TestCase
15 {
16     public function test_references_created_on_page_update()
17     {
18         /** @var Page $pageA */
19         /** @var Page $pageB */
20         $pageA = Page::query()->first();
21         $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
22
23         $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
24
25         $this->asEditor()->put($pageA->getUrl(), [
26             'name' => 'Reference test',
27             'html' => '<a href="' . $pageB->getUrl() . '">Testing</a>',
28         ]);
29
30         $this->assertDatabaseHas('references', [
31             'from_id'   => $pageA->id,
32             'from_type' => $pageA->getMorphClass(),
33             'to_id'     => $pageB->id,
34             'to_type'   => $pageB->getMorphClass(),
35         ]);
36     }
37
38     public function test_references_deleted_on_entity_delete()
39     {
40         /** @var Page $pageA */
41         /** @var Page $pageB */
42         $pageA = Page::query()->first();
43         $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
44
45         $this->createReference($pageA, $pageB);
46         $this->createReference($pageB, $pageA);
47
48         $this->assertDatabaseHas('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
49         $this->assertDatabaseHas('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
50
51         app(PageRepo::class)->destroy($pageA);
52         app(TrashCan::class)->empty();
53
54         $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
55         $this->assertDatabaseMissing('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
56     }
57
58     public function test_references_to_count_visible_on_entity_show_view()
59     {
60         $entities = $this->getEachEntityType();
61         /** @var Page $otherPage */
62         $otherPage = Page::query()->where('id', '!=', $entities['page']->id)->first();
63
64         $this->asEditor();
65         foreach ($entities as $entity) {
66             $this->createReference($entities['page'], $entity);
67         }
68
69         foreach ($entities as $entity) {
70             $resp = $this->get($entity->getUrl());
71             $resp->assertSee('Referenced on 1 page');
72             $resp->assertDontSee('Referenced on 1 pages');
73         }
74
75         $this->createReference($otherPage, $entities['page']);
76         $resp = $this->get($entities['page']->getUrl());
77         $resp->assertSee('Referenced on 2 pages');
78     }
79
80     public function test_references_to_visible_on_references_page()
81     {
82         $entities = $this->getEachEntityType();
83         $this->asEditor();
84         foreach ($entities as $entity) {
85             $this->createReference($entities['page'], $entity);
86         }
87
88         foreach ($entities as $entity) {
89             $resp = $this->get($entity->getUrl('/references'));
90             $resp->assertSee('References');
91             $resp->assertSee($entities['page']->name);
92             $resp->assertDontSee('There are no tracked references');
93         }
94     }
95
96     public function test_reference_not_visible_if_view_permission_does_not_permit()
97     {
98         /** @var Page $page */
99         /** @var Page $pageB */
100         $page = Page::query()->first();
101         $pageB = Page::query()->where('id', '!=', $page->id)->first();
102         $this->createReference($pageB, $page);
103
104         $this->setEntityRestrictions($pageB);
105
106         $this->asEditor()->get($page->getUrl('/references'))->assertDontSee($pageB->name);
107         $this->asAdmin()->get($page->getUrl('/references'))->assertSee($pageB->name);
108     }
109
110     public function test_reference_page_shows_empty_state_with_no_references()
111     {
112         /** @var Page $page */
113         $page = Page::query()->first();
114
115         $this->asEditor()
116             ->get($page->getUrl('/references'))
117             ->assertSee('There are no tracked references');
118     }
119
120     public function test_pages_leading_to_entity_updated_on_url_change()
121     {
122         /** @var Page $pageA */
123         /** @var Page $pageB */
124         /** @var Book $book */
125         $pageA = Page::query()->first();
126         $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
127         $book = Book::query()->first();
128
129         foreach ([$pageA, $pageB] as $page) {
130             $page->html = '<a href="' . $book->getUrl() . '">Link</a>';
131             $page->save();
132             $this->createReference($page, $book);
133         }
134
135         $this->asEditor()->put($book->getUrl(), [
136             'name' => 'my updated book slugaroo',
137         ]);
138
139         foreach ([$pageA, $pageB] as $page) {
140             $page->refresh();
141             $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo"', $page->html);
142             $this->assertDatabaseHas('page_revisions', [
143                 'page_id' => $page->id,
144                 'summary' => 'System auto-update of internal links',
145             ]);
146         }
147     }
148
149     public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
150     {
151         /** @var Page $bookPage */
152         /** @var Page $otherPage */
153         /** @var Book $book */
154         $bookPage = Page::query()->first();
155         $otherPage = Page::query()->where('id', '!=', $bookPage->id)->first();
156         $book = $bookPage->book;
157
158         $otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
159         $otherPage->save();
160         $this->createReference($otherPage, $bookPage);
161
162         $this->asEditor()->put($book->getUrl(), [
163             'name' => 'my updated book slugaroo',
164         ]);
165
166         $otherPage->refresh();
167         $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/page/' . $bookPage->slug . '"', $otherPage->html);
168         $this->assertDatabaseHas('page_revisions', [
169             'page_id' => $otherPage->id,
170             'summary' => 'System auto-update of internal links',
171         ]);
172     }
173
174     public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
175     {
176         /** @var Chapter $bookChapter */
177         /** @var Page $otherPage */
178         /** @var Book $book */
179         $bookChapter = Chapter::query()->first();
180         $otherPage = Page::query()->first();
181         $book = $bookChapter->book;
182
183         $otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
184         $otherPage->save();
185         $this->createReference($otherPage, $bookChapter);
186
187         $this->asEditor()->put($book->getUrl(), [
188             'name' => 'my updated book slugaroo',
189         ]);
190
191         $otherPage->refresh();
192         $this->assertStringContainsString('href="http://localhost/books/my-updated-book-slugaroo/chapter/' . $bookChapter->slug . '"', $otherPage->html);
193         $this->assertDatabaseHas('page_revisions', [
194             'page_id' => $otherPage->id,
195             'summary' => 'System auto-update of internal links',
196         ]);
197     }
198
199     public function test_markdown_links_leading_to_entity_updated_on_url_change()
200     {
201         /** @var Page $page */
202         /** @var Book $book */
203         $page = Page::query()->first();
204         $book = Book::query()->first();
205
206         $bookUrl = $book->getUrl();
207         $markdown = '
208         [An awesome link](' . $bookUrl . ')
209         [An awesome link with query & hash](' . $bookUrl . '?test=yes#cats)
210         [An awesome link with path](' . $bookUrl . '/an/extra/trail)
211         [An awesome link with title](' . $bookUrl . ' "title")
212         [ref]: ' . $bookUrl . '?test=yes#dogs
213         [ref_without_space]:' . $bookUrl . '
214         [ref_with_title]: ' . $bookUrl . ' "title"';
215         $page->markdown = $markdown;
216         $page->save();
217         $this->createReference($page, $book);
218
219         $this->asEditor()->put($book->getUrl(), [
220             'name' => 'my updated book slugadoo',
221         ]);
222
223         $page->refresh();
224         $expected = str_replace($bookUrl, 'http://localhost/books/my-updated-book-slugadoo', $markdown);
225         $this->assertEquals($expected, $page->markdown);
226     }
227
228     protected function createReference(Model $from, Model $to)
229     {
230         (new Reference())->forceFill([
231             'from_type' => $from->getMorphClass(),
232             'from_id'   => $from->id,
233             'to_type'   => $to->getMorphClass(),
234             'to_id'     => $to->id,
235         ])->save();
236     }
237 }