‪TYPO3CMS  11.5
IndexerTest.php
Go to the documentation of this file.
1 <?php
2 
3 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 
19 
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
31 class ‪IndexerTest extends UnitTestCase
32 {
36  protected ‪$resetSingletonInstances = true;
37 
38  protected function ‪setUp(): void
39  {
40  parent::setUp();
41  $request = (new ‪ServerRequest('https://foo.de', 'GET'));
42  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
43  $request = $request->withAttribute('normalizedParams', $normalizedParams);
44  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
45  }
46 
51  {
52  $html = 'test <a href="' . ‪StringUtility::getUniqueId() . '">test</a> test';
53  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
54  $result = $subject->extractHyperLinks($html);
55  self::assertCount(1, $result);
56  self::assertEquals('', $result[0]['localPath']);
57  }
58 
63  {
64  $baseURL = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getSiteUrl();
65  $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
66  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
67  $result = $subject->extractHyperLinks($html);
68  self::assertCount(1, $result);
69  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
70  }
71 
76  {
77  $html = 'test <a href="index.php">test</a> test';
78  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
79  $result = $subject->extractHyperLinks($html);
80  self::assertCount(1, $result);
81  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
82  }
83 
88  {
89  $html = 'test <a href="typo3/index.php">test</a> test';
90  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
91  $result = $subject->extractHyperLinks($html);
92  self::assertCount(1, $result);
93  self::assertEquals(‪Environment::getPublicPath() . '/typo3/index.php', $result[0]['localPath']);
94  }
95 
100  {
101  $absRefPrefix = '/' . ‪StringUtility::getUniqueId();
102  $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
103  ‪$GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
104  $config = [
105  'config' => [
106  'absRefPrefix' => $absRefPrefix,
107  ],
108  ];
109  ‪$GLOBALS['TSFE']->config = $config;
110  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
111  $result = $subject->extractHyperLinks($html);
112  self::assertCount(1, $result);
113  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
114  }
115 
119  public function ‪extractBaseHrefExtractsBaseHref(): void
120  {
121  $baseHref = 'http://example.com/';
122  $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
123  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
124  $result = $subject->extractBaseHref($html);
125  self::assertEquals($baseHref, $result);
126  }
127 
134  {
135  $body = <<<EOT
136 <html>
137 <head>
138 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
139 <title>Some Title</title>
140 <link href='css/normalize.css' rel='stylesheet'/>
141 </head>
142 <body>
143 <div>
144 <div class="non_searchable">
145  not searchable content
146 </div>
147 <!--TYPO3SEARCH_begin-->
148 <div class="searchable">
149  lorem ipsum
150 </div>
151 <!--TYPO3SEARCH_end-->
152 <div class="non_searchable">
153  not searchable content
154 </div>
155 </body>
156 </html>
157 EOT;
158  $expected = <<<EOT
159 
160 <div class="searchable">
161  lorem ipsum
162 </div>
163 
164 EOT;
165 
166  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
167  $result = $subject->typoSearchTags($body);
168  self::assertTrue($result);
169  self::assertEquals($expected, $body);
170  }
171 
177  public function ‪typoSearchTagsHandlesMultipleMarkerPairs(): void
178  {
179  $body = <<<EOT
180 <html>
181 <head>
182 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
183 <title>Some Title</title>
184 <link href='css/normalize.css' rel='stylesheet'/>
185 </head>
186 <body>
187 <div>
188 <div class="non_searchable">
189  not searchable content
190 </div>
191 <!--TYPO3SEARCH_begin-->
192 <div class="searchable">
193  lorem ipsum
194 </div>
195 <!--TYPO3SEARCH_end-->
196 <div class="non_searchable">
197  not searchable content
198 </div>
199 <!--TYPO3SEARCH_begin-->
200 <div class="searchable">
201  lorem ipsum2
202 </div>
203 <!--TYPO3SEARCH_end-->
204 <div class="non_searchable">
205  not searchable content
206 </div>
207 </body>
208 </html>
209 EOT;
210  $expected = <<<EOT
211 
212 <div class="searchable">
213  lorem ipsum
214 </div>
215 
216 <div class="searchable">
217  lorem ipsum2
218 </div>
219 
220 EOT;
221 
222  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
223  $result = $subject->typoSearchTags($body);
224  self::assertTrue($result);
225  self::assertEquals($expected, $body);
226  }
227 }
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathUsingAbsRefPrefix
‪extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
Definition: IndexerTest.php:98
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksDoesNotReturnNonExistingLocalPath
‪extractHyperLinksDoesNotReturnNonExistingLocalPath()
Definition: IndexerTest.php:49
‪TYPO3\CMS\IndexedSearch\Tests\Unit
Definition: IndexerTest.php:18
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:206
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksReturnsCorrectPathWithBaseUrl
‪extractHyperLinksReturnsCorrectPathWithBaseUrl()
Definition: IndexerTest.php:61
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsRemovesBodyContentOutsideMarkers
‪typoSearchTagsRemovesBodyContentOutsideMarkers()
Definition: IndexerTest.php:132
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\setUp
‪setUp()
Definition: IndexerTest.php:37
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:37
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathWithAbsolutePath
‪extractHyperLinksFindsCorrectPathWithAbsolutePath()
Definition: IndexerTest.php:74
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:104
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractBaseHrefExtractsBaseHref
‪extractBaseHrefExtractsBaseHref()
Definition: IndexerTest.php:118
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory
‪extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
Definition: IndexerTest.php:86
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsHandlesMultipleMarkerPairs
‪typoSearchTagsHandlesMultipleMarkerPairs()
Definition: IndexerTest.php:176
‪TYPO3\CMS\IndexedSearch\Indexer
Definition: Indexer.php:39
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest
Definition: IndexerTest.php:32
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: IndexerTest.php:35
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:843
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:35