View source
<?php
namespace Drupal\Tests\contact\Functional\Views;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\user\Entity\User;
class ContactLinkTest extends ViewTestBase {
public $userData;
public static $modules = [
'contact_test_views',
];
protected $defaultTheme = 'classy';
public static $testViews = [
'test_contact_link',
];
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
ViewTestData::createTestViews(get_class($this), [
'contact_test_views',
]);
$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) {
$result = $this
->xpath('//div[contains(@class, "views-field-contact")]//a');
$this
->assertEqual(count($result), count($names));
foreach ($names as $name) {
$account = $accounts[$name];
$result = $this
->xpath('//div[contains(@class, "views-field-contact")]//a[contains(@href, :url)]', [
':url' => $account
->toUrl('contact-form')
->toString(),
]);
$this
->assertGreaterThan(0, count($result));
}
}
}