Improve project api procedures

This commit is contained in:
Frédéric Guillot 2014-09-09 15:15:04 +02:00
parent 685d1cc44c
commit ef95c7c284
8 changed files with 438 additions and 20 deletions

View File

@ -565,7 +565,6 @@ class Task extends Base
$board = $this->db->table(Board::TABLE)->eq('project_id', $project_id)->asc('position')->findAllByColumn('id');
$columns = array();
$task_id = (int) $task_id;
// Prepare the columns
foreach ($board as $board_column_id) {

View File

@ -93,17 +93,72 @@ Procedures
### createProject
- Purpose: **Create a new project**
- Parameters: **name** (string)
- Parameters:
- **name** (string, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "createProject",
"id": 1797076613,
"params": {
"name": "PHP client"
}
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 1797076613,
"result": true
}
```
### getProjectById
- Purpose: **Get project information**
- Parameters: **project_id** (integer)
- Parameters:
- **project_id** (integer, required)
- Result on success: **project properties**
- Result on failure: **null**
Request example:
```json
"jsonrpc": "2.0",
"method": "getProjectById",
"id": 226760253,
"params": {
"project_id": 1
}
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 226760253,
"result": {
"id": "1",
"name": "API test",
"is_active": "1",
"token": "",
"last_modified": "1410263246",
"is_public": "0"
}
}
```
### getProjectByName
- Purpose: **Get project information**
@ -111,6 +166,36 @@ Procedures
- Result on success: **project properties**
- Result on failure: **null**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "getProjectByName",
"id": 1620253806,
"params": {
"name": "Test"
}
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 1620253806,
"result": {
"id": "1",
"name": "Test",
"is_active": "1",
"token": "",
"last_modified": "0",
"is_public": "0"
}
}
```
### getAllProjects
- Purpose: **Get all available projects**
@ -118,20 +203,233 @@ Procedures
- Result on success: **List of projects**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "getAllProjects",
"id": 134982303
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 134982303,
"result": [
{
"id": "2",
"name": "PHP client",
"is_active": "1",
"token": "",
"last_modified": "0",
"is_public": "0"
},
{
"id": "1",
"name": "Test",
"is_active": "1",
"token": "",
"last_modified": "0",
"is_public": "0"
}
]
}
```
### updateProject
- Purpose: **Update a project**
- Parameters: Key/value pair composed of the **id** (integer), **name** (string), **is_active** (integer, optional)
- Parameters:
- **id** (integer, required)
- **name** (string, required)
- **is_active** (integer, optional)
- **token** (string, optional)
- **is_public** (integer, optional)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "updateProject",
"id": 1853996288,
"params": {
"id": 1,
"name": "PHP client update"
}
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 1853996288,
"result": true
}
```
### removeProject
- Purpose: **Remove a project**
- Parameters: **project_id** (integer)
- Parameters:
**project_id** (integer, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "removeProject",
"id": 46285125,
"params": {
"project_id": "2"
}
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 46285125,
"result": true
}
```
### enableProject
- Purpose: **Enable a project**
- Parameters:
- **project_id** (integer, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "enableProject",
"id": 1775494839,
"params": [
"1"
]
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 1775494839,
"result": true
}
```
### disableProject
- Purpose: **Disable a project**
- Parameters:
- **project_id** (integer, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "disableProject",
"id": 1734202312,
"params": [
"1"
]
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 1734202312,
"result": true
}
```
### enableProjectPublicAccess
- Purpose: **Enable public access for a given project**
- Parameters:
- **project_id** (integer, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "enableProjectPublicAccess",
"id": 103792571,
"params": [
"1"
]
}
```
Response example:
```json
{
"jsonrpc": "2.0",
"id": 103792571,
"result": true
}
```
### disableProjectPublicAccess
- Purpose: **Disable public access for a given project**
- Parameters:
- **project_id** (integer, required)
- Result on success: **true**
- Result on failure: **false**
Request example:
```json
{
"jsonrpc": "2.0",
"method": "disableProjectPublicAccess",
"id": 942472945,
"params": [
"1"
]
}
```
Response example:
```json
"jsonrpc": "2.0",
"id": 942472945,
"result": true
}
```
@ -213,9 +511,9 @@ Procedures
- Purpose: **Create a new task**
- Parameters:
- **title** (string, required)
- **color_id** (string, required)
- **project_id** (integer, required)
- **column_id** (integer, required)
- **color_id** (string, optional)
- **column_id** (integer, optional)
- **description** (string, optional)
- **owner_id** (integer, optional)
- **creator_id** (integer, optional)

View File

@ -41,6 +41,7 @@ if ($language !== 'en_US') Translator::load($language);
$server = new Server;
$server->authentication(array('jsonrpc' => $config->get('api_token')));
/**
* Project procedures
*/
@ -62,7 +63,22 @@ $server->register('getAllProjects', function() use ($project) {
return $project->getAll();
});
$server->register('updateProject', function(array $values) use ($project) {
$server->register('updateProject', function($id, $name, $is_active = null, $is_public = null, $token = null) use ($project) {
$values = array(
'id' => $id,
'name' => $name,
'is_active' => $is_active,
'is_public' => $is_public,
'token' => $token,
);
foreach ($values as $key => $value) {
if (is_null($value)) {
unset($values[$key]);
}
}
list($valid,) = $project->validateModification($values);
return $valid && $project->update($values);
});
@ -71,6 +87,26 @@ $server->register('removeProject', function($project_id) use ($project) {
return $project->remove($project_id);
});
$server->register('enableProject', function($project_id) use ($project) {
return $project->enable($project_id);
});
$server->register('disableProject', function($project_id) use ($project) {
return $project->disable($project_id);
});
$server->register('enableProjectPublicAccess', function($project_id) use ($project) {
return $project->enablePublicAccess($project_id);
});
$server->register('disableProjectPublicAccess', function($project_id) use ($project) {
return $project->disablePublicAccess($project_id);
});
/**
* Board procedures
*/
$server->register('getBoard', function($project_id) use ($board) {
return $board->get($project_id);
});

View File

@ -24,7 +24,7 @@ class Api extends PHPUnit_Framework_TestCase
if ($projects) {
foreach ($projects as $project) {
$this->client->removeProject($project['id']);
$this->assertTrue($this->client->removeProject($project['id']));
}
}
}
@ -45,13 +45,13 @@ class Api extends PHPUnit_Framework_TestCase
{
$project = $this->client->getProjectById(1);
$this->assertNotEmpty($project);
$this->assertTrue($this->client->updateProject(array('id' => 1, 'name' => 'API test 2', 'is_active' => 0)));
$this->assertTrue($this->client->execute('updateProject', array('id' => 1, 'name' => 'API test 2', 'is_active' => 0)));
$project = $this->client->getProjectById(1);
$this->assertEquals('API test 2', $project['name']);
$this->assertEquals(0, $project['is_active']);
$this->assertTrue($this->client->updateProject(array('id' => 1, 'name' => 'API test', 'is_active' => 1)));
$this->assertTrue($this->client->execute('updateProject', array('id' => 1, 'name' => 'API test', 'is_active' => 1)));
$project = $this->client->getProjectById(1);
$this->assertEquals('API test', $project['name']);

View File

@ -40,6 +40,7 @@ require_once __DIR__.'/../../app/Model/Board.php';
require_once __DIR__.'/../../app/Model/Action.php';
require_once __DIR__.'/../../app/Model/Category.php';
require_once __DIR__.'/../../app/Model/SubTask.php';
require_once __DIR__.'/../../app/Model/File.php';
require_once __DIR__.'/../../app/Action/Base.php';
require_once __DIR__.'/../../app/Action/TaskClose.php';

View File

@ -15,7 +15,81 @@ class ProjectTest extends Base
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertNotEmpty($p->getById(1));
$project = $p->getById(1);
$this->assertNotEmpty($project);
$this->assertEquals(1, $project['is_active']);
$this->assertEquals(0, $project['is_public']);
$this->assertEmpty($project['token']);
}
public function testRemove()
{
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertTrue($p->remove(1));
$this->assertFalse($p->remove(1234));
}
public function testEnable()
{
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertTrue($p->disable(1));
$project = $p->getById(1);
$this->assertNotEmpty($project);
$this->assertEquals(0, $project['is_active']);
$this->assertFalse($p->disable(1111));
}
public function testDisable()
{
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertTrue($p->disable(1));
$this->assertTrue($p->enable(1));
$project = $p->getById(1);
$this->assertNotEmpty($project);
$this->assertEquals(1, $project['is_active']);
$this->assertFalse($p->enable(1234567));
}
public function testEnablePublicAccess()
{
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertTrue($p->enablePublicAccess(1));
$project = $p->getById(1);
$this->assertNotEmpty($project);
$this->assertEquals(1, $project['is_public']);
$this->assertNotEmpty($project['token']);
$this->assertFalse($p->enablePublicAccess(123));
}
public function testDisablePublicAccess()
{
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertTrue($p->enablePublicAccess(1));
$this->assertTrue($p->disablePublicAccess(1));
$project = $p->getById(1);
$this->assertNotEmpty($project);
$this->assertEquals(0, $project['is_public']);
$this->assertEmpty($project['token']);
$this->assertFalse($p->disablePublicAccess(123));
}
public function testAllowEverybody()
@ -66,8 +140,8 @@ class ProjectTest extends Base
// We create a project
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
// We revoke our admin user
$this->assertTrue($p->revokeUser(1, 1));
// We revoke our admin user (not existing row)
$this->assertFalse($p->revokeUser(1, 1));
// We should have nobody in the users list
$this->assertEmpty($p->getAllowedUsers(1));

View File

@ -35,6 +35,18 @@ class TaskTest extends Base
$this->assertEquals(time(), $task['date_modification']);
}
public function testRemove()
{
$t = new Task($this->registry);
$p = new Project($this->registry);
$this->assertEquals(1, $p->create(array('name' => 'UnitTest')));
$this->assertEquals(1, $t->create(array('title' => 'Task #1', 'project_id' => 1)));
$this->assertTrue($t->remove(1));
$this->assertFalse($t->remove(1234));
}
public function testMoveTaskWithBadPreviousPosition()
{
$t = new Task($this->registry);

View File

@ -70,11 +70,7 @@ class Table
$result = $this->db->execute($sql, $values);
if ($result !== false/* && $result->rowCount() > 0*/) {
return true;
}
return false;
return $result !== false && $result->rowCount() > 0;
}
@ -106,7 +102,9 @@ class Table
$this->conditions()
);
return false !== $this->db->execute($sql, $this->values);
$result = $this->db->execute($sql, $this->values);
return $result !== false && $result->rowCount() > 0;
}