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