[TASK] Update copyright year to 2013
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Tests / Unit / IndexerTest.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Tests\Unit;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2013 Dmitry Dulepov (dmitry.dulepov@gmail.com)
8 * All rights reserved
9 *
10 * This script is part of the Typo3 project. The Typo3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * This class contains unit tests for the indexer
29 *
30 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
31 * @author Christian Kuhn <lolli@schwarzbu.ch>
32 */
33 class IndexerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 /**
36 * Enable backup of global and system variables
37 *
38 * @var boolean
39 */
40 protected $backupGlobals = TRUE;
41
42 /**
43 * Indexer instance
44 *
45 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
46 */
47 protected $fixture = NULL;
48
49 /**
50 * A name of the temporary file
51 *
52 * @var string
53 */
54 protected $temporaryFileName = '';
55
56 /**
57 * Sets up the test
58 */
59 public function setUp() {
60 $this->fixture = $this->getMock('TYPO3\CMS\IndexedSearch\Indexer', array('dummy'));
61 }
62
63 /**
64 * Explicitly clean up the indexer object to prevent any memory leaks
65 */
66 public function tearDown() {
67 $this->fixture = NULL;
68 if ($this->temporaryFileName) {
69 @unlink($this->temporaryFileName);
70 }
71 }
72
73 /**
74 * @test
75 */
76 public function extractHyperLinksDoesNotReturnNonExistingLocalPath() {
77 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
78 $result = $this->fixture->extractHyperLinks($html);
79 $this->assertEquals(1, count($result));
80 $this->assertEquals('', $result[0]['localPath']);
81 }
82
83 /**
84 * @test
85 */
86 public function extractHyperLinksReturnsCorrectFileUsingT3Vars() {
87 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
88 $html = 'test <a href="testfile">test</a> test';
89 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
90 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $this->temporaryFileName,
91 );
92 $result = $this->fixture->extractHyperLinks($html);
93 $this->assertEquals(1, count($result));
94 $this->assertEquals($this->temporaryFileName, $result[0]['localPath']);
95 }
96
97 /**
98 * @test
99 */
100 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl() {
101 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
102 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
103 $result = $this->fixture->extractHyperLinks($html);
104 $this->assertEquals(1, count($result));
105 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
106 }
107
108 /**
109 * @test
110 */
111 public function extractHyperLinksFindsCorrectPathWithAbsolutePath() {
112 $html = 'test <a href="index.php">test</a> test';
113 $result = $this->fixture->extractHyperLinks($html);
114 $this->assertEquals(1, count($result));
115 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
116 }
117
118 /**
119 * @test
120 */
121 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory() {
122 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
123 $html = 'test <a href="' . $path . 'index.php">test</a> test';
124 $result = $this->fixture->extractHyperLinks($html);
125 $this->assertEquals(1, count($result));
126 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
127 }
128
129 /**
130 * @test
131 */
132 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix() {
133 $absRefPrefix = '/' . md5(uniqid(''));
134 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
135 $GLOBALS['TSFE'] = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
136 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
137 $result = $this->fixture->extractHyperLinks($html);
138 $this->assertEquals(1, count($result));
139 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
140 }
141
142 /**
143 * @test
144 */
145 public function extractBaseHrefExtractsBaseHref() {
146 $baseHref = 'http://example.com/';
147 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
148 $result = $this->fixture->extractBaseHref($html);
149 $this->assertEquals($baseHref, $result);
150 }
151
152 }
153
154
155 ?>