]> BookStack Code Mirror - bookstack/blob - tests/References/ReferencesTest.php
Fixed phpstan wanring about usage of static
[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
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_markdown_links_leading_to_entity_updated_on_url_change()
150     {
151         /** @var Page $page */
152         /** @var Book $book */
153         $page = Page::query()->first();
154         $book = Book::query()->first();
155
156         $bookUrl = $book->getUrl();
157         $markdown = '
158         [An awesome link](' . $bookUrl . ')
159         [An awesome link with query & hash](' . $bookUrl . '?test=yes#cats)
160         [An awesome link with path](' . $bookUrl . '/an/extra/trail)
161         [An awesome link with title](' . $bookUrl . ' "title")
162         [ref]: ' . $bookUrl . '?test=yes#dogs
163         [ref_without_space]:' . $bookUrl . '
164         [ref_with_title]: ' . $bookUrl . ' "title"';
165         $page->markdown = $markdown;
166         $page->save();
167         $this->createReference($page, $book);
168
169         $this->asEditor()->put($book->getUrl(), [
170             'name' => 'my updated book slugadoo',
171         ]);
172
173         $page->refresh();
174         $expected = str_replace($bookUrl, 'http://localhost/books/my-updated-book-slugadoo', $markdown);
175         $this->assertEquals($expected, $page->markdown);
176     }
177
178     protected function createReference(Model $from, Model $to)
179     {
180         (new Reference())->forceFill([
181             'from_type' => $from->getMorphClass(),
182             'from_id' => $from->id,
183             'to_type' => $to->getMorphClass(),
184             'to_id' => $to->id,
185         ])->save();
186     }
187
188 }