TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
Link/EmailViewHelperTest.php
Go to the documentation of this file.
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 
20 use TYPO3Fluid\Fluid\Core\ViewHelper\TagBuilder;
21 
26 {
30  protected $viewHelper;
31 
35  protected $cObjBackup;
36 
37  protected function setUp()
38  {
39  parent::setUp();
40  $GLOBALS['TSFE'] = new \stdClass();
41  $GLOBALS['TSFE']->cObj = $this->createMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class);
42  $this->viewHelper = $this->getMockBuilder($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\Link\EmailViewHelper::class))
43  ->setMethods(['renderChildren'])
44  ->getMock();
45  $this->injectDependenciesIntoViewHelper($this->viewHelper);
46  }
47 
52  {
53  $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
54  ->setMethods(['setTagName', 'addAttribute', 'setContent'])
55  ->getMock();
56  $mockTagBuilder->expects($this->once())->method('setTagName')->with('a');
57  $mockTagBuilder->expects($this->once())->method('addAttribute')->with('href', 'mailto:some@email.tld');
58  $mockTagBuilder->expects($this->once())->method('setContent')->with('some content');
59  $this->viewHelper->_set('tag', $mockTagBuilder);
60  $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('some content'));
61  $this->setArgumentsUnderTest(
62  $this->viewHelper,
63  [
64  'email' => 'some@email.tld',
65  ]
66  );
67  $this->viewHelper->initialize();
68  $this->viewHelper->render();
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->_set('tag', $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->initialize();
89  $this->viewHelper->render();
90  }
91 
96  {
97  return [
98  'Plain email' => [
99  'some@email.tld',
100  0,
101  '<a href="mailto:some@email.tld">some@email.tld</a>',
102  ],
103  'Plain email with spam protection' => [
104  'some@email.tld',
105  1,
106  '<a href="javascript:linkTo_UnCryptMailto(\'nbjmup+tpnfAfnbjm\/ume\');">some(at)email.tld</a>',
107  ],
108  'Plain email with ascii spam protection' => [
109  'some@email.tld',
110  'ascii',
111  '<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>',
112  ],
113  'Susceptible email' => [
114  '"><script>alert(\'email\')</script>',
115  0,
116  '<a href="mailto:&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
117  ],
118  'Susceptible email with spam protection' => [
119  '"><script>alert(\'email\')</script>',
120  1,
121  '<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>',
122  ],
123  'Susceptible email with ascii spam protection' => [
124  '"><script>alert(\'email\')</script>',
125  'ascii',
126  '<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>',
127  ],
128  ];
129  }
130 
138  public function renderEncodesEmailInFrontend($email, $spamProtectEmailAddresses, $expected)
139  {
141  $tsfe = $this->getMockBuilder(TypoScriptFrontendController::class)
142  ->setMethods(['dummy'])
143  ->disableOriginalConstructor()
144  ->getMock();
145  $tsfe->cObj = new ContentObjectRenderer();
146  $tsfe->spamProtectEmailAddresses = $spamProtectEmailAddresses;
147  $tsfe->config = [
148  'config' => [
149  'spamProtectEmailAddresses_atSubst' => '',
150  'spamProtectEmailAddresses_lastDotSubst' => '',
151  ],
152  ];
153  $GLOBALS['TSFE'] = $tsfe;
154  $viewHelper = $this->getMockBuilder(EmailViewHelper::class)
155  ->setMethods(['isFrontendAvailable', 'renderChildren'])
156  ->getMock();
157  $viewHelper->expects($this->once())->method('isFrontendAvailable')->willReturn(true);
158  $viewHelper->expects($this->once())->method('renderChildren')->willReturn(null);
159  $viewHelper->setArguments([
160  'email' => $email,
161  ]);
162  $viewHelper->initialize();
163  $this->assertSame(
164  $expected,
165  $viewHelper->render()
166  );
167  }
168 }
setArgumentsUnderTest(ViewHelperInterface $viewHelper, array $arguments=[])
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']