1 <?php namespace Entity;
3 use BookStack\Attribute;
5 use BookStack\Services\PermissionService;
7 class AttributeTests extends \TestCase
10 protected $defaultAttrCount = 20;
13 * Get an instance of a page that has many attributes.
14 * @param Attribute[]|bool $attributes
17 protected function getPageWithAttributes($attributes = false)
19 $page = Page::first();
22 $attributes = factory(Attribute::class, $this->defaultAttrCount)->make();
25 $page->attributes()->saveMany($attributes);
29 public function test_get_page_attributes()
31 $page = $this->getPageWithAttributes();
33 // Add some other attributes to check they don't interfere
34 factory(Attribute::class, $this->defaultAttrCount)->create();
36 $this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id)
39 $json = json_decode($this->response->getContent());
40 $this->assertTrue(count($json) === $this->defaultAttrCount, "Returned JSON item count is not as expected");
43 public function test_attribute_name_suggestions()
45 // Create some attributes with similar names to test with
47 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
48 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
49 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'city']));
50 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'county']));
51 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'planet']));
52 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'plans']));
53 $page = $this->getPageWithAttributes($attrs);
55 $this->asAdmin()->get('/ajax/attributes/suggest?search=dog')->seeJsonEquals([]);
56 $this->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country', 'county']);
57 $this->get('/ajax/attributes/suggest?search=cou')->seeJsonEquals(['country', 'county']);
58 $this->get('/ajax/attributes/suggest?search=pla')->seeJsonEquals(['planet', 'plans']);
61 public function test_entity_permissions_effect_attribute_suggestions()
63 $permissionService = $this->app->make(PermissionService::class);
65 // Create some attributes with similar names to test with and save to a page
67 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
68 $attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
69 $page = $this->getPageWithAttributes($attrs);
71 $this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
72 $this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
74 // Set restricted permission the page
75 $page->restricted = true;
77 $permissionService->buildJointPermissionsForEntity($page);
79 $this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
80 $this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals([]);
83 public function test_entity_attribute_updating()
85 $page = $this->getPageWithAttributes();
88 ['name' => 'color', 'value' => 'red'],
89 ['name' => 'color', 'value' => ' blue '],
90 ['name' => 'city', 'value' => 'London '],
91 ['name' => 'country', 'value' => ' England'],
93 $testResponseJsonData = [
94 ['name' => 'color', 'value' => 'red'],
95 ['name' => 'color', 'value' => 'blue'],
96 ['name' => 'city', 'value' => 'London'],
97 ['name' => 'country', 'value' => 'England'],
101 $this->asAdmin()->json("POST", "/ajax/attributes/update/page/" . $page->id, ['attributes' => $testJsonData]);
102 $updateData = json_decode($this->response->getContent());
103 // Check data is correct
104 $testDataCorrect = true;
105 foreach ($updateData->attributes as $data) {
106 $testItem = ['name' => $data->name, 'value' => $data->value];
107 if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
109 $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
110 $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
111 $this->assertTrue(isset($updateData->message), "No message returned in attribute update response");
114 $this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id);
115 $getResponseData = json_decode($this->response->getContent());
117 $this->assertTrue(count($getResponseData) === count($testJsonData), "The received attribute count is incorrect");
118 // Check data is correct
119 $testDataCorrect = true;
120 foreach ($getResponseData as $data) {
121 $testItem = ['name' => $data->name, 'value' => $data->value];
122 if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
124 $testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
125 $this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));