[!!!][TASK] Remove last usages of $GLOBALS[T3_VAR]
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Tests / Unit / IndexerTest.php
index 64998de..0e25159 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+declare(strict_types = 1);
 namespace TYPO3\CMS\IndexedSearch\Tests\Unit;
 
 /*
@@ -14,106 +15,199 @@ namespace TYPO3\CMS\IndexedSearch\Tests\Unit;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Core\Environment;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+use TYPO3\CMS\IndexedSearch\Indexer;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
 /**
- * This class contains unit tests for the indexer
- *
- * @author Dmitry Dulepov <dmitry.dulepov@gmail.com>
- * @author Christian Kuhn <lolli@schwarzbu.ch>
+ * Test case
  */
-class IndexerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
-
-       /**
-        * Indexer instance
-        *
-        * @var \PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\IndexedSearch\Indexer
-        */
-       protected $subject = NULL;
-
-       /**
-        * Sets up the test
-        */
-       protected function setUp() {
-               $this->subject = $this->getMock(\TYPO3\CMS\IndexedSearch\Indexer::class, array('dummy'));
-       }
-
-       /**
-        * @test
-        */
-       public function extractHyperLinksDoesNotReturnNonExistingLocalPath() {
-               $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
-               $result = $this->subject->extractHyperLinks($html);
-               $this->assertEquals(1, count($result));
-               $this->assertEquals('', $result[0]['localPath']);
-       }
-
-       /**
-        * @test
-        */
-       public function extractHyperLinksReturnsCorrectFileUsingT3Vars() {
-               $temporaryFileName = tempnam(PATH_site . 'typo3temp/', 't3unit-');
-               $this->testFilesToDelete[] = $temporaryFileName;
-               $html = 'test <a href="testfile">test</a> test';
-               $GLOBALS['T3_VAR']['ext']['indexed_search']['indexLocalFiles'] = array(
-                       \TYPO3\CMS\Core\Utility\GeneralUtility::shortMD5('testfile') => $temporaryFileName,
-               );
-               $result = $this->subject->extractHyperLinks($html);
-               $this->assertEquals(1, count($result));
-               $this->assertEquals($temporaryFileName, $result[0]['localPath']);
-       }
-
-       /**
-        * @test
-        */
-       public function extractHyperLinksRecurnsCorrectPathWithBaseUrl() {
-               $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
-               $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
-               $result = $this->subject->extractHyperLinks($html);
-               $this->assertEquals(1, count($result));
-               $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
-       }
-
-       /**
-        * @test
-        */
-       public function extractHyperLinksFindsCorrectPathWithAbsolutePath() {
-               $html = 'test <a href="index.php">test</a> test';
-               $result = $this->subject->extractHyperLinks($html);
-               $this->assertEquals(1, count($result));
-               $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
-       }
-
-       /**
-        * @test
-        */
-       public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory() {
-               $path = substr(PATH_typo3, strlen(PATH_site) - 1);
-               $html = 'test <a href="' . $path . 'index.php">test</a> test';
-               $result = $this->subject->extractHyperLinks($html);
-               $this->assertEquals(1, count($result));
-               $this->assertEquals(PATH_typo3 . 'index.php', $result[0]['localPath']);
-       }
-
-       /**
-        * @test
-        */
-       public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix() {
-               $absRefPrefix = '/' . $this->getUniqueId();
-               $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
-               $GLOBALS['TSFE'] = $this->getMock(\TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController::class, array(), array(), '', FALSE);
-               $GLOBALS['TSFE']->config['config']['absRefPrefix'] = $absRefPrefix;
-               $result = $this->subject->extractHyperLinks($html);
-               $this->assertEquals(1, count($result));
-               $this->assertEquals(PATH_site . 'index.php', $result[0]['localPath']);
-       }
-
-       /**
-        * @test
-        */
-       public function extractBaseHrefExtractsBaseHref() {
-               $baseHref = 'http://example.com/';
-               $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
-               $result = $this->subject->extractBaseHref($html);
-               $this->assertEquals($baseHref, $result);
-       }
+class IndexerTest extends UnitTestCase
+{
+    /**
+     * @var bool Reset singletons created by subject
+     */
+    protected $resetSingletonInstances = true;
+
+    /**
+     * @test
+     */
+    public function extractHyperLinksDoesNotReturnNonExistingLocalPath()
+    {
+        $html = 'test <a href="' . $this->getUniqueId() . '">test</a> test';
+        $subject = new Indexer();
+        $result = $subject->extractHyperLinks($html);
+        $this->assertEquals(1, count($result));
+        $this->assertEquals('', $result[0]['localPath']);
+    }
+
+    /**
+     * @test
+     */
+    public function extractHyperLinksReturnsCorrectPathWithBaseUrl()
+    {
+        $baseURL = \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('TYPO3_SITE_URL');
+        $html = 'test <a href="' . $baseURL . 'index.php">test</a> test';
+        $subject = new Indexer();
+        $result = $subject->extractHyperLinks($html);
+        $this->assertEquals(1, count($result));
+        $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
+    }
+
+    /**
+     * @test
+     */
+    public function extractHyperLinksFindsCorrectPathWithAbsolutePath()
+    {
+        $html = 'test <a href="index.php">test</a> test';
+        $subject = new Indexer();
+        $result = $subject->extractHyperLinks($html);
+        $this->assertEquals(1, count($result));
+        $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
+    }
+
+    /**
+     * @test
+     */
+    public function extractHyperLinksFindsCorrectPathForPathWithinTypo3Directory()
+    {
+        $html = 'test <a href="typo3/index.php">test</a> test';
+        $subject = new Indexer();
+        $result = $subject->extractHyperLinks($html);
+        $this->assertEquals(1, count($result));
+        $this->assertEquals(Environment::getPublicPath() . '/typo3/index.php', $result[0]['localPath']);
+    }
+
+    /**
+     * @test
+     */
+    public function extractHyperLinksFindsCorrectPathUsingAbsRefPrefix()
+    {
+        $absRefPrefix = '/' . $this->getUniqueId();
+        $html = 'test <a href="' . $absRefPrefix . 'index.php">test</a> test';
+        $GLOBALS['TSFE'] = $this->createMock(TypoScriptFrontendController::class);
+        $config = [
+            'config' => [
+                'absRefPrefix' => $absRefPrefix,
+            ],
+        ];
+        $GLOBALS['TSFE']->config = $config;
+        $subject = new Indexer();
+        $result = $subject->extractHyperLinks($html);
+        $this->assertEquals(1, count($result));
+        $this->assertEquals(Environment::getPublicPath() . '/index.php', $result[0]['localPath']);
+    }
+
+    /**
+     * @test
+     */
+    public function extractBaseHrefExtractsBaseHref()
+    {
+        $baseHref = 'http://example.com/';
+        $html = '<html><head><Base Href="' . $baseHref . '" /></head></html>';
+        $subject = new Indexer();
+        $result = $subject->extractBaseHref($html);
+        $this->assertEquals($baseHref, $result);
+    }
+
+    /**
+     * Tests whether indexer can extract content between "TYPO3SEARCH_begin" and "TYPO3SEARCH_end" markers
+     *
+     * @test
+     */
+    public function typoSearchTagsRemovesBodyContentOutsideMarkers()
+    {
+        $body = <<<EOT
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
+<title>Some Title</title>
+<link href='css/normalize.css' rel='stylesheet' type='text/css'/>
+</head>
+<body>
+<div>
+<div class="non_searchable">
+    not searchable content
+</div>
+<!--TYPO3SEARCH_begin-->
+<div class="searchable">
+    lorem ipsum
+</div>
+<!--TYPO3SEARCH_end-->
+<div class="non_searchable">
+    not searchable content
+</div>
+</body>
+</html>
+EOT;
+        $expected = <<<EOT
+
+<div class="searchable">
+    lorem ipsum
+</div>
+
+EOT;
+
+        $subject = new Indexer();
+        $result = $subject->typoSearchTags($body);
+        $this->assertTrue($result);
+        $this->assertEquals($expected, $body);
+    }
+
+    /**
+     * Tests whether indexer can extract content between multiple pairs of "TYPO3SEARCH" markers
+     *
+     * @test
+     */
+    public function typoSearchTagsHandlesMultipleMarkerPairs()
+    {
+        $body = <<<EOT
+<html>
+<head>
+<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
+<title>Some Title</title>
+<link href='css/normalize.css' rel='stylesheet' type='text/css'/>
+</head>
+<body>
+<div>
+<div class="non_searchable">
+    not searchable content
+</div>
+<!--TYPO3SEARCH_begin-->
+<div class="searchable">
+    lorem ipsum
+</div>
+<!--TYPO3SEARCH_end-->
+<div class="non_searchable">
+    not searchable content
+</div>
+<!--TYPO3SEARCH_begin-->
+<div class="searchable">
+    lorem ipsum2
+</div>
+<!--TYPO3SEARCH_end-->
+<div class="non_searchable">
+    not searchable content
+</div>
+</body>
+</html>
+EOT;
+        $expected = <<<EOT
+
+<div class="searchable">
+    lorem ipsum
+</div>
+
+<div class="searchable">
+    lorem ipsum2
+</div>
+
+EOT;
 
+        $subject = new Indexer();
+        $result = $subject->typoSearchTags($body);
+        $this->assertTrue($result);
+        $this->assertEquals($expected, $body);
+    }
 }