[TASK] Replace Space Indent into Tab indent (CGL Cleanup)
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / tests / tx_indexedsearch_indexerTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 Dmitry Dulepov (dmitry.dulepov@gmail.com)
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 require_once(t3lib_extMgm::extPath('indexed_search', 'class.indexer.php'));
27
28 /**
29 * This class contains unit tests for the indexer
30 *
31 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
32 * @author Christian Kuhn <lolli@schwarzbu.ch>
33 * @package TYPO3
34 * @subpackage tx_indexedsearch
35 */
36 class tx_indexedsearch_indexerTest extends tx_phpunit_testcase {
37
38 /**
39 * Indexer instance
40 *
41 * @var tx_indexedsearch_indexer
42 */
43 protected $indexer;
44
45 /**
46 * A name of the temporary file
47 *
48 * @var string
49 */
50 protected $temporaryFileName = '';
51
52 /**
53 * Sets up the test
54 *
55 * @return void
56 */
57 public function setUp() {
58 $this->indexer = t3lib_div::makeInstance('tx_indexedsearch_indexer');
59 }
60
61 /**
62 * Explicitly cleans up the indexer object to prevent any memory leaks
63 *
64 * @return void
65 */
66 public function tearDown() {
67 unset($this->indexer);
68 if ($this->temporaryFileName) {
69 @unlink($this->temporaryFileName);
70 }
71 }
72
73 /**
74 * Checks that non-existing files are not returned
75 *
76 * @return void
77 */
78 public function testNonExistingLocalPath() {
79 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
80 $result = $this->indexer->extractHyperLinks($html);
81
82 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
83 $this->assertEquals($result[0]['localPath'], '', 'Local path is incorrect');
84 }
85
86 /**
87 * Checks that using t3vars returns correct file
88 *
89 * @return void
90 */
91 public function testLocalPathWithT3Vars() {
92 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
93 $html = 'test <a href="testfile">test</a> test';
94 $savedValue = $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'];
95 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
96 t3lib_div::shortMD5('testfile') => $this->temporaryFileName
97 );
98 $result = $this->indexer->extractHyperLinks($html);
99 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = $savedValue;
100
101 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
102 $this->assertEquals($result[0]['localPath'], $this->temporaryFileName, 'Local path is incorrect');
103 }
104
105 /**
106 * Tests that a path with baseURL
107 *
108 * @return void
109 */
110 public function testLocalPathWithSiteURL() {
111 $baseURL = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
112 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
113 $result = $this->indexer->extractHyperLinks($html);
114
115 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
116 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
117 }
118
119 /**
120 * Tests absolute path
121 *
122 * @return void
123 */
124 public function testRelativeLocalPath() {
125 $html = 'test <a href="index.php">test</a> test';
126 $result = $this->indexer->extractHyperLinks($html);
127 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
128 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
129 }
130
131 /**
132 * Tests absolute path.
133 *
134 * @return void
135 */
136 public function testAbsoluteLocalPath() {
137 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
138 $html = 'test <a href="' . $path . 'index.php">test</a> test';
139 $result = $this->indexer->extractHyperLinks($html);
140
141 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
142 $this->assertEquals($result[0]['localPath'], PATH_typo3 . 'index.php', 'Local path is incorrect');
143 }
144
145 /**
146 * Tests that a path with the absRefPrefix returns correct result
147 *
148 * @return void
149 */
150 public function testLocalPathWithAbsRefPrefix() {
151 $absRefPrefix = '/' . md5(uniqid(''));
152 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
153 $savedPrefix = $GLOBALS['TSFE']->config['config']['absRefPrefix'];
154 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
155 $result = $this->indexer->extractHyperLinks($html);
156 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $savedPrefix;
157
158 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
159 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
160 }
161
162 /**
163 * Checks that base HREF is extracted correctly
164 *
165 * @return void
166 */
167 public function textExtractBaseHref() {
168 $baseHref = 'http://example.com/';
169 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
170 $result = $this->indexer->extractHyperLinks($html);
171
172 $this->assertEquals($baseHref, $result, 'Incorrect base href was extracted');
173 }
174 }
175 ?>