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