View source
<?php
namespace Drupal\Tests\search_api\Unit\Views;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\search_api\Plugin\views\argument\SearchApiAllTerms;
use Drupal\search_api\Plugin\views\query\SearchApiQuery;
use Drupal\search_api\Query\Condition;
use Drupal\search_api\Query\ConditionGroup;
use Drupal\search_api\Query\ConditionGroupInterface;
use Drupal\taxonomy\TermInterface;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Assert;
class AllTermsArgumentTest extends UnitTestCase {
use TaxonomyTestTrait;
protected $plugin;
protected $conditionGroup = NULL;
protected $aborted = FALSE;
public function setUp() {
parent::setUp();
$this
->setupContainer();
$this->plugin = new SearchApiAllTerms([], 'search_api_all_terms', [
'vocabulary_fields' => [
'voc_a' => [
'field_voc_a',
],
'voc_b' => [
'field_voc_b_1',
'field_voc_b_2',
],
],
]);
$this->plugin->options['break_phrase'] = TRUE;
$term_values = [
1 => [
'id' => 1,
'label' => 'Term 1',
'bundle' => 'voc_a',
],
2 => [
'id' => 2,
'label' => 'Term 2',
'bundle' => 'voc_a',
],
3 => [
'id' => 3,
'label' => 'Term 3',
'bundle' => 'voc_b',
],
4 => [
'id' => 4,
'label' => 'Term 4',
'bundle' => 'voc_c',
],
];
$terms = [];
foreach ($term_values as $tid => $values) {
$term = $this
->createMock(TermInterface::class);
foreach ($values as $field => $value) {
$term
->method($field)
->willReturn($value);
}
$terms[$tid] = $term;
}
$this->termStorage
->method('loadMultiple')
->willReturnCallback(function (array $tids) use ($terms) : array {
return array_intersect_key($terms, array_flip($tids));
});
}
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());
$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());
$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);
}
protected function executePluginQuery(string $argument) {
$this->conditionGroup = NULL;
$this->aborted = FALSE;
$this->plugin->value = NULL;
$this->plugin->argument_validated = NULL;
$this->plugin->argument = $argument;
$this->plugin
->query();
}
}