Passed
Pull Request — master (#103)
by Alexander
03:37
created

FunctionalTestCase   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 69
c 2
b 0
f 0
dl 0
loc 132
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A setUp() 0 13 1
A addSiteConfig() 0 9 1
A initializeRepository() 0 9 1
A getDlfConfiguration() 0 30 1
1
<?php
2
3
namespace Kitodo\Dlf\Tests\Functional;
4
5
use GuzzleHttp\Client as HttpClient;
6
use Symfony\Component\Yaml\Yaml;
7
use TYPO3\CMS\Core\Utility\GeneralUtility;
8
use TYPO3\CMS\Extbase\Object\ObjectManager;
9
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
10
11
/**
12
 * Base class for functional test cases. This provides some common configuration
13
 * and collects utility methods for functional tests.
14
 */
15
class FunctionalTestCase extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
16
{
17
    protected $testExtensionsToLoad = [
18
        'typo3conf/ext/dlf',
19
    ];
20
21
    protected $configurationToUseInTestInstance = [
22
        'SYS' => [
23
            'caching' => [
24
                'cacheConfigurations' => [
25
                    'tx_dlf_doc' => [
26
                        'backend' => \TYPO3\CMS\Core\Cache\Backend\NullBackend::class,
27
                    ],
28
                ],
29
            ],
30
        ],
31
        'EXTENSIONS' => [
32
            'dlf' => [], // = $this->getDlfConfiguration(), set in constructor
33
        ],
34
        'DB' => [
35
            'Connections' => [
36
                'Default' => [
37
                    // TODO: This is taken from the base class, minus "ONLY_FULL_GROUP_BY"; should probably rather be changed in DocumentRepository::getOaiDocumentList
38
                    '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\';',
39
                ],
40
            ],
41
        ],
42
    ];
43
44
    /**
45
     * By default, the testing framework wraps responses into a JSON object
46
     * that contains status code etc. as fields. Set this field to true to avoid
47
     * this behavior by not loading the json_response extension.
48
     *
49
     * @var bool
50
     */
51
    protected $disableJsonWrappedResponse = false;
52
53
    /** @var ObjectManager */
54
    protected $objectManager;
55
56
    /**
57
     * @var string
58
     */
59
    protected $baseUrl;
60
61
    /**
62
     * @var HttpClient
63
     */
64
    protected $httpClient;
65
66
    public function __construct()
67
    {
68
        parent::__construct();
69
70
        $this->configurationToUseInTestInstance['EXTENSIONS']['dlf'] = $this->getDlfConfiguration();
71
72
        if ($this->disableJsonWrappedResponse) {
73
            $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

73
            $this->frameworkExtensionsToLoad = array_filter(/** @scrutinizer ignore-deprecated */ $this->frameworkExtensionsToLoad, function ($ext) {
Loading history...
74
                return $ext !== 'Resources/Core/Functional/Extensions/json_response';
75
            });
76
        }
77
    }
78
79
    public function setUp(): void
80
    {
81
        parent::setUp();
82
83
        $this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
84
85
        $this->baseUrl = 'http://web:8000/public/typo3temp/var/tests/functional-' . $this->identifier . '/';
86
        $this->httpClient = new HttpClient([
87
            'base_uri' => $this->baseUrl,
88
            'http_errors' => false,
89
        ]);
90
91
        $this->addSiteConfig('dlf-testing', $this->baseUrl);
92
    }
93
94
    protected function getDlfConfiguration()
95
    {
96
        return [
97
            'solrFieldAutocomplete' => 'autocomplete',
98
            'solrFieldCollection' => 'collection',
99
            'solrFieldDefault' => 'default',
100
            'solrFieldFulltext' => 'fulltext',
101
            'solrFieldGeom' => 'geom',
102
            'solrFieldId' => 'id',
103
            'solrFieldLicense' => 'license',
104
            'solrFieldLocation' => 'location',
105
            'solrFieldPage' => 'page',
106
            'solrFieldPartof' => 'partof',
107
            'solrFieldPid' => 'pid',
108
            'solrFieldPurl' => 'purl',
109
            'solrFieldRecordId' => 'record_id',
110
            'solrFieldRestrictions' => 'restrictions',
111
            'solrFieldRoot' => 'root',
112
            'solrFieldSid' => 'sid',
113
            'solrFieldTerms' => 'terms',
114
            'solrFieldThumbnail' => 'thumbnail',
115
            'solrFieldTimestamp' => 'timestamp',
116
            'solrFieldTitle' => 'title',
117
            'solrFieldToplevel' => 'toplevel',
118
            'solrFieldType' => 'type',
119
            'solrFieldUid' => 'uid',
120
            'solrFieldUrn' => 'urn',
121
            'solrFieldVolume' => 'volume',
122
123
            'solrHost' => getenv('dlfTestingSolrHost'),
124
        ];
125
    }
126
127
    protected function addSiteConfig($identifier, $baseUrl)
128
    {
129
        $siteConfig = Yaml::parseFile(__DIR__ . '/../Fixtures/siteconfig.yaml');
130
        $siteConfig['base'] = $baseUrl;
131
        $siteConfig['languages'][0]['base'] = $baseUrl;
132
133
        $siteConfigPath = $this->instancePath . '/typo3conf/sites/' . $identifier;
134
        @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

134
        /** @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...
135
        file_put_contents($siteConfigPath . '/config.yaml', Yaml::dump($siteConfig));
136
    }
137
138
    protected function initializeRepository(string $className, int $storagePid)
139
    {
140
        $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

140
        $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...
141
142
        $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

142
        $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...
143
        $querySettings->setStoragePageIds([$storagePid]);
144
        $repository->setDefaultQuerySettings($querySettings);
145
146
        return $repository;
147
    }
148
}
149