Cleanup: Updated copyright comments
[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 * $Id$
26 *
27 */
28
29 require_once(t3lib_extMgm::extPath('indexed_search', 'class.indexer.php'));
30
31 /**
32 * This class contains unit tests for the indexer
33 *
34 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
35 * @author Christian Kuhn <lolli@schwarzbu.ch>
36 * @package TYPO3
37 * @subpackage tx_indexedsearch
38 */
39 class tx_indexedsearch_indexerTest extends tx_phpunit_testcase {
40
41 /**
42 * Indexer instance
43 *
44 * @var tx_indexedsearch_indexer
45 */
46 protected $indexer;
47
48 /**
49 * A name of the temporary file
50 *
51 * @var string
52 */
53 protected $temporaryFileName = '';
54
55 /**
56 * Sets up the test
57 *
58 * @return void
59 */
60 public function setUp() {
61 $this->indexer = t3lib_div::makeInstance('tx_indexedsearch_indexer');
62 }
63
64 /**
65 * Explicitly cleans up the indexer object to prevent any memory leaks
66 *
67 * @return void
68 */
69 public function tearDown() {
70 unset($this->indexer);
71 if ($this->temporaryFileName) {
72 @unlink($this->temporaryFileName);
73 }
74 }
75
76 /**
77 * Checks that non-existing files are not returned
78 *
79 * @return void
80 */
81 public function testNonExistingLocalPath() {
82 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
83 $result = $this->indexer->extractHyperLinks($html);
84
85 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
86 $this->assertEquals($result[0]['localPath'], '', 'Local path is incorrect');
87 }
88
89 /**
90 * Checks that using t3vars returns correct file
91 *
92 * @return void
93 */
94 public function testLocalPathWithT3Vars() {
95 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
96 $html = 'test <a href="testfile">test</a> test';
97 $savedValue = $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'];
98 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
99 t3lib_div::shortMD5('testfile') => $this->temporaryFileName
100 );
101 $result = $this->indexer->extractHyperLinks($html);
102 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = $savedValue;
103
104 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
105 $this->assertEquals($result[0]['localPath'], $this->temporaryFileName, 'Local path is incorrect');
106 }
107
108 /**
109 * Tests that a path with baseURL
110 *
111 * @return void
112 */
113 public function testLocalPathWithSiteURL() {
114 $baseURL = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
115 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
116 $result = $this->indexer->extractHyperLinks($html);
117
118 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
119 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
120 }
121
122 /**
123 * Tests absolute path
124 *
125 * @return void
126 */
127 public function testRelativeLocalPath() {
128 $html = 'test <a href="index.php">test</a> test';
129 $result = $this->indexer->extractHyperLinks($html);
130 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
131 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
132 }
133
134 /**
135 * Tests absolute path.
136 *
137 * @return void
138 */
139 public function testAbsoluteLocalPath() {
140 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
141 $html = 'test <a href="' . $path . 'index.php">test</a> test';
142 $result = $this->indexer->extractHyperLinks($html);
143
144 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
145 $this->assertEquals($result[0]['localPath'], PATH_typo3 . 'index.php', 'Local path is incorrect');
146 }
147
148 /**
149 * Tests that a path with the absRefPrefix returns correct result
150 *
151 * @return void
152 */
153 public function testLocalPathWithAbsRefPrefix() {
154 $absRefPrefix = '/' . md5(uniqid(''));
155 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
156 $savedPrefix = $GLOBALS['TSFE']->config['config']['absRefPrefix'];
157 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
158 $result = $this->indexer->extractHyperLinks($html);
159 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $savedPrefix;
160
161 $this->assertEquals(1, count($result), 'Wrong number of parsed links');
162 $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
163 }
164
165 /**
166 * Checks that base HREF is extracted correctly
167 *
168 * @return void
169 */
170 public function textExtractBaseHref() {
171 $baseHref = 'http://example.com/';
172 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
173 $result = $this->indexer->extractHyperLinks($html);
174
175 $this->assertEquals($baseHref, $result, 'Incorrect base href was extracted');
176 }
177 }
178
179 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/indexed_search/tests/class.tx_indexedsearch_indexer_testcase.php'])) {
180 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/indexed_search/tests/class.tx_indexedsearch_indexer_testcase.php']);
181 }
182
183 ?>