Passed
Pull Request — master (#123)
by
unknown
04:03
created

CollectionRepositoryTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 96
c 1
b 0
f 0
dl 0
loc 181
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A canGetIndexNameForSolr() 0 21 1
A canGetCollectionForMetadata() 0 15 2
A findCollectionsBySettings() 0 12 2
A canFindAllByUids() 0 13 2
A canFindCollectionsBySettings() 0 73 1
A setUp() 0 10 1
1
<?php
2
3
/**
4
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
5
 *
6
 * This file is part of the Kitodo and TYPO3 projects.
7
 *
8
 * @license GNU General Public License version 3 or later.
9
 * For the full copyright and license information, please read the
10
 * LICENSE.txt file that was distributed with this source code.
11
 */
12
13
namespace Kitodo\Dlf\Tests\Functional\Repository;
14
15
use TYPO3\CMS\Extbase\Persistence\Generic\QueryResult;
16
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
17
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase;
18
19
class CollectionRepositoryTest extends FunctionalTestCase
20
{
21
    /**
22
     * @var CollectionRepository
23
     */
24
    protected $collectionRepository;
25
26
    public function setUp(): void
27
    {
28
        parent::setUp();
29
30
        $this->collectionRepository = $this->initializeRepository(
31
            CollectionRepository::class,
32
            20000
33
        );
34
35
        $this->importCSVDataSet(__DIR__ . '/../../Fixtures/Repository/collections.csv');
36
    }
37
38
    /**
39
     *
40
     * @group find
41
     */
42
    public function canFindAllByUids(): void
43
    {
44
        $collections = $this->collectionRepository->findAllByUids([1101, 1102]);
45
        self::assertNotNull($collections);
46
        self::assertInstanceOf(QueryResult::class, $collections);
47
48
        $collectionsByLabel = [];
49
        foreach ($collections as $collection) {
50
            $collectionsByLabel[$collection->getLabel()] = $collection;
51
        }
52
53
        self::assertArrayHasKey('Musik', $collectionsByLabel);
54
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
55
    }
56
57
    /**
58
     * @test
59
     * @group find
60
     */
61
    public function canGetCollectionForMetadata(): void
62
    {
63
        $collections = $this->collectionRepository->getCollectionForMetadata("20000");
64
        self::assertNotNull($collections);
65
        self::assertInstanceOf(QueryResult::class, $collections);
66
67
        $collectionsByLabel = [];
68
        foreach ($collections as $collection) {
69
            $collectionsByLabel[$collection->getLabel()] = $collection;
70
        }
71
72
        self::assertArrayHasKey('Musik', $collectionsByLabel);
73
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
74
        self::assertArrayHasKey('Geschichte', $collectionsByLabel);
75
        self::assertArrayHasKey('Bildende Kunst', $collectionsByLabel);
76
    }
77
78
    /**
79
     * @param $settings
80
     * @return array
81
     */
82
    protected function findCollectionsBySettings($settings): array
83
    {
84
        $collections = $this->collectionRepository->findCollectionsBySettings($settings);
85
        self::assertNotNull($collections);
86
        self::assertInstanceOf(QueryResult::class, $collections);
87
88
        $collectionsByLabel = [];
89
        foreach ($collections as $collection) {
90
            $collectionsByLabel[$collection->getLabel()] = $collection;
91
        }
92
93
        return $collectionsByLabel;
94
    }
95
96
    /**
97
     * @test
98
     * @group find
99
     */
100
    public function canFindCollectionsBySettings(): void
101
    {
102
        $collectionsByLabel = $this->findCollectionsBySettings(['collections' => '1101, 1102']);
103
        self::assertCount(2, $collectionsByLabel);
104
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
105
        self::assertArrayHasKey('Musik', $collectionsByLabel);
106
107
        $collectionsByLabel = $this->findCollectionsBySettings(
108
            [
109
                'index_name' => ['Geschichte', 'collection-with-single-document'],
110
                'show_userdefined' => true
111
            ]
112
        );
113
        self::assertCount(2, $collectionsByLabel);
114
        self::assertArrayHasKey('Geschichte', $collectionsByLabel);
115
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
116
117
        $collectionsByLabel = $this->findCollectionsBySettings(['show_userdefined' => true]);
118
        self::assertCount(4, $collectionsByLabel);
119
        self::assertArrayHasKey('Musik', $collectionsByLabel);
120
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
121
        self::assertArrayHasKey('Geschichte', $collectionsByLabel);
122
        self::assertArrayHasKey('Bildende Kunst', $collectionsByLabel);
123
        self::assertEquals(
124
            'Bildende Kunst, Collection with single document, Geschichte, Musik',
125
            implode(', ', array_keys($collectionsByLabel))
126
        );
127
128
        $collectionsByLabel = $this->findCollectionsBySettings(['show_userdefined' => false]);
129
        self::assertCount(2, $collectionsByLabel);
130
        self::assertArrayHasKey('Musik', $collectionsByLabel);
131
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
132
133
        $collectionsByLabel = $this->findCollectionsBySettings(['hideEmptyOaiNames' => true]);
134
        self::assertCount(2, $collectionsByLabel);
135
        self::assertArrayHasKey('Musik', $collectionsByLabel);
136
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
137
138
        $collectionsByLabel = $this->findCollectionsBySettings(
139
            [
140
                'hideEmptyOaiNames' => true,
141
                'show_userdefined' => true
142
            ]
143
        );
144
        self::assertCount(3, $collectionsByLabel);
145
        self::assertArrayHasKey('Musik', $collectionsByLabel);
146
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
147
        self::assertArrayHasKey('Geschichte', $collectionsByLabel);
148
149
        $collectionsByLabel = $this->findCollectionsBySettings(
150
            [
151
                'hideEmptyOaiNames' => false,
152
                'show_userdefined' => true
153
            ]
154
        );
155
        self::assertCount(4, $collectionsByLabel);
156
        self::assertArrayHasKey('Musik', $collectionsByLabel);
157
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
158
        self::assertArrayHasKey('Geschichte', $collectionsByLabel);
159
        self::assertArrayHasKey('Bildende Kunst', $collectionsByLabel);
160
161
        $collectionsByLabel = $this->findCollectionsBySettings(
162
            [
163
                'collections' => '1101, 1102, 1103, 1104',
164
                'show_userdefined' => true,
165
                'hideEmptyOaiNames' => false,
166
                'index_name' => ['Geschichte', 'collection-with-single-document']
167
            ]
168
        );
169
170
        self::assertCount(2, $collectionsByLabel);
171
        self::assertArrayHasKey('Collection with single document', $collectionsByLabel);
172
        self::assertArrayHasKey('Geschichte', $collectionsByLabel);
173
    }
174
175
    /**
176
     * @test
177
     * @group find
178
     */
179
    public function canGetIndexNameForSolr(): void
180
    {
181
        $indexName = $this->collectionRepository->getIndexNameForSolr(
182
            ['show_userdefined' => true, 'storagePid' => '20000'], 'history'
183
        );
184
        $result = $indexName->fetchAllAssociative();
185
        self::assertEquals(1, $indexName->rowCount());
186
        self::assertEquals('Geschichte', $result[0]['index_name']);
187
        self::assertEquals('*:*', $result[0]['index_query']);
188
        self::assertEquals('1103', $result[0]['uid']);
189
190
        $indexName = $this->collectionRepository->getIndexNameForSolr(
191
            ['show_userdefined' => false, 'storagePid' => '20000'], 'history'
192
        );
193
        self::assertEquals(0, $indexName->rowCount());
194
195
        $indexName = $this->collectionRepository->getIndexNameForSolr(
196
            ['show_userdefined' => false, 'storagePid' => '20000'], 'collection-with-single-document'
197
        );
198
        self::assertEquals(1, $indexName->rowCount());
199
        self::assertEquals('collection-with-single-document', $indexName->fetchOne());
200
    }
201
}
202