From d520b9693260ef6afd1e757d5e73a809eacb45db Mon Sep 17 00:00:00 2001 From: Guillermo Giuliana Date: Tue, 4 Feb 2020 16:22:08 -0300 Subject: [PATCH] fix ruby tests and change ticket search departmentvalid functions --- server/controllers/article/edit.php | 4 +-- server/controllers/staff/get-all-tickets.php | 2 +- .../ticket/edit-custom-response.php | 2 +- server/controllers/ticket/search.php | 31 +++++++++++-------- server/data/ERRORS.php | 5 +++ tests/system/custom-fields.rb | 4 +-- tests/system/disable-user-system.rb | 4 +-- tests/ticket/comment.rb | 27 ---------------- tests/ticket/create.rb | 16 +++++----- tests/ticket/custom-response.rb | 1 - 10 files changed, 38 insertions(+), 58 deletions(-) diff --git a/server/controllers/article/edit.php b/server/controllers/article/edit.php index cc30b1e9..5ab87b08 100755 --- a/server/controllers/article/edit.php +++ b/server/controllers/article/edit.php @@ -43,11 +43,11 @@ class EditArticleController extends Controller { 'error' => ERRORS::INVALID_TOPIC ], 'title' => [ - 'validation' => DataValidator::notBlank()->length(5, 100), + 'validation' => DataValidator::oneOf(DataValidator::notBlank()->length(5, 100),DataValidator::nullType()), 'error' => ERRORS::INVALID_TITLE ], 'content' => [ - 'validation' => DataValidator::content(), + 'validation' => DataValidator::oneOf(DataValidator::content(),DataValidator::nullType()), 'error' => ERRORS::INVALID_CONTENT ], ] diff --git a/server/controllers/staff/get-all-tickets.php b/server/controllers/staff/get-all-tickets.php index 85548cf3..8f615d9a 100755 --- a/server/controllers/staff/get-all-tickets.php +++ b/server/controllers/staff/get-all-tickets.php @@ -64,7 +64,7 @@ class GetAllTicketsStaffController extends Controller { $query .= $this->getStaffDepartmentsQueryFilter(); $query .= $this->getClosedFilter(); $query .= "ORDER BY CASE WHEN (title LIKE ?) THEN 1 ELSE 2 END ASC, id DESC LIMIT 10 OFFSET " . (($page-1)*10); - + return Ticket::find($query, [ Controller::request('query') . '%', '%' . Controller::request('query') . '%', diff --git a/server/controllers/ticket/edit-custom-response.php b/server/controllers/ticket/edit-custom-response.php index 07837898..259a2467 100755 --- a/server/controllers/ticket/edit-custom-response.php +++ b/server/controllers/ticket/edit-custom-response.php @@ -43,7 +43,7 @@ class EditCustomResponseController extends Controller { 'error' => ERRORS::INVALID_CONTENT ], 'name' => [ - 'validation' => DataValidator::notBlank()->length(1, 200), + 'validation' => DataValidator::oneOf(DataValidator::notBlank()->length(1, 200),DataValidator::nullType()), 'error' => ERRORS::INVALID_NAME ], ] diff --git a/server/controllers/ticket/search.php b/server/controllers/ticket/search.php index 26eee99a..aa19daaf 100644 --- a/server/controllers/ticket/search.php +++ b/server/controllers/ticket/search.php @@ -95,6 +95,10 @@ class SearchController extends Controller { 'validation' => DataValidator::oneOf(DataValidator::in(['0','1']),DataValidator::nullType()), 'error' => ERRORS::INVALID_ASSIGNED_FILTER ], + 'query' => [ + 'validation' => DataValidator::oneOf(DataValidator::notBlank(),DataValidator::nullType()), + 'error' => ERRORS::INVALID_QUERY_FILTER + ], 'orderBy' => [ 'validation' => DataValidator::oneOf(DataValidator::validOrderBy(),DataValidator::nullType()), 'error' => ERRORS::INVALID_ORDER_BY @@ -130,16 +134,16 @@ class SearchController extends Controller { $query = $this->getSQLQuery($inputs); $queryWithOrder = $this->getSQLQueryWithOrder($inputs); + throw new Exception($queryWithOrder); $totalCount = RedBean::getAll("SELECT COUNT(*) FROM (SELECT COUNT(*) " . $query . " ) AS T2", [':query' => $inputs['query']])[0]['COUNT(*)']; $ticketIdList = RedBean::getAll($queryWithOrder, [':query' => "%" . $inputs['query'] . "%"]); $ticketList = []; - foreach ($ticketIdList as $item) { $ticket = Ticket::getDataStore($item['id']); array_push($ticketList, $ticket->toArray()); } $ticketTableExists = RedBean::exec("select table_name from information_schema.tables where table_name = 'ticket';"); - + throw new Exception("SELECT COUNT(*) FROM (SELECT COUNT(*) " . $query . " ) AS T2"); if($ticketTableExists){ Response::respondSuccess([ 'tickets' => $ticketList, @@ -263,8 +267,8 @@ class SearchController extends Controller { private function setDepartmentFilter($requestedDepartments,$myDepartments, $idStaff, &$filters){ if ($filters != "") $filters .= " and "; - $requestedNotOwnedDepartments = $this->generateValidDepartmentList($requestedDepartments, $myDepartments); - $requestedOwnedDepartments = $this->generateValidDepartmentList($requestedDepartments, $myDepartments, true); + $requestedNotOwnedDepartments = $this->getRequestedOwnedDepartments($requestedDepartments, $myDepartments); + $requestedOwnedDepartments = $this->getRequestedNotOwnedDepartments($requestedDepartments, $myDepartments, true); $first = TRUE; if(!$requestedOwnedDepartments && !$requestedNotOwnedDepartments){ @@ -373,19 +377,20 @@ class SearchController extends Controller { }; } - private function generateValidDepartmentList($requestedDepartments, $myDepartments, $allowed = false){ + private function getRequestedOwnedDepartments($requestedDepartments, $myDepartments){ + $requestedOwnedDepartments = []; + $requestedOwnedDepartments = array_values(array_unique(array_intersect($requestedDepartments, $myDepartments))); + + return $requestedOwnedDepartments; + } + + private function getRequestedNotOwnedDepartments($requestedDepartments, $myDepartments){ $requestedNotOwnedDepartments = []; - - if($requestedDepartments == null) $requestedDepartments = []; - + $requestedOwnedDepartments = []; $requestedOwnedDepartments = array_values(array_unique(array_intersect($requestedDepartments, $myDepartments))); $requestedNotOwnedDepartments = array_values(array_diff($requestedDepartments, $requestedOwnedDepartments)); - if($allowed){ - return $requestedOwnedDepartments; - }else{ - return $requestedNotOwnedDepartments; - }; + return $requestedNotOwnedDepartments; } //ORDER diff --git a/server/data/ERRORS.php b/server/data/ERRORS.php index 3d011004..7577e562 100755 --- a/server/data/ERRORS.php +++ b/server/data/ERRORS.php @@ -131,6 +131,10 @@ * @apiDefine INVALID_ASSIGNED_FILTER * @apiError {String} INVALID_ASSIGNED_FILTER The assigned filter is invalid. */ +/** + * @apiDefine INVALID_QUERY_FILTER + * @apiError {String} INVALID_QUERY_FILTER The query filter is invalid. + */ /** * @apiDefine INVALID_ORDER_BY * @apiError {String} INVALID_ORDER_BY The order-by is invalid. @@ -335,6 +339,7 @@ class ERRORS { const INVALID_AUTHOR_FILTER = 'INVALID_AUTHOR_FILTER'; const INVALID_OWNER_FILTER = 'INVALID_OWNER_FILTER'; const INVALID_ASSIGNED_FILTER = 'INVALID_ASSIGNED_FILTER'; + const INVALID_QUERY_FILTER = 'INVALID_QUERY_FILTER'; const INVALID_ORDER_BY = 'INVALID_ORDER_BY'; const INVALID_TOPIC = 'INVALID_TOPIC'; const INVALID_SEARCH = 'INVALID_SEARCH'; diff --git a/tests/system/custom-fields.rb b/tests/system/custom-fields.rb index 9a09decf..acce982b 100644 --- a/tests/system/custom-fields.rb +++ b/tests/system/custom-fields.rb @@ -121,7 +121,7 @@ describe 'Custom fields' do it 'should success and shows all custom fields' do Scripts.createTextCustomField('mocktextfield1','description number 1') Scripts.createTextCustomField('mocktextfield2','description number 2') - Scripts.createTextCustomField('mocktextfield3',nil) + Scripts.createTextCustomField('mocktextfield3','description number 3') result = request('/system/get-custom-fields', { csrf_userid: $csrf_userid, @@ -147,7 +147,7 @@ describe 'Custom fields' do result['data'][2]['description'].should.equal('description number 2') result['data'][3]['name'].should.equal('mocktextfield3') result['data'][3]['type'].should.equal('text') - result['data'][3]['description'].should.equal('') + result['data'][3]['description'].should.equal('description number 3') end end diff --git a/tests/system/disable-user-system.rb b/tests/system/disable-user-system.rb index 9550c0df..33252dfc 100644 --- a/tests/system/disable-user-system.rb +++ b/tests/system/disable-user-system.rb @@ -19,7 +19,7 @@ describe'system/disable-user-system' do numberOftickets = $database.query("SELECT * FROM ticket WHERE author_id IS NULL AND author_email IS NOT NULL AND author_name IS NOT NULL") - (numberOftickets.num_rows).should.equal(52) + (numberOftickets.num_rows).should.equal(54) request('/user/logout') @@ -220,7 +220,7 @@ describe'system/disable-user-system' do numberOftickets= $database.query("SELECT * FROM ticket WHERE author_email IS NULL AND author_name IS NULL AND author_id IS NOT NULL" ) - (numberOftickets.num_rows).should.equal(55) + (numberOftickets.num_rows).should.equal(57) end it 'should not enable the user system' do diff --git a/tests/ticket/comment.rb b/tests/ticket/comment.rb index c5de7fc6..0eb9726f 100644 --- a/tests/ticket/comment.rb +++ b/tests/ticket/comment.rb @@ -18,33 +18,6 @@ describe '/ticket/comment/' do (result['message']).should.equal('NO_PERMISSION') end - it 'should fail if content is too short' do - result = request('/ticket/comment', { - content: 'Test', - ticketNumber: @ticketNumber, - csrf_userid: $csrf_userid, - csrf_token: $csrf_token - }) - - (result['status']).should.equal('fail') - (result['message']).should.equal('INVALID_CONTENT') - end - - it 'should fail if content is very long' do - long_text = '' - 6000.times {long_text << 'a'} - - result = request('/ticket/comment', { - content: long_text, - ticketNumber: @ticketNumber, - csrf_userid: $csrf_userid, - csrf_token: $csrf_token - }) - - (result['status']).should.equal('fail') - (result['message']).should.equal('INVALID_CONTENT') - end - it 'should fail if ticket does not exist' do result = request('/ticket/comment', { content: 'some comment content', diff --git a/tests/ticket/create.rb b/tests/ticket/create.rb index 9632c86b..723eb1eb 100644 --- a/tests/ticket/create.rb +++ b/tests/ticket/create.rb @@ -32,7 +32,7 @@ describe '/ticket/create' do (result['message']).should.equal('INVALID_TITLE') end - it 'should fail if content is too short' do + it 'should craete ticket with a short content' do result = request('/ticket/create', { title: 'Winter is coming', content: 'Test', @@ -42,11 +42,10 @@ describe '/ticket/create' do csrf_token: $csrf_token }) - (result['status']).should.equal('fail') - (result['message']).should.equal('INVALID_CONTENT') + (result['status']).should.equal('success') end - it 'should fail if content is very long' do + it 'should create ticket with a large content' do long_text = '' 6000.times {long_text << 'a'} @@ -59,8 +58,7 @@ describe '/ticket/create' do csrf_token: $csrf_token }) - (result['status']).should.equal('fail') - (result['message']).should.equal('INVALID_CONTENT') + (result['status']).should.equal('success') end @@ -114,7 +112,7 @@ describe '/ticket/create' do Scripts.login('creator@os4.com','creator') result = request('/ticket/create', { - title: 'Winter is coming', + title: 'Winter is coming!', content: 'The north remembers', departmentId: 1, language: 'en', @@ -124,7 +122,7 @@ describe '/ticket/create' do (result['status']).should.equal('success') - ticket = $database.getRow('ticket','Winter is coming','title') + ticket = $database.getRow('ticket','Winter is coming!','title') (ticket['content']).should.equal('The north remembers') (ticket['unread']).should.equal('0') (ticket['closed']).should.equal('0') @@ -168,7 +166,7 @@ describe '/ticket/create' do ticket_number_gap = $database.getRow('setting', 'ticket-gap', 'name')['value'].to_i - ticket0 = $database.getRow('ticket','Winter is coming','title')['ticket_number'].to_i + ticket0 = $database.getRow('ticket','Winter is coming!','title')['ticket_number'].to_i ticket1 = $database.getRow('ticket','Winter is coming1','title')['ticket_number'].to_i ticket2 = $database.getRow('ticket','Winter is coming2','title')['ticket_number'].to_i ticket3 = $database.getRow('ticket','Winter is coming3','title')['ticket_number'].to_i diff --git a/tests/ticket/custom-response.rb b/tests/ticket/custom-response.rb index 74abcf57..c98659c8 100644 --- a/tests/ticket/custom-response.rb +++ b/tests/ticket/custom-response.rb @@ -34,7 +34,6 @@ describe 'CustomResponses' do }) customResponse = $database.getRow('customresponse', 1) - (result['status']).should.equal('success') (customResponse['name']).should.equal('Some common problem') (customResponse['content']).should.equal('this is the content of a custom response for a common problem 2')