[BUGFIX] Escape regex chars when building sWordRegEx
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Tests / Unit / Page / PageGeneratorTest.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Tests\Unit\Page;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
17 use TYPO3\CMS\Frontend\Tests\Unit\Page\Fixtures\PageGeneratorFixture;
18
19 /**
20 * Test case
21 *
22 * @author Markus Klein <klein.t3@mfc-linz.at>
23 */
24 class PageGeneratorTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
25
26 /**
27 * @var PageGeneratorFixture
28 */
29 protected $pageGeneratorFixture;
30
31 /**
32 * @var ContentObjectRenderer
33 */
34 protected $contentObjectRenderer;
35
36 /**
37 * Set up the helper objects
38 */
39 protected function setUp() {
40 $this->pageGeneratorFixture = new PageGeneratorFixture();
41 $this->contentObjectRenderer = new ContentObjectRenderer();
42 }
43
44 /**
45 * @return array
46 */
47 public function generateMetaTagHtmlGeneratesCorrectTagsDataProvider() {
48 return array(
49 'simple meta' => array(
50 array(
51 'author' => 'Markus Klein',
52 ),
53 FALSE,
54 array(
55 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS">',
56 '<meta name="author" content="Markus Klein">',
57 )
58 ),
59 'simple meta xhtml' => array(
60 array(
61 'author' => 'Markus Klein',
62 ),
63 TRUE,
64 array(
65 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS" />',
66 '<meta name="author" content="Markus Klein" />',
67 )
68 ),
69 'meta with nested stdWrap' => array(
70 array(
71 'author' => 'Markus ',
72 'author.' => array('stdWrap.' => array('wrap' => '|Klein'))
73 ),
74 FALSE,
75 array(
76 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS">',
77 '<meta name="author" content="Markus Klein">',
78 )
79 ),
80 'httpEquivalent meta' => array(
81 array(
82 'X-UA-Compatible' => 'IE=edge,chrome=1',
83 'X-UA-Compatible.' => array('httpEquivalent' => 1)
84 ),
85 FALSE,
86 array(
87 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS">',
88 '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">'
89 )
90 ),
91 'httpEquivalent meta xhtml' => array(
92 array(
93 'X-UA-Compatible' => 'IE=edge,chrome=1',
94 'X-UA-Compatible.' => array('httpEquivalent' => 1)
95 ),
96 TRUE,
97 array(
98 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS" />',
99 '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />'
100 )
101 ),
102 'refresh meta' => array(
103 array(
104 'refresh' => '10',
105 ),
106 FALSE,
107 array(
108 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS">',
109 '<meta http-equiv="refresh" content="10">',
110 )
111 ),
112 'meta with dot' => array(
113 array(
114 'DC.author' => 'Markus Klein',
115 ),
116 FALSE,
117 array(
118 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS">',
119 '<meta name="DC.author" content="Markus Klein">',
120 )
121 ),
122 'meta with colon' => array(
123 array(
124 'OG:title' => 'Magic Tests',
125 ),
126 FALSE,
127 array(
128 '<meta name="generator" content="TYPO3 ' . TYPO3_branch . ' CMS">',
129 '<meta name="OG:title" content="Magic Tests">',
130 )
131 ),
132 );
133 }
134
135 /**
136 * @test
137 * @dataProvider generateMetaTagHtmlGeneratesCorrectTagsDataProvider
138 *
139 * @param array $typoScript
140 * @param bool $xhtml
141 * @param array $expectedTags
142 */
143 public function generateMetaTagHtmlGeneratesCorrectTags(array $typoScript, $xhtml, array $expectedTags) {
144 $result = $this->pageGeneratorFixture->callGenerateMetaTagHtml($typoScript, $xhtml, $this->contentObjectRenderer);
145 $this->assertSame($expectedTags, $result);
146 }
147
148 /**
149 * @return array
150 */
151 public function initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider() {
152 return array(
153 'one simple search word' => array(
154 array('test'),
155 FALSE,
156 'test',
157 ),
158 'one simple search word with standalone words' => array(
159 array('test'),
160 TRUE,
161 '[[:space:]]test[[:space:]]',
162 ),
163 'two simple search words' => array(
164 array('test', 'test2'),
165 FALSE,
166 'test|test2',
167 ),
168 'two simple search words with standalone words' => array(
169 array('test', 'test2'),
170 TRUE,
171 '[[:space:]]test[[:space:]]|[[:space:]]test2[[:space:]]',
172 ),
173 'word with regex chars' => array(
174 array('A \\ word with / a bunch of [] regex () chars .*'),
175 FALSE,
176 'A word with \\/ a bunch of \\[\\] regex \\(\\) chars \\.\\*',
177 ),
178 );
179 }
180
181 /**
182 * @test
183 * @dataProvider initializeSearchWordDataInTsfeBuildsCorrectRegexDataProvider
184 *
185 * @param array $searchWordGetParameters The values that should be loaded in the sword_list GET parameter.
186 * @param bool $enableStandaloneSearchWords If TRUE the sword_standAlone option will be enabled.
187 * @param string $expectedRegex The expected regex after processing the search words.
188 */
189 public function initializeSearchWordDataInTsfeBuildsCorrectRegex(array $searchWordGetParameters, $enableStandaloneSearchWords, $expectedRegex) {
190
191 $_GET['sword_list'] = $searchWordGetParameters;
192
193 $GLOBALS['TSFE'] = new \stdClass();
194 if ($enableStandaloneSearchWords) {
195 $GLOBALS['TSFE']->config = array('config' => array('sword_standAlone' => 1));
196 }
197
198 $this->pageGeneratorFixture->callInitializeSearchWordDataInTsfe();
199 $this->assertEquals($GLOBALS['TSFE']->sWordRegEx, $expectedRegex);
200 }
201 }