PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/typo3/sysext/indexed_search/tests/tx_indexedsearch_indexerTest.php

https://github.com/foxsoft/typo3v4core
PHP | 183 lines | 73 code | 21 blank | 89 comment | 3 complexity | a564f702ff4f75f581b6a6f5d92e5ff7 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. *
  5. * (c) 2010 Dmitry Dulepov (dmitry.dulepov@gmail.com)
  6. * All rights reserved
  7. *
  8. * This script is part of the Typo3 project. The Typo3 project is
  9. * free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * The GNU General Public License can be found at
  15. * http://www.gnu.org/copyleft/gpl.html.
  16. *
  17. * This script is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * This copyright notice MUST APPEAR in all copies of the script!
  23. ***************************************************************/
  24. /**
  25. * $Id$
  26. *
  27. */
  28. require_once(t3lib_extMgm::extPath('indexed_search', 'class.indexer.php'));
  29. /**
  30. * This class contains unit tests for the indexer
  31. *
  32. * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
  33. * @author Christian Kuhn <lolli@schwarzbu.ch>
  34. * @package TYPO3
  35. * @subpackage tx_indexedsearch
  36. */
  37. class tx_indexedsearch_indexerTest extends tx_phpunit_testcase {
  38. /**
  39. * Indexer instance
  40. *
  41. * @var tx_indexedsearch_indexer
  42. */
  43. protected $indexer;
  44. /**
  45. * A name of the temporary file
  46. *
  47. * @var string
  48. */
  49. protected $temporaryFileName = '';
  50. /**
  51. * Sets up the test
  52. *
  53. * @return void
  54. */
  55. public function setUp() {
  56. $this->indexer = t3lib_div::makeInstance('tx_indexedsearch_indexer');
  57. }
  58. /**
  59. * Explicitly cleans up the indexer object to prevent any memory leaks
  60. *
  61. * @return void
  62. */
  63. public function tearDown() {
  64. unset($this->indexer);
  65. if ($this->temporaryFileName) {
  66. @unlink($this->temporaryFileName);
  67. }
  68. }
  69. /**
  70. * Checks that non-existing files are not returned
  71. *
  72. * @return void
  73. */
  74. public function testNonExistingLocalPath() {
  75. $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
  76. $result = $this->indexer->extractHyperLinks($html);
  77. $this->assertEquals(1, count($result), 'Wrong number of parsed links');
  78. $this->assertEquals($result[0]['localPath'], '', 'Local path is incorrect');
  79. }
  80. /**
  81. * Checks that using t3vars returns correct file
  82. *
  83. * @return void
  84. */
  85. public function testLocalPathWithT3Vars() {
  86. $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
  87. $html = 'test <a href="testfile">test</a> test';
  88. $savedValue = $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'];
  89. $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
  90. t3lib_div::shortMD5('testfile') => $this->temporaryFileName
  91. );
  92. $result = $this->indexer->extractHyperLinks($html);
  93. $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = $savedValue;
  94. $this->assertEquals(1, count($result), 'Wrong number of parsed links');
  95. $this->assertEquals($result[0]['localPath'], $this->temporaryFileName, 'Local path is incorrect');
  96. }
  97. /**
  98. * Tests that a path with baseURL
  99. *
  100. * @return void
  101. */
  102. public function testLocalPathWithSiteURL() {
  103. $baseURL = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
  104. $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
  105. $result = $this->indexer->extractHyperLinks($html);
  106. $this->assertEquals(1, count($result), 'Wrong number of parsed links');
  107. $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
  108. }
  109. /**
  110. * Tests absolute path
  111. *
  112. * @return void
  113. */
  114. public function testRelativeLocalPath() {
  115. $html = 'test <a href="index.php">test</a> test';
  116. $result = $this->indexer->extractHyperLinks($html);
  117. $this->assertEquals(1, count($result), 'Wrong number of parsed links');
  118. $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
  119. }
  120. /**
  121. * Tests absolute path.
  122. *
  123. * @return void
  124. */
  125. public function testAbsoluteLocalPath() {
  126. $path = substr(PATH_typo3, strlen(PATH_site) - 1);
  127. $html = 'test <a href="' . $path . 'index.php">test</a> test';
  128. $result = $this->indexer->extractHyperLinks($html);
  129. $this->assertEquals(1, count($result), 'Wrong number of parsed links');
  130. $this->assertEquals($result[0]['localPath'], PATH_typo3 . 'index.php', 'Local path is incorrect');
  131. }
  132. /**
  133. * Tests that a path with the absRefPrefix returns correct result
  134. *
  135. * @return void
  136. */
  137. public function testLocalPathWithAbsRefPrefix() {
  138. $absRefPrefix = '/' . md5(uniqid(''));
  139. $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
  140. $savedPrefix = $GLOBALS['TSFE']->config['config']['absRefPrefix'];
  141. $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
  142. $result = $this->indexer->extractHyperLinks($html);
  143. $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $savedPrefix;
  144. $this->assertEquals(1, count($result), 'Wrong number of parsed links');
  145. $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
  146. }
  147. /**
  148. * Checks that base HREF is extracted correctly
  149. *
  150. * @return void
  151. */
  152. public function textExtractBaseHref() {
  153. $baseHref = 'http://example.com/';
  154. $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
  155. $result = $this->indexer->extractHyperLinks($html);
  156. $this->assertEquals($baseHref, $result, 'Incorrect base href was extracted');
  157. }
  158. }
  159. if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/indexed_search/tests/class.tx_indexedsearch_indexer_testcase.php']) {
  160. include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/indexed_search/tests/class.tx_indexedsearch_indexer_testcase.php']);
  161. }
  162. ?>