Skip to content

Commit

Permalink
Add Ids query for Solr
Browse files Browse the repository at this point in the history
Add arSolrIdsQuery and associated tests
  • Loading branch information
anvit committed Aug 20, 2024
1 parent eb56967 commit 91fafa5
Show file tree
Hide file tree
Showing 2 changed files with 227 additions and 0 deletions.
66 changes: 66 additions & 0 deletions plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Access to Memory (AtoM) software.
*
* Access to Memory (AtoM) is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Access to Memory (AtoM) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Access to Memory (AtoM). If not, see <http://www.gnu.org/licenses/>.
*/

class arSolrIdsQuery extends arSolrTermsQuery
{
/**
* Constructor.
*
* @param mixed $searchQuery
* @param null|mixed $term
*/
public function __construct($ids = null)
{
$this->setIds($ids);
}

public function setIds($ids) {
$this->termField = 'id';
$this->termValues = $ids;
}

public function getIds() {
return $this->termValues;
}

protected function generateQueryParams()
{
$termField = $this->getTermField();
$ids = $this->getIds();
if (!isset($ids) || count($ids) == 0) {
throw new Exception('Ids are not set.');
}

$type = $this->getType();
if (!isset($type)) {
throw new Exception("Field 'type' is not set.");
}

$queryString = implode(" OR ", $ids);
$this->query = [
'query' => [
'edismax' => [
'query' => "{$type}.{$termField}:({$queryString})",
],
],
'offset' => $this->offset,
'limit' => $this->size,
];
}
}
161 changes: 161 additions & 0 deletions test/phpunit/arSolrPlugin/lib/query/ArSolrIdsQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

use PHPUnit\Framework\TestCase;

require_once 'plugins/arSolrPlugin/lib/query/arSolrAbstractQuery.class.php';

require_once 'plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php';

require_once 'plugins/arSolrPlugin/lib/query/arSolrIdsQuery.class.php';

/**
* @internal
*
* @covers \arSolrIdsQuery
* @covers \arSolrTermsQuery
*/
class ArSolrIdsQueryTest extends TestCase
{
public function testCreateEmptySolrIdsQuery()
{
$this->idsQuery = new arSolrIdsQuery();
$this->assertTrue($this->idsQuery instanceof arSolrIdsQuery, 'Assert plugin object is arSolrIdsQuery.');
}

public function createSolrIdsQueryProvider(): array
{
return [
'New arSolrIdsQuery with no ids' => [
'ids' => null,
'expectedField' => 'id',
'expectedValue' => null,
],
'New arSolrIdsQuery with empty ids' => [
'ids' => [],
'expectedField' => 'id',
'expectedValue' => [],
],
'New arSolrIdsQuery with one id' => [
'ids' => [123],
'expectedField' => 'id',
'expectedValue' => [123],
],
'New arSolrIdsQuery with multiple ids' => [
'ids' => [123, 456],
'expectedField' => 'id',
'expectedValue' => [123, 456],
],
];
}

/**
* @dataProvider createSolrIdsQueryProvider
*
* @param mixed $ids
* @param mixed $expectedField
* @param mixed $expectedValue
*/
public function testCreateSolrIdsQuery($ids, $expectedField, $expectedValue)
{
$this->idsQuery = new arSolrIdsQuery($ids);
$actualField = $this->idsQuery->getTermField();
$actualValues = $this->idsQuery->getIds();

$this->assertTrue($this->idsQuery instanceof arSolridsQuery, 'Assert plugin object is arSolrIdsQuery.');
$this->assertSame($expectedField, $actualField, 'Passed field does not match expected field.');
$this->assertSame($expectedValue, $actualValues, 'Passed value does not match expected value.');
}

public function getQueryParamsProvider(): array
{
return [
'Generate ids query with single id' => [
'ids' => [123],
'type' => 'test_type',
'expected' => [
'query' => [
'edismax' => [
'query' => 'test_type.id:(123)',
],
],
'offset' => 0,
'limit' => 10,
],
],
'Generate ids query with multiple ids' => [
'ids' => [123, 345, 456],
'type' => 'test_type',
'expected' => [
'query' => [
'edismax' => [
'query' => 'test_type.id:(123 OR 345 OR 456)',
],
],
'offset' => 0,
'limit' => 10,
],
],
];
}

/**
* @dataProvider getQueryParamsProvider
*
* @param mixed $ids
* @param mixed $type
* @param mixed $expected
*/
public function testGetQueryParams($ids, $type, $expected)
{
$this->idsQuery = new arSolrIdsQuery($ids);
$this->idsQuery->setType($type);

$actual = $this->idsQuery->getQueryParams();

$this->assertSame($expected, $actual, 'Params passed do not match expected.');
}

public function getQueryParamsExceptionProvider(): array
{
return [
'Generate ids query with missing type' => [
'ids' => [123],
'type' => '',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Field \'type\' is not set.',
],
'Generate ids query with no ids' => [
'ids' => null,
'type' => 'test_type',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Ids are not set.',
],
'Generate ids query with zero ids' => [
'ids' => [],
'type' => 'test_type',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Ids are not set.',
],
];
}

/**
* @dataProvider getQueryParamsExceptionProvider
*
* @param mixed $ids
* @param mixed $type
* @param mixed $expectedException
* @param mixed $expectedExceptionMessage
*/
public function testGetQueryParamsException($ids, $type, $expectedException, $expectedExceptionMessage)
{
$this->idsQuery = new arSolrIdsQuery($ids);
$this->idsQuery->setType($type);

$this->expectException($expectedException);
$this->expectExceptionMessage($expectedExceptionMessage);

$this->idsQuery->getQueryParams();
}

}

0 comments on commit 91fafa5

Please sign in to comment.