]> BookStack Code Mirror - bookstack/blob - tests/Entity/TagTest.php
Added content-view body classes generated from tags
[bookstack] / tests / Entity / TagTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\Page;
9 use Tests\TestCase;
10
11 class TagTest extends TestCase
12 {
13     protected $defaultTagCount = 20;
14
15     /**
16      * Get an instance of a page that has many tags.
17      */
18     protected function getEntityWithTags($class, ?array $tags = null): Entity
19     {
20         $entity = $class::first();
21
22         if (is_null($tags)) {
23             $tags = Tag::factory()->count($this->defaultTagCount)->make();
24         }
25
26         $entity->tags()->saveMany($tags);
27
28         return $entity;
29     }
30
31     public function test_tag_name_suggestions()
32     {
33         // Create some tags with similar names to test with
34         $attrs = collect();
35         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'country']));
36         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'color']));
37         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'city']));
38         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'county']));
39         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'planet']));
40         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'plans']));
41         $page = $this->getEntityWithTags(Page::class, $attrs->all());
42
43         $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->assertSimilarJson([]);
44         $this->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country', 'county']);
45         $this->get('/ajax/tags/suggest/names?search=cou')->assertSimilarJson(['country', 'county']);
46         $this->get('/ajax/tags/suggest/names?search=pla')->assertSimilarJson(['planet', 'plans']);
47     }
48
49     public function test_tag_value_suggestions()
50     {
51         // Create some tags with similar values to test with
52         $attrs = collect();
53         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'country', 'value' => 'cats']));
54         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'color', 'value' => 'cattery']));
55         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'city', 'value' => 'castle']));
56         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'county', 'value' => 'dog']));
57         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'planet', 'value' => 'catapult']));
58         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'plans', 'value' => 'dodgy']));
59         $page = $this->getEntityWithTags(Page::class, $attrs->all());
60
61         $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->assertSimilarJson([]);
62         $this->get('/ajax/tags/suggest/values?search=cat')->assertSimilarJson(['cats', 'cattery', 'catapult']);
63         $this->get('/ajax/tags/suggest/values?search=do')->assertSimilarJson(['dog', 'dodgy']);
64         $this->get('/ajax/tags/suggest/values?search=cas')->assertSimilarJson(['castle']);
65     }
66
67     public function test_entity_permissions_effect_tag_suggestions()
68     {
69         // Create some tags with similar names to test with and save to a page
70         $attrs = collect();
71         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'country']));
72         $attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'color']));
73         $page = $this->getEntityWithTags(Page::class, $attrs->all());
74
75         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country']);
76         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country']);
77
78         // Set restricted permission the page
79         $page->restricted = true;
80         $page->save();
81         $page->rebuildPermissions();
82
83         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country']);
84         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson([]);
85     }
86
87     public function test_tags_shown_on_search_listing()
88     {
89         $tags = [
90             Tag::factory()->make(['name' => 'category', 'value' => 'buckets']),
91             Tag::factory()->make(['name' => 'color', 'value' => 'red']),
92         ];
93
94         $page = $this->getEntityWithTags(Page::class, $tags);
95         $resp = $this->asEditor()->get('/search?term=[category]');
96         $resp->assertSee($page->name);
97         $this->withHtml($resp)->assertElementContains('[href="' . $page->getUrl() . '"]', 'category');
98         $this->withHtml($resp)->assertElementContains('[href="' . $page->getUrl() . '"]', 'buckets');
99         $this->withHtml($resp)->assertElementContains('[href="' . $page->getUrl() . '"]', 'color');
100         $this->withHtml($resp)->assertElementContains('[href="' . $page->getUrl() . '"]', 'red');
101     }
102
103     public function test_tags_index_shows_tag_name_as_expected_with_right_counts()
104     {
105         /** @var Page $page */
106         $page = Page::query()->first();
107         $page->tags()->create(['name' => 'Category', 'value' => 'GreatTestContent']);
108         $page->tags()->create(['name' => 'Category', 'value' => 'OtherTestContent']);
109
110         $resp = $this->asEditor()->get('/tags');
111         $resp->assertSee('Category');
112         $html = $this->withHtml($resp);
113         $html->assertElementCount('.tag-item', 1);
114         $resp->assertDontSee('GreatTestContent');
115         $resp->assertDontSee('OtherTestContent');
116         $html->assertElementContains('a[title="Total tag usages"]', '2');
117         $html->assertElementContains('a[title="Assigned to Pages"]', '2');
118         $html->assertElementContains('a[title="Assigned to Books"]', '0');
119         $html->assertElementContains('a[title="Assigned to Chapters"]', '0');
120         $html->assertElementContains('a[title="Assigned to Shelves"]', '0');
121         $html->assertElementContains('a[href$="/tags?name=Category"]', '2 unique values');
122
123         /** @var Book $book */
124         $book = Book::query()->first();
125         $book->tags()->create(['name' => 'Category', 'value' => 'GreatTestContent']);
126         $resp = $this->asEditor()->get('/tags');
127         $this->withHtml($resp)->assertElementContains('a[title="Total tag usages"]', '3');
128         $this->withHtml($resp)->assertElementContains('a[title="Assigned to Books"]', '1');
129         $this->withHtml($resp)->assertElementContains('a[href$="/tags?name=Category"]', '2 unique values');
130     }
131
132     public function test_tag_index_can_be_searched()
133     {
134         /** @var Page $page */
135         $page = Page::query()->first();
136         $page->tags()->create(['name' => 'Category', 'value' => 'GreatTestContent']);
137
138         $resp = $this->asEditor()->get('/tags?search=cat');
139         $this->withHtml($resp)->assertElementContains('.tag-item .tag-name', 'Category');
140
141         $resp = $this->asEditor()->get('/tags?search=content');
142         $this->withHtml($resp)->assertElementContains('.tag-item .tag-name', 'Category');
143         $this->withHtml($resp)->assertElementContains('.tag-item .tag-value', 'GreatTestContent');
144
145         $resp = $this->asEditor()->get('/tags?search=other');
146         $this->withHtml($resp)->assertElementNotExists('.tag-item .tag-name');
147     }
148
149     public function test_tag_index_search_will_show_mulitple_values_of_a_single_tag_name()
150     {
151         /** @var Page $page */
152         $page = Page::query()->first();
153         $page->tags()->create(['name' => 'Animal', 'value' => 'Catfish']);
154         $page->tags()->create(['name' => 'Animal', 'value' => 'Catdog']);
155
156         $resp = $this->asEditor()->get('/tags?search=cat');
157         $this->withHtml($resp)->assertElementContains('.tag-item .tag-value', 'Catfish');
158         $this->withHtml($resp)->assertElementContains('.tag-item .tag-value', 'Catdog');
159     }
160
161     public function test_tag_index_can_be_scoped_to_specific_tag_name()
162     {
163         /** @var Page $page */
164         $page = Page::query()->first();
165         $page->tags()->create(['name' => 'Category', 'value' => 'GreatTestContent']);
166         $page->tags()->create(['name' => 'Category', 'value' => 'OtherTestContent']);
167         $page->tags()->create(['name' => 'OtherTagName', 'value' => 'OtherValue']);
168
169         $resp = $this->asEditor()->get('/tags?name=Category');
170         $resp->assertSee('Category');
171         $resp->assertSee('GreatTestContent');
172         $resp->assertSee('OtherTestContent');
173         $resp->assertDontSee('OtherTagName');
174         $resp->assertSee('Active Filter:');
175         $this->withHtml($resp)->assertElementCount('table .tag-item', 2);
176         $this->withHtml($resp)->assertElementContains('form[action$="/tags"]', 'Clear Filter');
177     }
178
179     public function test_tags_index_adheres_to_page_permissions()
180     {
181         /** @var Page $page */
182         $page = Page::query()->first();
183         $page->tags()->create(['name' => 'SuperCategory', 'value' => 'GreatTestContent']);
184
185         $resp = $this->asEditor()->get('/tags');
186         $resp->assertSee('SuperCategory');
187         $resp = $this->get('/tags?name=SuperCategory');
188         $resp->assertSee('GreatTestContent');
189
190         $page->restricted = true;
191         $this->regenEntityPermissions($page);
192
193         $resp = $this->asEditor()->get('/tags');
194         $resp->assertDontSee('SuperCategory');
195         $resp = $this->get('/tags?name=SuperCategory');
196         $resp->assertDontSee('GreatTestContent');
197     }
198
199     public function test_tag_index_shows_message_on_no_results()
200     {
201         $resp = $this->asEditor()->get('/tags?search=testingval');
202         $resp->assertSee('No items available');
203         $resp->assertSee('Tags can be assigned via the page editor sidebar');
204     }
205
206     public function test_tag_classes_visible_on_entities()
207     {
208         $this->asEditor();
209
210         foreach ($this->getEachEntityType() as $entity) {
211             $entity->tags()->create(['name' => 'My Super Tag Name', 'value' => 'An-awesome-value']);
212             $html = $this->withHtml($this->get($entity->getUrl()));
213             $html->assertElementExists('body.tag-name-mysupertagname.tag-value-anawesomevalue.tag-pair-mysupertagname-anawesomevalue');
214         }
215     }
216
217     public function test_tag_classes_are_escaped()
218     {
219         $page = Page::query()->first();
220         $page->tags()->create(['name' => '<>']);
221         $resp = $this->asEditor()->get($page->getUrl());
222         $resp->assertDontSee('tag-name-<>', false);
223         $resp->assertSee('tag-name-&lt;&gt;', false);
224     }
225 }