‪TYPO3CMS  ‪main
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 
28 final class ‪IndexerTest extends UnitTestCase
29 {
30  protected bool ‪$resetSingletonInstances = true;
31 
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  $request = (new ‪ServerRequest('https://foo.de', 'GET'));
36  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
37  $request = $request->withAttribute('normalizedParams', $normalizedParams);
38  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
39  }
40 
45  {
46  $html = 'test <a href="' . ‪StringUtility::getUniqueId() . '">test</a> test';
47  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
48  $result = $subject->extractHyperLinks($html);
49  self::assertCount(1, $result);
50  self::assertEquals('', $result[0]['localPath']);
51  }
52 
57  {
58  $baseURL = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getSiteUrl();
59  $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
60  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
61  $result = $subject->extractHyperLinks($html);
62  self::assertCount(1, $result);
63  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
64  }
65 
70  {
71  $html = 'test <a href="index.php">test</a> test';
72  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
73  $result = $subject->extractHyperLinks($html);
74  self::assertCount(1, $result);
75  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
76  }
77 
82  {
83  $html = 'test <a href="typo3/index.php">test</a> test';
84  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
85  $result = $subject->extractHyperLinks($html);
86  self::assertCount(1, $result);
87  self::assertEquals(‪Environment::getPublicPath() . '/typo3/index.php', $result[0]['localPath']);
88  }
89 
94  {
95  $absRefPrefix = '/' . ‪StringUtility::getUniqueId();
96  $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
97  ‪$GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
98  $config = [
99  'config' => [
100  'absRefPrefix' => $absRefPrefix,
101  ],
102  ];
103  ‪$GLOBALS['TSFE']->config = $config;
104  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
105  $result = $subject->extractHyperLinks($html);
106  self::assertCount(1, $result);
107  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
108  }
109 
113  public function ‪extractBaseHrefExtractsBaseHref(): void
114  {
115  $baseHref = 'http://example.com/';
116  $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
117  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
118  $result = $subject->extractBaseHref($html);
119  self::assertEquals($baseHref, $result);
120  }
121 
128  {
129  $body = <<<EOT
130 <html>
131 <head>
132 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
133 <title>Some Title</title>
134 <link href='css/normalize.css' rel='stylesheet'/>
135 </head>
136 <body>
137 <div>
138 <div class="non_searchable">
139  not searchable content
140 </div>
141 <!--TYPO3SEARCH_begin-->
142 <div class="searchable">
143  lorem ipsum
144 </div>
145 <!--TYPO3SEARCH_end-->
146 <div class="non_searchable">
147  not searchable content
148 </div>
149 </body>
150 </html>
151 EOT;
152  $expected = <<<EOT
153 
154 <div class="searchable">
155  lorem ipsum
156 </div>
157 
158 EOT;
159 
160  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
161  $result = $subject->typoSearchTags($body);
162  self::assertTrue($result);
163  self::assertEquals($expected, $body);
164  }
165 
172  {
173  $body = <<<EOT
174 <html>
175 <head>
176 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
177 <title>Some Title</title>
178 <link href='css/normalize.css' rel='stylesheet'/>
179 </head>
180 <body>
181 <div>
182 <div class="non_searchable">
183  not searchable content
184 </div>
185 <!--TYPO3SEARCH_begin-->
186 <div class="searchable">
187  lorem ipsum
188 </div>
189 <!--TYPO3SEARCH_end-->
190 <div class="non_searchable">
191  not searchable content
192 </div>
193 <!--TYPO3SEARCH_begin-->
194 <div class="searchable">
195  lorem ipsum2
196 </div>
197 <!--TYPO3SEARCH_end-->
198 <div class="non_searchable">
199  not searchable content
200 </div>
201 </body>
202 </html>
203 EOT;
204  $expected = <<<EOT
205 
206 <div class="searchable">
207  lorem ipsum
208 </div>
209 
210 <div class="searchable">
211  lorem ipsum2
212 </div>
213 
214 EOT;
215 
216  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->addMethods(['dummy'])->getMock();
217  $result = $subject->typoSearchTags($body);
218  self::assertTrue($result);
219  self::assertEquals($expected, $body);
220  }
221 }
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathUsingAbsRefPrefix
‪extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
Definition: IndexerTest.php:93
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksDoesNotReturnNonExistingLocalPath
‪extractHyperLinksDoesNotReturnNonExistingLocalPath()
Definition: IndexerTest.php:44
‪TYPO3\CMS\IndexedSearch\Tests\Unit
Definition: IndexerTest.php:18
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksReturnsCorrectPathWithBaseUrl
‪extractHyperLinksReturnsCorrectPathWithBaseUrl()
Definition: IndexerTest.php:56
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsRemovesBodyContentOutsideMarkers
‪typoSearchTagsRemovesBodyContentOutsideMarkers()
Definition: IndexerTest.php:127
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\setUp
‪setUp()
Definition: IndexerTest.php:32
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathWithAbsolutePath
‪extractHyperLinksFindsCorrectPathWithAbsolutePath()
Definition: IndexerTest.php:69
‪TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController
Definition: TypoScriptFrontendController.php:102
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractBaseHrefExtractsBaseHref
‪extractBaseHrefExtractsBaseHref()
Definition: IndexerTest.php:113
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory
‪extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
Definition: IndexerTest.php:81
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsHandlesMultipleMarkerPairs
‪typoSearchTagsHandlesMultipleMarkerPairs()
Definition: IndexerTest.php:171
‪TYPO3\CMS\IndexedSearch\Indexer
Definition: Indexer.php:39
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest
Definition: IndexerTest.php:29
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: IndexerTest.php:30
‪TYPO3\CMS\Core\Http\NormalizedParams\createFromRequest
‪static static createFromRequest(ServerRequestInterface $request, array $systemConfiguration=null)
Definition: NormalizedParams.php:840
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static getUniqueId(string $prefix='')
Definition: StringUtility.php:57