1 <?php namespace Tests\Entity;
3 use BookStack\Actions\Tag;
4 use BookStack\Entities\Models\Entity;
5 use BookStack\Entities\Models\Page;
8 class TagTest extends TestCase
11 protected $defaultTagCount = 20;
14 * Get an instance of a page that has many tags.
16 protected function getEntityWithTags($class, ?array $tags = null): Entity
18 $entity = $class::first();
21 $tags = factory(Tag::class, $this->defaultTagCount)->make();
24 $entity->tags()->saveMany($tags);
28 public function test_tag_name_suggestions()
30 // Create some tags with similar names to test with
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());
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']);
46 public function test_tag_value_suggestions()
48 // Create some tags with similar values to test with
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());
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']);
64 public function test_entity_permissions_effect_tag_suggestions()
66 // Create some tags with similar names to test with and save to a page
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());
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']);
75 // Set restricted permission the page
76 $page->restricted = true;
78 $page->rebuildPermissions();
80 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertExactJson(['color', 'country']);
81 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertExactJson([]);
84 public function test_tags_shown_on_search_listing()
87 factory(Tag::class)->make(['name' => 'category', 'value' => 'buckets']),
88 factory(Tag::class)->make(['name' => 'color', 'value' => 'red']),
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');