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