View source
<?php
namespace Drupal\Tests\contact\Functional\Views;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\user\Entity\User;
class ContactLinkTest extends ViewTestBase {
public $userData;
protected static $modules = [
'contact_test_views',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_contact_link',
];
protected function setUp($import_test_views = TRUE, $modules = [
'contact_test_views',
]) : void {
parent::setUp($import_test_views, $modules);
$this->userData = $this->container
->get('user.data');
}
public function testContactLink() {
$accounts = [];
$accounts['root'] = User::load(1);
$admin_account = $this
->drupalCreateUser([
'administer users',
]);
$accounts['admin'] = $admin_account;
$no_contact_account = $this
->drupalCreateUser();
$accounts['no_contact'] = $no_contact_account;
$contact_account = $this
->drupalCreateUser([
'access user contact forms',
]);
$accounts['contact'] = $contact_account;
$this
->drupalLogin($admin_account);
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, [
'root',
'no_contact',
'contact',
]);
$this
->drupalLogin($no_contact_account);
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, []);
$this
->drupalLogin($contact_account);
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, [
'root',
'admin',
'no_contact',
]);
$this->userData
->set('contact', $no_contact_account
->id(), 'enabled', FALSE);
Cache::invalidateTags($no_contact_account
->getCacheTagsToInvalidate());
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, [
'root',
'admin',
]);
}
public function assertContactLinks(array $accounts, array $names) : void {
$this
->assertSession()
->elementsCount('xpath', '//div[contains(@class, "views-field-contact")]//a', count($names));
foreach ($names as $name) {
$account_url = $accounts[$name]
->toUrl('contact-form')
->toString();
$this
->assertSession()
->elementExists('xpath', "//div[contains(@class, 'views-field-contact')]//a[contains(@href, '{$account_url}')]");
}
}
}