3 namespace Tests\References;
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;
11 use BookStack\References\Reference;
14 class ReferencesTest extends TestCase
16 public function test_references_created_on_page_update()
18 /** @var Page $pageA */
19 /** @var Page $pageB */
20 $pageA = Page::query()->first();
21 $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
23 $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
25 $this->asEditor()->put($pageA->getUrl(), [
26 'name' => 'Reference test',
27 'html' => '<a href="' . $pageB->getUrl() . '">Testing</a>',
30 $this->assertDatabaseHas('references', [
31 'from_id' => $pageA->id,
32 'from_type' => $pageA->getMorphClass(),
33 'to_id' => $pageB->id,
34 'to_type' => $pageB->getMorphClass(),
38 public function test_references_deleted_on_entity_delete()
40 /** @var Page $pageA */
41 /** @var Page $pageB */
42 $pageA = Page::query()->first();
43 $pageB = Page::query()->where('id', '!=', $pageA->id)->first();
45 $this->createReference($pageA, $pageB);
46 $this->createReference($pageB, $pageA);
48 $this->assertDatabaseHas('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
49 $this->assertDatabaseHas('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
51 app(PageRepo::class)->destroy($pageA);
52 app(TrashCan::class)->empty();
54 $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
55 $this->assertDatabaseMissing('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
58 public function test_references_to_count_visible_on_entity_show_view()
60 $entities = $this->getEachEntityType();
61 /** @var Page $otherPage */
62 $otherPage = Page::query()->where('id', '!=', $entities['page']->id)->first();
65 foreach ($entities as $entity) {
66 $this->createReference($entities['page'], $entity);
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');
75 $this->createReference($otherPage, $entities['page']);
76 $resp = $this->get($entities['page']->getUrl());
77 $resp->assertSee('Referenced on 2 pages');
80 public function test_references_to_visible_on_references_page()
82 $entities = $this->getEachEntityType();
84 foreach ($entities as $entity) {
85 $this->createReference($entities['page'], $entity);
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');
96 public function test_reference_not_visible_if_view_permission_does_not_permit()
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);
104 $this->setEntityRestrictions($pageB);
106 $this->asEditor()->get($page->getUrl('/references'))->assertDontSee($pageB->name);
107 $this->asAdmin()->get($page->getUrl('/references'))->assertSee($pageB->name);
110 public function test_reference_page_shows_empty_state_with_no_references()
112 /** @var Page $page */
113 $page = Page::query()->first();
116 ->get($page->getUrl('/references'))
117 ->assertSee('There are no tracked references');
120 public function test_pages_leading_to_entity_updated_on_url_change()
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();
129 foreach ([$pageA, $pageB] as $page) {
130 $page->html = '<a href="' . $book->getUrl() . '">Link</a>';
132 $this->createReference($page, $book);
135 $this->asEditor()->put($book->getUrl(), [
136 'name' => 'my updated book slugaroo',
139 foreach ([$pageA, $pageB] as $page) {
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',
149 public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
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;
158 $otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
160 $this->createReference($otherPage, $bookPage);
162 $this->asEditor()->put($book->getUrl(), [
163 'name' => 'my updated book slugaroo',
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',
174 public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
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;
183 $otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
185 $this->createReference($otherPage, $bookChapter);
187 $this->asEditor()->put($book->getUrl(), [
188 'name' => 'my updated book slugaroo',
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',
199 public function test_markdown_links_leading_to_entity_updated_on_url_change()
201 /** @var Page $page */
202 /** @var Book $book */
203 $page = Page::query()->first();
204 $book = Book::query()->first();
206 $bookUrl = $book->getUrl();
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;
217 $this->createReference($page, $book);
219 $this->asEditor()->put($book->getUrl(), [
220 'name' => 'my updated book slugadoo',
224 $expected = str_replace($bookUrl, 'http://localhost/books/my-updated-book-slugadoo', $markdown);
225 $this->assertEquals($expected, $page->markdown);
228 protected function createReference(Model $from, Model $to)
230 (new Reference())->forceFill([
231 'from_type' => $from->getMorphClass(),
232 'from_id' => $from->id,
233 'to_type' => $to->getMorphClass(),