Exemplo n.º 1
0
 /**
  * Test label callback.
  */
 function testLabelCallback()
 {
     $this->assertEqual($this->account->label(), $this->account->getUsername(), 'The username should be used as label');
     // Setup a random anonymous name to be sure the name is used.
     $name = $this->randomMachineName();
     $this->config('user.settings')->set('anonymous', $name)->save();
     $this->assertEqual($this->anonymous->label(), $name, 'The variable anonymous should be used for name of uid 0');
     $this->assertEqual($this->anonymous->getDisplayName(), $name, 'The variable anonymous should be used for display name of uid 0');
     $this->assertEqual($this->anonymous->getUserName(), '', 'The raw anonymous user name should be empty string');
     // Set to test the altered username.
     \Drupal::state()->set('user_hooks_test_user_format_name_alter', TRUE);
     $this->assertEqual($this->account->getDisplayName(), '<em>' . $this->account->id() . '</em>', 'The user display name should be altered.');
     $this->assertEqual($this->account->getUsername(), $this->account->name->value, 'The user name should not be altered.');
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $account = $this->currentUser();
     $account_is_user = $this->user->id() == $account->id();
     if ($form_state->getValue('set') == 'new') {
         // Save a new shortcut set with links copied from the user's default set.
         /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
         $set = $this->shortcutSetStorage->create(array('id' => $form_state->getValue('id'), 'label' => $form_state->getValue('label')));
         $set->save();
         $replacements = array('%user' => $this->user->label(), '%set_name' => $set->label(), ':switch-url' => $this->url('<current>'));
         if ($account_is_user) {
             // Only administrators can create new shortcut sets, so we know they have
             // access to switch back.
             drupal_set_message($this->t('You are now using the new %set_name shortcut set. You can edit it from this page or <a href=":switch-url">switch back to a different one.</a>', $replacements));
         } else {
             drupal_set_message($this->t('%user is now using a new shortcut set called %set_name. You can edit it from this page.', $replacements));
         }
         $form_state->setRedirect('entity.shortcut_set.customize_form', array('shortcut_set' => $set->id()));
     } else {
         // Switch to a different shortcut set.
         /* @var \Drupal\shortcut\Entity\ShortcutSet $set */
         $set = $this->shortcutSetStorage->load($form_state->getValue('set'));
         $replacements = array('%user' => $this->user->getDisplayName(), '%set_name' => $set->label());
         drupal_set_message($account_is_user ? $this->t('You are now using the %set_name shortcut set.', $replacements) : $this->t('%user is now using the %set_name shortcut set.', $replacements));
     }
     // Assign the shortcut set to the provided user account.
     $this->shortcutSetStorage->assignUser($set, $this->user);
 }
Exemplo n.º 3
0
 /**
  * Tests access to the personal contact form.
  */
 function testPersonalContactAccess()
 {
     // Test allowed access to admin user's contact form.
     $this->drupalLogin($this->webUser);
     $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
     $this->assertResponse(200);
     // Check the page title is properly displayed.
     $this->assertRaw(t('Contact @username', array('@username' => $this->adminUser->getDisplayName())));
     // Test denied access to admin user's own contact form.
     $this->drupalLogout();
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
     $this->assertResponse(403);
     // Test allowed access to user with contact form enabled.
     $this->drupalLogin($this->webUser);
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(200);
     // Test that there is no access to personal contact forms for users
     // without an email address configured.
     $original_email = $this->contactUser->getEmail();
     $this->contactUser->setEmail(FALSE)->save();
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(404, 'Not found (404) returned when visiting a personal contact form for a user with no email address');
     // Test that the 'contact tab' does not appear on the user profiles
     // for users without an email address configured.
     $this->drupalGet('user/' . $this->contactUser->id());
     $contact_link = '/user/' . $this->contactUser->id() . '/contact';
     $this->assertResponse(200);
     $this->assertNoLinkByHref($contact_link, 'The "contact" tab is hidden on profiles for users with no email address');
     // Restore original email address.
     $this->contactUser->setEmail($original_email)->save();
     // Test denied access to the user's own contact form.
     $this->drupalGet('user/' . $this->webUser->id() . '/contact');
     $this->assertResponse(403);
     // Test always denied access to the anonymous user contact form.
     $this->drupalGet('user/0/contact');
     $this->assertResponse(403);
     // Test that anonymous users can access the contact form.
     $this->drupalLogout();
     user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array('access user contact forms'));
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(200);
     // Test that anonymous users can access admin user's contact form.
     $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
     $this->assertResponse(200);
     $this->assertCacheContext('user');
     // Revoke the personal contact permission for the anonymous user.
     user_role_revoke_permissions(RoleInterface::ANONYMOUS_ID, array('access user contact forms'));
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(403);
     $this->assertCacheContext('user');
     $this->drupalGet('user/' . $this->adminUser->id() . '/contact');
     $this->assertResponse(403);
     // Disable the personal contact form.
     $this->drupalLogin($this->adminUser);
     $edit = array('contact_default_status' => FALSE);
     $this->drupalPostForm('admin/config/people/accounts', $edit, t('Save configuration'));
     $this->assertText(t('The configuration options have been saved.'), 'Setting successfully saved.');
     $this->drupalLogout();
     // Re-create our contacted user with personal contact forms disabled by
     // default.
     $this->contactUser = $this->drupalCreateUser();
     // Test denied access to a user with contact form disabled.
     $this->drupalLogin($this->webUser);
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(403);
     // Test allowed access for admin user to a user with contact form disabled.
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(200);
     // Re-create our contacted user as a blocked user.
     $this->contactUser = $this->drupalCreateUser();
     $this->contactUser->block();
     $this->contactUser->save();
     // Test that blocked users can still be contacted by admin.
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(200);
     // Test that blocked users cannot be contacted by non-admins.
     $this->drupalLogin($this->webUser);
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(403);
     // Test enabling and disabling the contact page through the user profile
     // form.
     $this->drupalGet('user/' . $this->webUser->id() . '/edit');
     $this->assertNoFieldChecked('edit-contact--2');
     $this->assertFalse(\Drupal::service('user.data')->get('contact', $this->webUser->id(), 'enabled'), 'Personal contact form disabled');
     $this->drupalPostForm(NULL, array('contact' => TRUE), t('Save'));
     $this->assertFieldChecked('edit-contact--2');
     $this->assertTrue(\Drupal::service('user.data')->get('contact', $this->webUser->id(), 'enabled'), 'Personal contact form enabled');
     // Test with disabled global default contact form in combination with a user
     // that has the contact form enabled.
     $this->config('contact.settings')->set('user_default_enabled', FALSE)->save();
     $this->contactUser = $this->drupalCreateUser();
     \Drupal::service('user.data')->set('contact', $this->contactUser->id(), 'enabled', 1);
     $this->drupalGet('user/' . $this->contactUser->id() . '/contact');
     $this->assertResponse(200);
 }
Exemplo n.º 4
0
 /**
  * Tests CRUD operations.
  */
 public function testCRUD()
 {
     $types_data = ['profile_type_0' => ['label' => $this->randomMachineName()], 'profile_type_1' => ['label' => $this->randomMachineName()]];
     /** @var ProfileType[] $types */
     $types = [];
     foreach ($types_data as $id => $values) {
         $types[$id] = ProfileType::create(['id' => $id] + $values);
         $types[$id]->save();
     }
     $this->user1 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']);
     $this->user1->save();
     $this->user2 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']);
     $this->user2->save();
     $this->profileStorage = \Drupal::entityTypeManager()->getStorage('profile');
     // Create a new profile.
     $profile = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]);
     $this->assertIdentical($profile->id(), NULL);
     $this->assertTrue($profile->uuid());
     $this->assertIdentical($profile->getType(), $expected['type']);
     $expected_label = t('@type profile of @username (uid: @uid)', ['@type' => $types['profile_type_0']->label(), '@username' => $this->user1->getDisplayName(), '@uid' => $this->user1->id()]);
     $this->assertEqual($profile->label(), $expected_label, new FormattableMarkup('Expected "%expected" but got "%got"', ['%expected' => $expected_label, '%got' => $profile->label()]));
     $this->assertIdentical($profile->getOwnerId(), $this->user1->id());
     $this->assertIdentical($profile->getCreatedTime(), REQUEST_TIME);
     $this->assertIdentical($profile->getChangedTime(), REQUEST_TIME);
     // Save the profile.
     $status = $profile->save();
     $this->assertIdentical($status, SAVED_NEW);
     $this->assertTrue($profile->id());
     $this->assertIdentical($profile->getChangedTime(), REQUEST_TIME);
     // List profiles for the user and verify that the new profile appears.
     $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
     $list_ids = array_keys($list);
     $this->assertEqual($list_ids, [(int) $profile->id()]);
     // Reload and update the profile.
     /** @var Profile $profile */
     $profile = Profile::load($profile->id());
     $profile->setChangedTime($profile->getChangedTime() - 1000);
     $original = clone $profile;
     $status = $profile->save();
     $this->assertIdentical($status, SAVED_UPDATED);
     $this->assertIdentical($profile->id(), $original->id());
     $this->assertEqual($profile->getCreatedTime(), REQUEST_TIME);
     $this->assertEqual($original->getChangedTime(), REQUEST_TIME - 1000);
     // Changed time is only updated when saved through the UI form.
     // @see \Drupal\Core\Entity\ContentEntityForm::submitForm().
     $this->assertEqual($profile->getChangedTime(), REQUEST_TIME - 1000);
     // Create a second profile.
     $user1_profile1 = $profile;
     $profile = Profile::create(['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]);
     $status = $profile->save();
     $this->assertIdentical($status, SAVED_NEW);
     $user1_profile = $profile;
     // List profiles for the user and verify that both profiles appear.
     $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
     $list_ids = array_keys($list);
     $this->assertEqual($list_ids, [(int) $user1_profile1->id(), (int) $user1_profile->id()]);
     // Delete the second profile and verify that the first still exists.
     $user1_profile->delete();
     $this->assertFalse(Profile::load($user1_profile->id()));
     $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
     $list_ids = array_keys($list);
     $this->assertEqual($list_ids, [(int) $user1_profile1->id()]);
     // Create a new second profile.
     $user1_profile = Profile::create(['type' => $types['profile_type_1']->id(), 'uid' => $this->user1->id()]);
     $status = $user1_profile->save();
     $this->assertIdentical($status, SAVED_NEW);
     // Create a profile for the second user.
     $user2_profile1 = Profile::create(['type' => $types['profile_type_0']->id(), 'uid' => $this->user2->id()]);
     $status = $user2_profile1->save();
     $this->assertIdentical($status, SAVED_NEW);
     // Delete the first user and verify that all of its profiles are deleted.
     $this->user1->delete();
     $this->assertFalse(User::load($this->user1->id()));
     $list = $this->profileStorage->loadByProperties(['uid' => $this->user1->id()]);
     $list_ids = array_keys($list);
     $this->assertEqual($list_ids, []);
     // List profiles for the second user and verify that they still exist.
     $list = $this->profileStorage->loadByProperties(['uid' => $this->user2->id()]);
     $list_ids = array_keys($list);
     $this->assertEqual($list_ids, [(int) $user2_profile1->id()]);
     // @todo Rename a profile type; verify that existing profiles are updated.
 }
 /**
  * {@inheritdoc}
  */
 public function getDisplayName()
 {
     return $this->subject->getDisplayName();
 }
Exemplo n.º 6
0
 /**
  * Asserts that the currently logged-in user can not masquerade as a given target user.
  *
  * @param \Drupal\user\UserInterface $target_account
  *   The user to masquerade to.
  */
 protected function assertCanNotMasqueradeAs($target_account)
 {
     $edit = array('masquerade_as' => $target_account->getAccountName());
     $this->drupalPostForm('masquerade', $edit, t('Switch'));
     $this->assertRaw(t('You are not allowed to masquerade as %name.', array('%name' => $target_account->getDisplayName())));
     $this->assertNoText(t('Unmasquerade'));
 }
Exemplo n.º 7
0
 /**
  * Form constructor for the personal contact form.
  *
  * @param \Drupal\user\UserInterface $user
  *   The account for which a personal contact form should be generated.
  *
  * @return array
  *   The personal contact form as render array as expected by drupal_render().
  *
  * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
  *   Exception is thrown when user tries to access a contact form for a
  *   user who does not have an email address configured.
  */
 public function contactPersonalPage(UserInterface $user)
 {
     // Do not continue if the user does not have an email address configured.
     if (!$user->getEmail()) {
         throw new NotFoundHttpException();
     }
     $message = $this->entityManager()->getStorage('contact_message')->create(array('contact_form' => 'personal', 'recipient' => $user->id()));
     $form = $this->entityFormBuilder()->getForm($message);
     $form['#title'] = $this->t('Contact @username', array('@username' => $user->getDisplayName()));
     $form['#cache']['contexts'][] = 'user.permissions';
     return $form;
 }
Exemplo n.º 8
0
 /**
  * Masquerades as another user.
  *
  * @param \Drupal\user\UserInterface $account
  *   The user account to masquerade as.
  */
 protected function masqueradeAs(UserInterface $account)
 {
     $this->drupalGet('user/' . $account->id());
     $this->clickLink(t('Masquerade as @name', ['@name' => $account->getDisplayName()]));
     // @todo Fix when crsfTokenSeed is avalable.
     //    $this->drupalGet('user/' . $account->id() . '/masquerade', array(
     //      'query' => array(
     //        'token' => $this->drupalGetToken('user/' . $account->id() . '/masquerade'),
     //      ),
     //    ));
     $this->assertResponse(200);
     $this->assertText('You are now masquerading as ' . $account->label());
     // Update the logged in user account.
     // @see WebTestBase::drupalLogin()
     if (isset($this->session_id)) {
         $this->loggedInUser = $account;
         $this->loggedInUser->session_id = $this->session_id;
     }
 }