[DOCS] Document multiple --task options for scheduler
[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 EmailViewHelper
30 */
31 protected $viewHelper;
32
33 /**
34 * @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
35 */
36 protected $cObjBackup;
37
38 protected function setUp(): void
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->getAccessibleMock(EmailViewHelper::class, ['renderChildren']);
44 $this->injectDependenciesIntoViewHelper($this->viewHelper);
45 }
46
47 /**
48 * @test
49 */
50 public function renderCorrectlySetsTagNameAndAttributesAndContent()
51 {
52 $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
53 ->setMethods(['setTagName', 'addAttribute', 'setContent'])
54 ->getMock();
55 $mockTagBuilder->expects(self::atLeastOnce())->method('setTagName')->with('a');
56 $mockTagBuilder->expects(self::once())->method('addAttribute')->with('href', 'mailto:some@email.tld');
57 $mockTagBuilder->expects(self::once())->method('setContent')->with('some content');
58 $this->viewHelper->setTagBuilder($mockTagBuilder);
59 $this->viewHelper->expects(self::any())->method('renderChildren')->willReturn('some content');
60 $this->setArgumentsUnderTest(
61 $this->viewHelper,
62 [
63 'email' => 'some@email.tld',
64 ]
65 );
66 $this->viewHelper->initializeArgumentsAndRender();
67 }
68
69 /**
70 * @test
71 */
72 public function renderSetsTagContentToEmailIfRenderChildrenReturnNull()
73 {
74 $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
75 ->setMethods(['setTagName', 'addAttribute', 'setContent'])
76 ->getMock();
77 $mockTagBuilder->expects(self::once())->method('setContent')->with('some@email.tld');
78 $this->viewHelper->setTagBuilder($mockTagBuilder);
79 $this->viewHelper->expects(self::any())->method('renderChildren')->willReturn(null);
80 $this->setArgumentsUnderTest(
81 $this->viewHelper,
82 [
83 'email' => 'some@email.tld',
84 ]
85 );
86 $this->viewHelper->initializeArgumentsAndRender();
87 }
88
89 /**
90 * @return array
91 */
92 public function renderEncodesEmailInFrontendDataProvider()
93 {
94 return [
95 'Plain email' => [
96 'some@email.tld',
97 0,
98 '<a href="mailto:some@email.tld">some@email.tld</a>',
99 ],
100 'Plain email with spam protection' => [
101 'some@email.tld',
102 1,
103 '<a href="javascript:linkTo_UnCryptMailto(%27nbjmup%2BtpnfAfnbjm%5C%2Fume%27);">some(at)email.tld</a>',
104 ],
105 'Plain email with ascii spam protection' => [
106 'some@email.tld',
107 'ascii',
108 '<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>',
109 ],
110 'Susceptible email' => [
111 '"><script>alert(\'email\')</script>',
112 0,
113 '<a href="mailto:&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
114 ],
115 'Susceptible email with spam protection' => [
116 '"><script>alert(\'email\')</script>',
117 1,
118 '<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>',
119 ],
120 'Susceptible email with ascii spam protection' => [
121 '"><script>alert(\'email\')</script>',
122 'ascii',
123 '<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>',
124 ],
125 ];
126 }
127
128 /**
129 * @test
130 * @dataProvider renderEncodesEmailInFrontendDataProvider
131 * @param string $email
132 * @param string $spamProtectEmailAddresses
133 * @param string $expected
134 */
135 public function renderEncodesEmailInFrontend($email, $spamProtectEmailAddresses, $expected)
136 {
137 /** @var TypoScriptFrontendController $tsfe */
138 $tsfe = $this->getMockBuilder(TypoScriptFrontendController::class)
139 ->setMethods(['dummy'])
140 ->disableOriginalConstructor()
141 ->getMock();
142 $tsfe->cObj = new ContentObjectRenderer();
143 $tsfe->spamProtectEmailAddresses = $spamProtectEmailAddresses;
144 $tsfe->config = [
145 'config' => [
146 'spamProtectEmailAddresses_atSubst' => '',
147 'spamProtectEmailAddresses_lastDotSubst' => '',
148 ],
149 ];
150 $GLOBALS['TSFE'] = $tsfe;
151 $viewHelper = $this->getMockBuilder(EmailViewHelper::class)
152 ->setMethods(['isFrontendAvailable', 'renderChildren'])
153 ->getMock();
154 $viewHelper->expects(self::once())->method('isFrontendAvailable')->willReturn(true);
155 $viewHelper->expects(self::once())->method('renderChildren')->willReturn(null);
156 $viewHelper->setArguments([
157 'email' => $email,
158 ]);
159 $viewHelper->initialize();
160 self::assertSame(
161 $expected,
162 $viewHelper->render()
163 );
164 }
165 }