3 namespace Tests\Entity;
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
10 class TagTest extends TestCase
12 protected $defaultTagCount = 20;
15 * Get an instance of a page that has many tags.
17 protected function getEntityWithTags($class, ?array $tags = null): Entity
19 $entity = $class::first();
22 $tags = factory(Tag::class, $this->defaultTagCount)->make();
25 $entity->tags()->saveMany($tags);
30 public function test_tag_name_suggestions()
32 // Create some tags with similar names to test with
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());
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']);
48 public function test_tag_value_suggestions()
50 // Create some tags with similar values to test with
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());
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']);
66 public function test_entity_permissions_effect_tag_suggestions()
68 // Create some tags with similar names to test with and save to a page
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());
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']);
77 // Set restricted permission the page
78 $page->restricted = true;
80 $page->rebuildPermissions();
82 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertExactJson(['color', 'country']);
83 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertExactJson([]);
86 public function test_tags_shown_on_search_listing()
89 factory(Tag::class)->make(['name' => 'category', 'value' => 'buckets']),
90 factory(Tag::class)->make(['name' => 'color', 'value' => 'red']),
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');