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