2021-06-26 15:23:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Entity;
|
2016-05-13 20:20:21 +00:00
|
|
|
|
2018-09-25 11:30:50 +00:00
|
|
|
use BookStack\Actions\Tag;
|
2020-11-22 00:17:45 +00:00
|
|
|
use BookStack\Entities\Models\Entity;
|
|
|
|
use BookStack\Entities\Models\Page;
|
2021-05-24 15:12:09 +00:00
|
|
|
use Tests\TestCase;
|
2016-05-13 20:20:21 +00:00
|
|
|
|
2021-05-24 15:12:09 +00:00
|
|
|
class TagTest extends TestCase
|
2016-05-13 20:20:21 +00:00
|
|
|
{
|
|
|
|
protected $defaultTagCount = 20;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get an instance of a page that has many tags.
|
|
|
|
*/
|
2021-05-24 15:12:09 +00:00
|
|
|
protected function getEntityWithTags($class, ?array $tags = null): Entity
|
2016-05-13 20:20:21 +00:00
|
|
|
{
|
2018-03-30 13:09:51 +00:00
|
|
|
$entity = $class::first();
|
2016-05-13 20:20:21 +00:00
|
|
|
|
2021-05-24 15:12:09 +00:00
|
|
|
if (is_null($tags)) {
|
2021-10-30 20:29:59 +00:00
|
|
|
$tags = Tag::factory()->count($this->defaultTagCount)->make();
|
2016-05-13 20:20:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 13:09:51 +00:00
|
|
|
$entity->tags()->saveMany($tags);
|
2021-06-26 15:23:15 +00:00
|
|
|
|
2018-03-30 13:09:51 +00:00
|
|
|
return $entity;
|
2016-05-13 20:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_tag_name_suggestions()
|
|
|
|
{
|
|
|
|
// Create some tags with similar names to test with
|
|
|
|
$attrs = collect();
|
2021-10-30 20:29:59 +00:00
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'country']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'color']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'city']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'county']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'planet']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'plans']));
|
2021-05-24 15:16:58 +00:00
|
|
|
$page = $this->getEntityWithTags(Page::class, $attrs->all());
|
2016-05-13 20:20:21 +00:00
|
|
|
|
2021-10-30 20:29:59 +00:00
|
|
|
$this->asAdmin()->get('/ajax/tags/suggest/names?search=dog')->assertSimilarJson([]);
|
|
|
|
$this->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country', 'county']);
|
|
|
|
$this->get('/ajax/tags/suggest/names?search=cou')->assertSimilarJson(['country', 'county']);
|
|
|
|
$this->get('/ajax/tags/suggest/names?search=pla')->assertSimilarJson(['planet', 'plans']);
|
2016-05-15 19:12:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_tag_value_suggestions()
|
|
|
|
{
|
|
|
|
// Create some tags with similar values to test with
|
|
|
|
$attrs = collect();
|
2021-10-30 20:29:59 +00:00
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'country', 'value' => 'cats']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'color', 'value' => 'cattery']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'city', 'value' => 'castle']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'county', 'value' => 'dog']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'planet', 'value' => 'catapult']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'plans', 'value' => 'dodgy']));
|
2021-05-24 15:16:58 +00:00
|
|
|
$page = $this->getEntityWithTags(Page::class, $attrs->all());
|
2016-05-15 19:12:53 +00:00
|
|
|
|
2021-10-30 20:29:59 +00:00
|
|
|
$this->asAdmin()->get('/ajax/tags/suggest/values?search=ora')->assertSimilarJson([]);
|
|
|
|
$this->get('/ajax/tags/suggest/values?search=cat')->assertSimilarJson(['cats', 'cattery', 'catapult']);
|
|
|
|
$this->get('/ajax/tags/suggest/values?search=do')->assertSimilarJson(['dog', 'dodgy']);
|
|
|
|
$this->get('/ajax/tags/suggest/values?search=cas')->assertSimilarJson(['castle']);
|
2016-05-13 20:20:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function test_entity_permissions_effect_tag_suggestions()
|
|
|
|
{
|
|
|
|
// Create some tags with similar names to test with and save to a page
|
|
|
|
$attrs = collect();
|
2021-10-30 20:29:59 +00:00
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'country']));
|
|
|
|
$attrs = $attrs->merge(Tag::factory()->count(5)->make(['name' => 'color']));
|
2021-05-24 15:16:58 +00:00
|
|
|
$page = $this->getEntityWithTags(Page::class, $attrs->all());
|
2016-05-13 20:20:21 +00:00
|
|
|
|
2021-10-30 20:29:59 +00:00
|
|
|
$this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country']);
|
|
|
|
$this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country']);
|
2016-05-13 20:20:21 +00:00
|
|
|
|
|
|
|
// Set restricted permission the page
|
|
|
|
$page->restricted = true;
|
|
|
|
$page->save();
|
2019-09-19 23:18:28 +00:00
|
|
|
$page->rebuildPermissions();
|
2016-05-13 20:20:21 +00:00
|
|
|
|
2021-10-30 20:29:59 +00:00
|
|
|
$this->asAdmin()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson(['color', 'country']);
|
|
|
|
$this->asEditor()->get('/ajax/tags/suggest/names?search=co')->assertSimilarJson([]);
|
2016-05-13 20:20:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 15:12:09 +00:00
|
|
|
public function test_tags_shown_on_search_listing()
|
|
|
|
{
|
|
|
|
$tags = [
|
2021-10-30 20:29:59 +00:00
|
|
|
Tag::factory()->make(['name' => 'category', 'value' => 'buckets']),
|
|
|
|
Tag::factory()->make(['name' => 'color', 'value' => 'red']),
|
2021-05-24 15:12:09 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
$page = $this->getEntityWithTags(Page::class, $tags);
|
2021-06-26 15:23:15 +00:00
|
|
|
$resp = $this->asEditor()->get('/search?term=[category]');
|
2021-05-24 15:12:09 +00:00
|
|
|
$resp->assertSee($page->name);
|
|
|
|
$resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'category');
|
|
|
|
$resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'buckets');
|
|
|
|
$resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'color');
|
|
|
|
$resp->assertElementContains('[href="' . $page->getUrl() . '"]', 'red');
|
|
|
|
}
|
2016-05-13 20:20:21 +00:00
|
|
|
}
|