1 <?php namespace Tests\Entity;
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 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_get_page_tags()
35 $page = $this->getEntityWithTags(Page::class);
37 // Add some other tags to check they don't interfere
38 factory(Tag::class, $this->defaultTagCount)->create();
40 $this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
43 $json = json_decode($this->response->getContent());
44 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
47 public function test_get_chapter_tags()
49 $chapter = $this->getEntityWithTags(Chapter::class);
51 // Add some other tags to check they don't interfere
52 factory(Tag::class, $this->defaultTagCount)->create();
54 $this->asAdmin()->get("/ajax/tags/get/chapter/" . $chapter->id)
57 $json = json_decode($this->response->getContent());
58 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
61 public function test_get_book_tags()
63 $book = $this->getEntityWithTags(Book::class);
65 // Add some other tags to check they don't interfere
66 factory(Tag::class, $this->defaultTagCount)->create();
68 $this->asAdmin()->get("/ajax/tags/get/book/" . $book->id)
71 $json = json_decode($this->response->getContent());
72 $this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
75 public function test_tag_name_suggestions()
77 // Create some tags with similar names to test with
79 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
80 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
81 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
82 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
83 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
84 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
85 $page = $this->getEntityWithTags(Page::class, $attrs);
87 $this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->seeJsonEquals([]);
88 $this->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country', 'county']);
89 $this->get('/ajax/tags/suggest/names?search=cou')->seeJsonEquals(['country', 'county']);
90 $this->get('/ajax/tags/suggest/names?search=pla')->seeJsonEquals(['planet', 'plans']);
93 public function test_tag_value_suggestions()
95 // Create some tags with similar values to test with
97 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country', 'value' => 'cats']));
98 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color', 'value' => 'cattery']));
99 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city', 'value' => 'castle']));
100 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county', 'value' => 'dog']));
101 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet', 'value' => 'catapult']));
102 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans', 'value' => 'dodgy']));
103 $page = $this->getEntityWithTags(Page::class, $attrs);
105 $this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->seeJsonEquals([]);
106 $this->get('/ajax/tags/suggest/values?search=cat')->seeJsonEquals(['cats', 'cattery', 'catapult']);
107 $this->get('/ajax/tags/suggest/values?search=do')->seeJsonEquals(['dog', 'dodgy']);
108 $this->get('/ajax/tags/suggest/values?search=cas')->seeJsonEquals(['castle']);
111 public function test_entity_permissions_effect_tag_suggestions()
113 $permissionService = $this->app->make(PermissionService::class);
115 // Create some tags with similar names to test with and save to a page
117 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
118 $attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
119 $page = $this->getEntityWithTags(Page::class, $attrs);
121 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
122 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
124 // Set restricted permission the page
125 $page->restricted = true;
127 $page->rebuildPermissions();
129 $this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals(['color', 'country']);
130 $this->asEditor()->get('/ajax/tags/suggest/names?search=co')->seeJsonEquals([]);