public static function setNotificationSettingsForUser(User $user, $notificationType, $inbox = true, $email = true)
 {
     assert('is_string($notificationType)');
     assert('is_bool($inbox)');
     assert('is_bool($email)');
     $inboxAndEmailNotificationSettings = UserNotificationUtil::getNotificationSettingsByUser($user);
     $settingName = NotificationsUtil::resolveNotificationSettingNameFromType($notificationType);
     $inboxAndEmailNotificationSettings[$settingName]['inbox'] = $inbox;
     $inboxAndEmailNotificationSettings[$settingName]['email'] = $email;
     UserNotificationUtil::setValue($user, $inboxAndEmailNotificationSettings, 'inboxAndEmailNotificationSettings', false);
 }
 /**
  * @return UserNotificationConfigurationForm
  */
 public static function makeFormFromUserConfigurationByUser(User $user)
 {
     assert('$user instanceOf User && $user->id > 0');
     $form = new UserNotificationConfigurationForm($user);
     $notificationSettingsNames = UserNotificationUtil::getAllNotificationSettingAttributes();
     $notificationSettings = UserNotificationUtil::getNotificationSettingsByUser($user);
     foreach ($notificationSettingsNames as $setting) {
         list($settingName, $type) = UserNotificationUtil::getSettingNameAndTypeBySuffixedConfigurationAttribute($setting);
         $form->{$setting} = (bool) $notificationSettings[$settingName][$type];
     }
     return $form;
 }
 public function testSuperUserAllDefaultControllerActions()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     //Test all default controller actions that do not require any POST/GET variables to be passed.
     //This does not include portlet controller actions.
     $this->runControllerWithNoExceptionsAndGetContent('users/default');
     $this->runControllerWithNoExceptionsAndGetContent('users/default/index');
     $this->runControllerWithNoExceptionsAndGetContent('users/default/list');
     $this->runControllerWithNoExceptionsAndGetContent('users/default/create');
     $this->runControllerWithNoExceptionsAndGetContent('users/default/profile');
     //Access to admin configuration should be allowed.
     $this->runControllerWithNoExceptionsAndGetContent('configuration');
     //Default Controller actions requiring some sort of parameter via POST or GET
     //Load Model Edit Views
     $users = User::getAll();
     $this->assertEquals(5, count($users));
     $aUser = User::getByUsername('auser');
     $bUser = User::getByUsername('buser');
     $cUser = User::getByUsername('cuser');
     $dUser = User::getByUsername('duser');
     $super = User::getByUsername('super');
     $this->setGetArray(array('id' => $super->id));
     //Access to allowed to view Audit Trail.
     $this->runControllerWithNoExceptionsAndGetContent('users/default/auditEventsModalList');
     $this->setGetArray(array('id' => $aUser->id));
     //Access to allowed to view Audit Trail.
     $this->runControllerWithNoExceptionsAndGetContent('users/default/auditEventsModalList');
     $this->setGetArray(array('id' => $bUser->id));
     //Access to allowed to view Audit Trail.
     $this->runControllerWithNoExceptionsAndGetContent('users/default/auditEventsModalList');
     $this->setGetArray(array('id' => $super->id));
     //Access to User Role edit link and control available.
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/edit');
     $this->assertContains('User_role_SelectLink', $content);
     $this->assertContains('User_role_name', $content);
     $this->setGetArray(array('id' => $aUser->id));
     //Access to User Role edit link and control available.
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/edit');
     $this->assertContains('User_role_SelectLink', $content);
     $this->assertContains('User_role_name', $content);
     $users = User::getAll();
     $this->assertEquals(5, count($users));
     //Save user.
     $this->assertTrue($aUser->id > 0);
     $this->assertEquals('aUserson', $aUser->lastName);
     $this->assertEquals(null, $aUser->officePhone);
     $this->setGetArray(array('id' => $aUser->id));
     $this->setPostArray(array('User' => array('officePhone' => '456765421')));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/edit');
     $users = User::getAll();
     $this->assertEquals(5, count($users));
     $aUser = User::getById($aUser->id);
     $this->assertEquals('456765421', $aUser->officePhone);
     $this->assertEquals('aUserson', $aUser->lastName);
     //Test having a failed validation on the user during save.
     $this->setGetArray(array('id' => $aUser->id));
     $this->setPostArray(array('User' => array('lastName' => '')));
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/edit');
     $this->assertContains('Name cannot be blank', $content);
     $users = User::getAll();
     $this->assertEquals(5, count($users));
     //LastName for aUser should still be aUserson.
     //Need to forget aUser, since it has lastName = '' from the setAttributes called in actionEdit.
     //Retrieve aUser and confirm the lastName is still aUserson.
     $aUser->forget();
     $aUser = User::getByUsername('auser');
     $this->assertEquals('aUserson', $aUser->lastName);
     //Load Model Detail View
     $this->setGetArray(array('id' => $aUser->id));
     $this->resetPostArray();
     $this->runControllerWithNoExceptionsAndGetContent('users/default/details');
     //Load game dashboard view
     $this->setGetArray(array('id' => $aUser->id));
     $this->resetPostArray();
     $this->runControllerWithNoExceptionsAndGetContent('users/default/gameDashboard');
     //Load Model Security Detail View
     $this->setGetArray(array('id' => $aUser->id));
     $this->resetPostArray();
     $this->runControllerWithNoExceptionsAndGetContent('users/default/securityDetails');
     //Load Model Security Detail View for super user
     $this->setGetArray(array('id' => $super->id));
     $this->resetPostArray();
     $this->runControllerWithNoExceptionsAndGetContent('users/default/securityDetails');
     //Load Model MassEdit Views.
     //MassEdit view for single selected ids
     $this->setGetArray(array('selectedIds' => '4,5,6,7', 'selectAll' => ''));
     // Not Coding Standard
     $this->resetPostArray();
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/massEdit');
     $this->assertContains('<strong>4</strong>&#160;records selected for updating', $content);
     //MassEdit view for all result selected ids
     $users = User::getAll();
     $this->assertEquals(5, count($users));
     $this->setGetArray(array('selectAll' => '1'));
     $this->resetPostArray();
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/massEdit');
     $this->assertContains('<strong>5</strong>&#160;records selected for updating', $content);
     //save Model MassEdit for selected Ids
     //Test that the 4 contacts do not have the office phone number we are populating them with.
     $user1 = User::getById($aUser->id);
     $user2 = User::getById($bUser->id);
     $user3 = User::getById($cUser->id);
     $user4 = User::getById($dUser->id);
     $this->assertNotEquals('7788', $user1->officePhone);
     $this->assertNotEquals('7788', $user2->officePhone);
     $this->assertNotEquals('7788', $user3->officePhone);
     $this->assertNotEquals('7788', $user4->officePhone);
     $this->setGetArray(array('selectedIds' => $aUser->id . ',' . $bUser->id, 'selectAll' => '', 'User_page' => 1));
     $this->setPostArray(array('User' => array('officePhone' => '7788'), 'MassEdit' => array('officePhone' => 1)));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/massEdit');
     //Test that the 2 contacts have the new office phone number and the other contacts do not.
     $user1 = User::getById($aUser->id);
     $user2 = User::getById($bUser->id);
     $user3 = User::getById($cUser->id);
     $user4 = User::getById($dUser->id);
     $this->assertEquals('7788', $user1->officePhone);
     $this->assertEquals('7788', $user2->officePhone);
     $this->assertNotEquals('7788', $user3->officePhone);
     $this->assertNotEquals('7788', $user4->officePhone);
     //save Model MassEdit for entire search result
     $this->setGetArray(array('selectAll' => '1', 'User_page' => 1));
     $this->setPostArray(array('User' => array('officePhone' => '1234'), 'MassEdit' => array('officePhone' => 1)));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/massEdit');
     //Test that all accounts have the new phone number.
     $user1 = User::getById($aUser->id);
     $user2 = User::getById($bUser->id);
     $user3 = User::getById($cUser->id);
     $user4 = User::getById($dUser->id);
     $this->assertEquals('1234', $user1->officePhone);
     $this->assertEquals('1234', $user2->officePhone);
     $this->assertEquals('1234', $user3->officePhone);
     $this->assertEquals('1234', $user4->officePhone);
     //Run Mass Update using progress save.
     $pageSize = Yii::app()->pagination->getForCurrentUserByType('massEditProgressPageSize');
     $this->assertEquals(5, $pageSize);
     Yii::app()->pagination->setForCurrentUserByType('massEditProgressPageSize', 1);
     //The page size is smaller than the result set, so it should exit.
     $this->runControllerWithExitExceptionAndGetContent('users/default/massEdit');
     //save Modal MassEdit using progress load for page 2, 3, 4 and 5.
     $this->setGetArray(array('selectAll' => '1', 'User_page' => 2));
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/massEditProgressSave');
     $this->assertContains('"value":40', $content);
     $this->setGetArray(array('selectAll' => '1', 'User_page' => 3));
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/massEditProgressSave');
     $this->assertContains('"value":60', $content);
     $this->setGetArray(array('selectAll' => '1', 'User_page' => 4));
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/massEditProgressSave');
     $this->assertContains('"value":80', $content);
     $this->setGetArray(array('selectAll' => '1', 'User_page' => 5));
     $content = $this->runControllerWithNoExceptionsAndGetContent('users/default/massEditProgressSave');
     $this->assertContains('"value":100', $content);
     //Set page size back to old value.
     Yii::app()->pagination->setForCurrentUserByType('massEditProgressPageSize', $pageSize);
     //Autocomplete for User
     $this->setGetArray(array('term' => 'auser'));
     $this->runControllerWithNoExceptionsAndGetContent('users/default/autoComplete');
     //actionModalList
     $this->setGetArray(array('modalTransferInformation' => array('sourceIdFieldId' => 'x', 'sourceNameFieldId' => 'y', 'modalId' => 'z')));
     $this->runControllerWithNoExceptionsAndGetContent('users/default/modalList');
     //Change password view.
     $this->setGetArray(array('id' => $aUser->id));
     $this->resetPostArray();
     $this->runControllerWithNoExceptionsAndGetContent('users/default/changePassword');
     //Failed change password validation
     $this->setPostArray(array('ajax' => 'edit-form', 'UserPasswordForm' => array('newPassword' => '', 'newPassword_repeat' => '')));
     $content = $this->runControllerWithExitExceptionAndGetContent('users/default/changePassword');
     $this->assertTrue(strlen($content) > 50);
     //approximate, but should definetely be larger than 50.
     //Successful change password validation
     $this->setPostArray(array('ajax' => 'edit-form', 'UserPasswordForm' => array('newPassword' => 'aNewPassword', 'newPassword_repeat' => 'aNewPassword')));
     $content = $this->runControllerWithExitExceptionAndGetContent('users/default/changePassword');
     $this->assertEquals('[]', $content);
     //Successful saved password change.
     $this->setPostArray(array('save' => 'Save', 'UserPasswordForm' => array('newPassword' => 'bNewPassword', 'newPassword_repeat' => 'bNewPassword')));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/changePassword');
     //Login using new password successfully.
     $identity = new UserIdentity('auser', 'bNewPassword');
     $authenticated = $identity->authenticate();
     $this->assertEquals(0, $identity->errorCode);
     $this->assertTrue($authenticated);
     //User Configuration UI. Change aUser configuration values.
     //First make sure settings are not what we are setting them too.
     $this->assertNotEquals(9, Yii::app()->pagination->getByUserAndType($aUser, 'listPageSize'));
     $this->assertNotEquals(4, Yii::app()->pagination->getByUserAndType($aUser, 'subListPageSize'));
     //Load up configuration page.
     $this->setGetArray(array('id' => $aUser->id));
     $this->runControllerWithNoExceptionsAndGetContent('users/default/configurationEdit');
     //Post fake save that will fail validation.
     $this->setGetArray(array('id' => $aUser->id));
     $this->setPostArray(array('UserConfigurationForm' => array('listPageSize' => 0, 'subListPageSize' => 4)));
     $this->runControllerWithNoExceptionsAndGetContent('users/default/configurationEdit');
     //Post fake save that will pass validation.
     $this->setGetArray(array('id' => $aUser->id));
     $this->setPostArray(array('UserConfigurationForm' => array('listPageSize' => 9, 'subListPageSize' => 4)));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/configurationEdit');
     $this->assertEquals('User configuration saved successfully.', Yii::app()->user->getFlash('notification'));
     //Check to make sure user configuration is actually changed.
     $this->assertEquals(9, Yii::app()->pagination->getByUserAndType($aUser, 'listPageSize'));
     $this->assertEquals(4, Yii::app()->pagination->getByUserAndType($aUser, 'subListPageSize'));
     //Confirm current user has certain session values
     $this->assertNotEquals(7, Yii::app()->user->getState('listPageSize'));
     $this->assertNotEquals(4, Yii::app()->user->getState('subListPageSize'));
     //Change current user configuration values. (Yii::app()->user->userModel)
     //First make sure settings are not what we are setting them too.
     $this->assertNotEquals(7, Yii::app()->pagination->getForCurrentUserByType('listPageSize'));
     //Load up configuration page.
     $this->setGetArray(array('id' => Yii::app()->user->userModel->id));
     $this->resetPostArray();
     $this->runControllerWithNoExceptionsAndGetContent('users/default/configurationEdit');
     //Post fake save that will fail validation.
     $this->setGetArray(array('id' => Yii::app()->user->userModel->id));
     $this->setPostArray(array('UserConfigurationForm' => array('listPageSize' => 0, 'subListPageSize' => 4)));
     $this->runControllerWithNoExceptionsAndGetContent('users/default/configurationEdit');
     //Post fake save that will pass validation.
     $this->setGetArray(array('id' => Yii::app()->user->userModel->id));
     $this->setPostArray(array('UserConfigurationForm' => array('listPageSize' => 7, 'subListPageSize' => 4)));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/configurationEdit');
     $this->assertEquals('User configuration saved successfully.', Yii::app()->user->getFlash('notification'));
     //Check to make sure user configuration is actually changed.
     $this->assertEquals(7, Yii::app()->pagination->getForCurrentUserByType('listPageSize'));
     //Check getState data. since it should be updated for current user.
     $this->assertEquals(7, Yii::app()->user->getState('listPageSize'));
     $this->assertEquals(4, Yii::app()->user->getState('subListPageSize'));
     //User Notification Configuration UI. Change aUser notification configuration values.
     //First make sure settings all default values are true
     $notificationSettings = UserNotificationUtil::getNotificationSettingsByUser($aUser);
     $notificationSettingsNames = UserNotificationUtil::getAllNotificationSettingAttributes();
     foreach ($notificationSettingsNames as $setting) {
         list($settingName, $type) = UserNotificationUtil::getSettingNameAndTypeBySuffixedConfigurationAttribute($setting);
         $this->assertTrue((bool) $notificationSettings[$settingName][$type]);
     }
     //Load up notification configuration page.
     $this->setGetArray(array('id' => $aUser->id));
     $this->runControllerWithNoExceptionsAndGetContent('users/default/notificationConfiguration');
     //Post fake save that will pass validation.
     $this->setGetArray(array('id' => $aUser->id));
     $this->setPostArray(array('UserNotificationConfigurationForm' => array('enableConversationInvitesNotificationInbox' => 0)));
     $this->runControllerWithRedirectExceptionAndGetContent('users/default/notificationConfiguration');
     $this->assertEquals('User notifications configuration saved successfully.', Yii::app()->user->getFlash('notification'));
     //Check to make sure user notification configuration is actually changed.
     $this->assertFalse((bool) UserNotificationUtil::isEnabledByUserAndNotificationNameAndType($aUser, 'enableConversationInvitesNotification', 'inbox'));
 }