‪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 
20 use PHPUnit\Framework\Attributes\Test;
27 use TYPO3\CMS\IndexedSearch\Indexer;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪IndexerTest extends UnitTestCase
31 {
32  protected bool ‪$resetSingletonInstances = true;
33 
34  protected function ‪setUp(): void
35  {
36  parent::setUp();
37  $request = (new ‪ServerRequest('https://foo.de', 'GET'));
38  $normalizedParams = ‪NormalizedParams::createFromRequest($request);
39  $request = $request->withAttribute('normalizedParams', $normalizedParams);
40  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
41  }
42 
43  #[Test]
45  {
46  $html = 'test <a href="' . ‪StringUtility::getUniqueId() . '">test</a> test';
47  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
48  $result = $subject->extractHyperLinks($html);
49  self::assertCount(1, $result);
50  self::assertEquals('', $result[0]['localPath']);
51  }
52 
53  #[Test]
55  {
56  $baseURL = ‪$GLOBALS['TYPO3_REQUEST']->getAttribute('normalizedParams')->getSiteUrl();
57  $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
58  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
59  $result = $subject->extractHyperLinks($html);
60  self::assertCount(1, $result);
61  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
62  }
63 
64  #[Test]
66  {
67  $html = 'test <a href="index.php">test</a> test';
68  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
69  $result = $subject->extractHyperLinks($html);
70  self::assertCount(1, $result);
71  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
72  }
73 
74  #[Test]
76  {
77  $absRefPrefix = '/' . ‪StringUtility::getUniqueId();
78  $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
79  $typoScript = new ‪FrontendTypoScript(new ‪RootNode(), [], [], []);
80  $typoScript->setConfigArray([
81  'absRefPrefix' => $absRefPrefix,
82  ]);
83  $request = (new ‪ServerRequest())->withAttribute('frontend.typoscript', $typoScript);
84  ‪$GLOBALS['TYPO3_REQUEST'] = $request;
85  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
86  $result = $subject->extractHyperLinks($html);
87  self::assertCount(1, $result);
88  self::assertEquals(‪Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
89  }
90 
91  #[Test]
92  public function ‪extractBaseHrefExtractsBaseHref(): void
93  {
94  $baseHref = 'http://example.com/';
95  $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
96  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
97  $result = $subject->extractBaseHref($html);
98  self::assertEquals($baseHref, $result);
99  }
100 
104  #[Test]
106  {
107  $body = <<<EOT
108 <html>
109 <head>
110 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
111 <title>Some Title</title>
112 <link href='css/normalize.css' rel='stylesheet'/>
113 </head>
114 <body>
115 <div>
116 <div class="non_searchable">
117  not searchable content
118 </div>
119 <!--TYPO3SEARCH_begin-->
120 <div class="searchable">
121  lorem ipsum
122 </div>
123 <!--TYPO3SEARCH_end-->
124 <div class="non_searchable">
125  not searchable content
126 </div>
127 </body>
128 </html>
129 EOT;
130  $expected = <<<EOT
131 
132 <div class="searchable">
133  lorem ipsum
134 </div>
135 
136 EOT;
137 
138  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
139  $result = $subject->typoSearchTags($body);
140  self::assertTrue($result);
141  self::assertEquals($expected, $body);
142  }
143 
147  #[Test]
149  {
150  $body = <<<EOT
151 <html>
152 <head>
153 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
154 <title>Some Title</title>
155 <link href='css/normalize.css' rel='stylesheet'/>
156 </head>
157 <body>
158 <div>
159 <div class="non_searchable">
160  not searchable content
161 </div>
162 <!--TYPO3SEARCH_begin-->
163 <div class="searchable">
164  lorem ipsum
165 </div>
166 <!--TYPO3SEARCH_end-->
167 <div class="non_searchable">
168  not searchable content
169 </div>
170 <!--TYPO3SEARCH_begin-->
171 <div class="searchable">
172  lorem ipsum2
173 </div>
174 <!--TYPO3SEARCH_end-->
175 <div class="non_searchable">
176  not searchable content
177 </div>
178 </body>
179 </html>
180 EOT;
181  $expected = <<<EOT
182 
183 <div class="searchable">
184  lorem ipsum
185 </div>
186 
187 <div class="searchable">
188  lorem ipsum2
189 </div>
190 
191 EOT;
192 
193  $subject = $this->getMockBuilder(Indexer::class)->disableOriginalConstructor()->onlyMethods([])->getMock();
194  $result = $subject->typoSearchTags($body);
195  self::assertTrue($result);
196  self::assertEquals($expected, $body);
197  }
198 }
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathUsingAbsRefPrefix
‪extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
Definition: IndexerTest.php:75
‪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:54
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsRemovesBodyContentOutsideMarkers
‪typoSearchTagsRemovesBodyContentOutsideMarkers()
Definition: IndexerTest.php:105
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\setUp
‪setUp()
Definition: IndexerTest.php:34
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractHyperLinksFindsCorrectPathWithAbsolutePath
‪extractHyperLinksFindsCorrectPathWithAbsolutePath()
Definition: IndexerTest.php:65
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\extractBaseHrefExtractsBaseHref
‪extractBaseHrefExtractsBaseHref()
Definition: IndexerTest.php:92
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\typoSearchTagsHandlesMultipleMarkerPairs
‪typoSearchTagsHandlesMultipleMarkerPairs()
Definition: IndexerTest.php:148
‪TYPO3\CMS\Core\TypoScript\FrontendTypoScript
Definition: FrontendTypoScript.php:30
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest
Definition: IndexerTest.php:31
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:24
‪TYPO3\CMS\IndexedSearch\Tests\Unit\IndexerTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: IndexerTest.php:32
‪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