From 4ab4ab9a386e6b1d1f0a39d18148673db9be1ec1 Mon Sep 17 00:00:00 2001 From: maso Date: Wed, 8 Nov 2017 01:30:40 +0330 Subject: [PATCH] Adding comment api --- src/Tenant/Views/Comments.php | 66 ---- src/Tenant/Views/Invoices.php | 1 + src/Tenant/Views/Tickets.php | 28 -- src/Tenant/urls.php | 76 +++- tests/Tenant_REST/TicketCommentTest.php | 448 ++++++++++++++++++++++++ tests/phpunit.xml | 2 +- 6 files changed, 511 insertions(+), 110 deletions(-) delete mode 100644 src/Tenant/Views/Comments.php delete mode 100644 src/Tenant/Views/Tickets.php create mode 100644 tests/Tenant_REST/TicketCommentTest.php diff --git a/src/Tenant/Views/Comments.php b/src/Tenant/Views/Comments.php deleted file mode 100644 index a17e297..0000000 --- a/src/Tenant/Views/Comments.php +++ /dev/null @@ -1,66 +0,0 @@ -. - */ - -/** - * Comments view - * - * Manage Comments in system - * - * @author maso - */ -class Tenant_Views_Comments -{ - - /** - * Get a comment - * - * @param Pluf_HTTP_Request $request - * @param array $match - */ - public function get($request, $match) - {} - - /** - * Create a new comment - * - * @param Pluf_HTTP_Request $request - * @param array $match - */ - public function create($request, $match) - {} - - /** - * Delete a comment - * - * @param Pluf_HTTP_Request $request - * @param array $match - */ - public function delete($request, $match) - {} - - /** - * Update a comment - * - * @param Pluf_HTTP_Request $request - * @param array $match - */ - public function update($request, $match) - {} -} \ No newline at end of file diff --git a/src/Tenant/Views/Invoices.php b/src/Tenant/Views/Invoices.php index ef10175..607ae79 100644 --- a/src/Tenant/Views/Invoices.php +++ b/src/Tenant/Views/Invoices.php @@ -25,4 +25,5 @@ */ class Tenant_Views_Invoices { + // TODO: maso, 2017: adding payment } \ No newline at end of file diff --git a/src/Tenant/Views/Tickets.php b/src/Tenant/Views/Tickets.php deleted file mode 100644 index 5113cc1..0000000 --- a/src/Tenant/Views/Tickets.php +++ /dev/null @@ -1,28 +0,0 @@ -. - */ - -/** - * Comments view - * - * @author maso - */ -class Tenant_Views_Tickets -{ -} \ No newline at end of file diff --git a/src/Tenant/urls.php b/src/Tenant/urls.php index 7d7c83b..09aa8bc 100644 --- a/src/Tenant/urls.php +++ b/src/Tenant/urls.php @@ -133,48 +133,94 @@ * Comments of ticket */ array( - 'regex' => '#^/current/ticket/(?P\d+)/comment/find$#', - 'model' => 'Tenant_Views_Ticket', - 'method' => 'findComments', + 'regex' => '#^/current/ticket/(?P\d+)/comment/find$#', + 'model' => 'Pluf_Views', + 'method' => 'findManyToOne', 'http-method' => 'GET', 'precond' => array( 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Tenant_Comment', + 'parent' => 'Tenant_Ticket', + 'parentKey' => 'ticket', + 'listFilters' => array( + 'status', + 'type', + 'requester' + ), + 'listDisplay' => array(), + 'searchFields' => array( + 'subject', + 'description' + ), + 'sortFields' => array( + 'id', + 'status', + 'type', + 'modif_dtime', + 'creation_dtime' + ), + 'sortOrder' => array( + 'id', + 'DESC' + ) ) ), array( - 'regex' => '#^/current/ticket/(?P\d+)/comment/new$#', - 'model' => 'Tenant_Views_Ticket', - 'method' => 'createComments', + 'regex' => '#^/current/ticket/(?P\d+)/comment/new$#', + 'model' => 'Pluf_Views', + 'method' => 'createManyToOne', 'http-method' => 'POST', 'precond' => array( 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Tenant_Comment', + 'parent' => 'Tenant_Ticket', + 'parentKey' => 'ticket' ) ), array( - 'regex' => '#^/current/ticket/(?P\d+)/comment/(?P\d+)$#', - 'model' => 'Tenant_Views_Ticket', - 'method' => 'get', + 'regex' => '#^/current/ticket/(?P\d+)/comment/(?P\d+)$#', + 'model' => 'Pluf_Views', + 'method' => 'getManyToOne', 'http-method' => 'GET', 'precond' => array( 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Tenant_Comment', + 'parent' => 'Tenant_Ticket', + 'parentKey' => 'ticket' ) ), array( - 'regex' => '#^/current/ticket/(?P\d+)/comment/(?P\d+)$#', - 'model' => 'Tenant_Views_Ticket', - 'method' => 'get', + 'regex' => '#^/current/ticket/(?P\d+)/comment/(?P\d+)$#', + 'model' => 'Pluf_Views', + 'method' => 'updateManyToOne', 'http-method' => 'POST', 'precond' => array( 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Tenant_Comment', + 'parent' => 'Tenant_Ticket', + 'parentKey' => 'ticket' ) ), array( - 'regex' => '#^/current/ticket/(?P\d+)/comment/(?P\d+)$#', - 'model' => 'Tenant_Views_Ticket', - 'method' => 'get', + 'regex' => '#^/current/ticket/(?P\d+)/comment/(?P\d+)$#', + 'model' => 'Pluf_Views', + 'method' => 'deleteManyToOne', 'http-method' => 'DELETE', 'precond' => array( 'Pluf_Precondition::ownerRequired' + ), + 'params' => array( + 'model' => 'Tenant_Comment', + 'parent' => 'Tenant_Ticket', + 'parentKey' => 'ticket' ) ), diff --git a/tests/Tenant_REST/TicketCommentTest.php b/tests/Tenant_REST/TicketCommentTest.php new file mode 100644 index 0000000..322ba79 --- /dev/null +++ b/tests/Tenant_REST/TicketCommentTest.php @@ -0,0 +1,448 @@ +. + */ +use PHPUnit\Framework\TestCase; +require_once 'Pluf.php'; + +/** + * @backupGlobals disabled + * @backupStaticAttributes disabled + */ +class Tenant_REST_TicketCommentsTest extends TestCase +{ + + /** + * @beforeClass + */ + public static function installApps() + { + Pluf::start(dirname(__FILE__) . '/../conf/config-01.php'); + $m = new Pluf_Migration(array( + 'Pluf', + 'Tenant' + )); + $m->install(); + // Test user + $user = new Pluf_User(); + $user->login = 'test'; + $user->first_name = 'test'; + $user->last_name = 'test'; + $user->email = 'toto@example.com'; + $user->setPassword('test'); + $user->active = true; + $user->administrator = true; + if (true !== $user->create()) { + throw new Exception(); + } + + // Test tenant + $tenant = new Pluf_Tenant(); + $tenant->domain = 'localhost'; + $tenant->subdomain = 'test'; + $tenant->validate = true; + if (true !== $tenant->create()) { + throw new Pluf_Exception('Faile to create new tenant'); + } + + $client = new Test_Client(array()); + $GLOBALS['_PX_request']->tenant = $tenant; + + $per = new Pluf_RowPermission(); + $per->version = 1; + $per->model_id = $tenant->id; + $per->model_class = 'Pluf_Tenant'; + $per->owner_id = $user->id; + $per->owner_class = 'Pluf_User'; + $per->create(); + } + + /** + * @afterClass + */ + public static function uninstallApps() + { + $m = new Pluf_Migration(array( + 'Pluf', + 'Tenant' + )); + $m->unInstall(); + } + + /** + * Getting tenant tickets + * + * Call tenant to get list of tickets + * + * @test + */ + public function testFindTikcetComments() + { + $client = new Test_Client(array( + array( + 'app' => 'Tenant', + 'regex' => '#^/api/tenant#', + 'base' => '', + 'sub' => include 'Tenant/urls.php' + ), + array( + 'app' => 'User', + 'regex' => '#^/api/user#', + 'base' => '', + 'sub' => include 'User/urls.php' + ) + )); + // login + $response = $client->post('/api/user/login', array( + 'login' => 'test', + 'password' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200, 'Fail to login'); + + // Create ticket + $user = new Pluf_User(); + $user = $user->getUser('test'); + + $t = new Tenant_Ticket(); + $t->subject = 'test'; + $t->description = 'test'; + $t->type = 'bug'; + $t->status = 'new'; + $t->requester = $user; + $t->create(); + + // find comments + $response = $client->get('/api/tenant/current/ticket/' . $t->id . '/comment/find'); + Test_Assert::assertResponseNotNull($response, 'Find result is empty'); + Test_Assert::assertResponseStatusCode($response, 200, 'Find status code is not 200'); + Test_Assert::assertResponsePaginateList($response, 'Find result is not JSON paginated list'); + + // delete + $t->delete(); + } + + /** + * Getting not empety comments + * + * @test + */ + public function testFindTikcetCommentSNotEmpty() + { + $client = new Test_Client(array( + array( + 'app' => 'Tenant', + 'regex' => '#^/api/tenant#', + 'base' => '', + 'sub' => include 'Tenant/urls.php' + ), + array( + 'app' => 'User', + 'regex' => '#^/api/user#', + 'base' => '', + 'sub' => include 'User/urls.php' + ) + )); + // login + $response = $client->post('/api/user/login', array( + 'login' => 'test', + 'password' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200, 'Fail to login'); + + // Create ticket + $user = new Pluf_User(); + $user = $user->getUser('test'); + + $t = new Tenant_Ticket(); + $t->subject = 'test'; + $t->description = 'test'; + $t->type = 'bug'; + $t->status = 'new'; + $t->requester = $user; + $t->create(); + + $c = new Tenant_Comment(); + $c->title = 'test'; + $c->description = 'test'; + $c->author = $user; + $c->ticket = $t; + $c->create(); + + // find comments + $response = $client->get('/api/tenant/current/ticket/' . $t->id . '/comment/find'); + Test_Assert::assertResponseNotNull($response, 'Find result is empty'); + Test_Assert::assertResponseStatusCode($response, 200, 'Find status code is not 200'); + Test_Assert::assertResponsePaginateList($response); + Test_Assert::assertResponseNonEmptyPaginateList($response); + + // delete + $c->delete(); + $t->delete(); + } + + /** + * Getting comment + * + * @test + */ + public function testGetTikcetComment() + { + $client = new Test_Client(array( + array( + 'app' => 'Tenant', + 'regex' => '#^/api/tenant#', + 'base' => '', + 'sub' => include 'Tenant/urls.php' + ), + array( + 'app' => 'User', + 'regex' => '#^/api/user#', + 'base' => '', + 'sub' => include 'User/urls.php' + ) + )); + // login + $response = $client->post('/api/user/login', array( + 'login' => 'test', + 'password' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200, 'Fail to login'); + + // Create ticket + $user = new Pluf_User(); + $user = $user->getUser('test'); + + $t = new Tenant_Ticket(); + $t->subject = 'test'; + $t->description = 'test'; + $t->type = 'bug'; + $t->status = 'new'; + $t->requester = $user; + $t->create(); + + $c = new Tenant_Comment(); + $c->title = 'test'; + $c->description = 'test'; + $c->author = $user; + $c->ticket = $t; + $c->create(); + + // find comments + $response = $client->get('/api/tenant/current/ticket/' . $t->id . '/comment/' . $c->id); + Test_Assert::assertResponseNotNull($response, 'Find result is empty'); + Test_Assert::assertResponseStatusCode($response, 200, 'Find status code is not 200'); + Test_Assert::assertResponseAsModel($response); + Test_Assert::assertResponseNotAnonymousModel($response); + + // delete + $c->delete(); + $t->delete(); + } + + /** + * Creating comment + * + * @test + */ + public function testCreateTikcetComment() + { + $client = new Test_Client(array( + array( + 'app' => 'Tenant', + 'regex' => '#^/api/tenant#', + 'base' => '', + 'sub' => include 'Tenant/urls.php' + ), + array( + 'app' => 'User', + 'regex' => '#^/api/user#', + 'base' => '', + 'sub' => include 'User/urls.php' + ) + )); + // login + $response = $client->post('/api/user/login', array( + 'login' => 'test', + 'password' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200, 'Fail to login'); + + // Create ticket + $user = new Pluf_User(); + $user = $user->getUser('test'); + + $t = new Tenant_Ticket(); + $t->subject = 'test'; + $t->description = 'test'; + $t->type = 'bug'; + $t->status = 'new'; + $t->requester = $user; + $t->create(); + + // find comments + $response = $client->post('/api/tenant/current/ticket/' . $t->id . '/comment/new', array( + 'title' => 'test', + 'description' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200); + $tc = json_decode($response->content, true); + + // find comments + $response = $client->get('/api/tenant/current/ticket/' . $t->id . '/comment/find'); + Test_Assert::assertResponseNotNull($response, 'Find result is empty'); + Test_Assert::assertResponseStatusCode($response, 200, 'Find status code is not 200'); + Test_Assert::assertResponsePaginateList($response); + Test_Assert::assertResponseNonEmptyPaginateList($response); + + // delete + $c = new Tenant_Comment($tc['id']); + $c->delete(); + $t->delete(); + } + + /** + * Creating comment + * + * @test + */ + public function testUpdateTikcetComment() + { + $client = new Test_Client(array( + array( + 'app' => 'Tenant', + 'regex' => '#^/api/tenant#', + 'base' => '', + 'sub' => include 'Tenant/urls.php' + ), + array( + 'app' => 'User', + 'regex' => '#^/api/user#', + 'base' => '', + 'sub' => include 'User/urls.php' + ) + )); + // login + $response = $client->post('/api/user/login', array( + 'login' => 'test', + 'password' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200, 'Fail to login'); + + // Create ticket + $user = new Pluf_User(); + $user = $user->getUser('test'); + + $t = new Tenant_Ticket(); + $t->subject = 'test'; + $t->description = 'test'; + $t->type = 'bug'; + $t->status = 'new'; + $t->requester = $user; + $t->create(); + + // find comments + $response = $client->post('/api/tenant/current/ticket/' . $t->id . '/comment/new', array( + 'title' => 'test', + 'description' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200); + $tc = json_decode($response->content, true); + + // update + $response = $client->post('/api/tenant/current/ticket/' . $t->id . '/comment/' . $tc['id'], array( + 'title' => 'test new title', + 'description' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200); + + // find comments + $response = $client->get('/api/tenant/current/ticket/' . $t->id . '/comment/find'); + Test_Assert::assertResponseNotNull($response, 'Find result is empty'); + Test_Assert::assertResponseStatusCode($response, 200, 'Find status code is not 200'); + Test_Assert::assertResponsePaginateList($response); + Test_Assert::assertResponseNonEmptyPaginateList($response); + + // delete + $c = new Tenant_Comment($tc['id']); + $c->delete(); + $t->delete(); + } + + /** + * Creating comment + * + * @test + */ + public function testDeleteTikcetComment() + { + $client = new Test_Client(array( + array( + 'app' => 'Tenant', + 'regex' => '#^/api/tenant#', + 'base' => '', + 'sub' => include 'Tenant/urls.php' + ), + array( + 'app' => 'User', + 'regex' => '#^/api/user#', + 'base' => '', + 'sub' => include 'User/urls.php' + ) + )); + // login + $response = $client->post('/api/user/login', array( + 'login' => 'test', + 'password' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200, 'Fail to login'); + + // Create ticket + $user = new Pluf_User(); + $user = $user->getUser('test'); + + $t = new Tenant_Ticket(); + $t->subject = 'test'; + $t->description = 'test'; + $t->type = 'bug'; + $t->status = 'new'; + $t->requester = $user; + $t->create(); + + // find comments + $response = $client->post('/api/tenant/current/ticket/' . $t->id . '/comment/new', array( + 'title' => 'test', + 'description' => 'test' + )); + Test_Assert::assertResponseStatusCode($response, 200); + $tc = json_decode($response->content, true); + + // update + $response = $client->delete('/api/tenant/current/ticket/' . $t->id . '/comment/' . $tc['id']); + Test_Assert::assertResponseStatusCode($response, 200); + + // find comments + $response = $client->get('/api/tenant/current/ticket/' . $t->id . '/comment/find'); + Test_Assert::assertResponseNotNull($response, 'Find result is empty'); + Test_Assert::assertResponseStatusCode($response, 200, 'Find status code is not 200'); + Test_Assert::assertResponsePaginateList($response); + Test_Assert::assertResponseEmptyPaginateList($response); + + // delete + $t->delete(); + } +} + diff --git a/tests/phpunit.xml b/tests/phpunit.xml index fb34fc9..e6cc256 100644 --- a/tests/phpunit.xml +++ b/tests/phpunit.xml @@ -2,7 +2,7 @@ - + ./Pluf_Tenant_REST/ ./Tenant_REST/