TYPO3 CMS  TYPO3_8-7
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 
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->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->_set('tag', $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->initialize();
69  $this->viewHelper->render();
70  }
71 
76  {
77  $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
78  ->setMethods(['setTagName', 'addAttribute', 'setContent'])
79  ->getMock();
80  $mockTagBuilder->expects($this->once())->method('setContent')->with('some@email.tld');
81  $this->viewHelper->_set('tag', $mockTagBuilder);
82  $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue(null));
83  $this->setArgumentsUnderTest(
84  $this->viewHelper,
85  [
86  'email' => 'some@email.tld',
87  ]
88  );
89  $this->viewHelper->initialize();
90  $this->viewHelper->render();
91  }
92 
97  {
98  return [
99  'Plain email' => [
100  'some@email.tld',
101  0,
102  '<a href="mailto:some@email.tld">some@email.tld</a>',
103  ],
104  'Plain email with spam protection' => [
105  'some@email.tld',
106  1,
107  '<a href="javascript:linkTo_UnCryptMailto(\'nbjmup+tpnfAfnbjm\/ume\');">some(at)email.tld</a>',
108  ],
109  'Plain email with ascii spam protection' => [
110  'some@email.tld',
111  'ascii',
112  '<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>',
113  ],
114  'Susceptible email' => [
115  '"><script>alert(\'email\')</script>',
116  0,
117  '<a href="mailto:&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
118  ],
119  'Susceptible email with spam protection' => [
120  '"><script>alert(\'email\')</script>',
121  1,
122  '<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>',
123  ],
124  'Susceptible email with ascii spam protection' => [
125  '"><script>alert(\'email\')</script>',
126  'ascii',
127  '<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>',
128  ],
129  ];
130  }
131 
139  public function renderEncodesEmailInFrontend($email, $spamProtectEmailAddresses, $expected)
140  {
142  $tsfe = $this->getMockBuilder(TypoScriptFrontendController::class)
143  ->setMethods(['dummy'])
144  ->disableOriginalConstructor()
145  ->getMock();
146  $tsfe->cObj = new ContentObjectRenderer();
147  $tsfe->spamProtectEmailAddresses = $spamProtectEmailAddresses;
148  $tsfe->config = [
149  'config' => [
150  'spamProtectEmailAddresses_atSubst' => '',
151  'spamProtectEmailAddresses_lastDotSubst' => '',
152  ],
153  ];
154  $GLOBALS['TSFE'] = $tsfe;
155  $viewHelper = $this->getMockBuilder(EmailViewHelper::class)
156  ->setMethods(['isFrontendAvailable', 'renderChildren'])
157  ->getMock();
158  $viewHelper->expects($this->once())->method('isFrontendAvailable')->willReturn(true);
159  $viewHelper->expects($this->once())->method('renderChildren')->willReturn(null);
160  $viewHelper->setArguments([
161  'email' => $email,
162  ]);
163  $viewHelper->initialize();
164  $this->assertSame(
165  $expected,
166  $viewHelper->render()
167  );
168  }
169 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']