844b6e265a2313beeec35d40e241d47e015f7b02
[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\CMS\Core\Tests\UnitTestCase
21 {
22 /**
23 * Indexer instance
24 *
25 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
26 */
27 protected $subject = null;
28
29 /**
30 * Sets up the test
31 */
32 protected function setUp()
33 {
34 $this->subject = $this->getMock(\TYPO3\CMS\IndexedSearch\Indexer::class, array('dummy'));
35 }
36
37 /**
38 * @test
39 */
40 public function extractHyperLinksDoesNotReturnNonExistingLocalPath()
41 {
42 $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
43 $result = $this->subject->extractHyperLinks($html);
44 $this->assertEquals(1, count($result));
45 $this->assertEquals('', $result[0]['localPath']);
46 }
47
48 /**
49 * @test
50 */
51 public function extractHyperLinksReturnsCorrectFileUsingT3Vars()
52 {
53 $temporaryFileName = tempnam(PATH_site . 'typo3temp/', 't3unit-');
54 $this->testFilesToDelete[] = $temporaryFileName;
55 $html = 'test <a href="testfile">test</a> test';
56 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
57 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $temporaryFileName,
58 );
59 $result = $this->subject->extractHyperLinks($html);
60 $this->assertEquals(1, count($result));
61 $this->assertEquals($temporaryFileName, $result[0]['localPath']);
62 }
63
64 /**
65 * @test
66 */
67 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl()
68 {
69 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
70 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
71 $result = $this->subject->extractHyperLinks($html);
72 $this->assertEquals(1, count($result));
73 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
74 }
75
76 /**
77 * @test
78 */
79 public function extractHyperLinksFindsCorrectPathWithAbsolutePath()
80 {
81 $html = 'test <a href="index.php">test</a> test';
82 $result = $this->subject->extractHyperLinks($html);
83 $this->assertEquals(1, count($result));
84 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
85 }
86
87 /**
88 * @test
89 */
90 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
91 {
92 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
93 $html = 'test <a href="' . $path . 'index.php">test</a> test';
94 $result = $this->subject->extractHyperLinks($html);
95 $this->assertEquals(1, count($result));
96 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
97 }
98
99 /**
100 * @test
101 */
102 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
103 {
104 $absRefPrefix = '/' . $this->getUniqueId();
105 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
106 $GLOBALS['TSFE'] = $this->getMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array(), array(), '', false);
107 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
108 $result = $this->subject->extractHyperLinks($html);
109 $this->assertEquals(1, count($result));
110 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
111 }
112
113 /**
114 * @test
115 */
116 public function extractBaseHrefExtractsBaseHref()
117 {
118 $baseHref = 'http://example.com/';
119 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
120 $result = $this->subject->extractBaseHref($html);
121 $this->assertEquals($baseHref, $result);
122 }
123
124 /**
125 * Tests whether indexer can extract content between "TYPO3SEARCH_begin" and "TYPO3SEARCH_end" markers
126 *
127 * @test
128 */
129 public function typoSearchTagsRemovesBodyContentOutsideMarkers()
130 {
131 $body = <<<EOT
132 <html>
133 <head>
134 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
135 <title>Some Title</title>
136 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
137 </head>
138 <body>
139 <div>
140 <div class="non_searchable">
141 not searchable content
142 </div>
143 <!--TYPO3SEARCH_begin-->
144 <div class="searchable">
145 lorem ipsum
146 </div>
147 <!--TYPO3SEARCH_end-->
148 <div class="non_searchable">
149 not searchable content
150 </div>
151 </body>
152 </html>
153 EOT;
154 $expected = <<<EOT
155
156 <div class="searchable">
157 lorem ipsum
158 </div>
159
160 EOT;
161
162 $result = $this->subject->typoSearchTags($body);
163 $this->assertTrue($result);
164 $this->assertEquals($expected, $body);
165 }
166
167 /**
168 * Tests whether indexer can extract content between multiple pairs of "TYPO3SEARCH" markers
169 *
170 * @test
171 */
172 public function typoSearchTagsHandlesMultipleMarkerPairs()
173 {
174 $body = <<<EOT
175 <html>
176 <head>
177 <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
178 <title>Some Title</title>
179 <link href='css/normalize.css' rel='stylesheet' type='text/css'/>
180 </head>
181 <body>
182 <div>
183 <div class="non_searchable">
184 not searchable content
185 </div>
186 <!--TYPO3SEARCH_begin-->
187 <div class="searchable">
188 lorem ipsum
189 </div>
190 <!--TYPO3SEARCH_end-->
191 <div class="non_searchable">
192 not searchable content
193 </div>
194 <!--TYPO3SEARCH_begin-->
195 <div class="searchable">
196 lorem ipsum2
197 </div>
198 <!--TYPO3SEARCH_end-->
199 <div class="non_searchable">
200 not searchable content
201 </div>
202 </body>
203 </html>
204 EOT;
205 $expected = <<<EOT
206
207 <div class="searchable">
208 lorem ipsum
209 </div>
210
211 <div class="searchable">
212 lorem ipsum2
213 </div>
214
215 EOT;
216
217 $result = $this->subject->typoSearchTags($body);
218 $this->assertTrue($result);
219 $this->assertEquals($expected, $body);
220 }
221 }