[!!!][TASK] Handle E_NOTICE free unit tests
[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\TestingFramework\Core\Unit\UnitTestCase
21 {
22 /**
23 * Subject is not notice free, disable E_NOTICES
24 */
25 protected static $suppressNotices = true;
26
27 /**
28 * Indexer instance
29 *
30 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
31 */
32 protected $subject = null;
33
34 /**
35 * Sets up the test
36 */
37 protected function setUp()
38 {
39 $this->subject = $this->getMockBuilder(\TYPO3\CMS\IndexedSearch\Indexer::class)
40 ->setMethods(['dummy'])
41 ->getMock();
42 }
43
44 /**
45 * @test
46 */
47 public function extractHyperLinksDoesNotReturnNonExistingLocalPath()
48 {
49 $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
50 $result = $this->subject->extractHyperLinks($html);
51 $this->assertEquals(1, count($result));
52 $this->assertEquals('', $result[0]['localPath']);
53 }
54
55 /**
56 * @test
57 */
58 public function extractHyperLinksReturnsCorrectFileUsingT3Vars()
59 {
60 $temporaryFileName = tempnam(PATH_site . 'typo3temp/var/tests/', 't3unit-');
61 $this->testFilesToDelete[] = $temporaryFileName;
62 $html = 'test <a href="testfile">test</a> test';
63 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = [
64 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $temporaryFileName,
65 ];
66 $result = $this->subject->extractHyperLinks($html);
67 $this->assertEquals(1, count($result));
68 $this->assertEquals($temporaryFileName, $result[0]['localPath']);
69 }
70
71 /**
72 * @test
73 */
74 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl()
75 {
76 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
77 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
78 $result = $this->subject->extractHyperLinks($html);
79 $this->assertEquals(1, count($result));
80 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
81 }
82
83 /**
84 * @test
85 */
86 public function extractHyperLinksFindsCorrectPathWithAbsolutePath()
87 {
88 $html = 'test <a href="index.php">test</a> test';
89 $result = $this->subject->extractHyperLinks($html);
90 $this->assertEquals(1, count($result));
91 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
92 }
93
94 /**
95 * @test
96 */
97 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
98 {
99 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
100 $html = 'test <a href="' . $path . 'index.php">test</a> test';
101 $result = $this->subject->extractHyperLinks($html);
102 $this->assertEquals(1, count($result));
103 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
104 }
105
106 /**
107 * @test
108 */
109 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
110 {
111 $absRefPrefix = '/' . $this->getUniqueId();
112 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
113 $GLOBALS['TSFE'] = $this->createMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class);
114 $config = [
115 'config' => [
116 'absRefPrefix' => $absRefPrefix,
117 ],
118 ];
119 $GLOBALS['TSFE']->config = $config;
120 $result = $this->subject->extractHyperLinks($html);
121 $this->assertEquals(1, count($result));
122 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
123 }
124
125 /**
126 * @test
127 */
128 public function extractBaseHrefExtractsBaseHref()
129 {
130 $baseHref = 'http://example.com/';
131 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
132 $result = $this->subject->extractBaseHref($html);
133 $this->assertEquals($baseHref, $result);
134 }
135
136 /**
137 * Tests whether indexer can extract content between "TYPO3SEARCH_begin" and "TYPO3SEARCH_end" markers
138 *
139 * @test
140 */
141 public function typoSearchTagsRemovesBodyContentOutsideMarkers()
142 {
143 $body = <<<EOT
144 <html>
145 <head>
146 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
147 <title>Some Title</title>
148 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
149 </head>
150 <body>
151 <div>
152 <div class="non_searchable">
153 not searchable content
154 </div>
155 <!--TYPO3SEARCH_begin-->
156 <div class="searchable">
157 lorem ipsum
158 </div>
159 <!--TYPO3SEARCH_end-->
160 <div class="non_searchable">
161 not searchable content
162 </div>
163 </body>
164 </html>
165 EOT;
166 $expected = <<<EOT
167
168 <div class="searchable">
169 lorem ipsum
170 </div>
171
172 EOT;
173
174 $result = $this->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 $result = $this->subject->typoSearchTags($body);
230 $this->assertTrue($result);
231 $this->assertEquals($expected, $body);
232 }
233 }