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;
10 class TagTest extends BrowserKitTest
13 protected $defaultTagCount = 20;
16 * Get an instance of a page that has many tags.
17 * @param \BookStack\Actions\Tag[]|bool $tags
20 protected function getEntityWithTags($class, $tags = false): Entity
22 $entity = $class::first();
25 $tags = factory(Tag::class, $this->defaultTagCount)->make();
28 $entity->tags()->saveMany($tags);
32 public function test_get_page_tags()
34 $page = $this->getEntityWithTags(Page::class);
36 // Add some other tags to check they don't interfere
37 factory(Tag::class, $this->defaultTagCount)->create();
39 $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
42 $json = json_decode($this->response->getContent());
43 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
46 public function test_get_chapter_tags()
48 $chapter = $this->getEntityWithTags(Chapter::class);
50 // Add some other tags to check they don't interfere
51 factory(Tag::class, $this->defaultTagCount)->create();
53 $this->asAdmin()->get("/ajax/tags/get/chapter/" . $chapter->id)
56 $json = json_decode($this->response->getContent());
57 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
60 public function test_get_book_tags()
62 $book = $this->getEntityWithTags(Book::class);
64 // Add some other tags to check they don't interfere
65 factory(Tag::class, $this->defaultTagCount)->create();
67 $this->asAdmin()->get("/ajax/tags/get/book/" . $book->id)
70 $json = json_decode($this->response->getContent());
71 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
74 public function test_tag_name_suggestions()
76 // Create some tags with similar names to test with
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);
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']);
92 public function test_tag_value_suggestions()
94 // Create some tags with similar values to test with
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);
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']);
110 public function test_entity_permissions_effect_tag_suggestions()
112 $permissionService = $this->app->make(PermissionService::class);
114 // Create some tags with similar names to test with and save to a page
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);
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']);
123 // Set restricted permission the page
124 $page->restricted = true;
126 $page->rebuildPermissions();
128 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
129 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);