1 <?php namespace Tests\Entity;
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;
11 class TagTest extends BrowserKitTest
14 protected $defaultTagCount = 20;
17 * Get an instance of a page that has many tags.
18 * @param \BookStack\Actions\Tag[]|bool $tags
21 protected function getEntityWithTags($class, $tags = false): Entity
23 $entity = $class::first();
26 $tags = factory(Tag::class, $this->defaultTagCount)->make();
29 $entity->tags()->saveMany($tags);
33 public function test_tag_name_suggestions()
35 // Create some tags with similar names to test with
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);
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']);
51 public function test_tag_value_suggestions()
53 // Create some tags with similar values to test with
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);
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']);
69 public function test_entity_permissions_effect_tag_suggestions()
71 $permissionService = $this->app->make(PermissionService::class);
73 // Create some tags with similar names to test with and save to a page
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);
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']);
82 // Set restricted permission the page
83 $page->restricted = true;
85 $page->rebuildPermissions();
87 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
88 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);