06890f96f78ca22cae9afc91d4f4e4270a42944c
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / ContentObject / ContentObjectRendererTest.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\ContentObject;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2011 Oliver Hader <oliver@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Testcase for TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
28 *
29 * @package TYPO3
30 * @subpackage tslib
31 * @author Oliver Hader <oliver@typo3.org>
32 * @author Oliver Klee <typo3-coding@oliverklee.de>
33 */
34 class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
35
36 /**
37 * Enable backup of global and system variables
38 *
39 * @var boolean
40 */
41 protected $backupGlobals = TRUE;
42
43 /**
44 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface|\TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
45 */
46 private $cObj;
47
48 /**
49 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
50 */
51 private $tsfe;
52
53 /**
54 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\TypoScript\TemplateService
55 */
56 private $template;
57
58 /**
59 * @var array
60 */
61 private $typoScriptImage = array('file' => 'typo3/clear.gif');
62
63 public function setUp() {
64 $this->template = $this->getMock('TYPO3\\CMS\\Core\\TypoScript\\TemplateService', array('getFileName', 'linkData'));
65 $this->tsfe = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
66 $this->tsfe->tmpl = $this->template;
67 $this->tsfe->config = array();
68 $sysPageMock = $this->getMock('TYPO3\\CMS\\Frontend\\Page\\PageRepository');
69 $this->tsfe->sys_page = $sysPageMock;
70 $GLOBALS['TSFE'] = $this->tsfe;
71 $GLOBALS['TSFE']->csConvObj = new \TYPO3\CMS\Core\Charset\CharsetConverter();
72 $GLOBALS['TSFE']->renderCharset = 'utf-8';
73 $GLOBALS['TYPO3_CONF_VARS']['SYS']['TYPO3\\CMS\\Core\\Charset\\CharsetConverter_utils'] = 'mbstring';
74 $this->cObj = $this->getAccessibleMock('\\TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectRenderer', array('dummy'));
75 $this->cObj->start(array(), 'tt_content');
76 }
77
78 ////////////////////////
79 // Utitility functions
80 ////////////////////////
81 /**
82 * Converts the subject and the expected result into the target charset.
83 *
84 * @param string $charset the target charset
85 * @param string $subject the subject, will be modified
86 * @param string $expected the expected result, will be modified
87 */
88 protected function handleCharset($charset, &$subject, &$expected) {
89 $GLOBALS['TSFE']->renderCharset = $charset;
90 $subject = $GLOBALS['TSFE']->csConvObj->conv($subject, 'iso-8859-1', $charset);
91 $expected = $GLOBALS['TSFE']->csConvObj->conv($expected, 'iso-8859-1', $charset);
92 }
93
94 /////////////////////////////////////////////
95 // Tests concerning the getImgResource hook
96 /////////////////////////////////////////////
97 /**
98 * @test
99 */
100 public function getImgResourceHookGetsCalled() {
101 $this->template->expects($this->atLeastOnce())->method('getFileName')->with('typo3/clear.gif')->will($this->returnValue('typo3/clear.gif'));
102 $className = uniqid('tx_coretest');
103 $getImgResourceHookMock = $this->getMock('TYPO3\\CMS\\Frontend\\ContentObject\\ContentObjectGetImageResourceHookInterface', array('getImgResourcePostProcess'), array(), $className);
104 $getImgResourceHookMock->expects($this->once())->method('getImgResourcePostProcess')->will($this->returnCallback(array($this, 'isGetImgResourceHookCalledCallback')));
105 $getImgResourceHookObjects = array($getImgResourceHookMock);
106 $this->cObj->_setRef('getImgResourceHookObjects', $getImgResourceHookObjects);
107 $this->cObj->IMAGE($this->typoScriptImage);
108 }
109
110 /**
111 * Handles the arguments that have been sent to the getImgResource hook.
112 *
113 * @return array
114 * @see getImgResourceHookGetsCalled
115 */
116 public function isGetImgResourceHookCalledCallback() {
117 list($file, $fileArray, $imageResource, $parent) = func_get_args();
118 $this->assertEquals('typo3/clear.gif', $file);
119 $this->assertEquals('typo3/clear.gif', $imageResource['origFile']);
120 $this->assertTrue(is_array($fileArray));
121 $this->assertTrue($parent instanceof \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer);
122 return $imageResource;
123 }
124
125 //////////////////////////
126 // Tests concerning FORM
127 //////////////////////////
128 /**
129 * @test
130 */
131 public function formWithSecureFormMailEnabledDoesNotContainRecipientField() {
132 $GLOBALS['TYPO3_CONF_VARS']['FE']['secureFormmail'] = TRUE;
133 $this->assertNotContains('name="recipient', $this->cObj->FORM(array('recipient' => 'foo@bar.com', 'recipient.' => array()), array()));
134 }
135
136 /**
137 * @test
138 */
139 public function formWithSecureFormMailDisabledDoesNotContainRecipientField() {
140 $GLOBALS['TYPO3_CONF_VARS']['FE']['secureFormmail'] = FALSE;
141 $this->assertContains('name="recipient', $this->cObj->FORM(array('recipient' => 'foo@bar.com', 'recipient.' => array()), array()));
142 }
143
144 /////////////////////////////////////////
145 // Tests concerning getQueryArguments()
146 /////////////////////////////////////////
147 /**
148 * @test
149 */
150 public function getQueryArgumentsExcludesParameters() {
151 $_SERVER['QUERY_STRING'] = 'key1=value1' . '&key2=value2' . '&key3[key31]=value31' . '&key3[key32][key321]=value321' . '&key3[key32][key322]=value322';
152 $getQueryArgumentsConfiguration = array();
153 $getQueryArgumentsConfiguration['exclude'] = array();
154 $getQueryArgumentsConfiguration['exclude'][] = 'key1';
155 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key31]';
156 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key32][key321]';
157 $getQueryArgumentsConfiguration['exclude'] = implode(',', $getQueryArgumentsConfiguration['exclude']);
158 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('&key2=value2&key3[key32][key322]=value322');
159 $actualResult = $this->cObj->getQueryArguments($getQueryArgumentsConfiguration);
160 $this->assertEquals($expectedResult, $actualResult);
161 }
162
163 /**
164 * @test
165 */
166 public function getQueryArgumentsExcludesGetParameters() {
167 $_GET = array(
168 'key1' => 'value1',
169 'key2' => 'value2',
170 'key3' => array(
171 'key31' => 'value31',
172 'key32' => array(
173 'key321' => 'value321',
174 'key322' => 'value322'
175 )
176 )
177 );
178 $getQueryArgumentsConfiguration = array();
179 $getQueryArgumentsConfiguration['method'] = 'GET';
180 $getQueryArgumentsConfiguration['exclude'] = array();
181 $getQueryArgumentsConfiguration['exclude'][] = 'key1';
182 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key31]';
183 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key32][key321]';
184 $getQueryArgumentsConfiguration['exclude'] = implode(',', $getQueryArgumentsConfiguration['exclude']);
185 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('&key2=value2&key3[key32][key322]=value322');
186 $actualResult = $this->cObj->getQueryArguments($getQueryArgumentsConfiguration);
187 $this->assertEquals($expectedResult, $actualResult);
188 }
189
190 /**
191 * @test
192 */
193 public function getQueryArgumentsOverrulesSingleParameter() {
194 $_SERVER['QUERY_STRING'] = 'key1=value1';
195 $getQueryArgumentsConfiguration = array();
196 $overruleArguments = array(
197 // Should be overriden
198 'key1' => 'value1Overruled',
199 // Shouldn't be set: Parameter doesn't exist in source array and is not forced
200 'key2' => 'value2Overruled'
201 );
202 $expectedResult = '&key1=value1Overruled';
203 $actualResult = $this->cObj->getQueryArguments($getQueryArgumentsConfiguration, $overruleArguments);
204 $this->assertEquals($expectedResult, $actualResult);
205 }
206
207 /**
208 * @test
209 */
210 public function getQueryArgumentsOverrulesMultiDimensionalParameters() {
211 $_POST = array(
212 'key1' => 'value1',
213 'key2' => 'value2',
214 'key3' => array(
215 'key31' => 'value31',
216 'key32' => array(
217 'key321' => 'value321',
218 'key322' => 'value322'
219 )
220 )
221 );
222 $getQueryArgumentsConfiguration = array();
223 $getQueryArgumentsConfiguration['method'] = 'POST';
224 $getQueryArgumentsConfiguration['exclude'] = array();
225 $getQueryArgumentsConfiguration['exclude'][] = 'key1';
226 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key31]';
227 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key32][key321]';
228 $getQueryArgumentsConfiguration['exclude'] = implode(',', $getQueryArgumentsConfiguration['exclude']);
229 $overruleArguments = array(
230 // Should be overriden
231 'key2' => 'value2Overruled',
232 'key3' => array(
233 'key32' => array(
234 // Shouldn't be set: Parameter is excluded and not forced
235 'key321' => 'value321Overruled',
236 // Should be overriden: Parameter is not excluded
237 'key322' => 'value322Overruled',
238 // Shouldn't be set: Parameter doesn't exist in source array and is not forced
239 'key323' => 'value323Overruled'
240 )
241 )
242 );
243 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('&key2=value2Overruled&key3[key32][key322]=value322Overruled');
244 $actualResult = $this->cObj->getQueryArguments($getQueryArgumentsConfiguration, $overruleArguments);
245 $this->assertEquals($expectedResult, $actualResult);
246 }
247
248 /**
249 * @test
250 */
251 public function getQueryArgumentsOverrulesMultiDimensionalForcedParameters() {
252 $_SERVER['QUERY_STRING'] = 'key1=value1' . '&key2=value2' . '&key3[key31]=value31' . '&key3[key32][key321]=value321' . '&key3[key32][key322]=value322';
253 $_POST = array(
254 'key1' => 'value1',
255 'key2' => 'value2',
256 'key3' => array(
257 'key31' => 'value31',
258 'key32' => array(
259 'key321' => 'value321',
260 'key322' => 'value322'
261 )
262 )
263 );
264 $getQueryArgumentsConfiguration = array();
265 $getQueryArgumentsConfiguration['exclude'] = array();
266 $getQueryArgumentsConfiguration['exclude'][] = 'key1';
267 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key31]';
268 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key32][key321]';
269 $getQueryArgumentsConfiguration['exclude'][] = 'key3[key32][key322]';
270 $getQueryArgumentsConfiguration['exclude'] = implode(',', $getQueryArgumentsConfiguration['exclude']);
271 $overruleArguments = array(
272 // Should be overriden
273 'key2' => 'value2Overruled',
274 'key3' => array(
275 'key32' => array(
276 // Should be set: Parameter is excluded but forced
277 'key321' => 'value321Overruled',
278 // Should be set: Parameter doesn't exist in source array but is forced
279 'key323' => 'value323Overruled'
280 )
281 )
282 );
283 $expectedResult = $this->rawUrlEncodeSquareBracketsInUrl('&key2=value2Overruled&key3[key32][key321]=value321Overruled&key3[key32][key323]=value323Overruled');
284 $actualResult = $this->cObj->getQueryArguments($getQueryArgumentsConfiguration, $overruleArguments, TRUE);
285 $this->assertEquals($expectedResult, $actualResult);
286 $getQueryArgumentsConfiguration['method'] = 'POST';
287 $actualResult = $this->cObj->getQueryArguments($getQueryArgumentsConfiguration, $overruleArguments, TRUE);
288 $this->assertEquals($expectedResult, $actualResult);
289 }
290
291 /**
292 * Encodes square brackets in URL.
293 *
294 * @param string $string
295 * @return string
296 */
297 private function rawUrlEncodeSquareBracketsInUrl($string) {
298 return str_replace(array('[', ']'), array('%5B', '%5D'), $string);
299 }
300
301 //////////////////////////////
302 // Tests concerning crop
303 //////////////////////////////
304 /**
305 * @test
306 */
307 public function cropIsMultibyteSafe() {
308 $this->assertEquals('бла', $this->cObj->crop('бла', '3|...'));
309 }
310
311 //////////////////////////////
312 // Tests concerning cropHTML
313 //////////////////////////////
314 /**
315 * This is the data provider for the tests of crop and cropHTML below. It provides all combinations
316 * of charset, text type, and configuration options to be tested.
317 *
318 * @return array two-dimensional array with the second level like this:
319 * @see cropHtmlWithDataProvider
320 */
321 public function cropHtmlDataProvider() {
322 $plainText = 'Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j implemented the original version of the crop function.';
323 $textWithMarkup = '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a>' . ' implemented</strong> the original version of the crop function.';
324 $textWithEntities = 'Kasper Sk&aring;rh&oslash;j implemented the; original ' . 'version of the crop function.';
325 $charsets = array('iso-8859-1', 'utf-8', 'ascii', 'big5');
326 $data = array();
327 foreach ($charsets as $charset) {
328 $data = array_merge($data, array(
329 $charset . ' plain text; 11|...' => array(
330 '11|...',
331 $plainText,
332 'Kasper Sk' . chr(229) . 'r...',
333 $charset
334 ),
335 $charset . ' plain text; -58|...' => array(
336 '-58|...',
337 $plainText,
338 '...h' . chr(248) . 'j implemented the original version of the crop function.',
339 $charset
340 ),
341 $charset . ' plain text; 4|...|1' => array(
342 '4|...|1',
343 $plainText,
344 'Kasp...',
345 $charset
346 ),
347 $charset . ' plain text; 20|...|1' => array(
348 '20|...|1',
349 $plainText,
350 'Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j...',
351 $charset
352 ),
353 $charset . ' plain text; -5|...|1' => array(
354 '-5|...|1',
355 $plainText,
356 '...tion.',
357 $charset
358 ),
359 $charset . ' plain text; -49|...|1' => array(
360 '-49|...|1',
361 $plainText,
362 '...the original version of the crop function.',
363 $charset
364 ),
365 $charset . ' text with markup; 11|...' => array(
366 '11|...',
367 $textWithMarkup,
368 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'r...</a></strong>',
369 $charset
370 ),
371 $charset . ' text with markup; 13|...' => array(
372 '13|...',
373 $textWithMarkup,
374 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . '...</a></strong>',
375 $charset
376 ),
377 $charset . ' text with markup; 14|...' => array(
378 '14|...',
379 $textWithMarkup,
380 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a>...</strong>',
381 $charset
382 ),
383 $charset . ' text with markup; 15|...' => array(
384 '15|...',
385 $textWithMarkup,
386 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a> ...</strong>',
387 $charset
388 ),
389 $charset . ' text with markup; 29|...' => array(
390 '29|...',
391 $textWithMarkup,
392 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a> implemented</strong> th...',
393 $charset
394 ),
395 $charset . ' text with markup; -58|...' => array(
396 '-58|...',
397 $textWithMarkup,
398 '<strong><a href="mailto:kasper@typo3.org">...h' . chr(248) . 'j</a> implemented</strong> the original version of the crop function.',
399 $charset
400 ),
401 $charset . ' text with markup 4|...|1' => array(
402 '4|...|1',
403 $textWithMarkup,
404 '<strong><a href="mailto:kasper@typo3.org">Kasp...</a></strong>',
405 $charset
406 ),
407 $charset . ' text with markup; 11|...|1' => array(
408 '11|...|1',
409 $textWithMarkup,
410 '<strong><a href="mailto:kasper@typo3.org">Kasper...</a></strong>',
411 $charset
412 ),
413 $charset . ' text with markup; 13|...|1' => array(
414 '13|...|1',
415 $textWithMarkup,
416 '<strong><a href="mailto:kasper@typo3.org">Kasper...</a></strong>',
417 $charset
418 ),
419 $charset . ' text with markup; 14|...|1' => array(
420 '14|...|1',
421 $textWithMarkup,
422 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a>...</strong>',
423 $charset
424 ),
425 $charset . ' text with markup; 15|...|1' => array(
426 '15|...|1',
427 $textWithMarkup,
428 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a>...</strong>',
429 $charset
430 ),
431 $charset . ' text with markup; 29|...|1' => array(
432 '29|...|1',
433 $textWithMarkup,
434 '<strong><a href="mailto:kasper@typo3.org">Kasper Sk' . chr(229) . 'rh' . chr(248) . 'j</a> implemented</strong>...',
435 $charset
436 ),
437 $charset . ' text with markup; -66|...|1' => array(
438 '-66|...|1',
439 $textWithMarkup,
440 '<strong><a href="mailto:kasper@typo3.org">...Sk' . chr(229) . 'rh' . chr(248) . 'j</a> implemented</strong> the original version of the crop function.',
441 $charset
442 ),
443 $charset . ' text with entities 9|...' => array(
444 '9|...',
445 $textWithEntities,
446 'Kasper Sk...',
447 $charset
448 ),
449 $charset . ' text with entities 10|...' => array(
450 '10|...',
451 $textWithEntities,
452 'Kasper Sk&aring;...',
453 $charset
454 ),
455 $charset . ' text with entities 11|...' => array(
456 '11|...',
457 $textWithEntities,
458 'Kasper Sk&aring;r...',
459 $charset
460 ),
461 $charset . ' text with entities 13|...' => array(
462 '13|...',
463 $textWithEntities,
464 'Kasper Sk&aring;rh&oslash;...',
465 $charset
466 ),
467 $charset . ' text with entities 14|...' => array(
468 '14|...',
469 $textWithEntities,
470 'Kasper Sk&aring;rh&oslash;j...',
471 $charset
472 ),
473 $charset . ' text with entities 15|...' => array(
474 '15|...',
475 $textWithEntities,
476 'Kasper Sk&aring;rh&oslash;j ...',
477 $charset
478 ),
479 $charset . ' text with entities 16|...' => array(
480 '16|...',
481 $textWithEntities,
482 'Kasper Sk&aring;rh&oslash;j i...',
483 $charset
484 ),
485 $charset . ' text with entities -57|...' => array(
486 '-57|...',
487 $textWithEntities,
488 '...j implemented the; original version of the crop function.',
489 $charset
490 ),
491 $charset . ' text with entities -58|...' => array(
492 '-58|...',
493 $textWithEntities,
494 '...&oslash;j implemented the; original version of the crop function.',
495 $charset
496 ),
497 $charset . ' text with entities -59|...' => array(
498 '-59|...',
499 $textWithEntities,
500 '...h&oslash;j implemented the; original version of the crop function.',
501 $charset
502 ),
503 $charset . ' text with entities 4|...|1' => array(
504 '4|...|1',
505 $textWithEntities,
506 'Kasp...',
507 $charset
508 ),
509 $charset . ' text with entities 9|...|1' => array(
510 '9|...|1',
511 $textWithEntities,
512 'Kasper...',
513 $charset
514 ),
515 $charset . ' text with entities 10|...|1' => array(
516 '10|...|1',
517 $textWithEntities,
518 'Kasper...',
519 $charset
520 ),
521 $charset . ' text with entities 11|...|1' => array(
522 '11|...|1',
523 $textWithEntities,
524 'Kasper...',
525 $charset
526 ),
527 $charset . ' text with entities 13|...|1' => array(
528 '13|...|1',
529 $textWithEntities,
530 'Kasper...',
531 $charset
532 ),
533 $charset . ' text with entities 14|...|1' => array(
534 '14|...|1',
535 $textWithEntities,
536 'Kasper Sk&aring;rh&oslash;j...',
537 $charset
538 ),
539 $charset . ' text with entities 15|...|1' => array(
540 '15|...|1',
541 $textWithEntities,
542 'Kasper Sk&aring;rh&oslash;j...',
543 $charset
544 ),
545 $charset . ' text with entities 16|...|1' => array(
546 '16|...|1',
547 $textWithEntities,
548 'Kasper Sk&aring;rh&oslash;j...',
549 $charset
550 ),
551 $charset . ' text with entities -57|...|1' => array(
552 '-57|...|1',
553 $textWithEntities,
554 '...implemented the; original version of the crop function.',
555 $charset
556 ),
557 $charset . ' text with entities -58|...|1' => array(
558 '-58|...|1',
559 $textWithEntities,
560 '...implemented the; original version of the crop function.',
561 $charset
562 ),
563 $charset . ' text with entities -59|...|1' => array(
564 '-59|...|1',
565 $textWithEntities,
566 '...implemented the; original version of the crop function.',
567 $charset
568 ),
569 $charset . ' text with dash in html-element 28|...|1' => array(
570 '28|...|1',
571 'Some text with a link to <link email.address@example.org - mail "Open email window">my email.address@example.org</link> and text after it',
572 'Some text with a link to <link email.address@example.org - mail "Open email window">my...</link>',
573 $charset
574 )
575 ));
576 }
577 return $data;
578 }
579
580 /**
581 * Checks if stdWrap.cropHTML works with plain text cropping from left
582 *
583 * @test
584 * @dataProvider cropHtmlDataProvider
585 * @param string $settings
586 * @param string $subject the string to crop
587 * @param string $expected the expected cropped result
588 * @param string $charset the charset that will be set as renderCharset
589 */
590 public function cropHtmlWithDataProvider($settings, $subject, $expected, $charset) {
591 $this->handleCharset($charset, $subject, $expected);
592 $this->assertEquals($expected, $this->cObj->cropHTML($subject, $settings), 'cropHTML failed with settings: "' . $settings . '" and charset "' . $charset . '"');
593 }
594
595 /**
596 * Checks if stdWrap.cropHTML works with a complex content with many tags. Currently cropHTML
597 * counts multiple invisible characters not as one (as the browser will output the content).
598 *
599 * @test
600 */
601 public function cropHtmlWorksWithComplexContent() {
602 $GLOBALS['TSFE']->renderCharset = 'iso-8859-1';
603 $subject = '
604 <h1>Blog Example</h1>
605 <hr>
606 <div class="csc-header csc-header-n1">
607 <h2 class="csc-firstHeader">Welcome to Blog #1</h2>
608 </div>
609 <p class="bodytext">
610 A blog about TYPO3 extension development. In order to start blogging, read the <a href="#">Help section</a>. If you have any further questions, feel free to contact the administrator John Doe (<a href="mailto:john.doe@example.com">john.doe@example.com)</a>.
611 </p>
612 <div class="tx-blogexample-list-container">
613 <p class="bodytext">
614 Below are the most recent posts:
615 </p>
616 <ul>
617 <li>
618 <h3>
619 <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog]=&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=003b0131ed">The Post #1</a>
620 </h3>
621 <p class="bodytext">
622 Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut...
623 </p>
624 <p class="metadata">
625 Published on 26.08.2009 by Jochen Rau
626 </p>
627 <p>
628 Tags: [MVC]&nbsp;[Domain Driven Design]&nbsp;<br>
629 <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=f982643bc3">read more &gt;&gt;</a><br>
630 <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=edit&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=5b481bc8f0">Edit</a>&nbsp;<a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=delete&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=4e52879656">Delete</a>
631 </p>
632 </li>
633 </ul>
634 <p>
635 <a href="index.php?id=99&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=new&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=2718a4b1a0">Create a new Post</a>
636 </p>
637 </div>
638 <hr>
639 <p>
640 ? TYPO3 Association
641 </p>
642 ';
643 $result = $this->cObj->cropHTML($subject, '300');
644 $expected = '
645 <h1>Blog Example</h1>
646 <hr>
647 <div class="csc-header csc-header-n1">
648 <h2 class="csc-firstHeader">Welcome to Blog #1</h2>
649 </div>
650 <p class="bodytext">
651 A blog about TYPO3 extension development. In order to start blogging, read the <a href="#">Help section</a>. If you have any further questions, feel free to contact the administrator John Doe (<a href="mailto:john.doe@example.com">john.doe@example.com)</a>.
652 </p>
653 <div class="tx-blogexample-list-container">
654 <p class="bodytext">
655 Below are the most recent posts:
656 </p>
657 <ul>
658 <li>
659 <h3>
660 <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog]=&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=003b0131ed">The Pos</a></h3></li></ul></div>';
661 $this->assertEquals($expected, $result);
662 $result = $this->cObj->cropHTML($subject, '-100');
663 $expected = '<div class="tx-blogexample-list-container"><ul><li><p>Design]&nbsp;<br>
664 <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[action]=show&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=f982643bc3">read more &gt;&gt;</a><br>
665 <a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=edit&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=5b481bc8f0">Edit</a>&nbsp;<a href="index.php?id=99&amp;tx_blogexample_pi1[post][uid]=211&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=delete&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=4e52879656">Delete</a>
666 </p>
667 </li>
668 </ul>
669 <p>
670 <a href="index.php?id=99&amp;tx_blogexample_pi1[blog][uid]=70&amp;tx_blogexample_pi1[action]=new&amp;tx_blogexample_pi1[controller]=Post&amp;cHash=2718a4b1a0">Create a new Post</a>
671 </p>
672 </div>
673 <hr>
674 <p>
675 ? TYPO3 Association
676 </p>
677 ';
678 $this->assertEquals($expected, $result);
679 }
680
681 /**
682 * @return array
683 */
684 public function stdWrap_roundDataProvider() {
685 return array(
686 'rounding off without any configuration' => array(
687 1.123456789,
688 array(),
689 1
690 ),
691 'rounding up without any configuration' => array(
692 1.523456789,
693 array(),
694 2
695 ),
696 'regular rounding (off) to two decimals' => array(
697 0.123456789,
698 array(
699 'decimals' => 2
700 ),
701 0.12
702 ),
703 'regular rounding (up) to two decimals' => array(
704 0.1256789,
705 array(
706 'decimals' => 2
707 ),
708 0.13
709 ),
710 'rounding up to integer with type ceil' => array(
711 0.123456789,
712 array(
713 'roundType' => 'ceil'
714 ),
715 1
716 ),
717 'rounding down to integer with type floor' => array(
718 2.3481,
719 array(
720 'roundType' => 'floor'
721 ),
722 2
723 )
724 );
725 }
726
727 /**
728 * Test for the stdWrap function "round"
729 *
730 * @param float $float
731 * @param array $conf
732 * @param float $expected
733 * @return void
734 * @dataProvider stdWrap_roundDataProvider
735 * @test
736 */
737 public function stdWrap_round($float, $conf, $expected) {
738 $conf = array(
739 'round.' => $conf
740 );
741 $result = $this->cObj->stdWrap_round($float, $conf);
742 $this->assertEquals($expected, $result);
743 }
744
745 /**
746 * Data provider for the hash test
747 *
748 * @return array multi-dimensional array with the second level like this:
749 * @see hash
750 */
751 public function hashDataProvider() {
752 $data = array(
753 'testing md5' => array(
754 'joh316',
755 array(
756 'hash' => 'md5'
757 ),
758 'bacb98acf97e0b6112b1d1b650b84971'
759 ),
760 'testing sha1' => array(
761 'joh316',
762 array(
763 'hash' => 'sha1'
764 ),
765 '063b3d108bed9f88fa618c6046de0dccadcf3158'
766 ),
767 'testing non-existing hashing algorithm' => array(
768 'joh316',
769 array(
770 'hash' => 'non-existing'
771 ),
772 ''
773 ),
774 'testing stdWrap capability' => array(
775 'joh316',
776 array(
777 'hash.' => array(
778 'cObject' => 'TEXT',
779 'cObject.' => array(
780 'value' => 'md5'
781 )
782 )
783 ),
784 'bacb98acf97e0b6112b1d1b650b84971'
785 )
786 );
787 return $data;
788 }
789
790 /**
791 * Test for the stdWrap function "hash"
792 *
793 * @param string $text
794 * @param array $conf
795 * @param string $expected
796 * @return void
797 * @dataProvider hashDataProvider
798 * @test
799 */
800 public function stdWrap_hash($text, array $conf, $expected) {
801 $result = $this->cObj->stdWrap_hash($text, $conf);
802 $this->assertEquals($expected, $result);
803 }
804
805 /**
806 * Data provider for the numberFormat test
807 *
808 * @return array multi-dimensional array with the second level like this:
809 * @see numberFormat
810 */
811 public function numberFormatDataProvider() {
812 $data = array(
813 'testing decimals' => array(
814 0.8,
815 array(
816 'decimals' => 2
817 ),
818 '0.80'
819 ),
820 'testing dec_point' => array(
821 0.8,
822 array(
823 'decimals' => 1,
824 'dec_point' => ','
825 ),
826 '0,8'
827 ),
828 'testing thousands_sep' => array(
829 999.99,
830 array(
831 'decimals' => 0,
832 'thousands_sep.' => array(
833 'char' => 46
834 )
835 ),
836 '1.000'
837 ),
838 'testing mixture' => array(
839 1281731.45,
840 array(
841 'decimals' => 1,
842 'dec_point.' => array(
843 'char' => 44
844 ),
845 'thousands_sep.' => array(
846 'char' => 46
847 )
848 ),
849 '1.281.731,5'
850 )
851 );
852 return $data;
853 }
854
855 /**
856 * Check if stdWrap.numberFormat and all of its properties work properly
857 *
858 * @dataProvider numberFormatDataProvider
859 * @test
860 */
861 public function numberFormat($float, $formatConf, $expected) {
862 $result = $this->cObj->numberFormat($float, $formatConf);
863 $this->assertEquals($expected, $result);
864 }
865
866 /**
867 * Data provider for the replacement test
868 *
869 * @return array multi-dimensional array with the second level like this:
870 * @see replacement
871 */
872 public function replacementDataProvider() {
873 $data = array(
874 'multiple replacements, including regex' => array(
875 'There_is_a_cat,_a_dog_and_a_tiger_in_da_hood!_Yeah!',
876 array(
877 'replacement.' => array(
878 '120.' => array(
879 'search' => 'in da hood',
880 'replace' => 'around the block'
881 ),
882 '20.' => array(
883 'search' => '_',
884 'replace.' => array('char' => '32')
885 ),
886 '130.' => array(
887 'search' => '#a (Cat|Dog|Tiger)#i',
888 'replace' => 'an animal',
889 'useRegExp' => '1'
890 )
891 )
892 ),
893 'There is an animal, an animal and an animal around the block! Yeah!'
894 )
895 );
896 return $data;
897 }
898
899 /**
900 * Check if stdWrap.replacement and all of its properties work properly
901 *
902 * @dataProvider replacementDataProvider
903 * @test
904 */
905 public function replacement($input, $conf, $expected) {
906 $result = $this->cObj->stdWrap_replacement($input, $conf);
907 $this->assertEquals($expected, $result);
908 }
909
910 /**
911 * Data provider for the getQuery test
912 *
913 * @return array multi-dimensional array with the second level like this:
914 * @see getQuery
915 */
916 public function getQueryDataProvider() {
917 $data = array(
918 'testing empty conf' => array(
919 'tt_content',
920 array(),
921 array(
922 'SELECT' => '*'
923 )
924 ),
925 'testing #17284: adding uid/pid for workspaces' => array(
926 'tt_content',
927 array(
928 'selectFields' => 'header,bodytext'
929 ),
930 array(
931 'SELECT' => 'header,bodytext, tt_content.uid as uid, tt_content.pid as pid, tt_content.t3ver_state as t3ver_state'
932 )
933 ),
934 'testing #17284: no need to add' => array(
935 'tt_content',
936 array(
937 'selectFields' => 'tt_content.*'
938 ),
939 array(
940 'SELECT' => 'tt_content.*'
941 )
942 ),
943 'testing #17284: no need to add #2' => array(
944 'tt_content',
945 array(
946 'selectFields' => '*'
947 ),
948 array(
949 'SELECT' => '*'
950 )
951 ),
952 'testing #29783: joined tables, prefix tablename' => array(
953 'tt_content',
954 array(
955 'selectFields' => 'tt_content.header,be_users.username',
956 'join' => 'be_users ON tt_content.cruser_id = be_users.uid'
957 ),
958 array(
959 'SELECT' => 'tt_content.header,be_users.username, tt_content.uid as uid, tt_content.pid as pid, tt_content.t3ver_state as t3ver_state'
960 )
961 ),
962 'testing #34152: single count(*), add nothing' => array(
963 'tt_content',
964 array(
965 'selectFields' => 'count(*)'
966 ),
967 array(
968 'SELECT' => 'count(*)'
969 )
970 ),
971 'testing #34152: single max(crdate), add nothing' => array(
972 'tt_content',
973 array(
974 'selectFields' => 'max(crdate)'
975 ),
976 array(
977 'SELECT' => 'max(crdate)'
978 )
979 ),
980 'testing #34152: single min(crdate), add nothing' => array(
981 'tt_content',
982 array(
983 'selectFields' => 'min(crdate)'
984 ),
985 array(
986 'SELECT' => 'min(crdate)'
987 )
988 ),
989 'testing #34152: single sum(is_siteroot), add nothing' => array(
990 'tt_content',
991 array(
992 'selectFields' => 'sum(is_siteroot)'
993 ),
994 array(
995 'SELECT' => 'sum(is_siteroot)'
996 )
997 ),
998 'testing #34152: single avg(crdate), add nothing' => array(
999 'tt_content',
1000 array(
1001 'selectFields' => 'avg(crdate)'
1002 ),
1003 array(
1004 'SELECT' => 'avg(crdate)'
1005 )
1006 )
1007 );
1008 return $data;
1009 }
1010
1011 /**
1012 * Check if sanitizeSelectPart works as expected
1013 *
1014 * @dataProvider getQueryDataProvider
1015 * @test
1016 */
1017 public function getQuery($table, $conf, $expected) {
1018 $result = $this->cObj->getQuery($table, $conf, TRUE);
1019 foreach ($expected as $field => $value) {
1020 $this->assertEquals($value, $result[$field]);
1021 }
1022 }
1023
1024 /**
1025 * Data provider for the stdWrap_strftime test
1026 *
1027 * @return array multi-dimensional array with the second level like this:
1028 * @see stdWrap_strftime
1029 */
1030 public function stdWrap_strftimeReturnsFormattedStringDataProvider() {
1031 $data = array(
1032 'given timestamp' => array(
1033 1346500800, // This is 2012-09-01 12:00 in UTC/GMT
1034 array(
1035 'strftime' => '%d-%m-%Y',
1036 ),
1037 ),
1038 'empty string' => array(
1039 '',
1040 array(
1041 'strftime' => '%d-%m-%Y',
1042 ),
1043 ),
1044 'testing null' => array(
1045 NULL,
1046 array(
1047 'strftime' => '%d-%m-%Y',
1048 ),
1049 ),
1050 );
1051 return $data;
1052 }
1053
1054 /**
1055 * @test
1056 * @dataProvider stdWrap_strftimeReturnsFormattedStringDataProvider
1057 */
1058 public function stdWrap_strftimeReturnsFormattedString($content, $conf) {
1059 // Set exec_time to a hard timestamp (backed up by $this->backupGlobals = TRUE)
1060 $GLOBALS['EXEC_TIME'] = 1346500800;
1061 // Save current timezone and set to UTC to make the system unter test behave
1062 // the same in all server timezone settings
1063 $timezoneBackup = date_default_timezone_get();
1064 date_default_timezone_set('UTC');
1065
1066 $result = $this->cObj->stdWrap_strftime($content, $conf);
1067
1068 // Reset timezone
1069 date_default_timezone_set($timezoneBackup);
1070
1071 $this->assertEquals('01-09-2012', $result);
1072 }
1073
1074 }
1075
1076 ?>