TYPO3 API  SVNRelease
tx_indexedsearch_indexerTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2010-2011 Dmitry Dulepov (dmitry.dulepov@gmail.com)
00006 *  All rights reserved
00007 *
00008 *  This script is part of the Typo3 project. The Typo3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 /**
00025  * $Id: tx_indexedsearch_indexerTest.php 10120 2011-01-18 20:03:36Z ohader $
00026  *
00027  */
00028 
00029 require_once(t3lib_extMgm::extPath('indexed_search', 'class.indexer.php'));
00030 
00031 /**
00032   * This class contains unit tests for the indexer
00033   *
00034   * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
00035   * @author Christian Kuhn <lolli@schwarzbu.ch>
00036   * @package TYPO3
00037   * @subpackage tx_indexedsearch
00038   */
00039 class tx_indexedsearch_indexerTest extends tx_phpunit_testcase {
00040 
00041     /**
00042      * Indexer instance
00043      *
00044      * @var tx_indexedsearch_indexer
00045      */
00046     protected $indexer;
00047 
00048     /**
00049      * A name of the temporary file
00050      *
00051      * @var string
00052      */
00053     protected $temporaryFileName = '';
00054 
00055     /**
00056      * Sets up the test
00057      *
00058      * @return void
00059      */
00060     public function setUp() {
00061         $this->indexer = t3lib_div::makeInstance('tx_indexedsearch_indexer');
00062     }
00063 
00064     /**
00065      * Explicitly cleans up the indexer object to prevent any memory leaks
00066      *
00067      * @return void
00068      */
00069     public function tearDown() {
00070         unset($this->indexer);
00071         if ($this->temporaryFileName) {
00072             @unlink($this->temporaryFileName);
00073         }
00074     }
00075 
00076     /**
00077      * Checks that non-existing files are not returned
00078      *
00079      * @return void
00080      */
00081     public function testNonExistingLocalPath() {
00082         $html = 'test <a href="' . md5(uniqid('')) . '">test</a> test';
00083         $result = $this->indexer->extractHyperLinks($html);
00084 
00085         $this->assertEquals(1, count($result), 'Wrong number of parsed links');
00086         $this->assertEquals($result[0]['localPath'], '', 'Local path is incorrect');
00087     }
00088 
00089     /**
00090      * Checks that using t3vars returns correct file
00091      *
00092      * @return void
00093      */
00094     public function testLocalPathWithT3Vars() {
00095         $this->temporaryFileName = tempnam(sys_get_temp_dir(), 't3unit-');
00096         $html = 'test <a href="testfile">test</a> test';
00097         $savedValue = $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'];
00098         $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
00099             t3lib_div::shortMD5('testfile') => $this->temporaryFileName
00100         );
00101         $result = $this->indexer->extractHyperLinks($html);
00102         $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = $savedValue;
00103 
00104         $this->assertEquals(1, count($result), 'Wrong number of parsed links');
00105         $this->assertEquals($result[0]['localPath'], $this->temporaryFileName, 'Local path is incorrect');
00106     }
00107 
00108     /**
00109      * Tests that a path with baseURL
00110      *
00111      * @return void
00112      */
00113     public function testLocalPathWithSiteURL() {
00114         $baseURL = t3lib_div::getIndpEnv('TYPO3_SITE_URL');
00115         $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
00116         $result = $this->indexer->extractHyperLinks($html);
00117 
00118         $this->assertEquals(1, count($result), 'Wrong number of parsed links');
00119         $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
00120     }
00121 
00122     /**
00123      * Tests absolute path
00124      *
00125      * @return void
00126      */
00127     public function testRelativeLocalPath() {
00128         $html = 'test <a href="index.php">test</a> test';
00129         $result = $this->indexer->extractHyperLinks($html);
00130         $this->assertEquals(1, count($result), 'Wrong number of parsed links');
00131         $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
00132     }
00133 
00134     /**
00135      * Tests absolute path.
00136      *
00137      * @return void
00138      */
00139     public function testAbsoluteLocalPath() {
00140         $path = substr(PATH_typo3, strlen(PATH_site) - 1);
00141         $html = 'test <a href="' . $path . 'index.php">test</a> test';
00142         $result = $this->indexer->extractHyperLinks($html);
00143 
00144         $this->assertEquals(1, count($result), 'Wrong number of parsed links');
00145         $this->assertEquals($result[0]['localPath'], PATH_typo3 . 'index.php', 'Local path is incorrect');
00146     }
00147 
00148     /**
00149      * Tests that a path with the absRefPrefix returns correct result
00150      *
00151      * @return void
00152      */
00153     public function testLocalPathWithAbsRefPrefix() {
00154         $absRefPrefix = '/' . md5(uniqid(''));
00155         $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
00156         $savedPrefix = $GLOBALS['TSFE']->config['config']['absRefPrefix'];
00157         $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
00158         $result = $this->indexer->extractHyperLinks($html);
00159         $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $savedPrefix;
00160 
00161         $this->assertEquals(1, count($result), 'Wrong number of parsed links');
00162         $this->assertEquals($result[0]['localPath'], PATH_site . 'index.php', 'Local path is incorrect');
00163     }
00164 
00165     /**
00166      * Checks that base HREF is extracted correctly
00167      *
00168      * @return void
00169      */
00170     public function textExtractBaseHref() {
00171         $baseHref = 'http://example.com/';
00172         $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
00173         $result = $this->indexer->extractHyperLinks($html);
00174 
00175         $this->assertEquals($baseHref, $result, 'Incorrect base href was extracted');
00176     }
00177 }
00178 
00179 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/indexed_search/tests/class.tx_indexedsearch_indexer_testcase.php'])) {
00180     include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/indexed_search/tests/class.tx_indexedsearch_indexer_testcase.php']);
00181 }
00182 
00183 ?>