Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Pull Request — master (#846)
by Sebastian
03:24
created

FunctionalTestCase::importSolrDocuments()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 18
rs 9.8333
1
<?php
2
3
namespace Kitodo\Dlf\Tests\Functional;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Kitodo\Dlf\Common\Solr;
7
use Symfony\Component\Yaml\Yaml;
8
use TYPO3\CMS\Core\Localization\LanguageService;
9
use TYPO3\CMS\Core\Utility\ArrayUtility;
10
use TYPO3\CMS\Core\Utility\GeneralUtility;
11
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
12
use TYPO3\CMS\Extbase\Object\ObjectManager;
13
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
14
15
/**
16
 * Base class for functional test cases. This provides some common configuration
17
 * and collects utility methods for functional tests.
18
 */
19
class FunctionalTestCase extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
20
{
21
    protected $testExtensionsToLoad = [
22
        'typo3conf/ext/dlf',
23
    ];
24
25
    protected $configurationToUseInTestInstance = [
26
        'SYS' => [
27
            'caching' => [
28
                'cacheConfigurations' => [
29
                    'tx_dlf_doc' => [
30
                        'backend' => \TYPO3\CMS\Core\Cache\Backend\NullBackend::class,
31
                    ],
32
                ],
33
            ],
34
        ],
35
        'EXTENSIONS' => [
36
            'dlf' => [], // = $this->getDlfConfiguration(), set in constructor
37
        ],
38
        'DB' => [
39
            'Connections' => [
40
                'Default' => [
41
                    // TODO: This is taken from the base class, minus "ONLY_FULL_GROUP_BY"; should probably rather be changed in DocumentRepository::getOaiDocumentList
42
                    'initCommands' => 'SET SESSION sql_mode = \'STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE\';',
43
                ],
44
            ],
45
        ],
46
    ];
47
48
    /**
49
     * By default, the testing framework wraps responses into a JSON object
50
     * that contains status code etc. as fields. Set this field to true to avoid
51
     * this behavior by not loading the json_response extension.
52
     *
53
     * @var bool
54
     */
55
    protected $disableJsonWrappedResponse = false;
56
57
    /** @var ObjectManager */
58
    protected $objectManager;
59
60
    /**
61
     * @var string
62
     */
63
    protected $baseUrl;
64
65
    /**
66
     * @var HttpClient
67
     */
68
    protected $httpClient;
69
70
    public function __construct()
71
    {
72
        parent::__construct();
73
74
        $this->configurationToUseInTestInstance['EXTENSIONS']['dlf'] = $this->getDlfConfiguration();
75
76
        if ($this->disableJsonWrappedResponse) {
77
            $this->frameworkExtensionsToLoad = array_filter($this->frameworkExtensionsToLoad, function ($ext) {
0 ignored issues
show
Deprecated Code introduced by
The property TYPO3\TestingFramework\C...ameworkExtensionsToLoad has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

77
            /** @scrutinizer ignore-deprecated */ $this->frameworkExtensionsToLoad = array_filter($this->frameworkExtensionsToLoad, function ($ext) {
Loading history...
78
                return $ext !== 'Resources/Core/Functional/Extensions/json_response';
79
            });
80
        }
81
    }
82
83
    public function setUp(): void
84
    {
85
        parent::setUp();
86
87
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
88
89
        $this->baseUrl = 'http://web:8000/public/typo3temp/var/tests/functional-' . $this->identifier . '/';
90
        $this->httpClient = new HttpClient([
91
            'base_uri' => $this->baseUrl,
92
            'http_errors' => false,
93
        ]);
94
95
        $this->addSiteConfig('dlf-testing', $this->baseUrl);
96
    }
97
98
    protected function getDlfConfiguration()
99
    {
100
        return [
101
            'fileGrpImages' => 'DEFAULT,MAX',
102
            'fileGrpThumbs' => 'THUMBS',
103
            'fileGrpDownload' => 'DOWNLOAD',
104
            'fileGrpFulltext' => 'FULLTEXT',
105
            'fileGrpAudio' => 'AUDIO',
106
107
            'solrFieldAutocomplete' => 'autocomplete',
108
            'solrFieldCollection' => 'collection',
109
            'solrFieldDefault' => 'default',
110
            'solrFieldFulltext' => 'fulltext',
111
            'solrFieldGeom' => 'geom',
112
            'solrFieldId' => 'id',
113
            'solrFieldLicense' => 'license',
114
            'solrFieldLocation' => 'location',
115
            'solrFieldPage' => 'page',
116
            'solrFieldPartof' => 'partof',
117
            'solrFieldPid' => 'pid',
118
            'solrFieldPurl' => 'purl',
119
            'solrFieldRecordId' => 'record_id',
120
            'solrFieldRestrictions' => 'restrictions',
121
            'solrFieldRoot' => 'root',
122
            'solrFieldSid' => 'sid',
123
            'solrFieldTerms' => 'terms',
124
            'solrFieldThumbnail' => 'thumbnail',
125
            'solrFieldTimestamp' => 'timestamp',
126
            'solrFieldTitle' => 'title',
127
            'solrFieldToplevel' => 'toplevel',
128
            'solrFieldType' => 'type',
129
            'solrFieldUid' => 'uid',
130
            'solrFieldUrn' => 'urn',
131
            'solrFieldVolume' => 'volume',
132
133
            'solrHost' => getenv('dlfTestingSolrHost'),
134
        ];
135
    }
136
137
    protected function addSiteConfig($identifier, $baseUrl)
138
    {
139
        $siteConfig = Yaml::parseFile(__DIR__ . '/../Fixtures/siteconfig.yaml');
140
        $siteConfig['base'] = $baseUrl;
141
        $siteConfig['languages'][0]['base'] = $baseUrl;
142
143
        $siteConfigPath = $this->instancePath . '/typo3conf/sites/' . $identifier;
144
        @mkdir($siteConfigPath, 0775, true);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for mkdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

144
        /** @scrutinizer ignore-unhandled */ @mkdir($siteConfigPath, 0775, true);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
145
        file_put_contents($siteConfigPath . '/config.yaml', Yaml::dump($siteConfig));
146
    }
147
148
    protected function initializeRepository(string $className, int $storagePid)
149
    {
150
        $repository = $this->objectManager->get($className);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

150
        $repository = /** @scrutinizer ignore-deprecated */ $this->objectManager->get($className);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
151
152
        $querySettings = $this->objectManager->get(Typo3QuerySettings::class);
0 ignored issues
show
Deprecated Code introduced by
The function TYPO3\CMS\Extbase\Object\ObjectManager::get() has been deprecated: since TYPO3 10.4, will be removed in version 12.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

152
        $querySettings = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(Typo3QuerySettings::class);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
153
        $querySettings->setStoragePageIds([$storagePid]);
154
        $repository->setDefaultQuerySettings($querySettings);
155
156
        return $repository;
157
    }
158
159
    protected function importSolrDocuments(Solr $solr, string $path)
160
    {
161
        $jsonDocuments = json_decode(file_get_contents($path), true);
162
163
        $updateQuery = $solr->service->createUpdate();
164
        $documents = array_map(function ($jsonDoc) use ($updateQuery) {
165
            $document = $updateQuery->createDocument();
166
            foreach ($jsonDoc as $key => $value) {
167
                $document->setField($key, $value);
0 ignored issues
show
Bug introduced by
The method setField() does not exist on Solarium\Core\Query\DocumentInterface. It seems like you code against a sub-type of Solarium\Core\Query\DocumentInterface such as Solarium\Plugin\MinimumScoreFilter\Document or Solarium\QueryType\Update\Query\Document. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

167
                $document->/** @scrutinizer ignore-call */ 
168
                           setField($key, $value);
Loading history...
168
            }
169
            if (isset($jsonDoc['collection'])) {
170
                $document->setField('collection_faceting', $jsonDoc['collection']);
171
            }
172
            return $document;
173
        }, $jsonDocuments);
174
        $updateQuery->addDocuments($documents);
175
        $updateQuery->addCommit();
176
        $solr->service->update($updateQuery);
177
    }
178
179
    protected function initLanguageService(string $locale)
180
    {
181
        if (class_exists(\TYPO3\CMS\Core\Localization\LanguageServiceFactory::class)) {
182
            $GLOBALS['LANG'] = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Localization\LanguageServiceFactory::class)->create($locale);
183
        } else {
184
            $typo3MajorVersion = VersionNumberUtility::convertVersionStringToArray(VersionNumberUtility::getCurrentTypo3Version())['version_main'];
185
            $this->assertEquals(9, $typo3MajorVersion);
186
187
            $lang = new LanguageService();
0 ignored issues
show
Bug introduced by
The call to TYPO3\CMS\Core\Localizat...eService::__construct() has too few arguments starting with locales. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

187
            $lang = /** @scrutinizer ignore-call */ new LanguageService();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
188
            $lang->init($locale);
189
            $GLOBALS['LANG'] = $lang;
190
        }
191
    }
192
193
    /**
194
     * Assert that $sub is recursively contained within $super.
195
     */
196
    protected function assertArrayMatches(array $sub, array $super, string $message = '')
197
    {
198
        $this->assertEquals($sub, ArrayUtility::intersectRecursive($super, $sub), $message);
199
    }
200
}
201