[!!!][TASK] Remove CMS base ViewHelper classes
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Link / EmailViewHelperTest.php
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Link;
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
17 use TYPO3\CMS\Fluid\ViewHelpers\Link\EmailViewHelper;
18 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
19 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
20 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
21 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
22
23 /**
24 * Test case
25 */
26 class EmailViewHelperTest extends ViewHelperBaseTestcase
27 {
28 /**
29 * @var \TYPO3\CMS\Fluid\ViewHelpers\Link\EmailViewHelper
30 */
31 protected $viewHelper;
32
33 /**
34 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
35 */
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
49 /**
50 * @test
51 */
52 public function renderCorrectlySetsTagNameAndAttributesAndContent()
53 {
54 $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
55 ->setMethods(['setTagName', 'addAttribute', 'setContent'])
56 ->getMock();
57 $mockTagBuilder->expects($this->once())->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
71 /**
72 * @test
73 */
74 public function renderSetsTagContentToEmailIfRenderChildrenReturnNull()
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
91 /**
92 * @return array
93 */
94 public function renderEncodesEmailInFrontendDataProvider()
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(\'nbjmup+tpnfAfnbjm\/ume\');">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(\'nbjmup+\u0022\u003E\u003Ctdsjqu\u003Ebmfsu(\u0027fnbjm\u0027)\u003C0tdsjqu\u003E\');">&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
130 /**
131 * @test
132 * @dataProvider renderEncodesEmailInFrontendDataProvider
133 * @param string $email
134 * @param string $spamProtectEmailAddresses
135 * @param string $expected
136 */
137 public function renderEncodesEmailInFrontend($email, $spamProtectEmailAddresses, $expected)
138 {
139 /** @var TypoScriptFrontendController $tsfe */
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,
164 $viewHelper->render()
165 );
166 }
167 }