3 namespace Drupal\Tests\contact\Functional\Views;
5 use Drupal\Core\Cache\Cache;
6 use Drupal\Tests\views\Functional\ViewTestBase;
7 use Drupal\views\Tests\ViewTestData;
8 use Drupal\user\Entity\User;
11 * Tests the contact link field.
14 * @see \Drupal\contact\Plugin\views\field\ContactLink.
16 class ContactLinkTest extends ViewTestBase {
19 * Stores the user data service used by the test.
21 * @var \Drupal\user\UserDataInterface
30 public static $modules = ['contact_test_views'];
33 * Views used by this test.
37 public static $testViews = ['test_contact_link'];
42 protected function setUp($import_test_views = TRUE) {
43 parent::setUp($import_test_views);
45 ViewTestData::createTestViews(get_class($this), ['contact_test_views']);
47 $this->userData = $this->container->get('user.data');
53 public function testContactLink() {
55 $accounts['root'] = User::load(1);
56 // Create an account with access to all contact pages.
57 $admin_account = $this->drupalCreateUser(['administer users']);
58 $accounts['admin'] = $admin_account;
59 // Create an account with no access to contact pages.
60 $no_contact_account = $this->drupalCreateUser();
61 $accounts['no_contact'] = $no_contact_account;
63 // Create an account with access to contact pages.
64 $contact_account = $this->drupalCreateUser(['access user contact forms']);
65 $accounts['contact'] = $contact_account;
67 $this->drupalLogin($admin_account);
68 $this->drupalGet('test-contact-link');
69 // The admin user has access to all contact links beside his own.
70 $this->assertContactLinks($accounts, ['root', 'no_contact', 'contact']);
72 $this->drupalLogin($no_contact_account);
73 $this->drupalGet('test-contact-link');
74 // Ensure that the user without the permission doesn't see any link.
75 $this->assertContactLinks($accounts, []);
77 $this->drupalLogin($contact_account);
78 $this->drupalGet('test-contact-link');
79 $this->assertContactLinks($accounts, ['root', 'admin', 'no_contact']);
81 // Disable contact link for no_contact.
82 $this->userData->set('contact', $no_contact_account->id(), 'enabled', FALSE);
83 // @todo Remove cache invalidation in https://www.drupal.org/node/2477903.
84 Cache::invalidateTags($no_contact_account->getCacheTagsToInvalidate());
85 $this->drupalGet('test-contact-link');
86 $this->assertContactLinks($accounts, ['root', 'admin']);
90 * Asserts whether certain users contact links appear on the page.
92 * @param array $accounts
93 * All user objects used by the test.
95 * Users which should have contact links.
97 public function assertContactLinks(array $accounts, array $names) {
98 $result = $this->xpath('//div[contains(@class, "views-field-contact")]//a');
99 $this->assertEqual(count($result), count($names));
100 foreach ($names as $name) {
101 $account = $accounts[$name];
103 $result = $this->xpath('//div[contains(@class, "views-field-contact")]//a[contains(@href, :url)]', [':url' => $account->url('contact-form')]);
104 $this->assertTrue(count($result));