View source
<?php
namespace Drupal\contact\Tests\Views;
use Drupal\Core\Cache\Cache;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\user\Entity\User;
class ContactLinkTest extends ViewTestBase {
public $userData;
public static $modules = array(
'contact_test_views',
);
public static $testViews = array(
'test_contact_link',
);
protected function setUp() {
parent::setUp();
ViewTestData::createTestViews(get_class($this), array(
'contact_test_views',
));
$this->userData = $this->container
->get('user.data');
}
public function testContactLink() {
$accounts = array();
$accounts['root'] = User::load(1);
$admin_account = $this
->drupalCreateUser(array(
'administer users',
));
$accounts['admin'] = $admin_account;
$no_contact_account = $this
->drupalCreateUser();
$accounts['no_contact'] = $no_contact_account;
$contact_account = $this
->drupalCreateUser(array(
'access user contact forms',
));
$accounts['contact'] = $contact_account;
$this
->drupalLogin($admin_account);
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, array(
'root',
'no_contact',
'contact',
));
$this
->drupalLogin($no_contact_account);
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, array());
$this
->drupalLogin($contact_account);
$this
->drupalGet('test-contact-link');
$this
->assertContactLinks($accounts, array(
'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, array(
'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)]', array(
':url' => $account
->url('contact-form'),
));
$this
->assertTrue(count($result));
}
}
}