]> BookStack Code Mirror - bookstack/blob - tests/Entity/TagTest.php
Fix ajax tag suggestion for subdir installs
[bookstack] / tests / Entity / TagTest.php
1 <?php namespace Tests;
2
3 use BookStack\Role;
4 use BookStack\Tag;
5 use BookStack\Page;
6 use BookStack\Services\PermissionService;
7
8 class TagTest extends BrowserKitTest
9 {
10
11     protected $defaultTagCount = 20;
12
13     /**
14      * Get an instance of a page that has many tags.
15      * @param Tag[]|bool $tags
16      * @return mixed
17      */
18     protected function getPageWithTags($tags = false)
19     {
20         $page = Page::first();
21
22         if (!$tags) {
23             $tags = factory(Tag::class, $this->defaultTagCount)->make();
24         }
25
26         $page->tags()->saveMany($tags);
27         return $page;
28     }
29
30     public function test_get_page_tags()
31     {
32         $page = $this->getPageWithTags();
33
34         // Add some other tags to check they don't interfere
35         factory(Tag::class, $this->defaultTagCount)->create();
36
37         $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
38             ->shouldReturnJson();
39
40         $json = json_decode($this->response->getContent());
41         $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
42     }
43
44     public function test_tag_name_suggestions()
45     {
46         // Create some tags with similar names to test with
47         $attrs = collect();
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);
55
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']);
60     }
61
62     public function test_tag_value_suggestions()
63     {
64         // Create some tags with similar values to test with
65         $attrs = collect();
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);
73
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']);
78     }
79
80     public function test_entity_permissions_effect_tag_suggestions()
81     {
82         $permissionService = $this->app->make(PermissionService::class);
83
84         // Create some tags with similar names to test with and save to a page
85         $attrs = collect();
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);
89
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']);
92
93         // Set restricted permission the page
94         $page->restricted = true;
95         $page->save();
96         $permissionService->buildJointPermissionsForEntity($page);
97
98         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
99         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);
100     }
101
102 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.