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