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