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..845b415c --- /dev/null +++ b/server/controllers/ticket/create.php @@ -0,0 +1,67 @@ +requestTicketData(); + + $validateResult = $this->validateData(); + + if ($validateResult !== true) { + Response::respondError($validateResult); + } else { + $this->storeTicket(); + + Response::respondSuccess(); + } + } + + 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 ERRORS::SHORT_TITLE; + } + if (strlen($this->title) > 30) { + return ERRORS::LONG_TITLE; + } + if (strlen($this->content) < 5) { + return ERRORS::SHORT_CONTENT; + } + if (strlen($this->content) > 500) { + return ERRORS::LONG_CONTENT; + } + + return true; + } + + private function storeTicket() { + $ticket = new Ticket(); + $ticket->setProperties(array( + 'ticketId' => '', + 'title' => $this->title, + 'content' => $this->content, + 'language' => $this->language, + 'department' => $this->departmentId, + 'file' => '', + 'date' => date("F j, Y, g:i a"), + 'unread' => false, + 'closed' => false, + 'author' => '', + 'owner'=> '', + 'ownComments' => [] + )); + $ticket->store(); + } +} \ No newline at end of file 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'; } 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..8742c287 --- /dev/null +++ b/tests/ticket/create.rb @@ -0,0 +1,56 @@ +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