6 use BookStack\Services\PermissionService;
8 class TagTest extends BrowserKitTest
11 protected $defaultTagCount = 20;
14 * Get an instance of a page that has many tags.
15 * @param Tag[]|bool $tags
18 protected function getPageWithTags($tags = false)
20 $page = Page::first();
23 $tags = factory(Tag::class, $this->defaultTagCount)->make();
26 $page->tags()->saveMany($tags);
30 public function test_get_page_tags()
32 $page = $this->getPageWithTags();
34 // Add some other tags to check they don't interfere
35 factory(Tag::class, $this->defaultTagCount)->create();
37 $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
40 $json = json_decode($this->response->getContent());
41 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
44 public function test_tag_name_suggestions()
46 // Create some tags with similar names to test with
48 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
49 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
50 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
51 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
52 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
53 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
54 $page = $this->getPageWithTags($attrs);
56 $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->seeJsonEquals([]);
57 $this->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country', 'county']);
58 $this->get('/ajax/tags/suggest/names?search=cou')->seeJsonEquals(['country', 'county']);
59 $this->get('/ajax/tags/suggest/names?search=pla')->seeJsonEquals(['planet', 'plans']);
62 public function test_tag_value_suggestions()
64 // Create some tags with similar values to test with
66 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats']));
67 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery']));
68 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle']));
69 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog']));
70 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult']));
71 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy']));
72 $page = $this->getPageWithTags($attrs);
74 $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->seeJsonEquals([]);
75 $this->get('/ajax/tags/suggest/values?search=cat')->seeJsonEquals(['cats', 'cattery', 'catapult']);
76 $this->get('/ajax/tags/suggest/values?search=do')->seeJsonEquals(['dog', 'dodgy']);
77 $this->get('/ajax/tags/suggest/values?search=cas')->seeJsonEquals(['castle']);
80 public function test_entity_permissions_effect_tag_suggestions()
82 $permissionService = $this->app->make(PermissionService::class);
84 // Create some tags with similar names to test with and save to a page
86 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
87 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
88 $page = $this->getPageWithTags($attrs);
90 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
91 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
93 // Set restricted permission the page
94 $page->restricted = true;
96 $permissionService->buildJointPermissionsForEntity($page);
98 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
99 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);