384e90285cac9b2142911de4a61b977796034c6e
[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 class contains unit tests for the indexer
6 *
7 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
8 * @author Christian Kuhn <lolli@schwarzbu.ch>
9 * @package TYPO3
10 * @subpackage tx_indexedsearch
11 */
12 class IndexerTest extends \tx_phpunit_testcase {
13
14 /**
15 * Indexer instance
16 *
17 * @var \TYPO3\CMS\IndexedSearch\Indexer
18 */
19 protected $indexer;
20
21 /**
22 * A name of the temporary file
23 *
24 * @var string
25 */
26 protected $temporaryFileName = '';
27
28 /**
29 * Sets up the test
30 *
31 * @return void
32 */
33 public function setUp() {
34 $this->indexer = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_indexedsearch_indexer');
35 }
36
37 /**
38 * Explicitly cleans up the indexer object to prevent any memory leaks
39 *
40 * @return void
41 */
42 public function tearDown() {
43 unset($this->indexer);
44 if ($this->temporaryFileName) {
45 @unlink($this->temporaryFileName);
46 }
47 }
48
49 /**
50 * Checks that non-existing files are not returned
51 *
52 * @return void
53 */
54 public function testNonExistingLocalPath() {
55 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
56 $result = $this->indexer->extractHyperLinks($html);
57 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
58 $this->assertEquals($result[0]['localPath'], '', 'Local path is incorrect');
59 }
60
61 /**
62 * Checks that using t3vars returns correct file
63 *
64 * @return void
65 */
66 public function testLocalPathWithT3Vars() {
67 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
68 $html = 'test <a href="testfile">test</a> test';
69 $savedValue = $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'];
70 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
71 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $this->temporaryFileName
72 );
73 $result = $this->indexer->extractHyperLinks($html);
74 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = $savedValue;
75 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
76 $this->assertEquals($result[0]['localPath'], $this->temporaryFileName, 'Local path is incorrect');
77 }
78
79 /**
80 * Tests that a path with baseURL
81 *
82 * @return void
83 */
84 public function testLocalPathWithSiteURL() {
85 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
86 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
87 $result = $this->indexer->extractHyperLinks($html);
88 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
89 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
90 }
91
92 /**
93 * Tests absolute path
94 *
95 * @return void
96 */
97 public function testRelativeLocalPath() {
98 $html = 'test <a href="index.php">test</a> test';
99 $result = $this->indexer->extractHyperLinks($html);
100 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
101 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
102 }
103
104 /**
105 * Tests absolute path.
106 *
107 * @return void
108 */
109 public function testAbsoluteLocalPath() {
110 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
111 $html = 'test <a href="' . $path . 'index.php">test</a> test';
112 $result = $this->indexer->extractHyperLinks($html);
113 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
114 $this->assertEquals($result[0]['localPath'], PATH_typo3 . 'index.php', 'Local path is incorrect');
115 }
116
117 /**
118 * Tests that a path with the absRefPrefix returns correct result
119 *
120 * @return void
121 */
122 public function testLocalPathWithAbsRefPrefix() {
123 $absRefPrefix = '/' . md5(uniqid(''));
124 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
125 $savedPrefix = $GLOBALS['TSFE']->config['config']['absRefPrefix'];
126 if (!($GLOBALS['TSFE'] instanceof \TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController)) {
127 $GLOBALS['TSFE'] = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController');
128 }
129 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
130 $result = $this->indexer->extractHyperLinks($html);
131 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $savedPrefix;
132 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
133 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
134 }
135
136 /**
137 * Checks that base HREF is extracted correctly
138 *
139 * @return void
140 */
141 public function textExtractBaseHref() {
142 $baseHref = 'http://example.com/';
143 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
144 $result = $this->indexer->extractHyperLinks($html);
145 $this->assertEquals($baseHref, $result, 'Incorrect base href was extracted');
146 }
147
148 }
149
150
151 ?>