Passed
Branch master (b37043)
by Timo
11:18
created

IndexServiceTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 102
Duplicated Lines 13.73 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 14
loc 102
rs 10
c 1
b 0
f 0
wmc 4
lcom 1
cbo 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace ApacheSolrForTypo3\Solr\Tests\Integration\Domain\Index;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2015 Timo Schmidt <[email protected]>
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 2 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use ApacheSolrForTypo3\Solr\Domain\Index\IndexService;
29
use ApacheSolrForTypo3\Solr\IndexQueue\Indexer;
30
use ApacheSolrForTypo3\Solr\IndexQueue\Queue;
31
use ApacheSolrForTypo3\Solr\Site;
32
use ApacheSolrForTypo3\Solr\System\Environment\CliEnvironment;
33
use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest;
34
use ApacheSolrForTypo3\Solr\IndexQueue\RecordMonitor;
35
use TYPO3\CMS\Backend\Utility\BackendUtility;
36
use TYPO3\CMS\Core\Utility\GeneralUtility;
37
38
/**
39
 * Testcase for the record indexer
40
 *
41
 * @author Timo Schmidt
42
 * @package TYPO3
43
 * @subpackage solr
44
 */
45
class IndexServiceTest extends IntegrationTest
46
{
47
48
    /**
49
     * @var Queue
50
     */
51
    protected $indexQueue;
52
53
    /**
54
     * @return void
55
     */
56
    public function setUp()
57
    {
58
        parent::setUp();
59
        $this->indexQueue = GeneralUtility::makeInstance(Queue::class);
60
61
        /** @var $beUser  \TYPO3\CMS\Core\Authentication\BackendUserAuthentication */
62
        $beUser = GeneralUtility::makeInstance('TYPO3\CMS\Core\Authentication\BackendUserAuthentication');
63
        $GLOBALS['BE_USER'] = $beUser;
64
65
        /** @var $languageService  \TYPO3\CMS\Lang\LanguageService */
66
        $languageService = GeneralUtility::makeInstance('TYPO3\CMS\Lang\LanguageService');
67
        $languageService->csConvObj = GeneralUtility::makeInstance('TYPO3\CMS\Core\Charset\CharsetConverter');
68
        $GLOBALS['LANG'] = $languageService;
69
    }
70
71
    /**
72
     * @param string $table
73
     * @param integer $uid
74
     * @return \Apache_Solr_Response
75
     */
76
    protected function addToIndexQueue($table, $uid)
77
    {
78
        // write an index queue item
79
        $this->indexQueue->updateItem($table, $uid, null, time());
80
    }
81
82
83
    public function canResolveAbsRefPrefixDataProvider()
84
    {
85
        return [
86
            'absRefPrefixIsAuto' => [
87
                'absRefPrefix' => 'auto',
88
                'expectedUrl' => '/index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a',
89
            ],
90
            'absRefPrefixIsSlash' => [
91
                'absRefPrefix' => 'slash',
92
                'expectedUrl' => '/index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a',
93
            ],
94
            'absRefPrefixIsFoo' => [
95
                'absRefPrefix' => 'foo',
96
                'expectedUrl' => '/foo/index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a',
97
            ],
98
            'absRefPrefixIsNone' => [
99
                'absRefPrefix' => 'none',
100
                'expectedUrl' => 'index.php?id=1&tx_ttnews%5Btt_news%5D=111&L=0&cHash=0c0f0e52711b08554a6658230061fb1a',
101
            ]
102
        ];
103
    }
104
    /**
105
     *
106
     * @dataProvider canResolveAbsRefPrefixDataProvider
107
     * @param string $absRefPrefix
108
     * @param string $expectedUrl
109
     * @test
110
     */
111
    public function canResolveAbsRefPrefix($absRefPrefix, $expectedUrl)
112
    {
113
        $this->cleanUpSolrServerAndAssertEmpty();
114
115
        // create fake extension database table and TCA
116
        $this->importDumpFromFixture('fake_extension2_table.sql');
117
        $GLOBALS['TCA']['tx_fakeextension_domain_model_bar'] = include($this->getFixturePath('fake_extension2_bar_tca.php'));
118
        $GLOBALS['TCA']['tx_fakeextension_domain_model_directrelated'] = include($this->getFixturePath('fake_extension2_directrelated_tca.php'));
119
120
        $this->importDataSetFromFixture('can_index_custom_record_absRefPrefix_'.$absRefPrefix.'.xml');
121
122
        $this->addToIndexQueue('tx_fakeextension_domain_model_bar', 111);
123
124
            /** @var  $cliEnvironment CliEnvironment */
125
        $cliEnvironment = GeneralUtility::makeInstance(CliEnvironment::class);
126
        $cliEnvironment->backup();
127
        $cliEnvironment->initialize(PATH_site);
128
129
        /** @var $indexService IndexService */
130
        $site = Site::getFirstAvailableSite();
131
        $indexService = GeneralUtility::makeInstance(IndexService::class, $site);
132
133
        // run the indexer
134
        $indexService->indexItems(1);
135
136
        $cliEnvironment->restore();
137
138
        // do we have the record in the index with the value from the mm relation?
139
        sleep(2);
140
        $solrContent = file_get_contents('http://localhost:8080/solr/core_en/select?q=*:*');
141
        $this->assertContains('"numFound":1', $solrContent, 'Could not index document into solr');
142
        $this->assertContains('"url":"'.$expectedUrl.'"', $solrContent, 'Generated unexpected url with absRefPrefix = auto');
143
        $this->assertNotContains('auto', $solrContent, 'absRefPrefix=auto was not resolved');
144
        $this->cleanUpSolrServerAndAssertEmpty();
145
    }
146
}
147