[BUGFIX] Add missing namespace parts
[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 /**
18 * This class contains unit tests for the indexer
19 */
20 class IndexerTest extends \TYPO3\Components\TestingFramework\Core\Unit\UnitTestCase
21 {
22 /**
23 * Indexer instance
24 *
25 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
26 */
27 protected $subject = null;
28
29 /**
30 * Sets up the test
31 */
32 protected function setUp()
33 {
34 $this->subject = $this->getMockBuilder(\TYPO3\CMS\IndexedSearch\Indexer::class)
35 ->setMethods(['dummy'])
36 ->getMock();
37 }
38
39 /**
40 * @test
41 */
42 public function extractHyperLinksDoesNotReturnNonExistingLocalPath()
43 {
44 $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
45 $result = $this->subject->extractHyperLinks($html);
46 $this->assertEquals(1, count($result));
47 $this->assertEquals('', $result[0]['localPath']);
48 }
49
50 /**
51 * @test
52 */
53 public function extractHyperLinksReturnsCorrectFileUsingT3Vars()
54 {
55 $temporaryFileName = tempnam(PATH_site . 'typo3temp/var/tests/', 't3unit-');
56 $this->testFilesToDelete[] = $temporaryFileName;
57 $html = 'test <a href="testfile">test</a> test';
58 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = [
59 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $temporaryFileName,
60 ];
61 $result = $this->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 extractHyperLinksRecurnsCorrectPathWithBaseUrl()
70 {
71 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
72 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
73 $result = $this->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 $result = $this->subject->extractHyperLinks($html);
85 $this->assertEquals(1, count($result));
86 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
87 }
88
89 /**
90 * @test
91 */
92 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
93 {
94 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
95 $html = 'test <a href="' . $path . 'index.php">test</a> test';
96 $result = $this->subject->extractHyperLinks($html);
97 $this->assertEquals(1, count($result));
98 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
99 }
100
101 /**
102 * @test
103 */
104 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
105 {
106 $absRefPrefix = '/' . $this->getUniqueId();
107 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
108 $GLOBALS['TSFE'] = $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class);
109 $config = [
110 'config' => [
111 'absRefPrefix' => $absRefPrefix,
112 ],
113 ];
114 $GLOBALS['TSFE']->config = $config;
115 $result = $this->subject->extractHyperLinks($html);
116 $this->assertEquals(1, count($result));
117 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
118 }
119
120 /**
121 * @test
122 */
123 public function extractBaseHrefExtractsBaseHref()
124 {
125 $baseHref = 'http://example.com/';
126 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
127 $result = $this->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 $result = $this->subject->typoSearchTags($body);
170 $this->assertTrue($result);
171 $this->assertEquals($expected, $body);
172 }
173
174 /**
175 * Tests whether indexer can extract content between multiple pairs of "TYPO3SEARCH" markers
176 *
177 * @test
178 */
179 public function typoSearchTagsHandlesMultipleMarkerPairs()
180 {
181 $body = <<<EOT
182 <html>
183 <head>
184 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
185 <title>Some Title</title>
186 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
187 </head>
188 <body>
189 <div>
190 <div class="non_searchable">
191 not searchable content
192 </div>
193 <!--TYPO3SEARCH_begin-->
194 <div class="searchable">
195 lorem ipsum
196 </div>
197 <!--TYPO3SEARCH_end-->
198 <div class="non_searchable">
199 not searchable content
200 </div>
201 <!--TYPO3SEARCH_begin-->
202 <div class="searchable">
203 lorem ipsum2
204 </div>
205 <!--TYPO3SEARCH_end-->
206 <div class="non_searchable">
207 not searchable content
208 </div>
209 </body>
210 </html>
211 EOT;
212 $expected = <<<EOT
213
214 <div class="searchable">
215 lorem ipsum
216 </div>
217
218 <div class="searchable">
219 lorem ipsum2
220 </div>
221
222 EOT;
223
224 $result = $this->subject->typoSearchTags($body);
225 $this->assertTrue($result);
226 $this->assertEquals($expected, $body);
227 }
228 }