Skip to content
Snippets Groups Projects
Verified Commit 5fa98aca authored by Alex Pott's avatar Alex Pott
Browse files

Issue #124969 by Dave Reid, andregp, naxoc, pameeela, ravi.shankar,...

Issue #124969 by Dave Reid, andregp, naxoc, pameeela, ravi.shankar, bruno.bicudo, _pratik_, jnlar, yogeshmpawar, marcingy, StevenPatz, amitgoyal, Johnny Santos, larowlan, smustgrave, Dries, DanielVeza, mstrelan, cburschka, quietone, alexpott, bradlis7: Contact form opt-out line should be excluded from admin-sent and sender-copy e-mails

(cherry picked from commit 1dbf2cc2)
parent 6881cf41
No related branches found
No related tags found
6 merge requests!8376Drupal views: adding more granularity to the ‘use ajax’ functionality,!8300Issue #3443586 View area displays even when parent view has no results.,!7567Issue #3153723 by quietone, Hardik_Patel_12: Change the scaffolding...,!7565Issue #3153723 by quietone, Hardik_Patel_12: Change the scaffolding...,!7509Change label "Block description" to "Block type",!7344Issue #3292350 by O'Briat, KlemenDEV, hswong3i, smustgrave, quietone: Update...
Pipeline #136272 passed with warnings
Pipeline: drupal

#136303

    Pipeline: drupal

    #136297

      Pipeline: drupal

      #136286

        +1
        ......@@ -161,7 +161,13 @@ function contact_mail($key, &$message, $params) {
        $message['subject'] .= t('[@site-name] @subject', $variables, $options);
        $message['body'][] = t('Hello @recipient-name,', $variables, $options);
        $message['body'][] = t("@sender-name (@sender-url) has sent you a message via your contact form at @site-name.", $variables, $options);
        $message['body'][] = t("If you don't want to receive such emails, you can change your settings at @recipient-edit-url.", $variables, $options);
        // Only include the opt-out line in the original email and not in the
        // copy to the sender. Also exclude this if the email was sent from a
        // user administrator because they can always send emails even if the
        // contacted user has disabled their contact form.
        if ($key === 'user_mail' && !$params['sender']->hasPermission('administer users')) {
        $message['body'][] = t("If you don't want to receive such messages, you can change your settings at @recipient-edit-url.", $variables, $options);
        }
        $build = \Drupal::entityTypeManager()
        ->getViewBuilder('contact_message')
        ->view($contact_message, 'mail');
        ......
        ......@@ -336,18 +336,65 @@ protected function checkContactAccess($response, $contact_value = NULL) {
        * @param array $message
        * (optional) An array with the form fields being used. Defaults to an empty
        * array.
        * @param bool $user_copy
        * (optional) A boolean to determine whether to send a user copy email.
        * Defaults to FALSE.
        *
        * @return array
        * An array with the form fields being used.
        */
        protected function submitPersonalContact(AccountInterface $account, array $message = []) {
        protected function submitPersonalContact(AccountInterface $account, array $message = [], bool $user_copy = FALSE) {
        $message += [
        'subject[0][value]' => $this->randomMachineName(16) . '< " =+ >',
        'message[0][value]' => $this->randomMachineName(64) . '< " =+ >',
        'copy' => $user_copy,
        ];
        $this->drupalGet('user/' . $account->id() . '/contact');
        $this->submitForm($message, 'Send message');
        return $message;
        }
        /**
        * Tests that the opt-out message is included correctly in contact emails.
        */
        public function testPersonalContactForm(): void {
        $opt_out_message = "If you don't want to receive such messages, you can change your settings at";
        // Send an email from an admin (should not contain the opt-out message).
        $this->drupalLogin($this->adminUser);
        $this->submitPersonalContact($this->contactUser);
        $this->drupalLogout();
        $this->assertStringNotContainsString($opt_out_message, $this->getMails()[0]['body'], 'Opt-out message excluded in email.');
        // Send an email from a non-admin (should contain the opt-out message).
        $this->drupalLogin($this->webUser);
        $this->submitPersonalContact($this->contactUser);
        $this->assertMailString('body', $opt_out_message, 1, 'Opt-out message included in email.');
        }
        /**
        * Tests that the opt-out message is not included in user copy emails.
        */
        public function testPersonalContactFormUserCopy(): void {
        $opt_out_message = "If you don't want to receive such messages, you can change your settings at";
        // Send an email from an admin.
        $this->drupalLogin($this->adminUser);
        $this->submitPersonalContact($this->contactUser, [], TRUE);
        $this->drupalLogout();
        // Send an email from a non-admin.
        $this->drupalLogin($this->webUser);
        $this->submitPersonalContact($this->contactUser, [], TRUE);
        $user_copy_emails = $this->getMails(['id' => 'contact_user_copy']);
        // Tests that the opt-out message is not included in admin user copy emails.
        $this->assertStringNotContainsString($opt_out_message, $user_copy_emails[0]['body'], 'Opt-out message not included in admin user copy email.');
        // Tests that the opt-out message is not included in non-admin user copy emails.
        $this->assertStringNotContainsString($opt_out_message, $user_copy_emails[1]['body'], 'Opt-out message not included in non-admin user copy email.');
        }
        }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Please register or to comment