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

SolrIndexingTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 49
c 1
b 0
f 0
dl 0
loc 97
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreateCore() 0 11 1
A canIndexAndSearchDocument() 0 36 4
A createSolrCore() 0 12 1
A setUp() 0 15 1
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\SolrCore;
9
use Kitodo\Dlf\Domain\Repository\DocumentRepository;
10
use Kitodo\Dlf\Domain\Repository\SolrCoreRepository;
11
use Kitodo\Dlf\Tests\Functional\FunctionalTestCase;
12
use TYPO3\CMS\Core\Core\Bootstrap;
13
use TYPO3\CMS\Core\Utility\GeneralUtility;
14
use TYPO3\CMS\Extbase\Object\ObjectManager;
15
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
16
17
class SolrIndexingTest extends FunctionalTestCase
18
{
19
    /** @var PersistenceManager */
20
    protected $persistenceManager;
21
22
    /** @var DocumentRepository */
23
    protected $documentRepository;
24
25
    /** @var SolrCoreRepository */
26
    protected $solrCoreRepository;
27
28
    public function setUp(): void
29
    {
30
        parent::setUp();
31
32
        // Needed for Indexer::add, which uses the language service
33
        Bootstrap::initializeLanguageObject();
34
35
        $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

35
        $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...
36
37
        $this->documentRepository = $this->initializeRepository(DocumentRepository::class, 20000);
38
        $this->solrCoreRepository = $this->initializeRepository(SolrCoreRepository::class, 20000);
39
40
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/documents_1.xml');
41
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/libraries.xml');
42
        $this->importDataSet(__DIR__ . '/../../Fixtures/Common/metadata.xml');
43
    }
44
45
    /**
46
     * @test
47
     */
48
    public function canCreateCore()
49
    {
50
        $coreName = uniqid('testCore');
51
        $solr = Solr::getInstance($coreName);
52
        $this->assertNull($solr->core);
53
54
        $actualCoreName = Solr::createCore($coreName);
55
        $this->assertEquals($actualCoreName, $coreName);
56
57
        $solr = Solr::getInstance($coreName);
58
        $this->assertNotNull($solr->core);
59
    }
60
61
    /**
62
     * @test
63
     */
64
    public function canIndexAndSearchDocument()
65
    {
66
        $core = $this->createSolrCore();
67
68
        $document = $this->documentRepository->findByUid(1001);
69
        $document->setSolrcore($core->model->getUid());
70
        $this->persistenceManager->persistAll();
71
72
        $doc = Doc::getInstance($document->getLocation());
73
        $document->setDoc($doc);
74
75
        $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

75
        $indexingSuccessful = Indexer::add(/** @scrutinizer ignore-type */ $document);
Loading history...
76
        $this->assertTrue($indexingSuccessful);
77
78
        $solrSettings = [
79
            'solrcore' => $core->solr->core,
80
            'storagePid' => $document->getPid(),
81
        ];
82
83
        $result = $this->documentRepository->findSolrByCollection(null, $solrSettings, ['query' => '*']);
84
        $this->assertEquals(1, $result['numberOfToplevels']);
85
        $this->assertEquals(15, count($result['solrResults']['documents']));
86
87
        // Check that the title stored in Solr matches the title of database entry
88
        $docTitleInSolr = false;
89
        foreach ($result['solrResults']['documents'] as $solrDoc) {
90
            if ($solrDoc['toplevel'] && $solrDoc['uid'] === $document->getUid()) {
91
                $this->assertEquals($document->getTitle(), $solrDoc['title']);
92
                $docTitleInSolr = true;
93
                break;
94
            }
95
        }
96
        $this->assertTrue($docTitleInSolr);
97
98
        // $result['documents'] is hydrated from the database model
99
        $this->assertEquals($document->getTitle(), $result['documents'][$document->getUid()]['title']);
100
    }
101
102
    protected function createSolrCore(): object
103
    {
104
        $coreName = Solr::createCore();
105
        $solr = Solr::getInstance($coreName);
106
107
        $model = GeneralUtility::makeInstance(SolrCore::class);
108
        $model->setLabel('Testing Solr Core');
109
        $model->setIndexName($coreName);
110
        $this->solrCoreRepository->add($model);
111
        $this->persistenceManager->persistAll();
112
113
        return (object) compact('solr', 'model');
114
    }
115
}
116