/** * Test the username formatter. */ public function testUsername() { $view_id = $this->randomMachineName(); $view = View::create(['id' => $view_id, 'base_table' => 'comment_field_data', 'display' => ['default' => ['display_plugin' => 'default', 'id' => 'default', 'display_options' => ['fields' => ['name' => ['table' => 'comment_field_data', 'field' => 'name', 'id' => 'name', 'plugin_id' => 'field', 'type' => 'comment_username'], 'subject' => ['table' => 'comment_field_data', 'field' => 'subject', 'id' => 'subject', 'plugin_id' => 'field', 'type' => 'string', 'settings' => ['link_to_entity' => TRUE]]]]]]]); $view->save(); /* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */ $account_switcher = \Drupal::service('account_switcher'); /* @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = \Drupal::service('renderer'); $account_switcher->switchTo($this->adminUser); $executable = Views::getView($view_id); $build = $executable->preview(); $this->setRawContent($renderer->renderRoot($build)); $this->verbose($this->getRawContent()); $this->assertLink('My comment title'); $this->assertLink('Anonymous comment title'); $this->assertLink($this->adminUser->label()); $this->assertLink('barry (not verified)'); $account_switcher->switchTo(new AnonymousUserSession()); $executable = Views::getView($view_id); $executable->storage->invalidateCaches(); $build = $executable->preview(); $this->setRawContent($renderer->renderRoot($build)); // No access to user-profiles, so shouldn't be able to see links. $this->assertNoLink($this->adminUser->label()); // Note: External users aren't pointing to drupal user profiles. $this->assertLink('barry (not verified)'); $this->verbose($this->getRawContent()); $this->assertLink('My comment title'); $this->assertLink('Anonymous comment title'); }
/** * 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'); }
/** * 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.'); }
/** * {@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); }
/** * {@inheritdoc} */ public function submitForm(array &$form, array &$form_state) { $account = $this->currentUser(); $account_is_user = $this->user->id() == $account->id(); if ($form_state['values']['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['values']['id'], 'label' => $form_state['values']['label'])); $set->save(); $replacements = array('%user' => $this->user->label(), '%set_name' => $set->label(), '@switch-url' => $this->url($this->routeMatch->getRouteName(), array('user' => $this->user->id()))); 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['redirect_route'] = array('route_name' => 'shortcut.set_customize', 'route_parameters' => array('shortcut_set' => $set->id())); } else { // Switch to a different shortcut set. /* @var \Drupal\shortcut\Entity\ShortcutSet $set */ $set = $this->shortcutSetStorage->load($form_state['values']['set']); $replacements = array('%user' => $this->user->label(), '%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); }
/** * Control access to masquerade as a certain target user. * * Modules may implement this hook to control whether a user is allowed to * masquerade as a certain target user account. * * @param \Drupal\user\UserInterface $user * The currently logged-in user. * @param \Drupal\user\UserInterface $target_account * The target user account to check for masquerade access. * * @return bool * Either a Boolean or NULL: * - FALSE to explicitly deny access. If a module denies access, no other * module is able to grant access and access is denied. * - TRUE to grant access. Access is only granted if at least one module * grants access and no module denies access. * - NULL or nothing to not affect the operation. If no module explicitly * grants access, access is denied. */ function hook_masquerade_access(\Drupal\user\UserInterface $user, \Drupal\user\UserInterface $target_account) { // Explicitly deny access for uid 1. if ($target_account->id() == 1) { return FALSE; } // Example: If the target username is 'demo', always grant access for everone. if ($target_account->label() == 'demo') { return TRUE; } // In other cases do not alter access. }
/** * {@inheritdoc} */ public function submit(array $form, FormStateInterface $form_state) { // Cancel account immediately, if the current user has administrative // privileges, no confirmation mail shall be sent, and the user does not // attempt to cancel the own account. if ($this->currentUser()->hasPermission('administer users') && empty($form_state['values']['user_cancel_confirm']) && $this->entity->id() != $this->currentUser()->id()) { user_cancel($form_state['values'], $this->entity->id(), $form_state['values']['user_cancel_method']); $form_state->setRedirect('user.admin_account'); } else { // Store cancelling method and whether to notify the user in // $this->entity for user_cancel_confirm(). $this->entity->user_cancel_method = $form_state['values']['user_cancel_method']; $this->entity->user_cancel_notify = $form_state['values']['user_cancel_notify']; $this->entity->save(); _user_mail_notify('cancel_confirm', $this->entity); drupal_set_message($this->t('A confirmation request to cancel your account has been sent to your email address.')); $this->logger('user')->notice('Sent account cancellation request to %name %email.', array('%name' => $this->entity->label(), '%email' => '<' . $this->entity->getEmail() . '>')); $form_state->setRedirect('user.view', array('user' => $this->entity->id())); } }
/** * Tests output for properties held in common between articles and pages. * * @param \EasyRdf_Graph $graph * The EasyRDF graph object. * @param \Drupal\node\NodeInterface $node * The node being displayed. * @param string $message_prefix * The word to use in the test assertion message. */ protected function assertRdfaCommonNodeProperties($graph, NodeInterface $node, $message_prefix) { $uri = $node->url('canonical', array('absolute' => TRUE)); // Title. $expected_value = array('type' => 'literal', 'value' => $node->get('title')->value, 'lang' => 'en'); $this->assertTrue($graph->hasProperty($uri, 'http://schema.org/name', $expected_value), "{$message_prefix} title was found (schema:name)."); // Created date. $expected_value = array('type' => 'literal', 'value' => format_date($node->get('created')->value, 'custom', 'c', 'UTC'), 'lang' => 'en'); $this->assertTrue($graph->hasProperty($uri, 'http://schema.org/dateCreated', $expected_value), "{$message_prefix} created date was found (schema:dateCreated) in teaser."); // Body. $expected_value = array('type' => 'literal', 'value' => $node->get('body')->value, 'lang' => 'en'); $this->assertTrue($graph->hasProperty($uri, 'http://schema.org/text', $expected_value), "{$message_prefix} body was found (schema:text) in teaser."); // Author. $expected_value = array('type' => 'uri', 'value' => $this->authorUri); $this->assertTrue($graph->hasProperty($uri, 'http://schema.org/author', $expected_value), "{$message_prefix} author was found (schema:author) in teaser."); // Author type. $this->assertEqual($graph->type($this->authorUri), 'schema:Person', "{$message_prefix} author type was found (schema:Person)."); // Author name. $expected_value = array('type' => 'literal', 'value' => $this->adminUser->label()); $this->assertTrue($graph->hasProperty($this->authorUri, 'http://schema.org/name', $expected_value), "{$message_prefix} author name was found (schema:name)."); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { // Cancel account immediately, if the current user has administrative // privileges, no confirmation mail shall be sent, and the user does not // attempt to cancel the own account. if (!$form_state->isValueEmpty('access') && $form_state->isValueEmpty('user_cancel_confirm') && $this->entity->id() != $this->currentUser()->id()) { user_cancel($form_state->getValues(), $this->entity->id(), $form_state->getValue('user_cancel_method')); $form_state->setRedirectUrl($this->entity->urlInfo('collection')); } else { // Store cancelling method and whether to notify the user in // $this->entity for // \Drupal\user\Controller\UserController::confirmCancel(). $this->entity->user_cancel_method = $form_state->getValue('user_cancel_method'); $this->entity->user_cancel_notify = $form_state->getValue('user_cancel_notify'); $this->entity->save(); _user_mail_notify('cancel_confirm', $this->entity); drupal_set_message($this->t('A confirmation request to cancel your account has been sent to your email address.')); $this->logger('user')->notice('Sent account cancellation request to %name %email.', array('%name' => $this->entity->label(), '%email' => '<' . $this->entity->getEmail() . '>')); $form_state->setRedirect('entity.user.canonical', array('user' => $this->entity->id())); } }
/** * {@inheritdoc} */ public function label() { return $this->subject->label(); }
/** * Unmasquerades the current user. * * @param \Drupal\user\UserInterface $account * The user account to unmasquerade from. */ protected function unmasquerade(UserInterface $account) { $this->drupalGet('/user/' . $account->id()); $this->clickLink('Unmasquerade'); // @todo Fix when crsfTokenSeed is avalable. // $this->drupalGet('unmasquerade', array( // 'query' => array( // 'token' => $this->drupalGetToken('unmasquerade'), // ), // )); $this->assertResponse(200); $this->assertText('You are no longer 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; } }