Exemple #1
0
 /**
  * Creates a new administrative user's account.
  */
 public function actionAdminCreate()
 {
     $password = $this->prompt('Enter password:'******'required' => true]);
     if (!$password) {
         return self::EXIT_CODE_ERROR;
     }
     // create user model
     $user = new User();
     $user->setAttributes(['name' => $this->name, 'email' => $this->email, 'status' => User::STATUS_ACTIVE]);
     $user->newPassword = $password;
     $this->stdout("\n");
     if (!$user->validate()) {
         // validation errors
         $this->stdout('Validation errors...' . "\n", Console::FG_YELLOW);
         foreach ($user->getErrors() as $attribute => $errors) {
             $error = reset($errors);
             $this->stdout($user->getAttributeLabel($attribute) . ': ', Console::FG_RED);
             $this->stdout($error . "\n");
         }
         return self::EXIT_CODE_ERROR;
     }
     /* @var $api UserModule */
     $api = Yii::$app->getModule('user');
     try {
         $api->createAdmin($user);
         $this->stdout('User successfully created. ID: ' . $user->id . "\n", Console::FG_GREEN);
     } catch (Exception $ex) {
         // Database error
         $this->stdout('Database error.' . "\n", Console::FG_RED);
         return self::EXIT_CODE_ERROR;
     }
 }
Exemple #2
0
 /**
  * Test model validation and save
  *
  * @return User
  */
 public function testValidationAndSave()
 {
     /* @var $reviewer User */
     $reviewer = $this->getModule('Yii2')->grabFixture('users', 'activeUser1');
     $model = new User();
     $attributes = ['name' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 0, 'isValid' => false], ['value' => '', 'isValid' => false], ['value' => str_repeat('A', User::MAX_NAME_LENGTH + 1), 'isValid' => false], ['value' => str_repeat('A', User::MAX_EMAIL_LENGTH), 'isValid' => true]], 'email' => [['value' => null, 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => 0, 'isValid' => false], ['value' => '', 'isValid' => false], ['value' => 'wrong email', 'isValid' => false], ['value' => str_repeat('A', User::MAX_EMAIL_LENGTH - 4) . '@a.ru', 'isValid' => false], ['value' => '*****@*****.**', 'isValid' => true]], 'status' => [['value' => -100, 'isValid' => false], ['value' => 'wrong list value', 'isValid' => false], ['value' => User::STATUS_BLOCKED, 'isValid' => true], ['value' => User::STATUS_UNACTIVE, 'isValid' => true], ['value' => User::STATUS_ACTIVE, 'isValid' => true]], 'password' => [['value' => str_repeat('A', 256), 'isValid' => false], ['value' => [], 'isValid' => false], ['value' => '', 'isValid' => false], ['value' => str_repeat('A', 255), 'isValid' => true]], 'default_reviewer_id' => [['value' => null, 'isValid' => true], ['value' => 0, 'isValid' => true], ['value' => [], 'isValid' => true], ['value' => '', 'isValid' => true], ['value' => 'wrong integer', 'isValid' => false], ['value' => ['wrong integer'], 'isValid' => false], ['value' => $reviewer->id, 'isValid' => true]]];
     $this->getModule('\\Helper\\Unit')->validateModelAttributes($model, $attributes, $this);
     $model->newPassword = str_repeat('A', 255);
     $model->password = null;
     $this->getModule('\\Helper\\Unit')->validateModelAttributes($model, ['password' => [['value' => null, 'isValid' => true]]], $this);
     $this->assertTrue($model->validate());
     $this->assertTrue($model->save());
     // test unique e-mail
     $newModel = new User();
     $newModel->setAttributes($model->getAttributes());
     $this->assertFalse($newModel->validate());
     $this->assertArrayHasKey('email', $newModel->getErrors());
     return $model;
 }