Completed
Push — master ( f0bd62...1253ac )
by
unknown
16s queued 13s
created

SolrIndexingTest::canSearchInCollections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 36
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 23
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 36
rs 9.552
1
<?php
2
3
namespace Kitodo\Dlf\Tests\Functional\Common;
4
5
use Kitodo\Dlf\Common\Doc;
6
use Kitodo\Dlf\Common\Indexer;
7
use Kitodo\Dlf\Common\Solr;
8
use Kitodo\Dlf\Domain\Model\Collection;
9
use Kitodo\Dlf\Domain\Model\SolrCore;
10
use Kitodo\Dlf\Domain\Repository\CollectionRepository;
11
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
12
use Kitodo\Dlf\Domain\Repository\SolrCoreRepository;
13
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase;
14
use TYPO3\CMS\Core\Core\Bootstrap;
15
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
use TYPO3\CMS\Extbase\Object\ObjectManager;
17
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
18
19
class SolrIndexingTest extends FunctionalTestCase
20
{
21
    /** @var PersistenceManager */
22
    protected $persistenceManager;
23
24
    /** @var CollectionRepository */
25
    protected $collectionRepository;
26
27
    /** @var DocumentRepository */
28
    protected $documentRepository;
29
30
    /** @var SolrCoreRepository */
31
    protected $solrCoreRepository;
32
33
    public function setUp(): void
34
    {
35
        parent::setUp();
36
37
        // Needed for Indexer::add, which uses the language service
38
        Bootstrap::initializeLanguageObject();
39
40
        $this->persistenceManager = $this->objectManager->get(PersistenceManager::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

40
        $this->persistenceManager = /** @scrutinizer ignore-deprecated */ $this->objectManager->get(PersistenceManager::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...
41
42
        $this->collectionRepository = $this->initializeRepository(CollectionRepository::class, 20000);
43
        $this->documentRepository = $this->initializeRepository(DocumentRepository::class, 20000);
44
        $this->solrCoreRepository = $this->initializeRepository(SolrCoreRepository::class, 20000);
45
46
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/documents_1.xml');
47
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/libraries.xml');
48
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/metadata.xml');
49
    }
50
51
    /**
52
     * @test
53
     */
54
    public function canCreateCore()
55
    {
56
        $coreName = uniqid('testCore');
57
        $solr = Solr::getInstance($coreName);
58
        $this->assertNull($solr->core);
59
60
        $actualCoreName = Solr::createCore($coreName);
61
        $this->assertEquals($actualCoreName, $coreName);
62
63
        $solr = Solr::getInstance($coreName);
64
        $this->assertNotNull($solr->core);
65
    }
66
67
    /**
68
     * @test
69
     */
70
    public function canIndexAndSearchDocument()
71
    {
72
        $core = $this->createSolrCore();
73
74
        $document = $this->documentRepository->findByUid(1001);
75
        $document->setSolrcore($core->model->getUid());
76
        $this->persistenceManager->persistAll();
77
78
        $doc = Doc::getInstance($document->getLocation());
79
        $document->setDoc($doc);
80
81
        $indexingSuccessful = Indexer::add($document);
0 ignored issues
show
Bug introduced by
It seems like $document can also be of type null; however, parameter $document of Kitodo\Dlf\Common\Indexer::add() does only seem to accept Kitodo\Dlf\Domain\Model\Document, maybe add an additional type check? ( Ignorable by Annotation )

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

81
        $indexingSuccessful = Indexer::add(/** @scrutinizer ignore-type */ $document);
Loading history...
82
        $this->assertTrue($indexingSuccessful);
83
84
        $solrSettings = [
85
            'solrcore' => $core->solr->core,
86
            'storagePid' => $document->getPid(),
87
        ];
88
89
        $result = $this->documentRepository->findSolrByCollection(null, $solrSettings, ['query' => '*']);
90
        $this->assertEquals(1, $result['numberOfToplevels']);
91
        $this->assertEquals(15, count($result['solrResults']['documents']));
92
93
        // Check that the title stored in Solr matches the title of database entry
94
        $docTitleInSolr = false;
95
        foreach ($result['solrResults']['documents'] as $solrDoc) {
96
            if ($solrDoc['toplevel'] && $solrDoc['uid'] === $document->getUid()) {
97
                $this->assertEquals($document->getTitle(), $solrDoc['title']);
98
                $docTitleInSolr = true;
99
                break;
100
            }
101
        }
102
        $this->assertTrue($docTitleInSolr);
103
104
        // $result['documents'] is hydrated from the database model
105
        $this->assertEquals($document->getTitle(), $result['documents'][$document->getUid()]['title']);
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function canSearchInCollections()
112
    {
113
        $core = $this->createSolrCore();
114
115
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/documents_fulltext.xml');
116
        $this->importSolrDocuments($core->solr, __DIR__ . '/../../Fixtures/Common/documents_1.solr.json');
117
        $this->importSolrDocuments($core->solr, __DIR__ . '/../../Fixtures/Common/documents_fulltext.solr.json');
118
119
        $collections = $this->collectionRepository->findCollectionsBySettings([
120
            'index_name' => ['Musik', 'Projekt: Dresdner Hefte'],
121
        ]);
122
        $musik = $collections[0];
123
        $dresdnerHefte = $collections[1];
124
125
        $settings = [
126
            'solrcore' => $core->solr->core,
127
            'storagePid' => 20000,
128
        ];
129
130
        // No query: Only list toplevel result(s) in collection(s)
131
        $musikResults = $this->documentRepository->findSolrByCollection($musik, $settings, []);
132
        $dresdnerHefteResults = $this->documentRepository->findSolrByCollection($dresdnerHefte, $settings, []);
133
        $multiCollectionResults = $this->documentRepository->findSolrByCollection($collections, $settings, []);
134
        $this->assertGreaterThanOrEqual(1, $musikResults['solrResults']['numFound']);
135
        $this->assertGreaterThanOrEqual(1, $dresdnerHefteResults['solrResults']['numFound']);
136
        $this->assertEquals('533223312LOG_0000', $dresdnerHefteResults['solrResults']['documents'][0]['id']);
137
        $this->assertEquals(
138
            // Assuming there's no overlap
139
            $dresdnerHefteResults['solrResults']['numFound'] + $musikResults['solrResults']['numFound'],
140
            $multiCollectionResults['solrResults']['numFound']
141
        );
142
143
        // With query: List all results
144
        $metadataResults = $this->documentRepository->findSolrByCollection($dresdnerHefte, $settings, ['query' => 'Dresden']);
145
        $fulltextResults = $this->documentRepository->findSolrByCollection($dresdnerHefte, $settings, ['query' => 'Dresden', 'fulltext' => '1']);
146
        $this->assertGreaterThan($metadataResults['solrResults']['numFound'], $fulltextResults['solrResults']['numFound']);
147
    }
148
149
    protected function createSolrCore(): object
150
    {
151
        $coreName = Solr::createCore();
152
        $solr = Solr::getInstance($coreName);
153
154
        $model = GeneralUtility::makeInstance(SolrCore::class);
155
        $model->setLabel('Testing Solr Core');
156
        $model->setIndexName($coreName);
157
        $this->solrCoreRepository->add($model);
158
        $this->persistenceManager->persistAll();
159
160
        return (object) compact('solr', 'model');
161
    }
162
}
163