]> BookStack Code Mirror - bookstack/blob - tests/Entity/AttributeTests.php
8087660054f38f59c8fbc7c84fd3a4ac65c93371
[bookstack] / tests / Entity / AttributeTests.php
1 <?php namespace Entity;
2
3 use BookStack\Attribute;
4 use BookStack\Page;
5 use BookStack\Services\PermissionService;
6
7 class AttributeTests extends \TestCase
8 {
9
10     protected $defaultAttrCount = 20;
11
12     /**
13      * Get an instance of a page that has many attributes.
14      * @param Attribute[]|bool $attributes
15      * @return mixed
16      */
17     protected function getPageWithAttributes($attributes = false)
18     {
19         $page = Page::first();
20
21         if (!$attributes) {
22             $attributes = factory(Attribute::class, $this->defaultAttrCount)->make();
23         }
24
25         $page->attributes()->saveMany($attributes);
26         return $page;
27     }
28
29     public function test_get_page_attributes()
30     {
31         $page = $this->getPageWithAttributes();
32
33         // Add some other attributes to check they don't interfere
34         factory(Attribute::class, $this->defaultAttrCount)->create();
35
36         $this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id)
37             ->shouldReturnJson();
38
39         $json = json_decode($this->response->getContent());
40         $this->assertTrue(count($json) === $this->defaultAttrCount, "Returned JSON item count is not as expected");
41     }
42
43     public function test_attribute_name_suggestions()
44     {
45         // Create some attributes with similar names to test with
46         $attrs = collect();
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);
54
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']);
59     }
60
61     public function test_entity_permissions_effect_attribute_suggestions()
62     {
63         $permissionService = $this->app->make(PermissionService::class);
64
65         // Create some attributes with similar names to test with and save to a page
66         $attrs = collect();
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);
70
71         $this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
72         $this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
73
74         // Set restricted permission the page
75         $page->restricted = true;
76         $page->save();
77         $permissionService->buildJointPermissionsForEntity($page);
78
79         $this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
80         $this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals([]);
81     }
82
83     public function test_entity_attribute_updating()
84     {
85         $page = $this->getPageWithAttributes();
86
87         $testJsonData = [
88             ['name' => 'color', 'value' => 'red'],
89             ['name' => 'color', 'value' => ' blue '],
90             ['name' => 'city', 'value' => 'London '],
91             ['name' => 'country', 'value' => ' England'],
92         ];
93         $testResponseJsonData = [
94             ['name' => 'color', 'value' => 'red'],
95             ['name' => 'color', 'value' => 'blue'],
96             ['name' => 'city', 'value' => 'London'],
97             ['name' => 'country', 'value' => 'England'],
98         ];
99
100         // Do update request
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;
108         }
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");
112
113         // Do get request
114         $this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id);
115         $getResponseData = json_decode($this->response->getContent());
116         // Check counts
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;
123         }
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)));
126     }
127
128 }