[TASK] Use Environment API instead of PATH_site in indexed_search
[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\Core\Core\Environment;
18 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
19 use TYPO3\CMS\IndexedSearch\Indexer;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class IndexerTest extends UnitTestCase
26 {
27 /**
28 * @var bool Reset singletons created by subject
29 */
30 protected $resetSingletonInstances = true;
31
32 /**
33 * Subject is not notice free, disable E_NOTICES
34 */
35 protected static $suppressNotices = true;
36
37 /**
38 * @test
39 */
40 public function extractHyperLinksDoesNotReturnNonExistingLocalPath()
41 {
42 $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
43 $subject = new Indexer();
44 $result = $subject->extractHyperLinks($html);
45 $this->assertEquals(1, count($result));
46 $this->assertEquals('', $result[0]['localPath']);
47 }
48
49 /**
50 * @test
51 */
52 public function extractHyperLinksReturnsCorrectFileUsingT3Vars()
53 {
54 $temporaryFileName = tempnam(Environment::getVarPath() . '/tests/', 't3unit-');
55 $this->testFilesToDelete[] = $temporaryFileName;
56 $html = 'test <a href="testfile">test</a> test';
57 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = [
58 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $temporaryFileName,
59 ];
60 $subject = new Indexer();
61 $result = $subject->extractHyperLinks($html);
62 $this->assertEquals(1, count($result));
63 $this->assertEquals($temporaryFileName, $result[0]['localPath']);
64 }
65
66 /**
67 * @test
68 */
69 public function extractHyperLinksReturnsCorrectPathWithBaseUrl()
70 {
71 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
72 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
73 $subject = new Indexer();
74 $result = $subject->extractHyperLinks($html);
75 $this->assertEquals(1, count($result));
76 $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
77 }
78
79 /**
80 * @test
81 */
82 public function extractHyperLinksFindsCorrectPathWithAbsolutePath()
83 {
84 $html = 'test <a href="index.php">test</a> test';
85 $subject = new Indexer();
86 $result = $subject->extractHyperLinks($html);
87 $this->assertEquals(1, count($result));
88 $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
89 }
90
91 /**
92 * @test
93 */
94 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
95 {
96 $html = 'test <a href="typo3/index.php">test</a> test';
97 $subject = new Indexer();
98 $result = $subject->extractHyperLinks($html);
99 $this->assertEquals(1, count($result));
100 $this->assertEquals(Environment::getPublicPath() . '/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(Environment::getPublicPath() . '/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 }