function MailTest::testConvertRelativeUrlsIntoAbsolute

Same name in other branches
  1. 9 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testConvertRelativeUrlsIntoAbsolute()
  2. 10 core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testConvertRelativeUrlsIntoAbsolute()
  3. 11.x core/modules/system/tests/src/Kernel/Mail/MailTest.php \Drupal\Tests\system\Kernel\Mail\MailTest::testConvertRelativeUrlsIntoAbsolute()

Checks that relative paths in mails are converted into absolute URLs.

File

core/modules/system/tests/src/Kernel/Mail/MailTest.php, line 190

Class

MailTest
Performs tests on the pluggable mailing framework.

Namespace

Drupal\Tests\system\Kernel\Mail

Code

public function testConvertRelativeUrlsIntoAbsolute() {
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    $this->configureDefaultMailInterface('test_html_mail_collector');
    // Fetch the hostname and port for matching against.
    $http_host = \Drupal::request()->getSchemeAndHttpHost();
    // Random generator.
    $random = new Random();
    // One random tag name.
    $tag_name = strtolower($random->name(8, TRUE));
    // Test root relative urls.
    foreach ([
        'href',
        'src',
    ] as $attribute) {
        // Reset the state variable that holds sent messages.
        \Drupal::state()->set('system.test_mail_collector', []);
        $html = "<{$tag_name} {$attribute}=\"/root-relative\">root relative url in mail test</{$tag_name}>";
        $expected_html = "<{$tag_name} {$attribute}=\"{$http_host}/root-relative\">root relative url in mail test</{$tag_name}>";
        // Prepare render array.
        $render = [
            '#markup' => Markup::create($html),
        ];
        // Send a test message that mail_cancel_test_mail_alter should cancel.
        \Drupal::service('plugin.manager.mail')->mail('mail_html_test', 'render_from_message_param', 'relative_url@example.com', $language_interface->getId(), [
            'message' => $render,
        ]);
        // Retrieve sent message.
        $captured_emails = \Drupal::state()->get('system.test_mail_collector');
        $sent_message = end($captured_emails);
        // Wrap the expected HTML and assert.
        $expected_html = MailFormatHelper::wrapMail($expected_html);
        $this->assertSame($expected_html, $sent_message['body'], "Asserting that {$attribute} is properly converted for mails.");
    }
    // Test protocol relative urls.
    foreach ([
        'href',
        'src',
    ] as $attribute) {
        // Reset the state variable that holds sent messages.
        \Drupal::state()->set('system.test_mail_collector', []);
        $html = "<{$tag_name} {$attribute}=\"//example.com/protocol-relative\">protocol relative url in mail test</{$tag_name}>";
        $expected_html = "<{$tag_name} {$attribute}=\"//example.com/protocol-relative\">protocol relative url in mail test</{$tag_name}>";
        // Prepare render array.
        $render = [
            '#markup' => Markup::create($html),
        ];
        // Send a test message that mail_cancel_test_mail_alter should cancel.
        \Drupal::service('plugin.manager.mail')->mail('mail_html_test', 'render_from_message_param', 'relative_url@example.com', $language_interface->getId(), [
            'message' => $render,
        ]);
        // Retrieve sent message.
        $captured_emails = \Drupal::state()->get('system.test_mail_collector');
        $sent_message = end($captured_emails);
        // Wrap the expected HTML and assert.
        $expected_html = MailFormatHelper::wrapMail($expected_html);
        $this->assertSame($expected_html, $sent_message['body'], "Asserting that {$attribute} is properly converted for mails.");
    }
    // Test absolute urls.
    foreach ([
        'href',
        'src',
    ] as $attribute) {
        // Reset the state variable that holds sent messages.
        \Drupal::state()->set('system.test_mail_collector', []);
        $html = "<{$tag_name} {$attribute}=\"http://example.com/absolute\">absolute url in mail test</{$tag_name}>";
        $expected_html = "<{$tag_name} {$attribute}=\"http://example.com/absolute\">absolute url in mail test</{$tag_name}>";
        // Prepare render array.
        $render = [
            '#markup' => Markup::create($html),
        ];
        // Send a test message that mail_cancel_test_mail_alter should cancel.
        \Drupal::service('plugin.manager.mail')->mail('mail_html_test', 'render_from_message_param', 'relative_url@example.com', $language_interface->getId(), [
            'message' => $render,
        ]);
        // Retrieve sent message.
        $captured_emails = \Drupal::state()->get('system.test_mail_collector');
        $sent_message = end($captured_emails);
        // Wrap the expected HTML and assert.
        $expected_html = MailFormatHelper::wrapMail($expected_html);
        $this->assertSame($expected_html, $sent_message['body'], "Asserting that {$attribute} is properly converted for mails.");
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.