/**
  * test minLength validation.
  *
  * @return void
  */
 public function testMinLength()
 {
     $validator = new Validator();
     $validator->provider('purifier', 'App\\Model\\Validation\\PurifierValidator')->add('biography', 'minLength', ['rule' => ['purifierMinLength', 10], 'provider' => 'purifier', 'message' => 'You fail']);
     $expected = ['biography' => ['minLength' => 'You fail']];
     $this->assertEquals($expected, $validator->errors(['biography' => 'LessThan9']));
     $this->assertEmpty($validator->errors(['biography' => 'more than 10 characteres']));
 }
 /**
  * List add
  * 
  * @param controle_id 
  * @author Leonardo Cavalcante do Prado, Angelo Gustavo, Gabriel Rafal
  * @return void or, if a post method is called, the redirect action
  */
 public function list_add($controle_id = null, $separacao_id = null)
 {
     $query = $this->Amostrarepasse->find('all')->where(['fk_controle' => $controle_id]);
     $this->paginate = ['maxLimit' => 5];
     $this->set('amostras', $this->paginate($query));
     $this->set('controle_id', $controle_id);
     $this->set('separacao_id', $separacao_id);
     $amostrarepasse = $this->Amostrarepasse->newEntity();
     $validator = new Validator();
     $validator->add('n_amostra', 'custom', ['rule' => function ($num_amostra) {
         $query = $this->Amostrarepasse->find('All')->where(['n_amostra' => $num_amostra]);
         if ($query->isEmpty()) {
             return true;
         } else {
             return false;
         }
     }]);
     if ($this->request->is('post')) {
         $amostrarepasse = $this->Amostrarepasse->patchEntity($amostrarepasse, $this->request->data);
         $amostrarepasse->set(['fk_controle' => $controle_id]);
         $error = $validator->errors($amostrarepasse->toArray());
         if (empty($error)) {
             $this->Amostrarepasse->save($amostrarepasse);
             $this->Flash->success('O repasse da amostra foi salvo.');
             return $this->redirect(['action' => 'list_add', $controle_id, $separacao_id]);
         } else {
             $this->Flash->error('O repasse da amostra não pôde ser salvo, por favor, tente novamente.');
         }
     }
     $this->set(compact('amostrarepasse'));
     $this->set('_serialize', ['amostrarepasse']);
 }
 public function add()
 {
     /*
     		if($this->request->is('post')){
     			$person = $this->Persons->newEntity();
     			$person = $this->Persons->patchEntity($person, $this->request->data);
     			if($this->Persons->save($person)){
     				return $this->redirect(['action' => 'index']);
     			}
     			if ($person->errors()){
     				$this->Flash->error('please check entered values...');
     			}
     		}
     */
     $person = $this->Persons->newEntity();
     $this->set('person', $person);
     if ($this->request->is('post')) {
         $validator = new Validator();
         $validator->add('age', 'comparison', ['rule' => ['comparison', '>', 20]]);
         $errors = $validator->errors($this->request->data);
         if (!empty($errors)) {
             $this->Flash->error('comparison error');
         } else {
             $person = $this->Persons->patchEntity($person, $this->request->data);
             if ($this->Persons->save($person)) {
                 return $this->redirect(['action' => 'index']);
             }
         }
     }
 }
 /**
  * List add
  * 
  * @param separacoes_id 
  * @author Leonardo Cavalcante do Prado
  * @return void or, if a post method is called, the redirect action
  */
 public function list_add($separacoes_id = null)
 {
     $query = $this->Costagem->find('all')->where(['fk_separacoes' => $separacoes_id]);
     $this->paginate = ['maxLimit' => 5];
     $this->set('costagens', $this->paginate($query));
     $this->set('separacoes_id', $separacoes_id);
     $costagem = $this->Costagem->newEntity();
     $validator = new Validator();
     $validator->add('n_amostra', 'custom', ['rule' => function ($num_amostra) {
         $query = $this->Costagem->find('All')->where(['n_amostra' => $num_amostra]);
         if ($query->isEmpty()) {
             return true;
         } else {
             return false;
         }
     }]);
     if ($this->request->is('post')) {
         $costagem = $this->Costagem->patchEntity($costagem, $this->request->data);
         $costagem->set(['fk_separacoes' => $separacoes_id]);
         $errors = $validator->errors($costagem->toArray());
         if (empty($errors)) {
             $this->Costagem->save($costagem);
             $this->Flash->success('A contagem foi salva.');
             return $this->redirect(['action' => 'list_add', $separacoes_id]);
         } else {
             $this->Flash->error('A contagem não pôde ser salva, por favor, tente novamente.');
         }
     }
     $this->set(compact('costagem'));
     $this->set('_serialize', ['costagem']);
 }
 /**
  * Validates the internal properties using a validator object and returns any
  * validation errors found.
  *
  * @param \Cake\Validation\Validator $validator The validator to use when validating the entity.
  * @return array
  */
 public function validate(Validator $validator)
 {
     $data = $this->_properties;
     $new = $this->isNew();
     $validator->provider('entity', $this);
     $this->errors($validator->errors($data, $new === null ? true : $new));
     return $this->_errors;
 }
 /**
  * test maxDimension with success validation.
  *
  * @return void
  */
 public function testMaxDimensionSuccess()
 {
     $file = ['name' => 'avatar.png', 'tmp_name' => TEST_WWW_ROOT . 'img/avatar.png', 'error' => UPLOAD_ERR_OK, 'type' => 'image/png', 'size' => 201];
     $expected = ['avatar_file' => ['maxDimension' => 'You fail']];
     $this->assertEquals($expected, $this->validator->errors(['avatar_file' => $file]));
     $validator = new Validator();
     $validator->provider('upload', 'App\\Model\\Validation\\UploadValidator')->add('avatar_file', 'maxDimension', ['rule' => ['maxDimension', 160, 160], 'provider' => 'upload', 'message' => 'You fail']);
     $this->assertEmpty($validator->errors(['avatar_file' => $file]));
 }
Beispiel #7
0
 public function validateUser()
 {
     $this->_validateError = 'ok';
     $validator = new Validator();
     $validator->requirePresence('email')->add('email', 'validFormat', ['rule' => 'email', 'message' => 'E-mail must be valid'])->add('password', 'length', ['rule' => ['minLength', 6]])->requirePresence('name')->notEmpty('name', 'We need your name.')->requirePresence('username')->notEmpty('username', 'We need Your Username.')->add('username', 'length', ['rule' => ['minLength', 6]]);
     $errors = $validator->errors($this->_user);
     if (!empty($errors)) {
         $this->_validateError = $errors;
         return false;
     } else {
         return true;
     }
 }
Beispiel #8
0
 /**
  * Contact page.
  *
  * @return \Cake\Network\Response|void
  */
 public function index()
 {
     $contact = ['schema' => ['name' => ['type' => 'string', 'length' => 100], 'email' => ['type' => 'email', 'length' => 100], 'subject' => ['type' => 'string', 'length' => 255], 'message' => ['type' => 'string']], 'required' => ['name' => 1, 'email' => 1, 'message' => 1]];
     if ($this->request->is('post')) {
         $validator = new Validator();
         $validator->notEmpty('email', __('You need to put your E-mail.'))->add('email', 'validFormat', ['rule' => 'email', 'message' => __("You must specify a valid E-mail address.")])->notEmpty('name', __('You need to put your name.'))->notEmpty('message', __("You need to give a message."))->add('message', 'minLength', ['rule' => ['minLength', 10], 'message' => __("Your message can not contain less than {0} characters.", 10)]);
         $contact['errors'] = $validator->errors($this->request->data());
         if (empty($contact['errors'])) {
             $viewVars = ['ip' => $this->request->clientIp()];
             $viewVars = array_merge($this->request->data(), $viewVars);
             $this->getMailer('Contact')->send('contact', [$viewVars]);
             $this->Flash->success(__("Your message has been sent successfully, you will get a response shortly !"));
             return $this->redirect(['controller' => 'pages', 'action' => 'home']);
         }
     }
     $this->set(compact('contact'));
 }
Beispiel #9
0
 /**
  * Contact page.
  *
  * @return \Cake\Network\Response|void
  */
 public function index()
 {
     $contact = ['schema' => ['name' => ['type' => 'string', 'length' => 100], 'email' => ['type' => 'string', 'length' => 100], 'subject' => ['type' => 'string', 'length' => 255], 'message' => ['type' => 'string']], 'required' => ['name' => 1, 'email' => 1, 'message' => 1]];
     if ($this->request->is('post')) {
         $validator = new Validator();
         $validator->notEmpty('email', __('You need to put your E-mail.'))->add('email', 'validFormat', ['rule' => 'email', 'message' => __("You must specify a valid E-mail address.")])->notEmpty('name', __('You need to put your name.'))->notEmpty('message', __("You need to give a message."))->add('message', 'minLength', ['rule' => ['minLength', 10], 'message' => __("Your message can not contain less than {0} characters.", 10)]);
         $contact['errors'] = $validator->errors($this->request->data());
         if (empty($contact['errors'])) {
             $viewVars = ['ip' => $this->request->clientIp()];
             $viewVars = array_merge($this->request->data(), $viewVars);
             $email = new Email();
             $email->profile('default')->template('contact')->emailFormat('html')->from(['*****@*****.**' => 'Contact Form'])->to(Configure::read('Author.email'))->subject($viewVars['subject'] ? $viewVars['subject'] : 'Someone has contacted you')->viewVars($viewVars)->send();
             $this->Flash->success(__("Your message has been sent successfully, you will get a response shortly !"));
             return $this->redirect('/');
         }
     }
     $this->set(compact('contact'));
 }
 public function edit($id = NULL, $slug = NULL)
 {
     //Post Types
     $this->loadModel('Post_Type');
     $post_type_ids = $this->Post_Type->find('list');
     $this->set(compact('post_type_ids'));
     $this->loadModel('Categories');
     $category = $this->Categories->get($id);
     $this->set('title_for_layout', 'Category : ' . $category->title);
     if (empty($category)) {
         throw new NotFoundException('Could not find that category.');
     } else {
         $this->set(compact('category'));
     }
     if ($this->request->is(['post', 'put'])) {
         //Validation
         $validator = new Validator();
         $validator->requirePresence('post_type_id')->notEmpty('post_type_id', 'A post type is required.')->requirePresence('title')->notEmpty('title', 'A title is required.');
         $errors_array = $validator->errors($this->request->data());
         $error_msg = [];
         foreach ($errors_array as $errors) {
             if (is_array($errors)) {
                 foreach ($errors as $error) {
                     $error_msg[] = $error;
                 }
             } else {
                 $error_msg[] = $errors;
             }
         }
         if (!empty($error_msg)) {
             $this->Flash->set('Please fix the following error(s): ' . implode('\\n \\r', $error_msg), ['element' => 'alert-box', 'params' => ['class' => 'danger']]);
             return $this->redirect(['action' => 'edit', $id]);
         }
         $category->slug = Text::slug(strtolower($this->request->data('title')));
         //Save
         $this->Categories->patchEntity($category, $this->request->data);
         if ($this->Categories->save($category)) {
             $this->Flash->set('The category has been updated.', ['element' => 'alert-box', 'params' => ['class' => 'success']]);
             return $this->redirect(['action' => 'edit', $id]);
         }
         $this->Flash->set('Unable to update category.', ['element' => 'alert-box', 'params' => ['class' => 'danger']]);
     }
 }
 public function test()
 {
     if ($this->request->env('Shib-Session-ID') === null) {
         $this->Flash->warning('To test your attribute releases, you have to login first');
     }
     $this->loadModel('Releases');
     foreach ($this->Attributes->find('all') as $attribute) {
         unset($temp);
         unset($errors);
         $temp['validator'] = $attribute['validation'];
         $attribute['value'] = $this->request->env($attribute->name);
         $validated = 'N/A';
         if (!empty($attribute['validation'])) {
             $validator = new Validator();
             $validator->allowEmpty('value')->add('value', 'validFormat', ['rule' => array('custom', '/^(' . $attribute['validation'] . ')$/i'), 'message' => 'RegEx match fails.']);
             $errors = $validator->errors(array('value' => $attribute['value']));
             $validated = 'FAIL';
         }
         $temp['value'] = $attribute['value'];
         if (!empty($errors)) {
             $temp['errors'] = $errors['value'];
         }
         $attributes[$attribute['schema']][$attribute['name']] = $temp;
         if (!empty($attribute['value'])) {
             $persistentid_array = preg_split('/!/', $this->request->env('persistent-id'));
             $persistentid = end($persistentid_array);
             $attr_release = array('attribute_name' => $attribute['name'], 'idp' => $this->request->env('Shib-Identity-Provider'), 'persistentid' => $persistentid, 'validated' => $validated);
             $query = $this->Releases->find()->andWhere(['attribute_name' => $attribute['name'], 'idp' => $this->request->env('Shib-Identity-Provider'), 'persistentid' => $persistentid]);
             if ($query->isEmpty()) {
                 $release = $this->Releases->newEntity();
             } else {
                 $id = $query->first()->id;
                 $release = $this->Releases->get($id, ['contain' => []]);
             }
             $release = $this->Releases->patchEntity($release, $attr_release);
             $this->Releases->save($release);
         }
     }
     $this->set(compact('attributes'));
     $this->set('_serialize', ['attributes']);
 }
 public function edit($id = NULL, $slug = NULL)
 {
     $this->loadModel('Navigation');
     //Get all nav items
     $parent_ids = $this->Navigation->find('list');
     $this->set(compact('parent_ids'));
     $navigation = $this->Navigation->get($id);
     $this->set('title_for_layout', 'Navigation : ' . $navigation->title);
     if (empty($navigation)) {
         throw new NotFoundException('Could not find that navigation item.');
     } else {
         $this->set(compact('navigation'));
     }
     if ($this->request->is(['post', 'put'])) {
         //Validation
         $validator = new Validator();
         $validator->requirePresence('title')->notEmpty('title', 'A title is required.');
         $errors_array = $validator->errors($this->request->data());
         $error_msg = [];
         foreach ($errors_array as $errors) {
             if (is_array($errors)) {
                 foreach ($errors as $error) {
                     $error_msg[] = $error;
                 }
             } else {
                 $error_msg[] = $errors;
             }
         }
         if (!empty($error_msg)) {
             $this->Flash->set('Please fix the following error(s): ' . implode('\\n \\r', $error_msg), ['element' => 'alert-box', 'params' => ['class' => 'danger']]);
             return $this->redirect(['action' => 'edit', $id]);
         }
         //Save
         $this->Navigation->patchEntity($navigation, $this->request->data);
         if ($this->Navigation->save($navigation)) {
             $this->Flash->set('The navigation item has been updated.', ['element' => 'alert-box', 'params' => ['class' => 'success']]);
             return $this->redirect(['action' => 'edit', $id]);
         }
         $this->Flash->set('Unable to update navigation item.', ['element' => 'alert-box', 'params' => ['class' => 'danger']]);
     }
 }
 /**
  * Tests the email proxy method
  *
  * @return void
  */
 public function testEmail()
 {
     $validator = new Validator();
     $validator->email('username');
     $this->assertEmpty($validator->errors(['username' => '*****@*****.**']));
     $this->assertNotEmpty($validator->errors(['username' => 'not an email']));
 }
Beispiel #14
0
 /**
  * Reset password
  *
  * @param null|string $key
  *
  * @return \Cake\Network\Response|void
  */
 public function reset($key = null)
 {
     $this->set('title', __('Reset Password'));
     /** @var $usersTable UsersTable */
     $usersTable = TableRegistry::get('Pie/Users.Users');
     /** @var $user User */
     $user = $usersTable->find()->where(['status' => 1])->matching('UserDetails', function (Query $query) use($key) {
         return $query->where(['key' => 'reset_key', 'value' => $key]);
     })->contain('UserDetails')->first();
     if (!$user || is_null($key)) {
         throw new NotFoundException();
     }
     if ($this->request->is(['post', 'put'])) {
         $validator = new Validator();
         $validator->add('new_password', ['minLengthPassword' => ['rule' => ['minLength', 8], 'message' => __d('users', 'Minimum length of password is 8 characters.')]])->add('confirm_password', ['equalToPassword' => ['rule' => function ($value, $context) {
             if ($value === $context['data']['new_password']) {
                 return true;
             } else {
                 return false;
             }
         }, 'message' => __d('users', 'Entered passwords do not match.')]]);
         $errors = $validator->errors($this->request->data, $user->isNew());
         $user->errors($errors);
         if (empty($errors)) {
             $user->set('password', PasswordHasherFactory::build(Configure::read('pie.users.passwordHasher'))->hash($this->request->data('new_password')));
             if ($usersTable->save($user)) {
                 /** @var $userDetailsTable UserDetailsTable */
                 $userDetailsTable = TableRegistry::get('Pie/Users.UserDetails');
                 $userDetailsTable->delete($user->getDetails()['reset_key']);
                 $this->Flash->set(__('Your password has been reset successfully.'), ['element' => 'success']);
                 return $this->redirect(['action' => 'login']);
             }
         }
         $this->Flash->set(__('An error occurred. Please try again.'), ['element' => 'error']);
     }
     $this->set(compact('user'));
 }
Beispiel #15
0
 /**
  * Tests the hasAtMost method
  *
  * @return void
  */
 public function testHasAtMost()
 {
     $validator = new Validator();
     $validator->hasAtMost('things', 3);
     $this->assertEmpty($validator->errors(['things' => [1, 2, 3]]));
     $this->assertEmpty($validator->errors(['things' => [1]]));
     $this->assertNotEmpty($validator->errors(['things' => [1, 2, 3, 4]]));
     $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3]]]));
     $this->assertEmpty($validator->errors(['things' => ['_ids' => [1, 2]]]));
     $this->assertNotEmpty($validator->errors(['things' => ['_ids' => [1, 2, 3, 4]]]));
 }
Beispiel #16
0
 /**
  * Forgot password.
  *
  * @return \Cake\Network\Response|void
  */
 public function forgot()
 {
     if ($this->request->is('post')) {
         $validator = new Validator();
         $validator->notEmpty('email', __d('community', 'Profile email could not be empty.'))->add('email', 'valid', ['rule' => 'email', 'message' => __d('community', 'Please enter valid email.')]);
         $email = $this->request->data('email');
         $errors = $validator->errors($this->request->data());
         if (empty($errors) && $email) {
             $userData = $this->Users->find()->where(['email' => $email])->first();
             if (isset($userData['id'])) {
                 $user = $this->Users->patchEntity($userData, ['token' => $this->generateToken()]);
                 if ($result = $this->Users->save($user)) {
                     Event::dispatch('Controller.Users.Forgot.successful', $this, ['user' => $result]);
                     $this->Flash->success(__d('community', 'We sent you a letter with instructions to mail.'));
                     return $this->redirect(['action' => 'login']);
                 } else {
                     $this->Flash->error(__d('community', 'An error has occurred. Please, try again.'));
                 }
             } else {
                 $this->Flash->error(__d('community', 'User with such an email is not registered.'));
             }
         } else {
             $this->Flash->error(implode('. ', $errors['email']));
         }
     }
     $this->set('page_title', __d('community', 'Forgot your password?'));
 }
Beispiel #17
0
 /**
  * Integration test for compareWith validator.
  *
  * @return void
  */
 public function testCompareWithIntegration()
 {
     $validator = new Validator();
     $validator->add('password', ['compare' => ['rule' => ['compareWith', 'password_compare']]]);
     $data = ['password' => 'test', 'password_compare' => 'not the same'];
     $this->assertNotEmpty($validator->errors($data), 'Validation should fail.');
 }
Beispiel #18
0
 /**
  * Tests the validateUnique method with scope
  *
  * @return void
  */
 public function testValidateUniqueScope()
 {
     $table = TableRegistry::get('Users');
     $validator = new Validator();
     $validator->add('username', 'unique', ['rule' => ['validateUnique', ['derp' => 'erp', 'scope' => 'id']], 'provider' => 'table']);
     $validator->provider('table', $table);
     $data = ['username' => 'larry', 'id' => 3];
     $this->assertNotEmpty($validator->errors($data));
     $data = ['username' => 'larry', 'id' => 1];
     $this->assertEmpty($validator->errors($data));
     $data = ['username' => 'jose'];
     $this->assertEmpty($validator->errors($data));
 }
Beispiel #19
0
 /**
  * Adds a nested validator.
  *
  * Nesting validators allows you to define validators for array
  * types. For example, nested validators are ideal when you want to validate many
  * similar sub-documents or complex array types.
  *
  * This method assumes that the sub-document has a 1:N relationship with the parent.
  *
  * The providers of the parent validator will be synced into the nested validator, when
  * errors are checked. This ensures that any validation rule providers connected
  * in the parent will have the same values in the nested validator when rules are evaluated.
  *
  * @param string $field The root field for the nested validator.
  * @param \Cake\Validation\Validator $validator The nested validator.
  * @return $this
  */
 public function addNestedMany($field, Validator $validator)
 {
     $field = $this->field($field);
     $field->add(static::NESTED, ['rule' => function ($value, $context) use($validator) {
         if (!is_array($value)) {
             return false;
         }
         foreach ($this->providers() as $provider) {
             $validator->provider($provider, $this->provider($provider));
         }
         $errors = [];
         foreach ($value as $i => $row) {
             if (!is_array($row)) {
                 return false;
             }
             $check = $validator->errors($row, $context['newRecord']);
             if (!empty($check)) {
                 $errors[$i] = $check;
             }
         }
         return empty($errors) ? true : $errors;
     }]);
     return $this;
 }
Beispiel #20
0
 /**
  * Tests that the 'create' and 'update' modes are preserved when using
  * nested validators
  *
  * @return void
  */
 public function testNestedManyValidatorCreate()
 {
     $validator = new Validator();
     $inner = new Validator();
     $inner->add('username', 'email', ['rule' => 'email', 'on' => 'create']);
     $validator->addNestedMany('user', $inner);
     $this->assertNotEmpty($validator->errors(['user' => [['username' => 'example']]], true));
     $this->assertEmpty($validator->errors(['user' => [['username' => 'a']]], false));
 }
 public function edit($id = NULL, $slug = NULL)
 {
     //Post Types
     $this->loadModel('Post_Type');
     $post_type_ids = $this->Post_Type->find('list');
     $this->set(compact('post_type_ids'));
     //Categories
     $this->loadModel('Categories');
     $category_ids = $this->Categories->find('list');
     $this->set(compact('category_ids'));
     $this->loadModel('Articles');
     $article = $this->Articles->get($id);
     $this->set('title_for_layout', 'Article : ' . $article->title);
     if (empty($article)) {
         throw new NotFoundException('Could not find that article.');
     } else {
         $selected_categories = explode(',', $article->category_id);
         $this->set(compact('article'));
         $this->set(compact('selected_categories'));
     }
     if ($this->request->is(['post', 'put'])) {
         //Validation
         $validator = new Validator();
         $validator->requirePresence('post_type_id')->notEmpty('post_type_id', 'A post type is required.')->requirePresence('title')->notEmpty('title', 'A title is required.');
         $errors_array = $validator->errors($this->request->data, false);
         $error_msg = [];
         foreach ($errors_array as $errors) {
             if (is_array($errors)) {
                 foreach ($errors as $error) {
                     $error_msg[] = $error;
                 }
             } else {
                 $error_msg[] = $errors;
             }
         }
         if (!empty($error_msg)) {
             $this->Flash->set('Please fix the following error(s): ' . implode('\\n \\r', $error_msg), ['element' => 'alert-box', 'params' => ['class' => 'danger']]);
             return $this->redirect(['action' => 'edit', $id]);
         }
         //Slug
         $article->slug = Text::slug(strtolower($this->request->data('title')));
         //User ID
         $current_user = $this->Auth->user();
         $article->user_id = $current_user['id'];
         //Date
         $article->updated_at = date('Y-m-d H:i:s');
         //Category array
         $cat_array = $this->request->data('category_id');
         $cat_final = implode(',', $cat_array);
         $this->request->data['category_id'] = $cat_final;
         //Save
         $this->Articles->patchEntity($article, $this->request->data);
         if ($this->Articles->save($article)) {
             $this->Flash->set('The article has been updated.', ['element' => 'alert-box', 'params' => ['class' => 'success']]);
             return $this->redirect(['action' => 'edit', $id]);
         }
         $this->Flash->set('Unable to update article.', ['element' => 'alert-box', 'params' => ['class' => 'danger']]);
     }
 }
Beispiel #22
0
 /**
  * Tests that setting last to a rule will stop validating the rest of the rules
  *
  * @return void
  */
 public function testErrorsWithLastRule()
 {
     $validator = new Validator();
     $validator->add('email', 'alpha', ['rule' => 'alphanumeric', 'last' => true])->add('email', 'email', ['rule' => 'email', 'message' => 'Y u no write email?']);
     $errors = $validator->errors(['email' => 'not an email!']);
     $expected = ['email' => ['alpha' => 'The provided value is invalid']];
     $this->assertEquals($expected, $errors);
 }
 public function edit_password()
 {
     if ($this->request->session()->check('Auth.User')) {
         $this->redirect(['controller' => 'users', 'action' => 'login']);
     } else {
         if (!$this->session->check('User.CheckPassword')) {
             $this->redirect(['controller' => 'users', 'action' => 'login']);
         }
     }
     if ($this->request->is(['patch', 'post', 'put'])) {
         $Session_user = $this->session->read('User.CheckPassword');
         $this->request->data['email'] = $Session_user['email'];
         $user = $this->Auth->identify();
         if ($user) {
             $validator = new Validator();
             $validator->requirePresence('new_password', 'create')->add('new_password', ['length' => ['rule' => ['minLength', 8], 'message' => 'Password need to be at least 8 characters long'], 'letter' => ['rule' => ['custom', '/[a-z]+/'], 'message' => 'Password need to be at least one lowercase letter'], 'capital' => ['rule' => ['custom', '/[A-Z]+/'], 'message' => 'Password need to be at least one uppercase  letter'], 'number' => ['rule' => ['custom', '/[0-9]+/'], 'message' => 'Password need to be at least one number']])->notEmpty('new_password', 'Please fill new password');
             $validator->add('reenter_new_password', 'compareWith', ['rule' => ['compareWith', 'new_password'], 'message' => 'Reenter new Passwords not equal.'])->notEmpty('reenter_new_password', 'Please fill reenter new password');
             $errors = $validator->errors($this->request->data());
             // pr($errors);
             $er = array();
             if (!empty($errors)) {
                 foreach ($errors as $value) {
                     foreach ($value as $rs) {
                         $this->Flash->error(__($rs));
                     }
                 }
             } else {
                 $user = $this->Users->get($user['id']);
                 $password_db = $user['password'];
                 $created = $user['created'];
                 $modified = $user['modified'];
                 $user->last_edit = Time::now();
                 $new_password = $this->request->data['new_password'];
                 $user->password = $new_password;
                 if ($this->check_passwordLog($user['id'], $new_password, $password_db) == false) {
                     if ($this->Users->save($user)) {
                         $this->session->delete('User.CheckPassword');
                         //                        $LogUsers = $this->LogUsers->find('all', ['conditions' => ['user_id' =>  $user['id']],'limit' =>1]);
                         //                        $data_logUser  = $LogUsers->toArray();
                         //                        if(!empty($data_logUser))
                         //                        {
                         //                            $id_logUser = $data_logUser[0]['id'];
                         //                            $LogUsers = $this->LogUsers->get($id_logUser);
                         //                            $LogUsers->password  = $password_db;
                         //                            $LogUsers->last_edit = Time::now();
                         //                            $LogUsers->created = $created;
                         //                            $LogUsers->modified = $modified;
                         //                            if ($this->LogUsers->save($LogUsers)) {
                         //                                $this->Flash->success(__('Change password successfully'));
                         //                                $this->redirect(['action' => 'index']);
                         //                            }
                         //                        }
                         //                        else
                         //                        {
                         $LogUsers = $this->LogUsers->newEntity();
                         $LogUsers->user_id = $user['id'];
                         $LogUsers->password = $password_db;
                         $LogUsers->last_edit = Time::now();
                         $LogUsers->created = $created;
                         $LogUsers->modified = $modified;
                         if ($this->LogUsers->save($LogUsers)) {
                             $this->Flash->success(__('Change password successfully'));
                             $this->redirect(['action' => 'index']);
                         }
                         // }
                     } else {
                         $this->Flash->error(__('The user could not be saved. Please, try again.'));
                     }
                 } else {
                     $this->Flash->error(__('You had registered this password. Please, try again.'));
                 }
             }
         } else {
             $this->Flash->error(__('Email or Password is not correct.'));
         }
     }
 }