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