5 use BookStack\Services\PermissionService;
7 class TagTest extends BrowserKitTest
10 protected $defaultTagCount = 20;
13 * Get an instance of a page that has many tags.
14 * @param Tag[]|bool $tags
17 protected function getPageWithTags($tags = false)
19 $page = Page::first();
22 $tags = factory(Tag::class, $this->defaultTagCount)->make();
25 $page->tags()->saveMany($tags);
29 public function test_get_page_tags()
31 $page = $this->getPageWithTags();
33 // Add some other tags to check they don't interfere
34 factory(Tag::class, $this->defaultTagCount)->create();
36 $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
39 $json = json_decode($this->response->getContent());
40 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
43 public function test_tag_name_suggestions()
45 // Create some tags with similar names to test with
47 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
48 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
49 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
50 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
51 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
52 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
53 $page = $this->getPageWithTags($attrs);
55 $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->seeJsonEquals([]);
56 $this->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country', 'county']);
57 $this->get('/ajax/tags/suggest/names?search=cou')->seeJsonEquals(['country', 'county']);
58 $this->get('/ajax/tags/suggest/names?search=pla')->seeJsonEquals(['planet', 'plans']);
61 public function test_tag_value_suggestions()
63 // Create some tags with similar values to test with
65 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats']));
66 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery']));
67 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle']));
68 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog']));
69 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult']));
70 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy']));
71 $page = $this->getPageWithTags($attrs);
73 $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->seeJsonEquals([]);
74 $this->get('/ajax/tags/suggest/values?search=cat')->seeJsonEquals(['cats', 'cattery', 'catapult']);
75 $this->get('/ajax/tags/suggest/values?search=do')->seeJsonEquals(['dog', 'dodgy']);
76 $this->get('/ajax/tags/suggest/values?search=cas')->seeJsonEquals(['castle']);
79 public function test_entity_permissions_effect_tag_suggestions()
81 $permissionService = $this->app->make(PermissionService::class);
83 // Create some tags with similar names to test with and save to a page
85 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
86 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
87 $page = $this->getPageWithTags($attrs);
89 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
90 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
92 // Set restricted permission the page
93 $page->restricted = true;
95 $permissionService->buildJointPermissionsForEntity($page);
97 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
98 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);