]> BookStack Code Mirror - bookstack/blob - tests/Entity/TagTest.php
fix image delete confirm text
[bookstack] / tests / Entity / TagTest.php
1 <?php namespace Tests\Entity;
2
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Models\Chapter;
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\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_tag_name_suggestions()
34     {
35         // Create some tags with similar names to test with
36         $attrs = collect();
37         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
38         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
39         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
40         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
41         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
42         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
43         $page = $this->getEntityWithTags(Page::class, $attrs);
44
45         $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->seeJsonEquals([]);
46         $this->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country', 'county']);
47         $this->get('/ajax/tags/suggest/names?search=cou')->seeJsonEquals(['country', 'county']);
48         $this->get('/ajax/tags/suggest/names?search=pla')->seeJsonEquals(['planet', 'plans']);
49     }
50
51     public function test_tag_value_suggestions()
52     {
53         // Create some tags with similar values to test with
54         $attrs = collect();
55         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats']));
56         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery']));
57         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle']));
58         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog']));
59         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult']));
60         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy']));
61         $page = $this->getEntityWithTags(Page::class, $attrs);
62
63         $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->seeJsonEquals([]);
64         $this->get('/ajax/tags/suggest/values?search=cat')->seeJsonEquals(['cats', 'cattery', 'catapult']);
65         $this->get('/ajax/tags/suggest/values?search=do')->seeJsonEquals(['dog', 'dodgy']);
66         $this->get('/ajax/tags/suggest/values?search=cas')->seeJsonEquals(['castle']);
67     }
68
69     public function test_entity_permissions_effect_tag_suggestions()
70     {
71         $permissionService = $this->app->make(PermissionService::class);
72
73         // Create some tags with similar names to test with and save to a page
74         $attrs = collect();
75         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
76         $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
77         $page = $this->getEntityWithTags(Page::class, $attrs);
78
79         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
80         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
81
82         // Set restricted permission the page
83         $page->restricted = true;
84         $page->save();
85         $page->rebuildPermissions();
86
87         $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
88         $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);
89     }
90
91 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.