3 namespace Tests\References;
5 use BookStack\Entities\Repos\PageRepo;
6 use BookStack\Entities\Tools\TrashCan;
8 use BookStack\References\Reference;
11 class ReferencesTest extends TestCase
13 public function test_references_created_on_page_update()
15 $pageA = $this->entities->page();
16 $pageB = $this->entities->page();
18 $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
20 $this->asEditor()->put($pageA->getUrl(), [
21 'name' => 'Reference test',
22 'html' => '<a href="' . $pageB->getUrl() . '">Testing</a>',
25 $this->assertDatabaseHas('references', [
26 'from_id' => $pageA->id,
27 'from_type' => $pageA->getMorphClass(),
28 'to_id' => $pageB->id,
29 'to_type' => $pageB->getMorphClass(),
33 public function test_references_deleted_on_entity_delete()
35 $pageA = $this->entities->page();
36 $pageB = $this->entities->page();
38 $this->createReference($pageA, $pageB);
39 $this->createReference($pageB, $pageA);
41 $this->assertDatabaseHas('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
42 $this->assertDatabaseHas('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
44 app(PageRepo::class)->destroy($pageA);
45 app(TrashCan::class)->empty();
47 $this->assertDatabaseMissing('references', ['from_id' => $pageA->id, 'from_type' => $pageA->getMorphClass()]);
48 $this->assertDatabaseMissing('references', ['to_id' => $pageA->id, 'to_type' => $pageA->getMorphClass()]);
51 public function test_references_to_count_visible_on_entity_show_view()
53 $entities = $this->entities->all();
54 $otherPage = $this->entities->page();
57 foreach ($entities as $entity) {
58 $this->createReference($entities['page'], $entity);
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');
67 $this->createReference($otherPage, $entities['page']);
68 $resp = $this->get($entities['page']->getUrl());
69 $resp->assertSee('Referenced on 2 pages');
72 public function test_references_to_visible_on_references_page()
74 $entities = $this->entities->all();
76 foreach ($entities as $entity) {
77 $this->createReference($entities['page'], $entity);
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');
88 public function test_reference_not_visible_if_view_permission_does_not_permit()
90 $page = $this->entities->page();
91 $pageB = $this->entities->page();
92 $this->createReference($pageB, $page);
94 $this->entities->setPermissions($pageB);
96 $this->asEditor()->get($page->getUrl('/references'))->assertDontSee($pageB->name);
97 $this->asAdmin()->get($page->getUrl('/references'))->assertSee($pageB->name);
100 public function test_reference_page_shows_empty_state_with_no_references()
102 $page = $this->entities->page();
105 ->get($page->getUrl('/references'))
106 ->assertSee('There are no tracked references');
109 public function test_pages_leading_to_entity_updated_on_url_change()
111 $pageA = $this->entities->page();
112 $pageB = $this->entities->page();
113 $book = $this->entities->book();
115 foreach ([$pageA, $pageB] as $page) {
116 $page->html = '<a href="' . $book->getUrl() . '">Link</a>';
118 $this->createReference($page, $book);
121 $this->asEditor()->put($book->getUrl(), [
122 'name' => 'my updated book slugaroo',
125 foreach ([$pageA, $pageB] as $page) {
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',
135 public function test_pages_linking_to_other_page_updated_on_parent_book_url_change()
137 $bookPage = $this->entities->page();
138 $otherPage = $this->entities->page();
139 $book = $bookPage->book;
141 $otherPage->html = '<a href="' . $bookPage->getUrl() . '">Link</a>';
143 $this->createReference($otherPage, $bookPage);
145 $this->asEditor()->put($book->getUrl(), [
146 'name' => 'my updated book slugaroo',
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',
157 public function test_pages_linking_to_chapter_updated_on_parent_book_url_change()
159 $bookChapter = $this->entities->chapter();
160 $otherPage = $this->entities->page();
161 $book = $bookChapter->book;
163 $otherPage->html = '<a href="' . $bookChapter->getUrl() . '">Link</a>';
165 $this->createReference($otherPage, $bookChapter);
167 $this->asEditor()->put($book->getUrl(), [
168 'name' => 'my updated book slugaroo',
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',
179 public function test_markdown_links_leading_to_entity_updated_on_url_change()
181 $page = $this->entities->page();
182 $book = $this->entities->book();
184 $bookUrl = $book->getUrl();
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;
195 $this->createReference($page, $book);
197 $this->asEditor()->put($book->getUrl(), [
198 'name' => 'my updated book slugadoo',
202 $expected = str_replace($bookUrl, 'http://localhost/books/my-updated-book-slugadoo', $markdown);
203 $this->assertEquals($expected, $page->markdown);
206 protected function createReference(Model $from, Model $to)
208 (new Reference())->forceFill([
209 'from_type' => $from->getMorphClass(),
210 'from_id' => $from->id,
211 'to_type' => $to->getMorphClass(),