]> BookStack Code Mirror - bookstack/blob - tests/Entity/TagTest.php
Update passwords.php
[bookstack] / tests / Entity / TagTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Page;
7 use BookStack\Auth\Permissions\PermissionService;
8
9 class TagTest extends BrowserKitTest
10 {
11
12     protected $defaultTagCount = 20;
13
14     /**
15      * Get an instance of a page that has many tags.
16      * @param \BookStack\Actions\Tag[]|bool $tags
17      * @return mixed
18      */
19     protected function getEntityWithTags($class, $tags = false)
20     {
21         $entity = $class::first();
22
23         if (!$tags) {
24             $tags = factory(Tag::class, $this->defaultTagCount)->make();
25         }
26
27         $entity->tags()->saveMany($tags);
28         return $entity;
29     }
30
31     public function test_get_page_tags()
32     {
33         $page = $this->getEntityWithTags(Page::class);
34
35         // Add some other tags to check they don't interfere
36         factory(Tag::class, $this->defaultTagCount)->create();
37
38         $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
39             ->shouldReturnJson();
40
41         $json = json_decode($this->response->getContent());
42         $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
43     }
44
45     public function test_get_chapter_tags()
46     {
47         $chapter = $this->getEntityWithTags(Chapter::class);
48
49         // Add some other tags to check they don't interfere
50         factory(Tag::class, $this->defaultTagCount)->create();
51
52         $this->asAdmin()->get("/ajax/tags/get/chapter/" . $chapter->id)
53             ->shouldReturnJson();
54
55         $json = json_decode($this->response->getContent());
56         $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
57     }
58
59     public function test_get_book_tags()
60     {
61         $book = $this->getEntityWithTags(Book::class);
62
63         // Add some other tags to check they don't interfere
64         factory(Tag::class, $this->defaultTagCount)->create();
65
66         $this->asAdmin()->get("/ajax/tags/get/book/" . $book->id)
67             ->shouldReturnJson();
68
69         $json = json_decode($this->response->getContent());
70         $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
71     }
72
73     public function test_tag_name_suggestions()
74     {
75         // Create some tags with similar names to test with
76         $attrs = collect();
77         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
78         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
79         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
80         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
81         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
82         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
83         $page = $this->getEntityWithTags(Page::class, $attrs);
84
85         $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->seeJsonEquals([]);
86         $this->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country', 'county']);
87         $this->get('/ajax/tags/suggest/names?search=cou')->seeJsonEquals(['country', 'county']);
88         $this->get('/ajax/tags/suggest/names?search=pla')->seeJsonEquals(['planet', 'plans']);
89     }
90
91     public function test_tag_value_suggestions()
92     {
93         // Create some tags with similar values to test with
94         $attrs = collect();
95         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats']));
96         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery']));
97         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle']));
98         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog']));
99         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult']));
100         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy']));
101         $page = $this->getEntityWithTags(Page::class, $attrs);
102
103         $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->seeJsonEquals([]);
104         $this->get('/ajax/tags/suggest/values?search=cat')->seeJsonEquals(['cats', 'cattery', 'catapult']);
105         $this->get('/ajax/tags/suggest/values?search=do')->seeJsonEquals(['dog', 'dodgy']);
106         $this->get('/ajax/tags/suggest/values?search=cas')->seeJsonEquals(['castle']);
107     }
108
109     public function test_entity_permissions_effect_tag_suggestions()
110     {
111         $permissionService = $this->app->make(PermissionService::class);
112
113         // Create some tags with similar names to test with and save to a page
114         $attrs = collect();
115         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
116         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
117         $page = $this->getEntityWithTags(Page::class, $attrs);
118
119         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
120         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
121
122         // Set restricted permission the page
123         $page->restricted = true;
124         $page->save();
125         $permissionService->buildJointPermissionsForEntity($page);
126
127         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
128         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);
129     }
130
131 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.