From c4341002efabd64bca94bc68fce8e381f1f29f1f Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Fri, 17 Jun 2016 16:28:38 -0300 Subject: [PATCH 1/8] [AntonyAntonio] - Add initial 'ticket/create' controller [skip ci] --- server/controllers/ticket.php | 9 +++++++++ server/controllers/ticket/create.php | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 server/controllers/ticket.php create mode 100644 server/controllers/ticket/create.php diff --git a/server/controllers/ticket.php b/server/controllers/ticket.php new file mode 100644 index 00000000..95ab2dda --- /dev/null +++ b/server/controllers/ticket.php @@ -0,0 +1,9 @@ +setGroupPath('/ticket'); + +$ticketControllers->addController(new CreateController); + +$ticketControllers->finalize(); \ No newline at end of file diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php new file mode 100644 index 00000000..527c3fcd --- /dev/null +++ b/server/controllers/ticket/create.php @@ -0,0 +1,9 @@ + Date: Fri, 17 Jun 2016 17:16:10 -0300 Subject: [PATCH 2/8] [AntonyAntonio] - Add initial 'ticket/create' controller [skip ci] --- server/controllers/ticket/create.php | 40 +++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index 527c3fcd..51e85dca 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -4,6 +4,44 @@ class CreateController extends Controller { const PATH = '/create'; public function handler(){ - Response::respondError('TEST'); + $title = Controller::request('title'); + $content = Controller::request('content'); + $departmentId = Controller::request('departmentId'); + $language = Controller::request('language'); + if (strlen($title)<3 ){ + Response::respondError('title is so short'); + return; + } + if (strlen($title)>30){ + Response::respondError('title is so long'); + return; + } + if (strlen($content)<5){ + Response::respondError('content is so short'); + return; + } + if (strlen($content)>100){ + Response::respondError('content is so long'); + return; + } + $ticket = new Ticket(); + $ticket->setProperties(array( + 'ticketId' => '', + 'title' => $title, + 'content' => $content, + 'language' => $language, + 'department' => $departmentId, + 'file' => '', + 'date' => date("F j, Y, g:i a"), + 'unread' => false, + 'closed' => false, + 'author' => '', + 'owner'=> '', + 'ownComments' => [] + )); + + $ticket->store(); + Response::respondSuccess(); + } } \ No newline at end of file From ba15c81178a78b2942d6800b532eac0d6b1d29f7 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Fri, 17 Jun 2016 17:44:04 -0300 Subject: [PATCH 3/8] [AntonyAntonio] - Add functionality 'ticket/create' controller [skip ci] --- server/controllers/ticket/create.php | 66 ++++++++++++++++++---------- 1 file changed, 43 insertions(+), 23 deletions(-) diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index 51e85dca..9c709968 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -3,34 +3,57 @@ class CreateController extends Controller { const PATH = '/create'; + private $title ; + private $content; + private $departmentId; + private $language; + public function handler(){ - $title = Controller::request('title'); - $content = Controller::request('content'); - $departmentId = Controller::request('departmentId'); - $language = Controller::request('language'); - if (strlen($title)<3 ){ - Response::respondError('title is so short'); - return; + $this->requestTicketData(); + + $validateResult = $this->validateData(); + + if ($validateResult !== true) { + Response::respondError($validateResult); + } else { + $this->storeTicket(); + + Response::respondSuccess(); } - if (strlen($title)>30){ - Response::respondError('title is so long'); - return; + } + + private function requestTicketData(){ + $this->title = Controller::request('title'); + $this->content = Controller::request('content'); + $this->departmentId = Controller::request('departmentId'); + $this->language = Controller::request('language'); + } + + private function validateData() { + if (strlen($this->title)<3 ){ + return 'title is so short'; } - if (strlen($content)<5){ - Response::respondError('content is so short'); - return; + if (strlen($this->title)>30){ + return 'title is very long'; } - if (strlen($content)>100){ - Response::respondError('content is so long'); - return; + if (strlen($this->content)<5){ + return 'content is so short'; } + if (strlen($this->content)>100){ + return 'content is very long'; + } + + return true; + } + + private function storeTicket() { $ticket = new Ticket(); $ticket->setProperties(array( 'ticketId' => '', - 'title' => $title, - 'content' => $content, - 'language' => $language, - 'department' => $departmentId, + 'title' => $this->title, + 'content' => $this->content, + 'language' => $this->language, + 'department' => $this->departmentId, 'file' => '', 'date' => date("F j, Y, g:i a"), 'unread' => false, @@ -39,9 +62,6 @@ class CreateController extends Controller { 'owner'=> '', 'ownComments' => [] )); - $ticket->store(); - Response::respondSuccess(); - } } \ No newline at end of file From d19f8132e023a169fa9f6a4a54a2190ea75d38f0 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Fri, 17 Jun 2016 18:43:10 -0300 Subject: [PATCH 4/8] [Guillermo Giu] - Add tests for 'ticket/create' controller --- server/controllers/ticket/create.php | 6 ++-- tests/clean_db.sh | 2 +- tests/init.rb | 1 + tests/ticket/create.rb | 51 ++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 tests/ticket/create.rb diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index 9c709968..b6fae956 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -31,15 +31,15 @@ class CreateController extends Controller { private function validateData() { if (strlen($this->title)<3 ){ - return 'title is so short'; + return 'title is too short'; } if (strlen($this->title)>30){ return 'title is very long'; } if (strlen($this->content)<5){ - return 'content is so short'; + return 'content is too short'; } - if (strlen($this->content)>100){ + if (strlen($this->content)>500){ return 'content is very long'; } diff --git a/tests/clean_db.sh b/tests/clean_db.sh index e0b432ae..d99eb4c2 100755 --- a/tests/clean_db.sh +++ b/tests/clean_db.sh @@ -1,7 +1,7 @@ #!/bin/bash # DELETE ALL TABLES -TABLES=$(mysql -u root development -e "SHOW TABLES IN os_dev;" | awk '{ print $1}' | grep -v '^Tables') +TABLES=$(mysql -u root development -e "SHOW TABLES IN development;" | awk '{ print $1}' | grep -v '^Tables') for t in $TABLES do diff --git a/tests/init.rb b/tests/init.rb index f09d7691..86e90a75 100644 --- a/tests/init.rb +++ b/tests/init.rb @@ -9,3 +9,4 @@ require './scripts.rb' # TESTS require './user/signup.rb' +require './ticket/create.rb' diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb new file mode 100644 index 00000000..4e82416a --- /dev/null +++ b/tests/ticket/create.rb @@ -0,0 +1,51 @@ +describe '/user/login' do + it 'should fail if title is too short' do + result = request('/ticket/create',{ + title: 'GG' + }) + (result['status']).should.equal('fail') + (result['message']).should.equal('title is too short') + + end + + it 'should fail if title is very long' do + result = request('/ticket/create',{ + title: 'I WISH I WAS THE MONSTER YOU THINK I AM. -Tyrion' + }) + (result['status']).should.equal('fail') + (result['message']).should.equal('title is very long') + + end + + it 'should fail if content is too short' do + result = request('/ticket/create',{ + title: 'Winter is coming', + content: 'Test' + }) + (result['status']).should.equal('fail') + (result['message']).should.equal('content is too short') + end + + it 'should fail if content is very long' do + long_text = '' + 600.times {long_text << 'a'} + + result = request('/ticket/create',{ + title: 'Winter is coming', + content: long_text + }) + (result['status']).should.equal('fail') + (result['message']).should.equal('content is very long') + + end + + it 'should create ticket if pass data is valid' do + result = request('/ticket/create',{ + title: 'Winter is coming', + content: 'The north remembers' + }) + (result['status']).should.equal('success') + ticket = $database.getRow('tickets','Winter is coming','title') + (ticket['content']).should.equal('The north remembers') + end +end \ No newline at end of file From 9295554dd165028d19f77f0b33d2021cb374356b Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 21 Jun 2016 00:11:52 -0300 Subject: [PATCH 5/8] (Guillermo) Create path /ticket/create --- server/controllers/ticket/create.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index b6fae956..d192d56b 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -8,7 +8,7 @@ class CreateController extends Controller { private $departmentId; private $language; - public function handler(){ + public function handler() { $this->requestTicketData(); $validateResult = $this->validateData(); @@ -22,7 +22,7 @@ class CreateController extends Controller { } } - private function requestTicketData(){ + private function requestTicketData() { $this->title = Controller::request('title'); $this->content = Controller::request('content'); $this->departmentId = Controller::request('departmentId'); @@ -30,16 +30,16 @@ class CreateController extends Controller { } private function validateData() { - if (strlen($this->title)<3 ){ + if (strlen($this->title) < 3) { return 'title is too short'; } - if (strlen($this->title)>30){ + if (strlen($this->title) > 30) { return 'title is very long'; } - if (strlen($this->content)<5){ + if (strlen($this->content) < 5) { return 'content is too short'; } - if (strlen($this->content)>500){ + if (strlen($this->content) > 500) { return 'content is very long'; } From 839265e852fb637265576b80880f1becf35dc156 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 21 Jun 2016 00:19:44 -0300 Subject: [PATCH 6/8] (Guillermo) Create path /ticket/create --- server/controllers/ticket/create.php | 8 ++++---- server/models/ERRORS.php | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index d192d56b..e9ae2698 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -31,16 +31,16 @@ class CreateController extends Controller { private function validateData() { if (strlen($this->title) < 3) { - return 'title is too short'; + Response::respondError(ERRORS::SHORT_TITLE); } if (strlen($this->title) > 30) { - return 'title is very long'; + Response::respondError(ERRORS::LONG_TITLE); } if (strlen($this->content) < 5) { - return 'content is too short'; + Response::respondError(ERRORS::SHORT_CONTENT); } if (strlen($this->content) > 500) { - return 'content is very long'; + Response::respondError(ERRORS::LONG_CONTENT); } return true; diff --git a/server/models/ERRORS.php b/server/models/ERRORS.php index 144914e9..76d29f3c 100644 --- a/server/models/ERRORS.php +++ b/server/models/ERRORS.php @@ -2,4 +2,8 @@ class ERRORS { const INVALID_CREDENTIALS = 'User or password is not defined'; const SESSION_EXISTS = 'User is already logged in'; + const SHORT_TITLE = 'Title is too short'; + const LONG_TITLE = 'Title is very long'; + const SHORT_CONTENT = 'Content is too short'; + const LONG_CONTENT = 'Content is very long'; } From 8253577cc3b6e13d423e484a61498cea9576a9f6 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 21 Jun 2016 02:00:31 -0300 Subject: [PATCH 7/8] (Guillermo) Create path /ticket/create X --- server/controllers/ticket/create.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/controllers/ticket/create.php b/server/controllers/ticket/create.php index e9ae2698..845b415c 100644 --- a/server/controllers/ticket/create.php +++ b/server/controllers/ticket/create.php @@ -31,16 +31,16 @@ class CreateController extends Controller { private function validateData() { if (strlen($this->title) < 3) { - Response::respondError(ERRORS::SHORT_TITLE); + return ERRORS::SHORT_TITLE; } if (strlen($this->title) > 30) { - Response::respondError(ERRORS::LONG_TITLE); + return ERRORS::LONG_TITLE; } if (strlen($this->content) < 5) { - Response::respondError(ERRORS::SHORT_CONTENT); + return ERRORS::SHORT_CONTENT; } if (strlen($this->content) > 500) { - Response::respondError(ERRORS::LONG_CONTENT); + return ERRORS::LONG_CONTENT; } return true; From c0aeb36b1521571f554a19f87bd93f98a691bbd1 Mon Sep 17 00:00:00 2001 From: AntonyAntonio Date: Tue, 21 Jun 2016 20:16:43 -0300 Subject: [PATCH 8/8] (Guillermo) Create path /ticket/create 2.0 --- tests/ticket/create.rb | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index 4e82416a..8742c287 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -3,8 +3,9 @@ describe '/user/login' do result = request('/ticket/create',{ title: 'GG' }) + (result['status']).should.equal('fail') - (result['message']).should.equal('title is too short') + (result['message']).should.equal('Title is too short') end @@ -12,8 +13,9 @@ describe '/user/login' do result = request('/ticket/create',{ title: 'I WISH I WAS THE MONSTER YOU THINK I AM. -Tyrion' }) + (result['status']).should.equal('fail') - (result['message']).should.equal('title is very long') + (result['message']).should.equal('Title is very long') end @@ -22,8 +24,9 @@ describe '/user/login' do title: 'Winter is coming', content: 'Test' }) + (result['status']).should.equal('fail') - (result['message']).should.equal('content is too short') + (result['message']).should.equal('Content is too short') end it 'should fail if content is very long' do @@ -34,8 +37,9 @@ describe '/user/login' do title: 'Winter is coming', content: long_text }) + (result['status']).should.equal('fail') - (result['message']).should.equal('content is very long') + (result['message']).should.equal('Content is very long') end @@ -44,6 +48,7 @@ describe '/user/login' do title: 'Winter is coming', content: 'The north remembers' }) + (result['status']).should.equal('success') ticket = $database.getRow('tickets','Winter is coming','title') (ticket['content']).should.equal('The north remembers')