summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/Model/Category.php13
-rw-r--r--docs/api-json-rpc.markdown143
-rw-r--r--jsonrpc.php16
-rw-r--r--tests/functionals/ApiTest.php75
-rw-r--r--tests/units/CategoryTest.php57
5 files changed, 293 insertions, 11 deletions
diff --git a/app/Model/Category.php b/app/Model/Category.php
index 4f296944..db26ebca 100644
--- a/app/Model/Category.php
+++ b/app/Model/Category.php
@@ -123,11 +123,17 @@ class Category extends Base
public function remove($category_id)
{
$this->db->startTransaction();
- $r1 = $this->db->table(Task::TABLE)->eq('category_id', $category_id)->update(array('category_id' => 0));
- $r2 = $this->db->table(self::TABLE)->eq('id', $category_id)->remove();
+
+ $this->db->table(Task::TABLE)->eq('category_id', $category_id)->update(array('category_id' => 0));
+
+ if (! $this->db->table(self::TABLE)->eq('id', $category_id)->remove()) {
+ $this->db->cancelTransaction();
+ return false;
+ }
+
$this->db->closeTransaction();
- return $r1 && $r2;
+ return true;
}
/**
@@ -192,7 +198,6 @@ class Category extends Base
$v = new Validator($values, array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('The id must be an integer')),
- new Validators\Required('project_id', t('The project id is required')),
new Validators\Integer('project_id', t('The project id must be an integer')),
new Validators\Required('name', t('The name is required')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50)
diff --git a/docs/api-json-rpc.markdown b/docs/api-json-rpc.markdown
index 9f4240dc..3e35290b 100644
--- a/docs/api-json-rpc.markdown
+++ b/docs/api-json-rpc.markdown
@@ -889,39 +889,172 @@ Response example:
### createCategory
- Purpose: **Create a new category**
-- Parameters: Key/value pair composed of the **name** (string), **project_id** (integer)
+- Parameters:
+- **project_id** (integer, required)
+ - **name** (string, required, must unique for the given project)
- Result on success: **true**
- Result on failure: **false**
+Request example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "method": "createCategory",
+ "id": 541909890,
+ "params": {
+ "name": "Super category",
+ "project_id": 1
+ }
+}
+```
+
+Response example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "id": 541909890,
+ "result": true
+}
+```
+
### getCategory
- Purpose: **Get category information**
-- Parameters: **category_id** (integer)
+- Parameters:
+ - **category_id** (integer, required)
- Result on success: **category properties**
- Result on failure: **null**
+Request example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "method": "getCategory",
+ "id": 203539163,
+ "params": {
+ "category_id": 1
+ }
+}
+```
+
+Response example:
+
+```json
+{
+
+ "jsonrpc": "2.0",
+ "id": 203539163,
+ "result": {
+ "id": "1",
+ "name": "Super category",
+ "project_id": "1"
+ }
+}
+```
+
### getAllCategories
- Purpose: **Get all available categories**
-- Parameters: **project_id** (integer)
+- Parameters:
+ - **project_id** (integer, required)
- Result on success: **List of categories**
- Result on failure: **false**
+Request example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "method": "getAllCategories",
+ "id": 1261777968,
+ "params": {
+ "project_id": 1
+ }
+}
+```
+
+Response example:
+
+```json
+
+ "jsonrpc": "2.0",
+ "id": 1261777968,
+ "result": [
+ {
+ "id": "1",
+ "name": "Super category",
+ "project_id": "1"
+ }
+ ]
+}
+```
+
### updateCategory
- Purpose: **Update a category**
-- Parameters: Key/value pair composed of the **id** (integer), **name** (string), **project_id** (integer)
+- Parameters:
+ - **id** (integer, required)
+ - **name** (string, required)
- Result on success: **true**
- Result on failure: **false**
+Request example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "method": "updateCategory",
+ "id": 570195391,
+ "params": {
+ "id": 1,
+ "name": "Renamed category"
+ }
+}
+```
+
+Response example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "id": 570195391,
+ "result": true
+}
+```
+
### removeCategory
- Purpose: **Remove a category**
-- Parameters: **category_id** (integer)
+- Parameters:
+ - **category_id** (integer)
- Result on success: **true**
- Result on failure: **false**
+Request example:
+
+```json
+{
+ "jsonrpc": "2.0",
+ "method": "removeCategory",
+ "id": 88225706,
+ "params": {
+ "category_id": 1
+ }
+}
+```
+
+Response example:
+```json
+{
+ "jsonrpc": "2.0",
+ "id": 88225706,
+ "result": true
+}
+```
### createComment
diff --git a/jsonrpc.php b/jsonrpc.php
index 7c885334..0a6913d6 100644
--- a/jsonrpc.php
+++ b/jsonrpc.php
@@ -251,7 +251,13 @@ $server->register('removeUser', function($user_id) use ($user) {
/**
* Category procedures
*/
-$server->register('createCategory', function(array $values) use ($category) {
+$server->register('createCategory', function($project_id, $name) use ($category) {
+
+ $values = array(
+ 'project_id' => $project_id,
+ 'name' => $name,
+ );
+
list($valid,) = $category->validateCreation($values);
return $valid && $category->create($values);
});
@@ -264,7 +270,13 @@ $server->register('getAllCategories', function($project_id) use ($category) {
return $category->getAll($project_id);
});
-$server->register('updateCategory', function($values) use ($category) {
+$server->register('updateCategory', function($id, $name) use ($category) {
+
+ $values = array(
+ 'id' => $id,
+ 'name' => $name,
+ );
+
list($valid,) = $category->validateModification($values);
return $valid && $category->update($values);
});
diff --git a/tests/functionals/ApiTest.php b/tests/functionals/ApiTest.php
index bc0bbe42..1f71020a 100644
--- a/tests/functionals/ApiTest.php
+++ b/tests/functionals/ApiTest.php
@@ -418,4 +418,79 @@ class Api extends PHPUnit_Framework_TestCase
$this->assertEquals(1, $task['position']);
$this->assertEquals(3, $task['column_id']);
}
+
+ public function testCategoryCreation()
+ {
+ $category = array(
+ 'name' => 'Category',
+ 'project_id' => 1,
+ );
+
+ $this->assertTrue($this->client->execute('createCategory', $category));
+
+ // Duplicate
+
+ $category = array(
+ 'name' => 'Category',
+ 'project_id' => 1,
+ );
+
+ $this->assertFalse($this->client->execute('createCategory', $category));
+
+ // Missing project id
+
+ $category = array(
+ 'name' => 'Category',
+ );
+
+ $this->assertNull($this->client->execute('createCategory', $category));
+ }
+
+ public function testCategoryRead()
+ {
+ $category = $this->client->getCategory(1);
+
+ $this->assertTrue(is_array($category));
+ $this->assertNotEmpty($category);
+ $this->assertEquals(1, $category['id']);
+ $this->assertEquals('Category', $category['name']);
+ $this->assertEquals(1, $category['project_id']);
+ }
+
+ public function testGetAllCategories()
+ {
+ $categories = $this->client->getAllCategories(1);
+
+ $this->assertNotEmpty($categories);
+ $this->assertNotFalse($categories);
+ $this->assertTrue(is_array($categories));
+ $this->assertEquals(1, count($categories));
+ $this->assertEquals(1, $categories[0]['id']);
+ $this->assertEquals('Category', $categories[0]['name']);
+ $this->assertEquals(1, $categories[0]['project_id']);
+ }
+
+ public function testCategoryUpdate()
+ {
+ $category = array(
+ 'id' => 1,
+ 'name' => 'Renamed category',
+ );
+
+ $this->assertTrue($this->client->execute('updateCategory', $category));
+
+ $category = $this->client->getCategory(1);
+ $this->assertTrue(is_array($category));
+ $this->assertNotEmpty($category);
+ $this->assertEquals(1, $category['id']);
+ $this->assertEquals('Renamed category', $category['name']);
+ $this->assertEquals(1, $category['project_id']);
+ }
+
+ public function testCategoryRemove()
+ {
+ $this->assertTrue($this->client->removeCategory(1));
+ $this->assertFalse($this->client->removeCategory(1));
+ $this->assertFalse($this->client->removeCategory(1111));
+ }
}
diff --git a/tests/units/CategoryTest.php b/tests/units/CategoryTest.php
new file mode 100644
index 00000000..201fa589
--- /dev/null
+++ b/tests/units/CategoryTest.php
@@ -0,0 +1,57 @@
+<?php
+
+require_once __DIR__.'/Base.php';
+
+use Model\Task;
+use Model\Project;
+use Model\Category;
+use Model\User;
+
+class CategoryTest extends Base
+{
+ public function testCreation()
+ {
+ $t = new Task($this->registry);
+ $p = new Project($this->registry);
+ $c = new Category($this->registry);
+
+ $this->assertEquals(1, $p->create(array('name' => 'Project #1')));
+ $this->assertEquals(1, $c->create(array('name' => 'Category #1', 'project_id' => 1)));
+ $this->assertEquals(2, $c->create(array('name' => 'Category #2', 'project_id' => 1)));
+ $this->assertEquals(1, $t->create(array('title' => 'Task #1', 'project_id' => 1, 'category_id' => 2)));
+
+ $task = $t->getById(1);
+ $this->assertTrue(is_array($task));
+ $this->assertEquals(2, $task['category_id']);
+
+ $category = $c->getById(2);
+ $this->assertTrue(is_array($category));
+ $this->assertEquals(2, $category['id']);
+ $this->assertEquals('Category #2', $category['name']);
+ $this->assertEquals(1, $category['project_id']);
+ }
+
+ public function testRemove()
+ {
+ $t = new Task($this->registry);
+ $p = new Project($this->registry);
+ $c = new Category($this->registry);
+
+ $this->assertEquals(1, $p->create(array('name' => 'Project #1')));
+ $this->assertEquals(1, $c->create(array('name' => 'Category #1', 'project_id' => 1)));
+ $this->assertEquals(2, $c->create(array('name' => 'Category #2', 'project_id' => 1)));
+ $this->assertEquals(1, $t->create(array('title' => 'Task #1', 'project_id' => 1, 'category_id' => 2)));
+
+ $task = $t->getById(1);
+ $this->assertTrue(is_array($task));
+ $this->assertEquals(2, $task['category_id']);
+
+ $this->assertTrue($c->remove(1));
+ $this->assertTrue($c->remove(2));
+
+ // Make sure tasks assigned with that category are reseted
+ $task = $t->getById(1);
+ $this->assertTrue(is_array($task));
+ $this->assertEquals(0, $task['category_id']);
+ }
+}