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