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