Renamed attribute to tags & continued interface

Also fixed page create route broken in last commit
This commit is contained in:
Dan Brown 2016-05-13 21:20:21 +01:00
parent 1fa079b466
commit b80184cd93
18 changed files with 369 additions and 282 deletions

View File

@ -55,12 +55,12 @@ class Entity extends Ownable
}
/**
* Get the Attribute models that have been user assigned to this entity.
* Get the Tag models that have been user assigned to this entity.
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function attributes()
public function tags()
{
return $this->morphMany(Attribute::class, 'entity');
return $this->morphMany(Tag::class, 'entity');
}
/**

View File

@ -1,64 +0,0 @@
<?php namespace BookStack\Http\Controllers;
use BookStack\Repos\AttributeRepo;
use Illuminate\Http\Request;
use BookStack\Http\Requests;
class AttributeController extends Controller
{
protected $attributeRepo;
/**
* AttributeController constructor.
* @param $attributeRepo
*/
public function __construct(AttributeRepo $attributeRepo)
{
$this->attributeRepo = $attributeRepo;
}
/**
* Get all the Attributes for a particular entity
* @param $entityType
* @param $entityId
*/
public function getForEntity($entityType, $entityId)
{
$attributes = $this->attributeRepo->getForEntity($entityType, $entityId);
return response()->json($attributes);
}
/**
* Update the attributes for a particular entity.
* @param $entityType
* @param $entityId
* @param Request $request
* @return mixed
*/
public function updateForEntity($entityType, $entityId, Request $request)
{
$entity = $this->attributeRepo->getEntity($entityType, $entityId, 'update');
if ($entity === null) return $this->jsonError("Entity not found", 404);
$inputAttributes = $request->input('attributes');
$attributes = $this->attributeRepo->saveAttributesToEntity($entity, $inputAttributes);
return response()->json([
'attributes' => $attributes,
'message' => 'Attributes successfully updated'
]);
}
/**
* Get attribute name suggestions from a given search term.
* @param Request $request
*/
public function getNameSuggestions(Request $request)
{
$searchTerm = $request->get('search');
$suggestions = $this->attributeRepo->getNameSuggestions($searchTerm);
return response()->json($suggestions);
}
}

View File

@ -0,0 +1,64 @@
<?php namespace BookStack\Http\Controllers;
use BookStack\Repos\TagRepo;
use Illuminate\Http\Request;
use BookStack\Http\Requests;
class TagController extends Controller
{
protected $tagRepo;
/**
* TagController constructor.
* @param $tagRepo
*/
public function __construct(TagRepo $tagRepo)
{
$this->tagRepo = $tagRepo;
}
/**
* Get all the Tags for a particular entity
* @param $entityType
* @param $entityId
*/
public function getForEntity($entityType, $entityId)
{
$tags = $this->tagRepo->getForEntity($entityType, $entityId);
return response()->json($tags);
}
/**
* Update the tags for a particular entity.
* @param $entityType
* @param $entityId
* @param Request $request
* @return mixed
*/
public function updateForEntity($entityType, $entityId, Request $request)
{
$entity = $this->tagRepo->getEntity($entityType, $entityId, 'update');
if ($entity === null) return $this->jsonError("Entity not found", 404);
$inputTags = $request->input('tags');
$tags = $this->tagRepo->saveTagsToEntity($entity, $inputTags);
return response()->json([
'tags' => $tags,
'message' => 'Tags successfully updated'
]);
}
/**
* Get tag name suggestions from a given search term.
* @param Request $request
*/
public function getNameSuggestions(Request $request)
{
$searchTerm = $request->get('search');
$suggestions = $this->tagRepo->getNameSuggestions($searchTerm);
return response()->json($suggestions);
}
}

View File

@ -28,7 +28,7 @@ Route::group(['middleware' => 'auth'], function () {
// Pages
Route::get('/{bookSlug}/page/create', 'PageController@create');
Route::get('/{bookSlug}/draft/{pageId}', 'PageController@editDraft');
Route::post('/{bookSlug}/page/{pageId}', 'PageController@store');
Route::post('/{bookSlug}/draft/{pageId}', 'PageController@store');
Route::get('/{bookSlug}/page/{pageSlug}', 'PageController@show');
Route::get('/{bookSlug}/page/{pageSlug}/export/pdf', 'PageController@exportPdf');
Route::get('/{bookSlug}/page/{pageSlug}/export/html', 'PageController@exportHtml');
@ -85,11 +85,11 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/ajax/page/{id}', 'PageController@getPageAjax');
Route::delete('/ajax/page/{id}', 'PageController@ajaxDestroy');
// Attribute routes (AJAX)
Route::group(['prefix' => 'ajax/attributes'], function() {
Route::get('/get/{entityType}/{entityId}', 'AttributeController@getForEntity');
Route::get('/suggest', 'AttributeController@getNameSuggestions');
Route::post('/update/{entityType}/{entityId}', 'AttributeController@updateForEntity');
// Tag routes (AJAX)
Route::group(['prefix' => 'ajax/tags'], function() {
Route::get('/get/{entityType}/{entityId}', 'TagController@getForEntity');
Route::get('/suggest', 'TagController@getNameSuggestions');
Route::post('/update/{entityType}/{entityId}', 'TagController@updateForEntity');
});
// Links

View File

@ -582,7 +582,7 @@ class PageRepo extends EntityRepo
{
Activity::removeEntity($page);
$page->views()->delete();
$page->attributes()->delete();
$page->tags()->delete();
$page->revisions()->delete();
$page->permissions()->delete();
$this->permissionService->deleteJointPermissionsForEntity($page);

View File

@ -1,29 +1,29 @@
<?php namespace BookStack\Repos;
use BookStack\Attribute;
use BookStack\Tag;
use BookStack\Entity;
use BookStack\Services\PermissionService;
/**
* Class AttributeRepo
* Class TagRepo
* @package BookStack\Repos
*/
class AttributeRepo
class TagRepo
{
protected $attribute;
protected $tag;
protected $entity;
protected $permissionService;
/**
* AttributeRepo constructor.
* @param Attribute $attr
* TagRepo constructor.
* @param Tag $attr
* @param Entity $ent
* @param PermissionService $ps
*/
public function __construct(Attribute $attr, Entity $ent, PermissionService $ps)
public function __construct(Tag $attr, Entity $ent, PermissionService $ps)
{
$this->attribute = $attr;
$this->tag = $attr;
$this->entity = $ent;
$this->permissionService = $ps;
}
@ -37,13 +37,13 @@ class AttributeRepo
public function getEntity($entityType, $entityId, $action = 'view')
{
$entityInstance = $this->entity->getEntityInstance($entityType);
$searchQuery = $entityInstance->where('id', '=', $entityId)->with('attributes');
$searchQuery = $entityInstance->where('id', '=', $entityId)->with('tags');
$searchQuery = $this->permissionService->enforceEntityRestrictions($searchQuery, $action);
return $searchQuery->first();
}
/**
* Get all attributes for a particular entity.
* Get all tags for a particular entity.
* @param string $entityType
* @param int $entityId
* @return mixed
@ -53,42 +53,42 @@ class AttributeRepo
$entity = $this->getEntity($entityType, $entityId);
if ($entity === null) return collect();
return $entity->attributes;
return $entity->tags;
}
/**
* Get attribute name suggestions from scanning existing attribute names.
* Get tag name suggestions from scanning existing tag names.
* @param $searchTerm
* @return array
*/
public function getNameSuggestions($searchTerm)
{
if ($searchTerm === '') return [];
$query = $this->attribute->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'attributes', 'entity_id', 'entity_type');
$query = $this->tag->where('name', 'LIKE', $searchTerm . '%')->groupBy('name')->orderBy('name', 'desc');
$query = $this->permissionService->filterRestrictedEntityRelations($query, 'tags', 'entity_id', 'entity_type');
return $query->get(['name'])->pluck('name');
}
/**
* Save an array of attributes to an entity
* Save an array of tags to an entity
* @param Entity $entity
* @param array $attributes
* @param array $tags
* @return array|\Illuminate\Database\Eloquent\Collection
*/
public function saveAttributesToEntity(Entity $entity, $attributes = [])
public function saveTagsToEntity(Entity $entity, $tags = [])
{
$entity->attributes()->delete();
$newAttributes = [];
foreach ($attributes as $attribute) {
if (trim($attribute['name']) === '') continue;
$newAttributes[] = $this->newInstanceFromInput($attribute);
$entity->tags()->delete();
$newTags = [];
foreach ($tags as $tag) {
if (trim($tag['name']) === '') continue;
$newTags[] = $this->newInstanceFromInput($tag);
}
return $entity->attributes()->saveMany($newAttributes);
return $entity->tags()->saveMany($newTags);
}
/**
* Create a new Attribute instance from user input.
* Create a new Tag instance from user input.
* @param $input
* @return static
*/
@ -98,7 +98,7 @@ class AttributeRepo
$value = isset($input['value']) ? trim($input['value']) : '';
// Any other modification or cleanup required can go here
$values = ['name' => $name, 'value' => $value];
return $this->attribute->newInstance($values);
return $this->tag->newInstance($values);
}
}

View File

@ -4,12 +4,12 @@
* Class Attribute
* @package BookStack
*/
class Attribute extends Model
class Tag extends Model
{
protected $fillable = ['name', 'value', 'order'];
/**
* Get the entity that this attribute belongs to
* Get the entity that this tag belongs to
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function entity()

View File

@ -54,7 +54,7 @@ $factory->define(BookStack\Role::class, function ($faker) {
];
});
$factory->define(BookStack\Attribute::class, function ($faker) {
$factory->define(BookStack\Tag::class, function ($faker) {
return [
'name' => $faker->city,
'value' => $faker->sentence(3)

View File

@ -3,7 +3,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAttributesTable extends Migration
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
@ -12,7 +12,7 @@ class CreateAttributesTable extends Migration
*/
public function up()
{
Schema::create('attributes', function (Blueprint $table) {
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->integer('entity_id');
$table->string('entity_type', 100);
@ -35,6 +35,6 @@ class CreateAttributesTable extends Migration
*/
public function down()
{
Schema::drop('attributes');
Schema::drop('tags');
}
}

View File

@ -400,75 +400,75 @@ module.exports = function (ngApp, events) {
}]);
ngApp.controller('PageAttributeController', ['$scope', '$http', '$attrs',
ngApp.controller('PageTagController', ['$scope', '$http', '$attrs',
function ($scope, $http, $attrs) {
const pageId = Number($attrs.pageId);
$scope.attributes = [];
$scope.tags = [];
/**
* Push an empty attribute to the end of the scope attributes.
* Push an empty tag to the end of the scope tags.
*/
function addEmptyAttribute() {
$scope.attributes.push({
function addEmptyTag() {
$scope.tags.push({
name: '',
value: ''
});
}
/**
* Get all attributes for the current book and add into scope.
* Get all tags for the current book and add into scope.
*/
function getAttributes() {
$http.get('/ajax/attributes/get/page/' + pageId).then((responseData) => {
$scope.attributes = responseData.data;
addEmptyAttribute();
function getTags() {
$http.get('/ajax/tags/get/page/' + pageId).then((responseData) => {
$scope.tags = responseData.data;
addEmptyTag();
});
}
getAttributes();
getTags();
/**
* Set the order property on all attributes.
* Set the order property on all tags.
*/
function setAttributeOrder() {
for (let i = 0; i < $scope.attributes.length; i++) {
$scope.attributes[i].order = i;
function setTagOrder() {
for (let i = 0; i < $scope.tags.length; i++) {
$scope.tags[i].order = i;
}
}
/**
* When an attribute changes check if another empty editable
* When an tag changes check if another empty editable
* field needs to be added onto the end.
* @param attribute
* @param tag
*/
$scope.attributeChange = function(attribute) {
let cPos = $scope.attributes.indexOf(attribute);
if (cPos !== $scope.attributes.length-1) return;
$scope.tagChange = function(tag) {
let cPos = $scope.tags.indexOf(tag);
if (cPos !== $scope.tags.length-1) return;
if (attribute.name !== '' || attribute.value !== '') {
addEmptyAttribute();
if (tag.name !== '' || tag.value !== '') {
addEmptyTag();
}
};
/**
* When an attribute field loses focus check the attribute to see if its
* When an tag field loses focus check the tag to see if its
* empty and therefore could be removed from the list.
* @param attribute
* @param tag
*/
$scope.attributeBlur = function(attribute) {
let isLast = $scope.attributes.length - 1 === $scope.attributes.indexOf(attribute);
if (attribute.name === '' && attribute.value === '' && !isLast) {
let cPos = $scope.attributes.indexOf(attribute);
$scope.attributes.splice(cPos, 1);
$scope.tagBlur = function(tag) {
let isLast = $scope.tags.length - 1 === $scope.tags.indexOf(tag);
if (tag.name === '' && tag.value === '' && !isLast) {
let cPos = $scope.tags.indexOf(tag);
$scope.tags.splice(cPos, 1);
}
};
$scope.saveAttributes = function() {
setAttributeOrder();
let postData = {attributes: $scope.attributes};
$http.post('/ajax/attributes/update/page/' + pageId, postData).then((responseData) => {
$scope.attributes = responseData.data.attributes;
addEmptyAttribute();
$scope.saveTags = function() {
setTagOrder();
let postData = {tags: $scope.tags};
$http.post('/ajax/tags/update/page/' + pageId, postData).then((responseData) => {
$scope.tags = responseData.data.tags;
addEmptyTag();
events.emit('success', responseData.data.message);
})
};

View File

@ -239,6 +239,17 @@ div[editor-type="markdown"] .title-input.page-title input[type="text"] {
}
}
input.outline {
border: 0;
border-bottom: 2px solid #DDD;
border-radius: 0;
&:focus, &:active {
border: 0;
border-bottom: 2px solid #AAA;
outline: 0;
}
}
#login-form label[for="remember"] {
margin: 0;
}

View File

@ -26,6 +26,13 @@ table {
}
}
table.no-style {
td {
border: 0;
padding: 0;
}
}
table.list-table {
margin: 0 -$-xs;
td {

View File

@ -208,11 +208,70 @@ $btt-size: 40px;
background-color: #FFF;
border: 1px solid #BBB;
border-radius: 3px;
padding: $-l;
position: fixed;
right: $-xl*2;
top: 100px;
z-index: 99;
height: 800px;
width: 480px;
overflow-y: scroll;
display: flex;
align-items: stretch;
flex-direction: row;
> div {
flex: 1;
position: relative;
}
.tabs {
display: block;
border-right: 1px solid #DDD;
width: 54px;
flex: 0;
}
.tabs i {
padding: 0;
margin: 0;
}
.tabs [tab-button] {
display: block;
cursor: pointer;
color: #666;
padding: $-m;
border-bottom: 1px solid rgba(255, 255, 255, 0.3);
&.active {
color: #444;
background-color: rgba(0, 0, 0, 0.1);
}
}
div[tab-content] .padded {
padding: 0 $-m;
}
h4 {
font-size: 24px;
margin: $-m 0 0 0;
padding: 0 $-m;
}
.tags input {
max-width: 100%;
width: 100%;
min-width: 50px;
}
.tags td {
padding-right: $-s;
padding-top: $-s;
}
button.pos {
position: absolute;
bottom: 0;
display: block;
width: 100%;
padding: $-s;
border: 0;
margin: 0;
box-shadow: none;
border-radius: 0;
&:hover{
box-shadow: none;
}
}
}

View File

@ -16,17 +16,7 @@
@include('pages/form', ['model' => $page])
</form>
<div class="floating-toolbox" ng-controller="PageAttributeController" page-id="{{ $page->id or 0 }}">
<form ng-submit="saveAttributes()">
<table>
<tr ng-repeat="attribute in attributes">
<td><input type="text" ng-model="attribute.name" ng-change="attributeChange(attribute)" ng-blur="attributeBlur(attribute)" placeholder="Attribute Name"></td>
<td><input type="text" ng-model="attribute.value" ng-change="attributeChange(attribute)" ng-blur="attributeBlur(attribute)" placeholder="Value"></td>
</tr>
</table>
<button class="button pos" type="submit">Save attributes</button>
</form>
</div>
@include('pages/form-toolbox')
</div>
@include('partials/image-manager', ['imageType' => 'gallery', 'uploaded_to' => $page->id])

View File

@ -0,0 +1,20 @@
<div class="floating-toolbox">
<div class="tabs primary-background-light">
<span tab-button class="active"><i class="zmdi zmdi-tag"></i></span>
<span tab-button><i class="zmdi zmdi-wrench"></i></span>
</div>
<div tab-content ng-controller="PageTagController" page-id="{{ $page->id or 0 }}">
<form ng-submit="saveTags()" >
<h4>Page Tags</h4>
<div class="padded tags">
<table class="no-style" style="width: 100%;">
<tr ng-repeat="tag in tags">
<td><input class="outline" type="text" ng-model="tag.name" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag"></td>
<td><input class="outline" type="text" ng-model="tag.value" ng-change="tagChange(tag)" ng-blur="tagBlur(tag)" placeholder="Tag Value (Optional)"></td>
</tr>
</table>
</div>
<button class="button pos" type="submit">Save Tags</button>
</form>
</div>
</div>

View File

@ -1,9 +1,9 @@
@if(Setting::get('app-color'))
<style>
header, #back-to-top {
header, #back-to-top, .primary-background {
background-color: {{ Setting::get('app-color') }};
}
.faded-small {
.faded-small, .primary-background-light {
background-color: {{ Setting::get('app-color-light') }};
}
.button-base, .button, input[type="button"], input[type="submit"] {

View File

@ -1,128 +0,0 @@
<?php namespace Entity;
use BookStack\Attribute;
use BookStack\Page;
use BookStack\Services\PermissionService;
class AttributeTests extends \TestCase
{
protected $defaultAttrCount = 20;
/**
* Get an instance of a page that has many attributes.
* @param Attribute[]|bool $attributes
* @return mixed
*/
protected function getPageWithAttributes($attributes = false)
{
$page = Page::first();
if (!$attributes) {
$attributes = factory(Attribute::class, $this->defaultAttrCount)->make();
}
$page->attributes()->saveMany($attributes);
return $page;
}
public function test_get_page_attributes()
{
$page = $this->getPageWithAttributes();
// Add some other attributes to check they don't interfere
factory(Attribute::class, $this->defaultAttrCount)->create();
$this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id)
->shouldReturnJson();
$json = json_decode($this->response->getContent());
$this->assertTrue(count($json) === $this->defaultAttrCount, "Returned JSON item count is not as expected");
}
public function test_attribute_name_suggestions()
{
// Create some attributes with similar names to test with
$attrs = collect();
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'city']));
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'county']));
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'planet']));
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'plans']));
$page = $this->getPageWithAttributes($attrs);
$this->asAdmin()->get('/ajax/attributes/suggest?search=dog')->seeJsonEquals([]);
$this->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country', 'county']);
$this->get('/ajax/attributes/suggest?search=cou')->seeJsonEquals(['country', 'county']);
$this->get('/ajax/attributes/suggest?search=pla')->seeJsonEquals(['planet', 'plans']);
}
public function test_entity_permissions_effect_attribute_suggestions()
{
$permissionService = $this->app->make(PermissionService::class);
// Create some attributes with similar names to test with and save to a page
$attrs = collect();
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'country']));
$attrs = $attrs->merge(factory(Attribute::class, 5)->make(['name' => 'color']));
$page = $this->getPageWithAttributes($attrs);
$this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
$this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
// Set restricted permission the page
$page->restricted = true;
$page->save();
$permissionService->buildJointPermissionsForEntity($page);
$this->asAdmin()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals(['color', 'country']);
$this->asEditor()->get('/ajax/attributes/suggest?search=co')->seeJsonEquals([]);
}
public function test_entity_attribute_updating()
{
$page = $this->getPageWithAttributes();
$testJsonData = [
['name' => 'color', 'value' => 'red'],
['name' => 'color', 'value' => ' blue '],
['name' => 'city', 'value' => 'London '],
['name' => 'country', 'value' => ' England'],
];
$testResponseJsonData = [
['name' => 'color', 'value' => 'red'],
['name' => 'color', 'value' => 'blue'],
['name' => 'city', 'value' => 'London'],
['name' => 'country', 'value' => 'England'],
];
// Do update request
$this->asAdmin()->json("POST", "/ajax/attributes/update/page/" . $page->id, ['attributes' => $testJsonData]);
$updateData = json_decode($this->response->getContent());
// Check data is correct
$testDataCorrect = true;
foreach ($updateData->attributes as $data) {
$testItem = ['name' => $data->name, 'value' => $data->value];
if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
}
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
$this->assertTrue(isset($updateData->message), "No message returned in attribute update response");
// Do get request
$this->asAdmin()->get("/ajax/attributes/get/page/" . $page->id);
$getResponseData = json_decode($this->response->getContent());
// Check counts
$this->assertTrue(count($getResponseData) === count($testJsonData), "The received attribute count is incorrect");
// Check data is correct
$testDataCorrect = true;
foreach ($getResponseData as $data) {
$testItem = ['name' => $data->name, 'value' => $data->value];
if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
}
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));
}
}

128
tests/Entity/TagTests.php Normal file
View File

@ -0,0 +1,128 @@
<?php namespace Entity;
use BookStack\Tag;
use BookStack\Page;
use BookStack\Services\PermissionService;
class TagTests extends \TestCase
{
protected $defaultTagCount = 20;
/**
* Get an instance of a page that has many tags.
* @param Tag[]|bool $tags
* @return mixed
*/
protected function getPageWithTags($tags = false)
{
$page = Page::first();
if (!$tags) {
$tags = factory(Tag::class, $this->defaultTagCount)->make();
}
$page->tags()->saveMany($tags);
return $page;
}
public function test_get_page_tags()
{
$page = $this->getPageWithTags();
// Add some other tags to check they don't interfere
factory(Tag::class, $this->defaultTagCount)->create();
$this->asAdmin()->get("/ajax/tags/get/page/" . $page->id)
->shouldReturnJson();
$json = json_decode($this->response->getContent());
$this->assertTrue(count($json) === $this->defaultTagCount, "Returned JSON item count is not as expected");
}
public function test_tag_name_suggestions()
{
// Create some tags with similar names to test with
$attrs = collect();
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'city']));
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'county']));
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'planet']));
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'plans']));
$page = $this->getPageWithTags($attrs);
$this->asAdmin()->get('/ajax/tags/suggest?search=dog')->seeJsonEquals([]);
$this->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country', 'county']);
$this->get('/ajax/tags/suggest?search=cou')->seeJsonEquals(['country', 'county']);
$this->get('/ajax/tags/suggest?search=pla')->seeJsonEquals(['planet', 'plans']);
}
public function test_entity_permissions_effect_tag_suggestions()
{
$permissionService = $this->app->make(PermissionService::class);
// Create some tags with similar names to test with and save to a page
$attrs = collect();
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'country']));
$attrs = $attrs->merge(factory(Tag::class, 5)->make(['name' => 'color']));
$page = $this->getPageWithTags($attrs);
$this->asAdmin()->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country']);
$this->asEditor()->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country']);
// Set restricted permission the page
$page->restricted = true;
$page->save();
$permissionService->buildJointPermissionsForEntity($page);
$this->asAdmin()->get('/ajax/tags/suggest?search=co')->seeJsonEquals(['color', 'country']);
$this->asEditor()->get('/ajax/tags/suggest?search=co')->seeJsonEquals([]);
}
public function test_entity_tag_updating()
{
$page = $this->getPageWithTags();
$testJsonData = [
['name' => 'color', 'value' => 'red'],
['name' => 'color', 'value' => ' blue '],
['name' => 'city', 'value' => 'London '],
['name' => 'country', 'value' => ' England'],
];
$testResponseJsonData = [
['name' => 'color', 'value' => 'red'],
['name' => 'color', 'value' => 'blue'],
['name' => 'city', 'value' => 'London'],
['name' => 'country', 'value' => 'England'],
];
// Do update request
$this->asAdmin()->json("POST", "/ajax/tags/update/page/" . $page->id, ['tags' => $testJsonData]);
$updateData = json_decode($this->response->getContent());
// Check data is correct
$testDataCorrect = true;
foreach ($updateData->tags as $data) {
$testItem = ['name' => $data->name, 'value' => $data->value];
if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
}
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($updateData)));
$this->assertTrue(isset($updateData->message), "No message returned in tag update response");
// Do get request
$this->asAdmin()->get("/ajax/tags/get/page/" . $page->id);
$getResponseData = json_decode($this->response->getContent());
// Check counts
$this->assertTrue(count($getResponseData) === count($testJsonData), "The received tag count is incorrect");
// Check data is correct
$testDataCorrect = true;
foreach ($getResponseData as $data) {
$testItem = ['name' => $data->name, 'value' => $data->value];
if (!in_array($testItem, $testResponseJsonData)) $testDataCorrect = false;
}
$testMessage = "Expected data was not found in the response.\nExpected Data: %s\nRecieved Data: %s";
$this->assertTrue($testDataCorrect, sprintf($testMessage, json_encode($testResponseJsonData), json_encode($getResponseData)));
}
}