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