[TASK] Properly reset unit test singletons
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Tests / Unit / IndexerTest.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Tests\Unit;
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
17 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
18 use TYPO3\CMS\IndexedSearch\Indexer;
19 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20
21 /**
22 * Test case
23 */
24 class IndexerTest extends UnitTestCase
25 {
26 /**
27 * @var bool Reset singletons created by subject
28 */
29 protected $resetSingletonInstances = true;
30
31 /**
32 * Subject is not notice free, disable E_NOTICES
33 */
34 protected static $suppressNotices = true;
35
36 /**
37 * @test
38 */
39 public function extractHyperLinksDoesNotReturnNonExistingLocalPath()
40 {
41 $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
42 $subject = new Indexer();
43 $result = $subject->extractHyperLinks($html);
44 $this->assertEquals(1, count($result));
45 $this->assertEquals('', $result[0]['localPath']);
46 }
47
48 /**
49 * @test
50 */
51 public function extractHyperLinksReturnsCorrectFileUsingT3Vars()
52 {
53 $temporaryFileName = tempnam(PATH_site . 'typo3temp/var/tests/', 't3unit-');
54 $this->testFilesToDelete[] = $temporaryFileName;
55 $html = 'test <a href="testfile">test</a> test';
56 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = [
57 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $temporaryFileName,
58 ];
59 $subject = new Indexer();
60 $result = $subject->extractHyperLinks($html);
61 $this->assertEquals(1, count($result));
62 $this->assertEquals($temporaryFileName, $result[0]['localPath']);
63 }
64
65 /**
66 * @test
67 */
68 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl()
69 {
70 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
71 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
72 $subject = new Indexer();
73 $result = $subject->extractHyperLinks($html);
74 $this->assertEquals(1, count($result));
75 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
76 }
77
78 /**
79 * @test
80 */
81 public function extractHyperLinksFindsCorrectPathWithAbsolutePath()
82 {
83 $html = 'test <a href="index.php">test</a> test';
84 $subject = new Indexer();
85 $result = $subject->extractHyperLinks($html);
86 $this->assertEquals(1, count($result));
87 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
88 }
89
90 /**
91 * @test
92 */
93 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
94 {
95 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
96 $html = 'test <a href="' . $path . 'index.php">test</a> test';
97 $subject = new Indexer();
98 $result = $subject->extractHyperLinks($html);
99 $this->assertEquals(1, count($result));
100 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
101 }
102
103 /**
104 * @test
105 */
106 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
107 {
108 $absRefPrefix = '/' . $this->getUniqueId();
109 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
110 $GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
111 $config = [
112 'config' => [
113 'absRefPrefix' => $absRefPrefix,
114 ],
115 ];
116 $GLOBALS['TSFE']->config = $config;
117 $subject = new Indexer();
118 $result = $subject->extractHyperLinks($html);
119 $this->assertEquals(1, count($result));
120 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
121 }
122
123 /**
124 * @test
125 */
126 public function extractBaseHrefExtractsBaseHref()
127 {
128 $baseHref = 'http://example.com/';
129 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
130 $subject = new Indexer();
131 $result = $subject->extractBaseHref($html);
132 $this->assertEquals($baseHref, $result);
133 }
134
135 /**
136 * Tests whether indexer can extract content between "TYPO3SEARCH_begin" and "TYPO3SEARCH_end" markers
137 *
138 * @test
139 */
140 public function typoSearchTagsRemovesBodyContentOutsideMarkers()
141 {
142 $body = <<<EOT
143 <html>
144 <head>
145 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
146 <title>Some Title</title>
147 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
148 </head>
149 <body>
150 <div>
151 <div class="non_searchable">
152 not searchable content
153 </div>
154 <!--TYPO3SEARCH_begin-->
155 <div class="searchable">
156 lorem ipsum
157 </div>
158 <!--TYPO3SEARCH_end-->
159 <div class="non_searchable">
160 not searchable content
161 </div>
162 </body>
163 </html>
164 EOT;
165 $expected = <<<EOT
166
167 <div class="searchable">
168 lorem ipsum
169 </div>
170
171 EOT;
172
173 $subject = new Indexer();
174 $result = $subject->typoSearchTags($body);
175 $this->assertTrue($result);
176 $this->assertEquals($expected, $body);
177 }
178
179 /**
180 * Tests whether indexer can extract content between multiple pairs of "TYPO3SEARCH" markers
181 *
182 * @test
183 */
184 public function typoSearchTagsHandlesMultipleMarkerPairs()
185 {
186 $body = <<<EOT
187 <html>
188 <head>
189 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
190 <title>Some Title</title>
191 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
192 </head>
193 <body>
194 <div>
195 <div class="non_searchable">
196 not searchable content
197 </div>
198 <!--TYPO3SEARCH_begin-->
199 <div class="searchable">
200 lorem ipsum
201 </div>
202 <!--TYPO3SEARCH_end-->
203 <div class="non_searchable">
204 not searchable content
205 </div>
206 <!--TYPO3SEARCH_begin-->
207 <div class="searchable">
208 lorem ipsum2
209 </div>
210 <!--TYPO3SEARCH_end-->
211 <div class="non_searchable">
212 not searchable content
213 </div>
214 </body>
215 </html>
216 EOT;
217 $expected = <<<EOT
218
219 <div class="searchable">
220 lorem ipsum
221 </div>
222
223 <div class="searchable">
224 lorem ipsum2
225 </div>
226
227 EOT;
228
229 $subject = new Indexer();
230 $result = $subject->typoSearchTags($body);
231 $this->assertTrue($result);
232 $this->assertEquals($expected, $body);
233 }
234 }