]> BookStack Code Mirror - bookstack/blob - tests/Entity/TagTest.php
Apply fixes from StyleCI
[bookstack] / tests / Entity / TagTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use Tests\TestCase;
9
10 class TagTest extends TestCase
11 {
12     protected $defaultTagCount = 20;
13
14     /**
15      * Get an instance of a page that has many tags.
16      */
17     protected function getEntityWithTags($class, ?array $tags = null): Entity
18     {
19         $entity = $class::first();
20
21         if (is_null($tags)) {
22             $tags = factory(Tag::class, $this->defaultTagCount)->make();
23         }
24
25         $entity->tags()->saveMany($tags);
26
27         return $entity;
28     }
29
30     public function test_tag_name_suggestions()
31     {
32         // Create some tags with similar names to test with
33         $attrs = collect();
34         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
35         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
36         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
37         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
38         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
39         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
40         $page = $this->getEntityWithTags(Page::class, $attrs->all());
41
42         $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->assertExactJson([]);
43         $this->get('/ajax/tags/suggest/names?search=co')->assertExactJson(['color', 'country', 'county']);
44         $this->get('/ajax/tags/suggest/names?search=cou')->assertExactJson(['country', 'county']);
45         $this->get('/ajax/tags/suggest/names?search=pla')->assertExactJson(['planet', 'plans']);
46     }
47
48     public function test_tag_value_suggestions()
49     {
50         // Create some tags with similar values to test with
51         $attrs = collect();
52         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats']));
53         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery']));
54         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle']));
55         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog']));
56         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult']));
57         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy']));
58         $page = $this->getEntityWithTags(Page::class, $attrs->all());
59
60         $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->assertExactJson([]);
61         $this->get('/ajax/tags/suggest/values?search=cat')->assertExactJson(['cats', 'cattery', 'catapult']);
62         $this->get('/ajax/tags/suggest/values?search=do')->assertExactJson(['dog', 'dodgy']);
63         $this->get('/ajax/tags/suggest/values?search=cas')->assertExactJson(['castle']);
64     }
65
66     public function test_entity_permissions_effect_tag_suggestions()
67     {
68         // Create some tags with similar names to test with and save to a page
69         $attrs = collect();
70         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
71         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
72         $page = $this->getEntityWithTags(Page::class, $attrs->all());
73
74         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertExactJson(['color', 'country']);
75         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertExactJson(['color', 'country']);
76
77         // Set restricted permission the page
78         $page->restricted = true;
79         $page->save();
80         $page->rebuildPermissions();
81
82         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertExactJson(['color', 'country']);
83         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertExactJson([]);
84     }
85
86     public function test_tags_shown_on_search_listing()
87     {
88         $tags = [
89             factory(Tag::class)->make(['name' => 'category', 'value' => 'buckets']),
90             factory(Tag::class)->make(['name' => 'color', 'value' => 'red']),
91         ];
92
93         $page = $this->getEntityWithTags(Page::class, $tags);
94         $resp = $this->asEditor()->get('/search?term=[category]');
95         $resp->assertSee($page->name);
96         $resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'category');
97         $resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'buckets');
98         $resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'color');
99         $resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'red');
100     }
101 }