‪TYPO3CMS  9.5
IndexerTest.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
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 
21 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
22 
26 class ‪IndexerTest extends UnitTestCase
27 {
31  protected ‪$resetSingletonInstances = true;
32 
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 
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 
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 
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 
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 
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 
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 
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 
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 }
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathUsingAbsRefPrefix
‪extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
Definition: IndexerTest.php:101
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksDoesNotReturnNonExistingLocalPath
‪extractHyperLinksDoesNotReturnNonExistingLocalPath()
Definition: IndexerTest.php:35
‪TYPO3\CMS\IndexedSearch\Tests\Unit
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksReturnsCorrectPathWithBaseUrl
‪extractHyperLinksReturnsCorrectPathWithBaseUrl()
Definition: IndexerTest.php:64
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsRemovesBodyContentOutsideMarkers
‪typoSearchTagsRemovesBodyContentOutsideMarkers()
Definition: IndexerTest.php:135
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksReturnsCorrectFileUsingT3Vars
‪extractHyperLinksReturnsCorrectFileUsingT3Vars()
Definition: IndexerTest.php:47
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathWithAbsolutePath
‪extractHyperLinksFindsCorrectPathWithAbsolutePath()
Definition: IndexerTest.php:77
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:97
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractBaseHrefExtractsBaseHref
‪extractBaseHrefExtractsBaseHref()
Definition: IndexerTest.php:121
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory
‪extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
Definition: IndexerTest.php:89
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsHandlesMultipleMarkerPairs
‪typoSearchTagsHandlesMultipleMarkerPairs()
Definition: IndexerTest.php:179
‪TYPO3\CMS\IndexedSearch\Indexer
Definition: Indexer.php:38
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest
Definition: IndexerTest.php:27
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: IndexerTest.php:30
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165