[!!!][TASK] Remove last usages of $GLOBALS[T3_VAR]
[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 extractHyperLinksReturnsCorrectPathWithBaseUrl()
49 {
50 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
51 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
52 $subject = new Indexer();
53 $result = $subject->extractHyperLinks($html);
54 $this->assertEquals(1, count($result));
55 $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
56 }
57
58 /**
59 * @test
60 */
61 public function extractHyperLinksFindsCorrectPathWithAbsolutePath()
62 {
63 $html = 'test <a href="index.php">test</a> test';
64 $subject = new Indexer();
65 $result = $subject->extractHyperLinks($html);
66 $this->assertEquals(1, count($result));
67 $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
68 }
69
70 /**
71 * @test
72 */
73 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
74 {
75 $html = 'test <a href="typo3/index.php">test</a> test';
76 $subject = new Indexer();
77 $result = $subject->extractHyperLinks($html);
78 $this->assertEquals(1, count($result));
79 $this->assertEquals(Environment::getPublicPath() . '/typo3/index.php', $result[0]['localPath']);
80 }
81
82 /**
83 * @test
84 */
85 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
86 {
87 $absRefPrefix = '/' . $this->getUniqueId();
88 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
89 $GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
90 $config = [
91 'config' => [
92 'absRefPrefix' => $absRefPrefix,
93 ],
94 ];
95 $GLOBALS['TSFE']->config = $config;
96 $subject = new Indexer();
97 $result = $subject->extractHyperLinks($html);
98 $this->assertEquals(1, count($result));
99 $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
100 }
101
102 /**
103 * @test
104 */
105 public function extractBaseHrefExtractsBaseHref()
106 {
107 $baseHref = 'http://example.com/';
108 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
109 $subject = new Indexer();
110 $result = $subject->extractBaseHref($html);
111 $this->assertEquals($baseHref, $result);
112 }
113
114 /**
115 * Tests whether indexer can extract content between "TYPO3SEARCH_begin" and "TYPO3SEARCH_end" markers
116 *
117 * @test
118 */
119 public function typoSearchTagsRemovesBodyContentOutsideMarkers()
120 {
121 $body = <<<EOT
122 <html>
123 <head>
124 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
125 <title>Some Title</title>
126 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
127 </head>
128 <body>
129 <div>
130 <div class="non_searchable">
131 not searchable content
132 </div>
133 <!--TYPO3SEARCH_begin-->
134 <div class="searchable">
135 lorem ipsum
136 </div>
137 <!--TYPO3SEARCH_end-->
138 <div class="non_searchable">
139 not searchable content
140 </div>
141 </body>
142 </html>
143 EOT;
144 $expected = <<<EOT
145
146 <div class="searchable">
147 lorem ipsum
148 </div>
149
150 EOT;
151
152 $subject = new Indexer();
153 $result = $subject->typoSearchTags($body);
154 $this->assertTrue($result);
155 $this->assertEquals($expected, $body);
156 }
157
158 /**
159 * Tests whether indexer can extract content between multiple pairs of "TYPO3SEARCH" markers
160 *
161 * @test
162 */
163 public function typoSearchTagsHandlesMultipleMarkerPairs()
164 {
165 $body = <<<EOT
166 <html>
167 <head>
168 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
169 <title>Some Title</title>
170 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
171 </head>
172 <body>
173 <div>
174 <div class="non_searchable">
175 not searchable content
176 </div>
177 <!--TYPO3SEARCH_begin-->
178 <div class="searchable">
179 lorem ipsum
180 </div>
181 <!--TYPO3SEARCH_end-->
182 <div class="non_searchable">
183 not searchable content
184 </div>
185 <!--TYPO3SEARCH_begin-->
186 <div class="searchable">
187 lorem ipsum2
188 </div>
189 <!--TYPO3SEARCH_end-->
190 <div class="non_searchable">
191 not searchable content
192 </div>
193 </body>
194 </html>
195 EOT;
196 $expected = <<<EOT
197
198 <div class="searchable">
199 lorem ipsum
200 </div>
201
202 <div class="searchable">
203 lorem ipsum2
204 </div>
205
206 EOT;
207
208 $subject = new Indexer();
209 $result = $subject->typoSearchTags($body);
210 $this->assertTrue($result);
211 $this->assertEquals($expected, $body);
212 }
213 }