Skip to content

Commit

Permalink
Fixes bugs in Ushahidi_Repository_Post::updatePostTags
Browse files Browse the repository at this point in the history
Fixes errors in the DB::delete call and adds logic to delete all tags
when $tags are empty. Addresses issues #1400
  • Loading branch information
Zack Halloran authored and rjmackay committed Oct 13, 2016
1 parent 96887df commit 00fa9ef
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 36 deletions.
83 changes: 47 additions & 36 deletions application/classes/Ushahidi/Repository/Post.php
Expand Up @@ -898,51 +898,62 @@ protected function updatePostValues($post_id, $attributes)

protected function updatePostTags($post_id, $tags)
{
// Load existing tags
$existing = $this->getTagsForPost($post_id);
// deletes all tags if $tags is empty
if (empty($tags))
{
DB::delete('posts_tags')
->where('post_id', '=', $post_id)
->execute($this->db);
}
else
{
// Load existing tags
$existing = $this->getTagsForPost($post_id);

$insert = DB::insert('posts_tags', ['post_id', 'tag_id']);
$insert = DB::insert('posts_tags', ['post_id', 'tag_id']);

$tag_ids = [];
$new_tags = FALSE;
foreach ($tags as $tag)
{
if (is_array($tag)) {
$tag = $tag['id'];
}
$tag_ids = [];
$new_tags = FALSE;

// Find the tag by id or name
// @todo this should happen before we even get here
$tag_entity = $this->tag_repo->getByTag($tag);
if (! $tag_entity->id)
foreach ($tags as $tag)
{
$tag_entity = $this->tag_repo->get($tag);
if (is_array($tag)) {
$tag = $tag['id'];
}

// Find the tag by id or name
// @todo this should happen before we even get here
$tag_entity = $this->tag_repo->getByTag($tag);
if (! $tag_entity->id)
{
$tag_entity = $this->tag_repo->get($tag);
}

// Does the post already have this tag?
if (! in_array($tag_entity->id, $existing))
{
// Add to insert query
$insert->values([$post_id, $tag_entity->id]);
$new_tags = TRUE;
}

$tag_ids[] = $tag_entity->id;
}

// Does the post already have this tag?
if (! in_array($tag_entity->id, $existing))
// Save
if ($new_tags)
{
// Add to insert query
$insert->values([$post_id, $tag_entity->id]);
$new_tags = TRUE;
$insert->execute($this->db);
}

$tag_ids[] = $tag_entity->id;
}

// Save
if ($new_tags)
{
$insert->execute($this->db);
}

// Remove any other tags
if (! empty($tag_ids))
{
DB::delete('posts_tags')
->where('tag_id', 'NOT IN', $tag_ids)
->where('post_id', '=', '$post_id')
->execute($this->db);
// Remove any other tags
if (! empty($tag_ids))
{
DB::delete('posts_tags')
->where('tag_id', 'NOT IN', $tag_ids)
->and_where('post_id', '=', $post_id)
->execute($this->db);
}
}
}

Expand Down
86 changes: 86 additions & 0 deletions application/tests/features/api.posts.feature
Expand Up @@ -462,6 +462,92 @@ Feature: Testing the Posts API
And the "values.last_location_point.0.lon" property equals "-85.39"
Then the guzzle status code should be 200

@update
Scenario: Updating a Post to update tags
Given that I want to update a "Post"
And that the request "data" is:
"""
{
"form":1,
"title":"Updated Test Post",
"type":"report",
"status":"published",
"locale":"en_US",
"values":
{
"full_name":["David Kobia"],
"description":["Skinny, homeless Kenyan last seen in the vicinity of the greyhound station"],
"date_of_birth":[],
"missing_date":["2012/09/25"],
"last_location":["atlanta"],
"last_location_point":[
{
"lat": 33.755,
"lon": -85.39
}
],
"missing_status":["believed_missing"]
},
"tags":["disaster"],
"completed_stages":[1]
}
"""
And that its "id" is "1"
When I request "/posts"
Then the response is JSON
And the response has a "id" property
And the type of the "id" property is "numeric"
And the "id" property equals "1"
And the response has a "tags.0.id" property
And the response does not have a "tags.1.id" property
And the response has a "title" property
And the "title" property equals "Updated Test Post"
And the "values.last_location_point.0.lon" property equals "-85.39"
Then the guzzle status code should be 200

@update
Scenario: Updating a Post to remove all tags
Given that I want to update a "Post"
And that the request "data" is:
"""
{
"form":1,
"title":"Updated Test Post",
"type":"report",
"status":"published",
"locale":"en_US",
"values":
{
"full_name":["David Kobia"],
"description":["Skinny, homeless Kenyan last seen in the vicinity of the greyhound station"],
"date_of_birth":[],
"missing_date":["2012/09/25"],
"last_location":["atlanta"],
"last_location_point":[
{
"lat": 33.755,
"lon": -85.39
}
],
"missing_status":["believed_missing"]
},
"tags":[],
"completed_stages":[1]
}
"""
And that its "id" is "1"
When I request "/posts"
Then the response is JSON
And the response has a "id" property
And the type of the "id" property is "numeric"
And the "id" property equals "1"
And the response has a "tags" property
And the "tags" property is empty
And the response has a "title" property
And the "title" property equals "Updated Test Post"
And the "values.last_location_point.0.lon" property equals "-85.39"
Then the guzzle status code should be 200

@update
Scenario: Updating a Post using JSON date formate
Given that I want to update a "Post"
Expand Down

0 comments on commit 00fa9ef

Please sign in to comment.