Skip to content

Commit

Permalink
Merge pull request #2687 from ushahidi/release/comrades-and-fixes
Browse files Browse the repository at this point in the history
Release comrades changes and category fixes
  • Loading branch information
rjmackay authored Apr 5, 2018
2 parents 1dbafad + 3b5ff2c commit 4994bca
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 23 deletions.
43 changes: 32 additions & 11 deletions application/classes/Ushahidi/Repository/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ protected function getTable()
// ReadRepository
public function getEntity(Array $data = null)
{
if (!empty($data['id']))
{
if (!empty($data['id'])) {
// If this is a top level category
if(empty($data['parent_id'])) {
if (empty($data['parent_id'])) {
// Load children
$data['children'] = DB::select('id')
->from('tags')
->where('parent_id','=',$data['id'])
->where('parent_id', '=', $data['id'])
->execute($this->db)
->as_array(null, 'id');
}
Expand All @@ -74,10 +73,9 @@ public function getSearchFields()
protected function setSearchConditions(SearchData $search)
{
$query = $this->search_query;
foreach (['tag', 'type', 'parent_id'] as $key)
{
foreach (['tag', 'type', 'parent_id'] as $key) {
if ($search->$key) {
$query->where($key, '=', $search->$key);
$query->where($key, '=', $search->$key);
}
}

Expand All @@ -86,9 +84,9 @@ protected function setSearchConditions(SearchData $search)
$query->where('tag', 'LIKE', "%{$search->q}%");
}

if($search->level) {
if ($search->level) {
//searching for top-level-tags
if($search->level === 'parent') {
if ($search->level === 'parent') {
$query->where('parent_id', '=', null);
}
}
Expand Down Expand Up @@ -162,8 +160,31 @@ public function delete(Entity $entity)
public function deleteTag($id)
{
// Remove tag from attribute options
$this->removeTagFromAttributeOptions($entity->id);

$this->removeTagFromAttributeOptions($id);
return $this->delete(compact('id'));
}

/**
* Checks if the assigned role is valid for this tag.
* True if there is no role or if it's a parent with no children
* @param Validation $validation
* @param $fullData
* @return bool
*/
public function isRoleValid(Validation $validation, $fullData)
{
$valid = true;
$entityFullData = $this->getEntity($fullData);
$isChild = !!$entityFullData->parent_id;
$hasRole = !!$entityFullData->role;
$parent = $isChild ? $this->selectOne(['id' => $entityFullData->parent_id]) : null;
if ($hasRole && $isChild && $parent) {
$parent = $this->getEntity($parent);
$valid = $parent->role == $entityFullData->role;
}
if (!$valid) {
$validation->error('role', 'tag.role');
}
return $valid;
}
}
1 change: 1 addition & 0 deletions application/classes/Ushahidi/Validator/Tag/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function getRules()
],
'role' => [
[[$this->role_repo, 'exists'], [':value']],
[[$this->repo, 'isRoleValid'], [':validation', ':fulldata']]
]
];
}
Expand Down
6 changes: 4 additions & 2 deletions application/messages/tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

return array(
'isSlugAvailable' => ':field :value is already in use',
'description.regex' => 'The description must contain only letters, numbers, spaces and punctuation',
'tag.regex' => 'The category name must contain only letters, numbers, spaces and punctuation',
'isRoleValid' => 'Role must match the parent category',
'tag.role.isRoleValid' => 'Role must match the parent category',
'description.regex' => 'The description must contain only letters, numbers, spaces and punctuation',
'tag.regex' => 'The category name must contain only letters, numbers, spaces and punctuation',
);

2 changes: 1 addition & 1 deletion codeship-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
tag: staging
command: >
ANSIBLE_GITHUB_REPO=platform-cloud-ansible
ANSIBLE_GITHUB_REPO_VERSION=master
ANSIBLE_GITHUB_REPO_VERSION=develop
ush-ansible-playbook.sh -vv -i hosts/rackspace_staging platform-api-update.yml
-e PLATFORM_CLIENT_VERSION="$CI_COMMIT_ID"
Expand Down
33 changes: 33 additions & 0 deletions tests/datasets/ushahidi/Base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,7 @@ tags:
slug: "test-tag"
priority: 0
type: 'category'
role: '["admin", "user"]'
-
id: 2
parent_id:
Expand Down Expand Up @@ -1128,6 +1129,38 @@ tags:
priority: 0
type: "category"
role: '["admin"]'
-
id: 8
parent_id:
role: 'admin'
tag: "Test tag - no children"
slug: "test-tag-no-children"
priority: 0
type: 'category'
-
id: 9
parent_id:
role: 'admin'
tag: "Test tag - with children"
slug: "test-tag-with-children"
priority: 0
type: 'category'
-
id: 10
role: 'admin'
parent_id: 9
tag: "Child 1"
slug: "child-one"
priority: 0
type: 'category'
-
id: 11
role: 'admin'
parent_id: 9
tag: "Child 2"
slug: "child-two"
priority: 0
type: 'category'
posts_tags:
-
post_id: 1
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/acl.feature
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ Feature: API Access Control Layer
Then the response is JSON
And the response has a "count" property
And the type of the "count" property is "numeric"
And the "count" property equals "7"
And the "count" property equals "11"
Then the guzzle status code should be 200

@rolesEnabled
Expand Down
1 change: 1 addition & 0 deletions tests/integration/media.feature
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ Feature: Testing the Media API
And the response has a "errors" property
Then the guzzle status code should be 404

@resetFixture
Scenario: Fail to create a new Media with size greater than limit
Given that I want to make a new "Media"
And that the post field "caption" is "ihub"
Expand Down
61 changes: 53 additions & 8 deletions tests/integration/tags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ Feature: Testing the Tags API
"type":"category",
"priority":1,
"color":"00ff00",
"role":
[
"user",
"admin"
]
"role": ["admin", "user"]
}
"""
When I request "/tags"
Expand All @@ -31,7 +27,6 @@ Feature: Testing the Tags API
And the "priority" property equals "1"
And the "type" property equals "category"
And the response has a "role" property
And the type of the "role" property is "array"
And the "parent.id" property equals "1"
Then the guzzle status code should be 200

Expand Down Expand Up @@ -218,7 +213,7 @@ Feature: Testing the Tags API
Then the response is JSON
And the response has a "count" property
And the type of the "count" property is "numeric"
And the "count" property equals "7"
And the "count" property equals "11"
Then the guzzle status code should be 200

@resetFixture
Expand All @@ -243,7 +238,7 @@ Feature: Testing the Tags API
"""
When I request "/tags"
Then the response is JSON
And the "count" property equals "5"
And the "count" property equals "9"
Then the guzzle status code should be 200

@resetFixture
Expand Down Expand Up @@ -301,3 +296,53 @@ Feature: Testing the Tags API
When I request "/tags"
And the response has a "errors" property
Then the guzzle status code should be 404

Scenario: Creating a new child for a tag with role=admin
Given that I want to make a new "Tag"
And that the request "data" is:
"""
{
"parent_id":9,
"tag":"Valid child",
"slug":"valid-child",
"description":"I am a valid tag",
"type":"category",
"priority":1,
"color":"00ff00",
"role": "admin"
}
"""
When I request "/tags"
Then the response is JSON
And the response has a "id" property
And the type of the "id" property is "numeric"
And the "tag" property equals "Valid child"
And the "slug" property equals "valid-child"
And the "description" property equals "I am a valid tag"
And the "color" property equals "#00ff00"
And the "priority" property equals "1"
And the "type" property equals "category"
And the response has a "role" property
And the type of the "role" property is "array"
And the "parent.id" property equals "9"
Then the guzzle status code should be 200

Scenario: Creating a new invalid child for a tag with role=admin
Given that I want to make a new "Tag"
And that the request "data" is:
"""
{
"parent_id":9,
"tag":"Not a valid tag role",
"slug":"not-valid-tag-role",
"description":"My role is invalid",
"type":"category",
"priority":1,
"color":"00ff00",
"role":"nope"
}
"""
When I request "/tags"
Then the response is JSON
And the response has a "errors" property
Then the guzzle status code should be 422

0 comments on commit 4994bca

Please sign in to comment.