[TASK] EXT:indexed_search tests
[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 class contains unit tests for the indexer
6 *
7 * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
8 * @author Christian Kuhn <lolli@schwarzbu.ch>
9 * @package TYPO3
10 * @subpackage tx_indexedsearch
11 */
12 class IndexerTest extends \tx_phpunit_testcase {
13
14 /**
15 * Enable backup of global and system variables
16 *
17 * @var boolean
18 */
19 protected $backupGlobals = TRUE;
20
21 /**
22 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
23 * because resource types cannot be handled during serializing
24 *
25 * @var array
26 */
27 protected $backupGlobalsBlacklist = array('TYPO3_DB');
28
29 /**
30 * Indexer instance
31 *
32 * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
33 */
34 protected $fixture = NULL;
35
36 /**
37 * A name of the temporary file
38 *
39 * @var string
40 */
41 protected $temporaryFileName = '';
42
43 /**
44 * Sets up the test
45 */
46 public function setUp() {
47 $this->fixture = $this->getMock('TYPO3\CMS\IndexedSearch\Indexer', array('dummy'));
48 }
49
50 /**
51 * Explicitly clean up the indexer object to prevent any memory leaks
52 */
53 public function tearDown() {
54 $this->fixture = NULL;
55 if ($this->temporaryFileName) {
56 @unlink($this->temporaryFileName);
57 }
58 }
59
60 /**
61 * @test
62 */
63 public function extractHyperLinksDoesNotReturnNonExistingLocalPath() {
64 $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
65 $result = $this->fixture->extractHyperLinks($html);
66 $this->assertEquals(1, count($result));
67 $this->assertEquals('', $result[0]['localPath']);
68 }
69
70 /**
71 * @test
72 */
73 public function extractHyperLinksReturnsCorrectFileUsingT3Vars() {
74 $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
75 $html = 'test <a href="testfile">test</a> test';
76 $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
77 \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $this->temporaryFileName,
78 );
79 $result = $this->fixture->extractHyperLinks($html);
80 $this->assertEquals(1, count($result));
81 $this->assertEquals($this->temporaryFileName, $result[0]['localPath']);
82 }
83
84 /**
85 * @test
86 */
87 public function extractHyperLinksRecurnsCorrectPathWithBaseUrl() {
88 $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
89 $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
90 $result = $this->fixture->extractHyperLinks($html);
91 $this->assertEquals(1, count($result));
92 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
93 }
94
95 /**
96 * @test
97 */
98 public function extractHyperLinksFindsCorrectPathWithAbsolutePath() {
99 $html = 'test <a href="index.php">test</a> test';
100 $result = $this->fixture->extractHyperLinks($html);
101 $this->assertEquals(1, count($result));
102 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
103 }
104
105 /**
106 * @test
107 */
108 public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory() {
109 $path = substr(PATH_typo3, strlen(PATH_site) - 1);
110 $html = 'test <a href="' . $path . 'index.php">test</a> test';
111 $result = $this->fixture->extractHyperLinks($html);
112 $this->assertEquals(1, count($result));
113 $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
114 }
115
116 /**
117 * @test
118 */
119 public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix() {
120 $absRefPrefix = '/' . md5(uniqid(''));
121 $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
122 $GLOBALS['TSFE'] = $this->getMock('TYPO3\\CMS\\Frontend\\Controller\\TypoScriptFrontendController', array(), array(), '', FALSE);
123 $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
124 $result = $this->fixture->extractHyperLinks($html);
125 $this->assertEquals(1, count($result));
126 $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
127 }
128
129 /**
130 * @test
131 */
132 public function extractBaseHrefExtractsBaseHref() {
133 $baseHref = 'http://example.com/';
134 $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
135 $result = $this->fixture->extractBaseHref($html);
136 $this->assertEquals($baseHref, $result);
137 }
138
139 }
140
141
142 ?>