[TASK] Merge submodule linkvalidator into core
[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 * Indexer instance
37 *
38 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
39 */
40 protected $fixture = NULL;
41
42 /**
43 * A name of the temporary file
44 *
45 * @var string
46 */
47 protected $temporaryFileName = '';
48
49 /**
50 * Sets up the test
51 */
52 public function setUp() {
53 $this->fixture = $this->getMock('TYPO3\CMS\IndexedSearch\Indexer', array('dummy'));
54 }
55
56 /**
57 * Explicitly clean up the indexer object to prevent any memory leaks
58 */
59 public function tearDown() {
60 $this->fixture = NULL;
61 if ($this->temporaryFileName) {
62 @unlink($this->temporaryFileName);
63 }
64 }
65
66 /**
67 * @test
68 */
69 public function extractHyperLinksDoesNotReturnNonExistingLocalPath() {
70 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
71 $result = $this->fixture->extractHyperLinks($html);
72 $this->assertEquals(1, count($result));
73 $this->assertEquals('', $result[0]['localPath']);
74 }
75
76 /**
77 * @test
78 */
79 public function extractHyperLinksReturnsCorrectFileUsingT3Vars() {
80 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
81 $html = 'test <a href="testfile">test</a> test';
82 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
83 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $this->temporaryFileName,
84 );
85 $result = $this->fixture->extractHyperLinks($html);
86 $this->assertEquals(1, count($result));
87 $this->assertEquals($this->temporaryFileName, $result[0]['localPath']);
88 }
89
90 /**
91 * @test
92 */
93 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl() {
94 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
95 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
96 $result = $this->fixture->extractHyperLinks($html);
97 $this->assertEquals(1, count($result));
98 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
99 }
100
101 /**
102 * @test
103 */
104 public function extractHyperLinksFindsCorrectPathWithAbsolutePath() {
105 $html = 'test <a href="index.php">test</a> test';
106 $result = $this->fixture->extractHyperLinks($html);
107 $this->assertEquals(1, count($result));
108 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
109 }
110
111 /**
112 * @test
113 */
114 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory() {
115 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
116 $html = 'test <a href="' . $path . 'index.php">test</a> test';
117 $result = $this->fixture->extractHyperLinks($html);
118 $this->assertEquals(1, count($result));
119 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
120 }
121
122 /**
123 * @test
124 */
125 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix() {
126 $absRefPrefix = '/' . md5(uniqid(''));
127 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
128 $GLOBALS['TSFE'] = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
129 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
130 $result = $this->fixture->extractHyperLinks($html);
131 $this->assertEquals(1, count($result));
132 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
133 }
134
135 /**
136 * @test
137 */
138 public function extractBaseHrefExtractsBaseHref() {
139 $baseHref = 'http://example.com/';
140 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
141 $result = $this->fixture->extractBaseHref($html);
142 $this->assertEquals($baseHref, $result);
143 }
144
145 }
146
147
148 ?>