/** * @param string $scenario * @param array $expected * @dataProvider getScenarioAttributes * */ public function testScenarios($scenario, $expected) { $model = new User(['scenario' => $scenario]); $actual = $model->safeAttributes(); sort($expected); sort($actual); expect('safe attributes are correct', $actual)->equals($expected); }
public function testValidateWrongNewPassword() { $model = new User(['username' => 'TestName', 'email' => '*****@*****.**', 'newPassword' => 'short', 'newPasswordRepeat' => '', 'status' => 1]); $model->scenario = User::SCENARIO_ADMIN_CREATE; expect('model is not valid', $model->validate())->false(); expect('password is too short', $model->errors)->hasKey('newPassword'); expect('password repeat is required', $model->errors)->hasKey('newPasswordRepeat'); $model = new \app\modules\user\models\backend\User(['username' => 'TestName', 'email' => '*****@*****.**', 'newPassword' => 'correct-password', 'newPasswordRepeat' => 'qwerty', 'status' => 1]); $model->scenario = User::SCENARIO_ADMIN_CREATE; expect('model is not valid', $model->validate())->false(); expect('new password is correct', $model->errors)->hasntKey('newPassword'); expect('password repeat is incorrect', $model->errors)->hasKey('newPasswordRepeat'); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = User::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]); $this->load($params); if (!$this->validate()) { $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'role' => $this->role]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['>=', 'created_at', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])->andFilterWhere(['<=', 'created_at', $this->date_to ? strtotime($this->date_to . ' 23:59:59') : null]); return $dataProvider; }
?> <?php echo $form->field($model, 'email')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'newPassword')->passwordInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'newPasswordRepeat')->passwordInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'status')->dropDownList(User::getStatusesArray()); ?> <?php echo $form->field($model, 'role')->dropDownList(ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description')); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? '<i class="material-icons">check</i> Добавить' : '<i class="material-icons">check</i> Сохранить', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary', 'name' => 'submit-button']); ?> </div> <?php ActiveForm::end(); ?>
/** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
use app\modules\user\models\backend\User; use app\modules\user\widgets\backend\grid\RoleColumn; use kartik\date\DatePicker; use yii\helpers\ArrayHelper; use yii\grid\GridView; use yii\helpers\Html; /* @var $this yii\web\View */ /* @var $searchModel \app\modules\user\models\backend\search\UserSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = Module::t('module', 'ADMIN_USERS'); $this->params['breadcrumbs'][] = $this->title; ?> <div class="users-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a(Module::t('module', 'ADMIN_USERS_ADD'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => ['id', ['filter' => DatePicker::widget(['model' => $searchModel, 'attribute' => 'date_from', 'attribute2' => 'date_to', 'type' => DatePicker::TYPE_RANGE, 'separator' => '-', 'pluginOptions' => ['format' => 'yyyy-mm-dd']]), 'attribute' => 'created_at', 'format' => 'datetime', 'filterOptions' => ['style' => 'max-width: 180px']], ['class' => LinkColumn::className(), 'attribute' => 'username'], 'email:email', ['class' => SetColumn::className(), 'filter' => User::getStatusesArray(), 'attribute' => 'status', 'name' => 'statusName', 'cssCLasses' => [User::STATUS_ACTIVE => 'success', User::STATUS_WAIT => 'warning', User::STATUS_BLOCKED => 'default']], ['class' => RoleColumn::className(), 'filter' => ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description'), 'attribute' => 'role'], ['class' => ActionColumn::className()]]]); ?> </div>