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;
9 class TagTest extends BrowserKitTest
12 protected $defaultTagCount = 20;
15 * Get an instance of a page that has many tags.
16 * @param \BookStack\Actions\Tag[]|bool $tags
19 protected function getEntityWithTags($class, $tags = false)
21 $entity = $class::first();
24 $tags = factory(Tag::class, $this->defaultTagCount)->make();
27 $entity->tags()->saveMany($tags);
31 public function test_get_page_tags()
33 $page = $this->getEntityWithTags(Page::class);
35 // Add some other tags to check they don't interfere
36 factory(Tag::class, $this->defaultTagCount)->create();
38 $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
41 $json = json_decode($this->response->getContent());
42 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
45 public function test_get_chapter_tags()
47 $chapter = $this->getEntityWithTags(Chapter::class);
49 // Add some other tags to check they don't interfere
50 factory(Tag::class, $this->defaultTagCount)->create();
52 $this->asAdmin()->get("/ajax/tags/get/chapter/" . $chapter->id)
55 $json = json_decode($this->response->getContent());
56 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
59 public function test_get_book_tags()
61 $book = $this->getEntityWithTags(Book::class);
63 // Add some other tags to check they don't interfere
64 factory(Tag::class, $this->defaultTagCount)->create();
66 $this->asAdmin()->get("/ajax/tags/get/book/" . $book->id)
69 $json = json_decode($this->response->getContent());
70 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
73 public function test_tag_name_suggestions()
75 // Create some tags with similar names to test with
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);
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']);
91 public function test_tag_value_suggestions()
93 // Create some tags with similar values to test with
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);
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']);
109 public function test_entity_permissions_effect_tag_suggestions()
111 $permissionService = $this->app->make(PermissionService::class);
113 // Create some tags with similar names to test with and save to a page
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);
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']);
122 // Set restricted permission the page
123 $page->restricted = true;
125 $permissionService->buildJointPermissionsForEntity($page);
127 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
128 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);