Skip to content

Commit

Permalink
Add more tests for API
Browse files Browse the repository at this point in the history
  • Loading branch information
nikrou committed Oct 6, 2014
1 parent ea86415 commit 2df0f83
Show file tree
Hide file tree
Showing 11 changed files with 601 additions and 492 deletions.
23 changes: 12 additions & 11 deletions admin/include/functions.php
Expand Up @@ -197,6 +197,7 @@ function delete_elements($ids, $physical_deletion=false) {
}
}

// @TODO: why wordwrap ???
$ids_str = wordwrap(implode(', ', $ids), 80, "\n");

// destruction of the comments on the image
Expand Down Expand Up @@ -328,7 +329,7 @@ function update_category($ids='all') {
if (count($ids) == 0) {
return false;
}
$where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')';
$where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')'; // @TODO: why wordwrap ???
}

// find all categories where the setted representative is not possible :
Expand Down Expand Up @@ -367,8 +368,8 @@ function update_category($ids='all') {
* Removes all entries from the table which correspond to a deleted image.
*/
function images_integrity() {
$query = 'SELECT image_id FROM '.IMAGE_CATEGORY_TABLE;
$query .= ' LEFT JOIN '.IMAGES_TABLE.' ON id = image_id WHERE id IS NULL;';
$query = 'SELECT image_id FROM '.IMAGES_TABLE;
$query .= ' LEFT JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id WHERE id IS NULL;';
$result = pwg_query($query);
$orphan_image_ids = query2array($query, null, 'image_id');

Expand Down Expand Up @@ -458,7 +459,7 @@ function update_global_rank() {

// use function()
$cat_map_callback = function($m) use ($cat_map) {
return $cat_map[$m[1]]["rank"];
return $cat_map[$m[1]]['rank'];
};

foreach ($cat_map as $id=>$cat) {
Expand Down Expand Up @@ -512,7 +513,7 @@ function set_cat_visible($categories, $value, $unlock_child = false) {
}

$query = 'UPDATE '.CATEGORIES_TABLE;
$query .= ' SET visible = \'true\' WHERE id IN ('.implode(',', $cats).')';
$query .= ' SET visible = \'true\' WHERE id IN ('.implode(',', $cats).')'; // @TODO: use filtered IN()
pwg_query($query);
} else { // locking a category => all its child categories become locked
$subcats = get_subcat_ids($categories);
Expand Down Expand Up @@ -620,7 +621,7 @@ function set_cat_status($categories, $value) {

if (count($parent_ids) > 0) {
$query = 'SELECT id,status FROM '.CATEGORIES_TABLE;
$query .= ' WHERE id IN ('.implode(',', $parent_ids).');';
$query .= ' WHERE id IN ('.implode(',', $parent_ids).');'; // @TODO: use filtered IN()
$parent_cats= query2array($query, 'id');
}

Expand Down Expand Up @@ -1463,7 +1464,7 @@ function get_user_access_level_html_options($MinLevelAccess=ACCESS_FREE, $MaxLev
*/
function create_tag($tag_name) {
// does the tag already exists?
$query = 'SELECT id FROM '.TAGS_TABLE.' WHERE name = \''.$tag_name.'\';';
$query = 'SELECT id FROM '.TAGS_TABLE.' WHERE name = \''.pwg_db_real_escape_string($tag_name).'\';';
$existing_tags = query2array($query, null, 'id');

if (count($existing_tags) == 0) {
Expand All @@ -1478,11 +1479,11 @@ function create_tag($tag_name) {
$inserted_id = pwg_db_insert_id(TAGS_TABLE);

return array(
'info' => l10n('Tag "%s" was added', stripslashes($tag_name)),
'info' => l10n('Tag "%s" was added', stripslashes($tag_name)), // @TODO: remove stripslashes
'id' => $inserted_id,
);
} else {
return array('error' => l10n('Tag "%s" already exists', stripslashes($tag_name)));
return array('error' => l10n('Tag "%s" already exists', stripslashes($tag_name))); // @TODO: remove stripslashes
}
}

Expand Down Expand Up @@ -2019,8 +2020,8 @@ function delete_element_derivatives($infos, $type='all') {
}
$path = substr_replace($path, $pattern, $dot, 0);
if (($glob=glob(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$path)) !== false) {
foreach( $glob as $file) {
@unlink($file);
foreach($glob as $file) {
unlink($file);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions behat.yml.dist
Expand Up @@ -11,6 +11,7 @@ default:
picture: picture.php?/%%d/category/%d

config_file: sites/behat/local/config/database.inc.php
upload_dir: sites/behat/upload/
data_location: sites/behat/_data/
sql_init_file: features/sql/db_init.sql
sql_cleanup_file: features/sql/db_cleanup.sql
Expand Down
72 changes: 72 additions & 0 deletions features/api/pwg.images.addSimple.feature
@@ -0,0 +1,72 @@
@api
Feature: API
In order to manage my gallery
As a developer
I need to be able to upload images

Background: init
Given a user:
| username | password | status |
| user1 | pass1 | webmaster |
And an album:
| name |
| album 1|
Then save "category_id"
And "user1" can access "album 1"

Scenario: add an image to a category
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.images.addSimple" with values:
| name | photo 1 |
| category | SAVED:category_id |
| FILE:image | features/media/img_1.png |
Then the response code should be 200

Scenario: Add an image with infos
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.images.addSimple" with values:
| name | A nice title |
| category | SAVED:category_id |
| level | 2 |
| tags | My first tag, Another tag |
| FILE:image | features/media/img_1.png |
Then save "image_id" from property "result/image_id"
When I send a POST request to "pwg.images.getInfo" with values:
| image_id | SAVED:image_id |
Then the response has property "result/id" equals to "SAVED:image_id"
Then the response has property "result/name" equals to "A nice title"
Then the response has property "result/categories/0/id" equals to "SAVED:category_id"
Then the response has property "result/categories/0/name" equals to "album 1"
Then the response has property "result/tags" with size 2
Then the response has property "result/tags/0/name" equals to "Another tag"
Then the response has property "result/tags/1/name" equals to "My first tag"

Scenario: Add an image with a tag containing a comma
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.images.addSimple" with values:
| name | A very nice title |
| category | SAVED:category_id |
| tags | My first tag, My second\, tag |
| FILE:image | features/media/img_2.png |
Then save "image_id" from property "result/image_id"
When I send a POST request to "pwg.images.getInfo" with values:
| image_id | SAVED:image_id |
Then the response has property "result/name" equals to "A very nice title"
Then the response has property "result/tags/0/name" equals to "My first tag"
Then the response has property "result/tags/1/name" equals to "My second, tag"

Scenario: Update an image
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.images.addSimple" with values:
| name | An another title |
| category | SAVED:category_id |
| FILE:image | features/media/img_3.png |
Then save "image_id" from property "result/image_id"
When I send a POST request to "pwg.images.addSimple" with values:
| name | A very nice title |
| image_id | SAVED:image_id |
| FILE:image | features/media/img_4.png |
When I send a POST request to "pwg.images.getInfo" with values:
| image_id | SAVED:image_id |
Then the response has property "result/name" equals to "A very nice title"
Then the response has property "result/file" equals to "img_4.png"
31 changes: 31 additions & 0 deletions features/api/pwg.images.delete.feature
@@ -0,0 +1,31 @@
@api
Feature: API
In order to manage my gallery
As a developer
I need to be able to upload images

Background: init
Given a user:
| username | password | status |
| user1 | pass1 | webmaster |
And an album:
| name |
| album 1 |
And images:
| name | album | file |
| photo 1 | album 1 | features/media/img_1.png |
| photo 2 | album 1 | features/media/img_2.png |
Then save "image_id"
And "user1" can access "album 1"

Scenario: add an image to a category
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.images.delete" with values:
| image_id | SAVED:image_id |
| pwg_token | |
Then the response code should be 200
When I send a POST request to "pwg.images.exist" with values:
| md5sum_list | ba76685694786b04eb77002248dad3c0 |
Then the response code should be 200
And the response has property "result/ba76685694786b04eb77002248dad3c0" equals to ""

26 changes: 26 additions & 0 deletions features/api/pwg.images.exist.feature
@@ -0,0 +1,26 @@
@api
Feature: API
In order to manage my gallery
As a developer
I need to be able to upload images

Background: init
Given a user:
| username | password | status |
| user1 | pass1 | webmaster |
And an album:
| name |
| album 1 |
And images:
| name | album |
| photo 1 | album 1 |
Then save "image_id"

Scenario: add an image to a category
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.images.exist" with values:
| md5sum_list | 91d77ceb2814758cbd5ee991f51b7ecd |
Then the response code should be 200
And the response has property "result/91d77ceb2814758cbd5ee991f51b7ecd" equals to "SAVED:image_id"


29 changes: 29 additions & 0 deletions features/api/pwg.tags.add.feature
@@ -0,0 +1,29 @@
@api
Feature: API
In order to manage my gallery
As a developer
I need to be able to upload images

Background: init
Given a user:
| username | password | status |
| user1 | pass1 | webmaster |

Scenario: add a tag
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.tags.add" with values:
| name | A new tag |
Then the response code should be 200
And the response has property "result/info" equals to 'Tag "A new tag" has been added'

Scenario: add an existing tag
Given I am authenticated for api as "user1" with password "pass1"
When I send a POST request to "pwg.tags.add" with values:
| name | A new tag |
Then the response code should be 200
When I send a POST request to "pwg.tags.add" with values:
| name | A new tag |
Then the response code should be 500
And the response has property "message" equals to 'Tag "A new tag" already exists'


15 changes: 10 additions & 5 deletions features/bootstrap/ApiContext.php
Expand Up @@ -128,6 +128,7 @@ public function iSendARequestWithValues($http_method, $method, TableNode $values
foreach ($values->getRowsHash() as $key => $val) {
if (preg_match('`^SAVED:(.*)$`', $val, $matches)) {
$value = $this->getMainContext()->getSubcontext('db')->getSaved($matches[1]);

if ($key=='tags') { // @TODO: find a better way to add ~~ around tags id
$value = '~~'.$value.'~~';
}
Expand Down Expand Up @@ -187,12 +188,12 @@ public function theResponseIsJson() {
public function theResponseHasProperty($property) {
$data = $this->getJson();

$value = $this->getProperty($data, $property);
return $this->getProperty($data, $property);
}


/**
* @Given /^the response has property "([^"]*)" equals to "([^"]*)"$/
* @Given /^the response has property "([^"]*)" equals to '([^']*)'$/
*/
public function theResponseHasPropertyEqualsTo($property, $value) {
$data = $this->getJson();
Expand Down Expand Up @@ -226,10 +227,14 @@ private function getProperty($data, $property) {
$data;
$n = 0;
while ($n<count($parts)) {
if (!isset($data[$parts[$n]])) {
throw new Exception("Complex property '".$property."' is not set!\n");
if (is_null($data[$parts[$n]]) && ($n+1)==count($parts)) {
$data = '';
} else {
if (!isset($data[$parts[$n]])) {
throw new Exception("Complex property '".$property."' is not set!\n");
}
$data = $data[$parts[$n]];
}
$data = $data[$parts[$n]];
$n++;
}
return $data;
Expand Down

0 comments on commit 2df0f83

Please sign in to comment.