You are here

function ContactPersonalTest::testPersonalContactAccess in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/contact/src/Tests/ContactPersonalTest.php \Drupal\contact\Tests\ContactPersonalTest::testPersonalContactAccess()

Tests access to the personal contact form.

File

core/modules/contact/src/Tests/ContactPersonalTest.php, line 111
Contains \Drupal\contact\Tests\ContactPersonalTest.

Class

ContactPersonalTest
Tests personal contact form functionality.

Namespace

Drupal\contact\Tests

Code

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
      ->getUsername(),
  )));

  // 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);
}