‪TYPO3CMS  ‪main
EmailViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
25 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
26 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
27 use TYPO3Fluid\Fluid\View\TemplateView;
28 
29 final class ‪EmailViewHelperTest extends FunctionalTestCase
30 {
32 
33  protected const ‪LANGUAGE_PRESETS = [
34  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
35  ];
36 
37  #[Test]
38  public function ‪renderCreatesProperMarkupInBackend(): void
39  {
40  $context = $this->get(RenderingContextFactory::class)->create();
41  $context->getTemplatePaths()->setTemplateSource('<f:link.email email="foo@example.com">send mail</f:link.email>');
42  self::assertEquals('<a href="mailto:foo@example.com">send mail</a>', (new TemplateView($context))->render());
43  }
44 
45  #[Test]
47  {
48  $context = $this->get(RenderingContextFactory::class)->create();
49  $context->getTemplatePaths()->setTemplateSource('<f:link.email email="foo@example.com" />');
50  self::assertEquals('<a href="mailto:foo@example.com">foo@example.com</a>', (new TemplateView($context))->render());
51  }
52 
53  public static function ‪renderEncodesEmailInFrontendDataProvider(): array
54  {
55  return [
56  'Plain email' => [
57  '<f:link.email email="some@email.tld" />',
58  ['config.spamProtectEmailAddresses = 0'],
59  '<a href="mailto:some@email.tld">some@email.tld</a>',
60  ],
61  'Plain email with prefilled subject' => [
62  '<f:link.email email="some@email.tld" subject="This is a test email" />',
63  ['config.spamProtectEmailAddresses = 0'],
64  '<a href="mailto:some@email.tld?subject=This%20is%20a%20test%20email">some@email.tld</a>',
65  ],
66  'Plain email with prefilled subject and body' => [
67  '<f:link.email email="some@email.tld" subject="This is a test email" body="Hey Andrew,\nyou should check out these simple email links." />',
68  ['config.spamProtectEmailAddresses = 0'],
69  '<a href="mailto:some@email.tld?subject=This%20is%20a%20test%20email&amp;body=Hey%20Andrew%2C%5Cnyou%20should%20check%20out%20these%20simple%20email%20links.">some@email.tld</a>',
70  ],
71  'Plain email with subject, cc and bcc' => [
72  '<f:link.email email="some@email.tld" subject="This is a test email" cc="benni@example.com,mack@example.com" bcc="all-team-members@example.com" />',
73  ['config.spamProtectEmailAddresses = 0'],
74  '<a href="mailto:some@email.tld?subject=This%20is%20a%20test%20email&amp;cc=benni%40example.com%2Cmack%40example.com&amp;bcc=all-team-members%40example.com">some@email.tld</a>',
75  ],
76  'Plain email with spam protection' => [
77  '<f:link.email email="some@email.tld" />',
78  ['config.spamProtectEmailAddresses = 1'],
79  '<a href="#" data-mailto-token="nbjmup+tpnfAfnbjm/ume" data-mailto-vector="1">some(at)email.tld</a>',
80  ],
81  'Plain email with spam protection and markup substitution' => [
82  '<f:link.email email="some@email.tld" />',
83  [
84  'config.spamProtectEmailAddresses = 1',
85  'config.spamProtectEmailAddresses_atSubst = <span class="at"></span>',
86  'config.spamProtectEmailAddresses_lastDotSubst = <span class="dot"></span>',
87  ],
88  '<a href="#" data-mailto-token="nbjmup+tpnfAfnbjm/ume" data-mailto-vector="1">some<span class="at"></span>email<span class="dot"></span>tld</a>',
89  ],
90  'Susceptible email' => [
91  '<f:link.email email="\"><script>alert(\'email\')</script>" />',
92  ['config.spamProtectEmailAddresses = 0'],
93  '<a href="mailto:&quot;&gt;&lt;script&gt;alert(&#039;email&#039;)&lt;/script&gt;">&quot;&gt;&lt;script&gt;alert(&#039;email&#039;)&lt;/script&gt;</a>',
94  ],
95  'Susceptible email with spam protection' => [
96  '<f:link.email email="\"><script>alert(\'email\')</script>" />',
97  ['config.spamProtectEmailAddresses = 1'],
98  '<a href="#" data-mailto-token="nbjmup+&quot;&gt;&lt;tdsjqu&gt;bmfsu(&#039;fnbjm&#039;)&lt;0tdsjqu&gt;" data-mailto-vector="1">&quot;&gt;&lt;script&gt;alert(&#039;email&#039;)&lt;/script&gt;</a>',
99  ],
100  ];
101  }
102 
103  #[DataProvider('renderEncodesEmailInFrontendDataProvider')]
104  #[Test]
105  public function ‪renderEncodesEmailInFrontend(string $template, array $typoScript, string $expected): void
106  {
107  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/pages.csv');
108  $this->importCSVDataSet(__DIR__ . '/../../Fixtures/be_users.csv');
109  $this->setUpBackendUser(1);
111  'test',
112  $this->‪buildSiteConfiguration(1, '/'),
113  );
114  (new ‪ConnectionPool())->getConnectionForTable('sys_template')->insert('sys_template', [
115  'pid' => 1,
116  'root' => 1,
117  'clear' => 1,
118  'config' => implode("\n", $typoScript) . "\n" . <<<EOT
119 page = PAGE
120 page.10 = FLUIDTEMPLATE
121 page.10 {
122  template = TEXT
123  template.value = $template
124 }
125 EOT
126  ]);
127  $response = $this->executeFrontendSubRequest((new InternalRequest())->withPageId(1));
128  self::assertStringContainsString($expected, (string)$response->getBody());
129  }
130 }
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:36
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:49
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:86
‪TYPO3\CMS\Fluid\Core\Rendering\RenderingContextFactory
Definition: RenderingContextFactory.php:51
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46