/** * This method is called before each test method. * * @param \Codeception\Event\TestEvent $event */ public function _before($event) { // delete this signed up user User::deleteAll(['email' => '*****@*****.**', 'username' => 'demo']); // delete roles Role::deleteAll(); }
/** * Assigns the appropriate role to the registered user. * If this is the first registered user in our system, he will get the * theCreator role (this should be you), if not, he will get the member role. * * @param integer $id The id of the registered user. * @return string Role name. */ public static function assignRole($id) { // make sure there are no leftovers Role::deleteAll(['user_id' => $id]); $usersCount = User::find()->count(); $auth = Yii::$app->authManager; // this is the first user in our system, give him theCreator role if ($usersCount == 1) { $role = $auth->getRole('theCreator'); $auth->assign($role, $id); } else { $role = $auth->getRole('member'); $auth->assign($role, $id); } // return assigned role name in case you want to use this method in tests return $role->name; }
public static function assignRole($id) { Role::deleteAll(['user_id' => $id]); $usersCount = User::find()->count(); $auth = Yii::$app->authManager; // FIRST USER if ($usersCount === 1) { $array = (array) AuthItem::getDevRole(); foreach ($array as $key) { $role = $key->name; } $auth->assign($role, $id); return $role; } else { $array = (array) AuthItem::getParentRole(); foreach ($array as $key) { $role = $key->name; } $auth->assign($role, $id); return $role; } }
/** * Relation with Role model. * * @return \yii\db\ActiveQuery */ public function getRole() { // User has_one Role via Role.user_id -> id return $this->hasOne(Role::className(), ['user_id' => 'id']); }
/** * Deletes an existing User model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id The user id. * @return \yii\web\Response * * @throws NotFoundHttpException */ public function actionDelete($id) { $this->findModel($id)->delete(); // delete this user's role from auth_assignment table if ($role = Role::find()->where(['user_id' => $id])->one()) { $role->delete(); } Yii::$app->session->setFlash('success', 'Deleted successfully'); return $this->redirect(['index']); }
/** * Clean up the objects against which you tested. */ protected function tearDown() { // delete roles Role::deleteAll(); }
/** * Deletes an existing User model. * If deletion is successful, the browser will be redirected to the 'index' page. * * @param integer $id The user id. * @return \yii\web\Response * * @throws NotFoundHttpException */ public function actionDelete($id) { $this->findModel($id)->delete(); // delete this user's role from auth_assignment table if ($role = Role::find()->where(['user_id' => $id])->one()) { $role->delete(); } return $this->redirect(['index']); }