diff --git a/server/controllers/system/edit-department.php b/server/controllers/system/edit-department.php index 7d96e81a..3b2279e4 100755 --- a/server/controllers/system/edit-department.php +++ b/server/controllers/system/edit-department.php @@ -21,6 +21,7 @@ DataValidator::with('CustomValidations', true); * @apiUse NO_PERMISSION * @apiUse INVALID_NAME * @apiUse INVALID_DEPARTMENT + * @apiUse NAME_ALREADY_USED * * @apiSuccess {Object} data Empty object * @@ -39,10 +40,7 @@ class EditDepartmentController extends Controller { 'error' => ERRORS::INVALID_DEPARTMENT ], 'name' => [ - 'validation' => DataValidator::AllOf( - DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_NAME, LengthConfig::MAX_LENGTH_NAME), - DataValidator::ValidDepartmentName() - ), + 'validation' => DataValidator::notBlank()->length(LengthConfig::MIN_LENGTH_NAME, LengthConfig::MAX_LENGTH_NAME), 'error' => ERRORS::INVALID_NAME ], ] @@ -55,6 +53,11 @@ class EditDepartmentController extends Controller { $private = Controller::request('private'); $departmentInstance = Department::getDataStore($departmentId); + $createdDepartment = Department::getDataStore($newName, 'name'); + + if(!$createdDepartment->isNull() && $createdDepartment->name !== $departmentInstance->name){ + throw new RequestException(ERRORS::NAME_ALREADY_USED); + } if($private && $departmentId == Setting::getSetting('default-department-id')->getValue()){ throw new RequestException(ERRORS::DEFAULT_DEPARTMENT_CAN_NOT_BE_PRIVATE); diff --git a/tests/scripts.rb b/tests/scripts.rb index 925e0e47..1c5d6610 100644 --- a/tests/scripts.rb +++ b/tests/scripts.rb @@ -162,11 +162,12 @@ class Scripts }) end - def self.createDepartment(nameDepartment = 'validnameDepartment') + def self.createDepartment(nameDepartment, isPrivate = 0) request('/system/add-department', { csrf_userid: $csrf_userid, csrf_token: $csrf_token, - name: nameDepartment + name: nameDepartment, + private: isPrivate }) end diff --git a/tests/system/edit-department.rb b/tests/system/edit-department.rb index 85700e40..a6f2280e 100644 --- a/tests/system/edit-department.rb +++ b/tests/system/edit-department.rb @@ -44,19 +44,45 @@ describe'system/edit-department' do result['status'].should.equal('fail') result['message'].should.equal('INVALID_NAME') + end + + it 'should success if you change for the same name' do + Scripts.createDepartment('thisisAnewName') + + department = $database.getLastRow('department') + (department['private']).should.equal(0) + + + result = request('/system/edit-department', { + csrf_userid: $csrf_userid, + csrf_token: $csrf_token, + name: department['name'], + departmentId: department['id'], + private:1 + }) + + row = $database.getRow('department', 'thisisAnewName', 'name') + (row['private']).should.equal(1) + + result['status'].should.equal('success') + + end + + it 'shouild fail if you use an used name' do + Scripts.createDepartment('thistitleisunique') + lastDepartment = $database.getLastRow('department') result = request('/system/edit-department', { csrf_userid: $csrf_userid, csrf_token: $csrf_token, - name: lastDepartment['name'], - departmentId: 4 + name: 'thisisAnewName', + departmentId: lastDepartment['id'], + private:1 }) - result['status'].should.equal('fail') - result['message'].should.equal('INVALID_NAME') - + result['message'].should.equal('NAME_ALREADY_USED') end end