-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add arSolrTermsQuery and associated tests. Also fix typo in a property name in arSolrTermQuery.
- Loading branch information
Showing
3 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
<?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 arSolrTermsQuery extends arSolrAbstractQuery | ||
{ | ||
/** | ||
* Query Params. | ||
* | ||
* @var mixed | ||
*/ | ||
protected array $query = []; | ||
|
||
/** | ||
* Query Term Field. | ||
* | ||
* @var string | ||
*/ | ||
protected ?string $termField = null; | ||
|
||
/** | ||
* Query Term Value. | ||
* | ||
* @var array | ||
*/ | ||
protected ?array $termValues = null; | ||
|
||
/** | ||
* Field type. | ||
* | ||
* @var string | ||
*/ | ||
protected ?string $type = null; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param mixed $searchQuery | ||
* @param null|mixed $term | ||
*/ | ||
public function __construct($term = null) | ||
{ | ||
if ($term) { | ||
foreach ($term as $field => $values) { | ||
$this->setTerms($field, $values); | ||
} | ||
} | ||
} | ||
|
||
public function setTerms($field, $values) | ||
{ | ||
$this->termField = $field; | ||
$this->termValues = $values; | ||
} | ||
|
||
public function getTermValues() | ||
{ | ||
return $this->termValues; | ||
} | ||
|
||
public function getTermField() | ||
{ | ||
return $this->termField; | ||
} | ||
|
||
public function getQueryParams() | ||
{ | ||
$this->generateQueryParams(); | ||
|
||
return $this->query; | ||
} | ||
|
||
public function setType($type) | ||
{ | ||
if (empty($type)) { | ||
return; | ||
} | ||
|
||
$this->type = $type; | ||
} | ||
|
||
public function getType() | ||
{ | ||
return $this->type; | ||
} | ||
|
||
protected function generateQueryParams() | ||
{ | ||
$termField = $this->getTermField(); | ||
if (!isset($termField)) { | ||
throw new Exception('Term field is not set.'); | ||
} | ||
|
||
$termValues = $this->getTermValues(); | ||
if (!isset($termValues)) { | ||
throw new Exception('Term values are not set.'); | ||
} | ||
|
||
$type = $this->getType(); | ||
if (!isset($type)) { | ||
throw new Exception("Field 'type' is not set."); | ||
} | ||
|
||
$queryString = implode(" OR ", $termValues); | ||
$this->query = [ | ||
'query' => [ | ||
'edismax' => [ | ||
'query' => "{$type}.{$termField}:({$queryString})", | ||
], | ||
], | ||
'offset' => $this->offset, | ||
'limit' => $this->size, | ||
]; | ||
} | ||
} |
199 changes: 199 additions & 0 deletions
199
test/phpunit/arSolrPlugin/lib/query/ArSolrTermsQueryTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
require_once 'plugins/arSolrPlugin/lib/query/arSolrAbstractQuery.class.php'; | ||
|
||
require_once 'plugins/arSolrPlugin/lib/query/arSolrTermsQuery.class.php'; | ||
|
||
/** | ||
* @internal | ||
* | ||
* @covers \arSolrTermsQuery | ||
*/ | ||
class ArSolrTermsQueryTest extends TestCase | ||
{ | ||
public function createSolrTermsQueryProvider(): array | ||
{ | ||
return [ | ||
'New arSolrTermsQuery with no term' => [ | ||
'term' => null, | ||
'expectedField' => null, | ||
'expectedValue' => null, | ||
], | ||
'New arSolrTermsQuery with empty term' => [ | ||
'term' => ['' => []], | ||
'expectedField' => '', | ||
'expectedValue' => [], | ||
], | ||
'New arSolrTermsQuery with one string term' => [ | ||
'term' => ['tField' => ['tValue']], | ||
'expectedField' => 'tField', | ||
'expectedValue' => ['tValue'], | ||
], | ||
'New arSolrTermsQuery with multiple string terms' => [ | ||
'term' => ['tField' => ['tValue', 'tValue2']], | ||
'expectedField' => 'tField', | ||
'expectedValue' => ['tValue', 'tValue2'], | ||
], | ||
]; | ||
} | ||
|
||
public function testCreateEmptySolrTermsQuery() | ||
{ | ||
$this->termsQuery = new arSolrTermsQuery(); | ||
$this->assertTrue($this->termsQuery instanceof arSolrTermsQuery, 'Assert plugin object is arSolrTermsQuery.'); | ||
} | ||
|
||
/** | ||
* @dataProvider createSolrTermsQueryProvider | ||
* | ||
* @param mixed $term | ||
* @param mixed $expectedField | ||
* @param mixed $expectedValue | ||
*/ | ||
public function testCreateSolrTermsQuery($term, $expectedField, $expectedValue) | ||
{ | ||
$this->termsQuery = new arSolrTermsQuery($term); | ||
$actualField = $this->termsQuery->getTermField(); | ||
$actualValues = $this->termsQuery->getTermValues(); | ||
|
||
$this->assertTrue($this->termsQuery instanceof arSolrTermsQuery, 'Assert plugin object is arSolrTermsQuery.'); | ||
$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 terms query with specified type' => [ | ||
'term' => ['test_field' => ['testVal']], | ||
'type' => 'test_type', | ||
'expected' => [ | ||
'query' => [ | ||
'edismax' => [ | ||
'query' => 'test_type.test_field:(testVal)', | ||
], | ||
], | ||
'offset' => 0, | ||
'limit' => 10, | ||
], | ||
], | ||
'Generate terms query with multiple values' => [ | ||
'term' => ['test_field' => ['testVal', 'testVal2']], | ||
'type' => 'test_type', | ||
'expected' => [ | ||
'query' => [ | ||
'edismax' => [ | ||
'query' => 'test_type.test_field:(testVal OR testVal2)', | ||
], | ||
], | ||
'offset' => 0, | ||
'limit' => 10, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getQueryParamsProvider | ||
* | ||
* @param mixed $term | ||
* @param mixed $type | ||
* @param mixed $expected | ||
*/ | ||
public function testGetQueryParams($term, $type, $expected) | ||
{ | ||
$this->termsQuery = new arSolrTermsQuery($term); | ||
$this->termsQuery->setType($type); | ||
|
||
$actual = $this->termsQuery->getQueryParams(); | ||
|
||
$this->assertSame($expected, $actual, 'Params passed do not match expected.'); | ||
} | ||
|
||
public function getQueryParamsExceptionProvider(): array | ||
{ | ||
return [ | ||
'Generate terms query with missing type' => [ | ||
'term' => ['test_field' => ['testVal']], | ||
'type' => '', | ||
'expectedException' => '\Exception', | ||
'expectedExceptionMessage' => 'Field \'type\' is not set.', | ||
], | ||
'Generate terms query with missing term' => [ | ||
'term' => [], | ||
'type' => 'test_type', | ||
'expectedException' => '\Exception', | ||
'expectedExceptionMessage' => 'Term field is not set.', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getQueryParamsExceptionProvider | ||
* | ||
* @param mixed $term | ||
* @param mixed $type | ||
* @param mixed $expectedException | ||
* @param mixed $expectedExceptionMessage | ||
*/ | ||
public function testGetQueryParamsException($term, $type, $expectedException, $expectedExceptionMessage) | ||
{ | ||
$this->termsQuery = new arSolrTermsQuery($term); | ||
$this->termsQuery->setType($type); | ||
|
||
$this->expectException($expectedException); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
|
||
$this->termsQuery->getQueryParams(); | ||
} | ||
|
||
public function getQueryParamsUsingSetExceptionProvider(): array | ||
{ | ||
return [ | ||
'Generate term query with missing term field' => [ | ||
'termField' => null, | ||
'termValue' => null, | ||
'type' => '', | ||
'expectedException' => '\Exception', | ||
'expectedExceptionMessage' => 'Term field is not set.', | ||
], | ||
'Generate term query with missing term value' => [ | ||
'termField' => 'tField', | ||
'termValue' => null, | ||
'type' => '', | ||
'expectedException' => '\Exception', | ||
'expectedExceptionMessage' => 'Term values are not set.', | ||
], | ||
'Generate term query with missing missing type' => [ | ||
'termField' => 'tField', | ||
'termValue' => ['tValue'], | ||
'type' => '', | ||
'expectedException' => '\Exception', | ||
'expectedExceptionMessage' => 'Field \'type\' is not set.', | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider getQueryParamsUsingSetExceptionProvider | ||
* | ||
* @param mixed $termField | ||
* @param mixed $termValue | ||
* @param mixed $type | ||
* @param mixed $expectedException | ||
* @param mixed $expectedExceptionMessage | ||
*/ | ||
public function testGetQueryParamsUsingSetException($termField, $termValue, $type, $expectedException, $expectedExceptionMessage) | ||
{ | ||
$this->termsQuery = new arSolrTermsQuery(); | ||
$this->termsQuery->setTerms($termField, $termValue); | ||
$this->termsQuery->setType($type); | ||
|
||
$this->expectException($expectedException); | ||
$this->expectExceptionMessage($expectedExceptionMessage); | ||
|
||
$this->termsQuery->getQueryParams(); | ||
} | ||
} |