Skip to content

Commit

Permalink
Merge branch 'release-1.7.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jan 5, 2022
2 parents 20f110a + ddabc99 commit e68a9b6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
8 changes: 2 additions & 6 deletions model/Metadata/Service/ClassMetadataSearcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,9 @@ private function getRelatedProperties(array $result, array $allProperties): arra

private function getMainClassProperties(string $classUri): array
{
$result = $this->executeQuery('_id', $classUri);
$results = (array) $this->executeQuery('_id', $classUri);

if ($result->getTotalCount() === 0) {
return [];
}

return current($result);
return !empty($results) ? current($results) : [];
}

private function filterDuplicatedProperties(array $allProperties): array
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Metadata/Service/ClassMetadataSearcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@
use oat\generis\test\TestCase;
use oat\oatbox\log\LoggerService;
use oat\tao\elasticsearch\ElasticSearch;
use oat\tao\elasticsearch\Query;
use oat\tao\elasticsearch\SearchResult;
use oat\tao\model\AdvancedSearch\AdvancedSearchChecker;
use oat\tao\model\Lists\Business\Domain\ClassCollection;
use oat\tao\model\Lists\Business\Domain\ClassMetadataSearchRequest;
use oat\tao\model\Lists\Business\Domain\MetadataCollection;
use oat\tao\model\Lists\Business\Input\ClassMetadataSearchInput;
use oat\tao\model\Lists\Business\Service\ClassMetadataService;
use oat\tao\model\search\SearchProxy;
use oat\taoAdvancedSearch\model\Metadata\Service\ClassMetadataSearcher;
use PHPUnit\Framework\MockObject\MockObject;
use Traversable;

class ClassMetadataSearcherTest extends TestCase
{
Expand Down Expand Up @@ -163,6 +166,56 @@ public function testFindAllUsingElasticSearch(): void
$this->assertSame('class1', $rawResult[0]['class']);
}

public function testFindAllUsingElasticSearchWithEmptyClassProperties(): void
{
$property = $this->createMock(core_kernel_classes_Property::class);
$property->method('getRelatedClass')
->willReturn(null);

$class = $this->createMock(core_kernel_classes_Property::class);
$class->method('getLabel')
->willReturn('Class label');

$this->ontology
->method('getProperty')
->willReturn($property);

$this->ontology
->method('getClass')
->willReturn($class);

$this->advancedSearchChecker
->method('isEnabled')
->willReturn(true);

$this->elasticSearch
->method('search')
->with($this->callback(function (Query $query) {
return (($query->getQueryString() == '_id:"class1"')
&& ($query->getIndex() == 'property-list'));
}))
->willReturn(new SearchResult([], 0));

$result = $this->subject->findAll(
new ClassMetadataSearchInput(
(new ClassMetadataSearchRequest())->setClassUri('class1')
)
);

$generator = $result->getIterator();
$this->assertInstanceOf(Traversable::class, $generator);

$items = iterator_to_array($generator);
$this->assertCount(1, $items);
$this->assertNull($items[0]->getParentClass());
$this->assertEquals('class1', $items[0]->getClass());

$metadata = $items[0]->getMetaData();
$this->assertInstanceOf(MetadataCollection::class, $metadata);
$this->assertEquals(0, $metadata->count());
$this->assertEmpty(iterator_to_array($metadata));
}

private function getMockResult(string $classId, ?string $parentClassUri, array $classPath): array
{
return [
Expand All @@ -174,12 +227,14 @@ private function getMockResult(string $classId, ?string $parentClassUri, array $
'propertyUri' => 'propertyUri1',
'propertyLabel' => 'propertyLabel1',
'propertyType' => 'list',
'propertyAlias' => null,
'propertyValues' => [],
],
[
'propertyUri' => 'propertyUri2',
'propertyLabel' => 'propertyLabel2',
'propertyType' => 'text',
'propertyAlias' => null,
'propertyValues' => [],
]
],
Expand Down

0 comments on commit e68a9b6

Please sign in to comment.