Merge branch 'master' into validations-architecture-backend
This commit is contained in:
commit
6147fc260d
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
include 'ticket/create.php';
|
||||
|
||||
$ticketControllers = new ControllerGroup();
|
||||
$ticketControllers->setGroupPath('/ticket');
|
||||
|
||||
$ticketControllers->addController(new CreateController);
|
||||
|
||||
$ticketControllers->finalize();
|
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
class CreateController extends Controller {
|
||||
const PATH = '/create';
|
||||
|
||||
private $title ;
|
||||
private $content;
|
||||
private $departmentId;
|
||||
private $language;
|
||||
|
||||
public function handler() {
|
||||
$this->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();
|
||||
}
|
||||
}
|
|
@ -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';
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -9,3 +9,4 @@ require './scripts.rb'
|
|||
|
||||
# TESTS
|
||||
require './user/signup.rb'
|
||||
require './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
|
Loading…
Reference in New Issue