diff --git a/core/modules/contact/contact.views.inc b/core/modules/contact/contact.views.inc new file mode 100644 index 0000000000000000000000000000000000000000..6795b0eb5be70a709755ae6d6d111962c1de44aa --- /dev/null +++ b/core/modules/contact/contact.views.inc @@ -0,0 +1,21 @@ + array( + 'title' => t('Contact link'), + 'help' => t('Provide a simple link to the user contact page.'), + 'id' => 'contact_link', + ), + ); +} diff --git a/core/modules/contact/lib/Drupal/contact/Plugin/views/field/ContactLink.php b/core/modules/contact/lib/Drupal/contact/Plugin/views/field/ContactLink.php new file mode 100644 index 0000000000000000000000000000000000000000..475a72a4e0db63cf79496bffb01c966e92dfa795 --- /dev/null +++ b/core/modules/contact/lib/Drupal/contact/Plugin/views/field/ContactLink.php @@ -0,0 +1,74 @@ +options['text']) ? t('contact') : $this->options['text']; + } + + /** + * {@inheritdoc} + */ + public function access() { + // The access logic is implemented per row. + return TRUE; + } + + + /** + * {@inheritdoc} + */ + public function render_link(EntityInterface $entity, \stdClass $values) { + + if (empty($entity)) { + return; + } + + // Check access when we pull up the user account so we know + // if the user has made the contact page available. + $uid = $entity->id(); + + $path = "user/$uid/contact"; + if (!_contact_personal_tab_access($entity->getBCEntity())) { + return; + } + + $this->options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $path; + + $title = t('Contact %user', array('%user' => $entity->name->value)); + $this->options['alter']['attributes'] = array('title' => $title); + + if (!empty($this->options['text'])) { + return $this->options['text']; + } + else { + return $title; + } + } + +} diff --git a/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactLinkTest.php b/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactLinkTest.php new file mode 100644 index 0000000000000000000000000000000000000000..c2f6a214eac999150bb7f8a22776e667516a5a69 --- /dev/null +++ b/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactLinkTest.php @@ -0,0 +1,118 @@ + 'Contact: Link Field', + 'description' => 'Tests the contact link field.', + 'group' => 'Views module integration', + ); + } + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + ViewTestData::importTestViews(get_class($this), array('contact_test_views')); + + $this->userData = $this->container->get('user.data'); + } + + /** + * Tests contact link. + */ + public function testContactLink() { + $accounts = array(); + $accounts['root'] = user_load(1); + // Create an account with access to all contact pages. + $admin_account = $this->drupalCreateUser(array('administer users')); + $accounts['admin'] = $admin_account; + // Create an account with no access to contact pages. + $no_contact_account = $this->drupalCreateUser(); + $accounts['no_contact'] = $no_contact_account; + + // Create an account with access to contact pages. + $contact_account = $this->drupalCreateUser(array('access user contact forms')); + $accounts['contact'] = $contact_account; + + $this->drupalLogin($admin_account); + $this->drupalGet('test-contact-link'); + // The admin user has access to all contact links beside his own. + $this->assertContactLinks($accounts, array('root', 'no_contact', 'contact')); + + $this->drupalLogin($no_contact_account); + $this->drupalGet('test-contact-link'); + // Ensure that the user without the permission doesn't see any link. + $this->assertContactLinks($accounts, array()); + + $this->drupalLogin($contact_account); + $this->drupalGet('test-contact-link'); + $this->assertContactLinks($accounts, array('root', 'admin', 'no_contact')); + + // Disable contact link for no_contact. + $this->userData->set('contact', $no_contact_account->id(), 'enabled', FALSE); + $this->drupalGet('test-contact-link'); + $this->assertContactLinks($accounts, array('root', 'admin')); + } + + /** + * Asserts whether certain users contact links appear on the page. + * + * @param array $accounts + * All user objects used by the test. + * @param array $names + * Users which should have contact links. + */ + 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]; + + $uri = $account->uri(); + $contact_uri = $uri['path'] . '/contact'; + $result = $this->xpath('//div[contains(@class, "views-field-contact")]//a[contains(@href, :uri)]', array(':uri' => $contact_uri)); + $this->assertTrue(count($result)); + } + } + +} diff --git a/core/modules/contact/tests/modules/contact_test_views/contact_test_views.info.yml b/core/modules/contact/tests/modules/contact_test_views/contact_test_views.info.yml new file mode 100644 index 0000000000000000000000000000000000000000..eb3b5e35012cbd06860876dbd026af2279fd178d --- /dev/null +++ b/core/modules/contact/tests/modules/contact_test_views/contact_test_views.info.yml @@ -0,0 +1,11 @@ +name: 'Contact test views' +type: module +description: 'Provides default views for views contact tests.' +package: Testing +version: VERSION +core: 8.x +dependencies: + - contact + - views + - user +hidden: true diff --git a/core/modules/contact/tests/modules/contact_test_views/contact_test_views.module b/core/modules/contact/tests/modules/contact_test_views/contact_test_views.module new file mode 100644 index 0000000000000000000000000000000000000000..b3d9bbc7f3711e882119cd6b3af051245d859d04 --- /dev/null +++ b/core/modules/contact/tests/modules/contact_test_views/contact_test_views.module @@ -0,0 +1 @@ +