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