[TASK] Re-work/simplify copyright header in PHP files - Part 8
[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 file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * This class contains unit tests for the indexer
19 *
20 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
21 * @author Christian Kuhn <lolli@schwarzbu.ch>
22 */
23 class IndexerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
24
25 /**
26 * Indexer instance
27 *
28 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
29 */
30 protected $fixture = NULL;
31
32 /**
33 * A name of the temporary file
34 *
35 * @var string
36 */
37 protected $temporaryFileName = '';
38
39 /**
40 * Sets up the test
41 */
42 public function setUp() {
43 $this->fixture = $this->getMock('TYPO3\CMS\IndexedSearch\Indexer', array('dummy'));
44 }
45
46 /**
47 * Explicitly clean up the indexer object to prevent any memory leaks
48 */
49 public function tearDown() {
50 if ($this->temporaryFileName) {
51 @unlink($this->temporaryFileName);
52 }
53 parent::tearDown();
54 }
55
56 /**
57 * @test
58 */
59 public function extractHyperLinksDoesNotReturnNonExistingLocalPath() {
60 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
61 $result = $this->fixture->extractHyperLinks($html);
62 $this->assertEquals(1, count($result));
63 $this->assertEquals('', $result[0]['localPath']);
64 }
65
66 /**
67 * @test
68 */
69 public function extractHyperLinksReturnsCorrectFileUsingT3Vars() {
70 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
71 $html = 'test <a href="testfile">test</a> test';
72 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
73 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $this->temporaryFileName,
74 );
75 $result = $this->fixture->extractHyperLinks($html);
76 $this->assertEquals(1, count($result));
77 $this->assertEquals($this->temporaryFileName, $result[0]['localPath']);
78 }
79
80 /**
81 * @test
82 */
83 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl() {
84 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
85 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
86 $result = $this->fixture->extractHyperLinks($html);
87 $this->assertEquals(1, count($result));
88 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
89 }
90
91 /**
92 * @test
93 */
94 public function extractHyperLinksFindsCorrectPathWithAbsolutePath() {
95 $html = 'test <a href="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 extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory() {
105 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
106 $html = 'test <a href="' . $path . 'index.php">test</a> test';
107 $result = $this->fixture->extractHyperLinks($html);
108 $this->assertEquals(1, count($result));
109 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
110 }
111
112 /**
113 * @test
114 */
115 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix() {
116 $absRefPrefix = '/' . md5(uniqid(''));
117 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
118 $GLOBALS['TSFE'] = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
119 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
120 $result = $this->fixture->extractHyperLinks($html);
121 $this->assertEquals(1, count($result));
122 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
123 }
124
125 /**
126 * @test
127 */
128 public function extractBaseHrefExtractsBaseHref() {
129 $baseHref = 'http://example.com/';
130 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
131 $result = $this->fixture->extractBaseHref($html);
132 $this->assertEquals($baseHref, $result);
133 }
134
135 }