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