‪TYPO3CMS  10.4
EmailViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
21 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
22 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
23 
27 class ‪EmailViewHelperTest extends ViewHelperBaseTestcase
28 {
32  protected ‪$viewHelper;
33 
37  protected ‪$cObjBackup;
38 
39  protected function ‪setUp(): void
40  {
41  parent::setUp();
42  ‪$GLOBALS['TSFE'] = new \stdClass();
43  ‪$GLOBALS['TSFE']->cObj = $this->createMock(ContentObjectRenderer::class);
44  $this->viewHelper = $this->getAccessibleMock(EmailViewHelper::class, ['renderChildren']);
45  $this->injectDependenciesIntoViewHelper($this->viewHelper);
46  }
47 
52  {
53  $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
54  ->setMethods(['setTagName', 'addAttribute', 'setContent'])
55  ->getMock();
56  $mockTagBuilder->expects(self::atLeastOnce())->method('setTagName')->with('a');
57  $mockTagBuilder->expects(self::once())->method('addAttribute')->with('href', 'mailto:some@email.tld');
58  $mockTagBuilder->expects(self::once())->method('setContent')->with('some content');
59  $this->viewHelper->setTagBuilder($mockTagBuilder);
60  $this->viewHelper->expects(self::any())->method('renderChildren')->willReturn('some content');
61  $this->setArgumentsUnderTest(
62  $this->viewHelper,
63  [
64  'email' => 'some@email.tld',
65  ]
66  );
67  $this->viewHelper->initializeArgumentsAndRender();
68  }
69 
74  {
75  $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
76  ->setMethods(['setTagName', 'addAttribute', 'setContent'])
77  ->getMock();
78  $mockTagBuilder->expects(self::once())->method('setContent')->with('some@email.tld');
79  $this->viewHelper->setTagBuilder($mockTagBuilder);
80  $this->viewHelper->expects(self::any())->method('renderChildren')->willReturn(null);
81  $this->setArgumentsUnderTest(
82  $this->viewHelper,
83  [
84  'email' => 'some@email.tld',
85  ]
86  );
87  $this->viewHelper->initializeArgumentsAndRender();
88  }
89 
94  {
95  return [
96  'Plain email' => [
97  'some@email.tld',
98  0,
99  '<a href="mailto:some@email.tld">some@email.tld</a>',
100  ],
101  'Plain email with spam protection' => [
102  'some@email.tld',
103  1,
104  '<a href="javascript:linkTo_UnCryptMailto(%27nbjmup%2BtpnfAfnbjm%5C%2Fume%27);">some(at)email.tld</a>',
105  ],
106  'Plain email with ascii spam protection' => [
107  'some@email.tld',
108  'ascii',
109  '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#115;&#111;&#109;&#101;&#64;&#101;&#109;&#97;&#105;&#108;&#46;&#116;&#108;&#100;">some(at)email.tld</a>',
110  ],
111  'Susceptible email' => [
112  '"><script>alert(\'email\')</script>',
113  0,
114  '<a href="mailto:&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
115  ],
116  'Susceptible email with spam protection' => [
117  '"><script>alert(\'email\')</script>',
118  1,
119  '<a href="javascript:linkTo_UnCryptMailto(%27nbjmup%2B%5Cu0022%5Cu003E%5Cu003Ctdsjqu%5Cu003Ebmfsu%28%5Cu0027fnbjm%5Cu0027%29%5Cu003C0tdsjqu%5Cu003E%27);">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
120  ],
121  'Susceptible email with ascii spam protection' => [
122  '"><script>alert(\'email\')</script>',
123  'ascii',
124  '<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;&#34;&#62;&#60;&#115;&#99;&#114;&#105;&#112;&#116;&#62;&#97;&#108;&#101;&#114;&#116;&#40;&#39;&#101;&#109;&#97;&#105;&#108;&#39;&#41;&#60;&#47;&#115;&#99;&#114;&#105;&#112;&#116;&#62;">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
125  ],
126  ];
127  }
128 
136  public function ‪renderEncodesEmailInFrontend($email, $spamProtectEmailAddresses, $expected)
137  {
139  $tsfe = $this->getMockBuilder(TypoScriptFrontendController::class)
140  ->setMethods(['dummy'])
141  ->disableOriginalConstructor()
142  ->getMock();
143  $tsfe->cObj = new ContentObjectRenderer();
144  $tsfe->spamProtectEmailAddresses = $spamProtectEmailAddresses;
145  $tsfe->config = [
146  'config' => [
147  'spamProtectEmailAddresses_atSubst' => '',
148  'spamProtectEmailAddresses_lastDotSubst' => '',
149  ],
150  ];
151  ‪$GLOBALS['TSFE'] = $tsfe;
152  ‪$viewHelper = $this->getMockBuilder(EmailViewHelper::class)
153  ->setMethods(['isFrontendAvailable', 'renderChildren'])
154  ->getMock();
155  ‪$viewHelper->expects(self::once())->method('isFrontendAvailable')->willReturn(true);
156  ‪$viewHelper->expects(self::once())->method('renderChildren')->willReturn(null);
157  ‪$viewHelper->setArguments([
158  'email' => $email,
159  ]);
160  ‪$viewHelper->initialize();
161  self::assertSame(
162  $expected,
164  );
165  }
166 }
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:98
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
Definition: ContentObjectRenderer.php:97