TYPO3 CMS  TYPO3_7-6
EmailViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /* *
5  * This script is part of the TYPO3 project - inspiring people to share! *
6  * *
7  * TYPO3 is free software; you can redistribute it and/or modify it under *
8  * the terms of the GNU General Public License version 2 as published by *
9  * the Free Software Foundation. *
10  * *
11  * This script is distributed in the hope that it will be useful, but *
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
13  * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
14  * Public License for more details. *
15  * */
18 
23 {
27  protected $viewHelper;
28 
32  protected $cObjBackup;
33 
34  protected function setUp()
35  {
36  parent::setUp();
37  $GLOBALS['TSFE'] = new \stdClass();
38  $GLOBALS['TSFE']->cObj = $this->getMock(\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer::class, [], [], '', false);
39  $this->viewHelper = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\Link\EmailViewHelper::class), ['renderChildren']);
40  $this->injectDependenciesIntoViewHelper($this->viewHelper);
41  $this->viewHelper->initializeArguments();
42  }
43 
48  {
49  $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, ['setTagName', 'addAttribute', 'setContent']);
50  $mockTagBuilder->expects($this->once())->method('setTagName')->with('a');
51  $mockTagBuilder->expects($this->once())->method('addAttribute')->with('href', 'mailto:some@email.tld');
52  $mockTagBuilder->expects($this->once())->method('setContent')->with('some content');
53  $this->viewHelper->_set('tag', $mockTagBuilder);
54  $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue('some content'));
55  $this->viewHelper->initialize();
56  $this->viewHelper->render('some@email.tld');
57  }
58 
63  {
64  $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, ['setTagName', 'addAttribute', 'setContent']);
65  $mockTagBuilder->expects($this->once())->method('setContent')->with('some@email.tld');
66  $this->viewHelper->_set('tag', $mockTagBuilder);
67  $this->viewHelper->expects($this->any())->method('renderChildren')->will($this->returnValue(null));
68  $this->viewHelper->initialize();
69  $this->viewHelper->render('some@email.tld');
70  }
71 
76  {
77  return [
78  'Plain email' => [
79  'some@email.tld',
80  0,
81  '<a href="mailto:some@email.tld">some@email.tld</a>',
82  ],
83  'Plain email with spam protection' => [
84  'some@email.tld',
85  1,
86  '<a href="javascript:linkTo_UnCryptMailto(\'nbjmup+tpnfAfnbjm\/ume\');">some(at)email.tld</a>',
87  ],
88  'Plain email with ascii spam protection' => [
89  'some@email.tld',
90  'ascii',
91  '<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>',
92  ],
93  'Susceptible email' => [
94  '"><script>alert(\'email\')</script>',
95  0,
96  '<a href="mailto:&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;">&quot;&gt;&lt;script&gt;alert(\'email\')&lt;/script&gt;</a>',
97  ],
98  'Susceptible email with spam protection' => [
99  '"><script>alert(\'email\')</script>',
100  1,
101  '<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>',
102  ],
103  'Susceptible email with ascii spam protection' => [
104  '"><script>alert(\'email\')</script>',
105  'ascii',
106  '<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>',
107  ],
108  ];
109  }
110 
118  public function renderEncodesEmailInFrontend($email, $spamProtectEmailAddresses, $expected)
119  {
121  $tsfe = $this->getMock(TypoScriptFrontendController::class, ['dummy'], [], '', false);
122  $tsfe->cObj = new ContentObjectRenderer();
123  $tsfe->spamProtectEmailAddresses = $spamProtectEmailAddresses;
124  $tsfe->config = [
125  'config' => [
126  'spamProtectEmailAddresses_atSubst' => '',
127  'spamProtectEmailAddresses_lastDotSubst' => '',
128  ],
129  ];
130  $GLOBALS['TSFE'] = $tsfe;
131  $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, ['dummy']);
132  $mockTagBuilder->setTagName = 'a';
133  $viewHelper = $this->getMock($this->buildAccessibleProxy(\TYPO3\CMS\Fluid\ViewHelpers\Link\EmailViewHelper::class), ['isFrontendAvailable', 'renderChildren']);
134  $viewHelper->_set('tag', $mockTagBuilder);
135  $viewHelper->expects($this->once())->method('isFrontendAvailable')->willReturn(true);
136  $viewHelper->expects($this->once())->method('renderChildren')->willReturn(null);
137  $viewHelper->initialize();
138  $this->assertSame(
139  $expected,
140  $viewHelper->render($email)
141  );
142  }
143 }
injectDependenciesIntoViewHelper(\TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper $viewHelper)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']