f6b6013b43aef02f34bc24284032fb27fa26088c
[yaffs-website] / web / core / modules / contact / src / Tests / Views / ContactLinkTest.php
1 <?php
2
3 namespace Drupal\contact\Tests\Views;
4
5 use Drupal\Core\Cache\Cache;
6 use Drupal\views\Tests\ViewTestBase;
7 use Drupal\views\Tests\ViewTestData;
8 use Drupal\user\Entity\User;
9
10 /**
11  * Tests the contact link field.
12  *
13  * @group contact
14  * @see \Drupal\contact\Plugin\views\field\ContactLink.
15  */
16 class ContactLinkTest extends ViewTestBase {
17
18   /**
19    * Stores the user data service used by the test.
20    *
21    * @var \Drupal\user\UserDataInterface
22    */
23   public $userData;
24
25   /**
26    * Modules to enable.
27    *
28    * @var array
29    */
30   public static $modules = ['contact_test_views'];
31
32   /**
33    * Views used by this test.
34    *
35    * @var array
36    */
37   public static $testViews = ['test_contact_link'];
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     ViewTestData::createTestViews(get_class($this), ['contact_test_views']);
46
47     $this->userData = $this->container->get('user.data');
48   }
49
50   /**
51    * Tests contact link.
52    */
53   public function testContactLink() {
54     $accounts = [];
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;
62
63     // Create an account with access to contact pages.
64     $contact_account = $this->drupalCreateUser(['access user contact forms']);
65     $accounts['contact'] = $contact_account;
66
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']);
71
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, []);
76
77     $this->drupalLogin($contact_account);
78     $this->drupalGet('test-contact-link');
79     $this->assertContactLinks($accounts, ['root', 'admin', 'no_contact']);
80
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']);
87   }
88
89   /**
90    * Asserts whether certain users contact links appear on the page.
91    *
92    * @param array $accounts
93    *   All user objects used by the test.
94    * @param array $names
95    *   Users which should have contact links.
96    */
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];
102
103       $result = $this->xpath('//div[contains(@class, "views-field-contact")]//a[contains(@href, :url)]', [':url' => $account->url('contact-form')]);
104       $this->assertTrue(count($result));
105     }
106   }
107
108 }