1 <?php namespace Entity;
5 use BookStack\Services\PermissionService;
7 class TagTests extends \TestCase
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?search=co')->seeJsonEquals(['color', 'country']);
90 $this->asEditor()->get('/ajax/tags/suggest?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?search=co')->seeJsonEquals(['color', 'country']);
98 $this->asEditor()->get('/ajax/tags/suggest?search=co')->seeJsonEquals([]);
101 public function test_entity_tag_updating()
103 $page = $this->getPageWithTags();
106 ['name' => 'color', 'value' => 'red'],
107 ['name' => 'color', 'value' => ' blue '],
108 ['name' => 'city', 'value' => 'London '],
109 ['name' => 'country', 'value' => ' England'],
111 $testResponseJsonData = [
112 ['name' => 'color', 'value' => 'red'],
113 ['name' => 'color', 'value' => 'blue'],
114 ['name' => 'city', 'value' => 'London'],
115 ['name' => 'country', 'value' => 'England'],
119 $this->asAdmin()->json("POST", "/ajax/tags/update/page/" . $page->id, ['tags' => $testJsonData]);
120 $updateData = json_decode($this->response->getContent());
121 // Check data is correct
122 $testDataCorrect = true;
123 foreach ($updateData->tags as $data) {
124 $testItem = ['name' => $data->name, 'value' => $data->value];
125 if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
127 $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
128 $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
129 $this->assertTrue(isset($updateData->message), "No message returned in tag update response");
132 $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id);
133 $getResponseData = json_decode($this->response->getContent());
135 $this->assertTrue(count($getResponseData) === count($testJsonData), "The received tag count is incorrect");
136 // Check data is correct
137 $testDataCorrect = true;
138 foreach ($getResponseData as $data) {
139 $testItem = ['name' => $data->name, 'value' => $data->value];
140 if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
142 $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
143 $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));