public function AllTermsArgumentTest::testConditionalFilter in Search API 8
Tests whether the contextual filter works correctly.
@covers ::query
File
- tests/
src/ Unit/ Views/ AllTermsArgumentTest.php, line 109
Class
- AllTermsArgumentTest
- Tests whether the SearchApiAllTerms argument plugin works correctly.
Namespace
Drupal\Tests\search_api\Unit\ViewsCode
public function testConditionalFilter() {
$query = $this
->createMock(SearchApiQuery::class);
$query
->method('createConditionGroup')
->willReturnCallback(function (string $conjunction, array $tags) {
Assert::assertEmpty($tags);
if (isset($this->conditionGroup)) {
return new ConditionGroup($conjunction);
}
return $this->conditionGroup = new ConditionGroup($conjunction);
});
$query
->method('addConditionGroup')
->willReturnCallback(function (ConditionGroupInterface $added_condition_group, $group = NULL) {
Assert::assertNull($group);
Assert::assertSame($this->conditionGroup, $added_condition_group);
});
$query
->method('abort')
->willReturnCallback(function ($message = NULL) {
$this->aborted = TRUE;
if ($message !== NULL) {
if ($message instanceof TranslatableMarkup) {
$message = strtr($message
->getUntranslatedString(), $message
->getArguments());
}
$this->aborted = $message;
}
});
$this->plugin->query = $query;
$this
->executePluginQuery('1,2,3');
$this
->assertFalse($this->aborted);
$this
->assertEquals('AND', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', 1),
new Condition('field_voc_a', 2),
(new ConditionGroup('OR'))
->addCondition('field_voc_b_1', 3)
->addCondition('field_voc_b_2', 3),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
$this
->executePluginQuery('1+2+3');
$this
->assertFalse($this->aborted);
$this
->assertEquals('OR', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', [
1,
2,
], 'IN'),
new Condition('field_voc_b_1', [
3,
], 'IN'),
new Condition('field_voc_b_2', [
3,
], 'IN'),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
// Set the filter to negated.
$this->plugin->options['not'] = TRUE;
$this
->executePluginQuery('1,2,3');
$this
->assertFalse($this->aborted);
$this
->assertEquals('OR', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', 1, '<>'),
new Condition('field_voc_a', 2, '<>'),
(new ConditionGroup('AND'))
->addCondition('field_voc_b_1', 3, '<>')
->addCondition('field_voc_b_2', 3, '<>'),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
$this
->executePluginQuery('1+2+3');
$this
->assertFalse($this->aborted);
$this
->assertEquals('AND', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', [
1,
2,
], 'NOT IN'),
new Condition('field_voc_b_1', [
3,
], 'NOT IN'),
new Condition('field_voc_b_2', [
3,
], 'NOT IN'),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
// Check various error conditions.
$this->plugin->options['not'] = FALSE;
$this
->executePluginQuery('1,2,foo');
$this
->assertEquals('Invalid taxonomy term ID given for "All taxonomy term fields" contextual filter.', $this->aborted);
$this
->executePluginQuery('1+2+foo');
$this
->assertFalse($this->aborted);
$this
->assertEquals('OR', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', [
1,
2,
], 'IN'),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
$this
->executePluginQuery('1,2,4');
$this
->assertEquals('"All taxonomy term fields" contextual filter could not be applied as taxonomy term Term 4 (ID: 4) belongs to vocabulary voc_c, not contained in any indexed fields.', $this->aborted);
$this
->executePluginQuery('1+2+4');
$this
->assertFalse($this->aborted);
$this
->assertEquals('OR', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', [
1,
2,
], 'IN'),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
$this->plugin->options['not'] = TRUE;
$this
->executePluginQuery('1+2+4');
$this
->assertEquals('"All taxonomy term fields" contextual filter could not be applied as taxonomy term Term 4 (ID: 4) belongs to vocabulary voc_c, not contained in any indexed fields.', $this->aborted);
$this
->executePluginQuery('1,2,4');
$this
->assertFalse($this->aborted);
$this
->assertEquals('OR', $this->conditionGroup
->getConjunction());
$expected = [
new Condition('field_voc_a', 1, '<>'),
new Condition('field_voc_a', 2, '<>'),
];
$this
->assertEquals($expected, $this->conditionGroup
->getConditions());
$this->plugin->options['not'] = FALSE;
$this->plugin->options['break_phrase'] = FALSE;
$this
->executePluginQuery('1,2,3');
$this
->assertEquals('No valid taxonomy term IDs given for "All taxonomy term fields" contextual filter.', $this->aborted);
$this->plugin->options['not'] = TRUE;
$this
->executePluginQuery('1,2,3');
$this
->assertFalse($this->aborted);
}