View source
<?php
namespace Drupal\Tests\facets\Kernel\Entity;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\facets\Entity\Facet;
use Drupal\facets\Exception\InvalidQueryTypeException;
use Drupal\facets\FacetSourceInterface;
use Drupal\facets\Plugin\facets\facet_source\SearchApiDisplay;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\facets\Exception\Exception;
class FacetFacetSourceTest extends EntityKernelTestBase {
public static $modules = [
'facets',
'facets_custom_widget',
'facets_search_api_dependency',
'search_api',
'search_api_db',
'search_api_test_db',
'search_api_test_example_content',
'search_api_test_views',
'views',
'rest',
'serialization',
];
public function setUp() {
parent::setUp();
$this
->installEntitySchema('facets_facet');
$this
->installEntitySchema('entity_test_mulrev_changed');
$this
->installEntitySchema('search_api_task');
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
\Drupal::configFactory()
->getEditable('search_api.settings')
->set('tracking_page_size', 100)
->save();
$this
->installConfig([
'search_api_test_example_content',
'search_api_test_db',
]);
$this
->installConfig('search_api_test_views');
}
public function testFacetSource() {
$entity = new Facet([], 'facets_facet');
$this
->assertNull($entity
->getFacetSourceId());
$displays = $this->container
->get('plugin.manager.search_api.display')
->getDefinitions();
$this
->assertArrayHasKey('views_page:search_api_test_view__page_1', $displays);
$facet_sources = $this->container
->get('plugin.manager.facets.facet_source')
->getDefinitions();
$this
->assertArrayHasKey('search_api:views_page__search_api_test_view__page_1', $facet_sources);
$display_name = 'search_api:views_page__search_api_test_view__page_1';
$entity
->setFacetSourceId($display_name);
$this
->assertEquals($display_name, $entity
->getFacetSourceId());
$this
->assertInstanceOf(SearchApiDisplay::class, $entity
->getFacetSources()[$display_name]);
$this
->assertInstanceOf(SearchApiDisplay::class, $entity
->getFacetSource());
$this
->assertInstanceOf(FacetSourceInterface::class, $entity
->getFacetSourceConfig());
$this
->assertEquals($display_name, $entity
->getFacetSourceConfig()
->getName());
$this
->assertEquals('f', $entity
->getFacetSourceConfig()
->getFilterKey());
}
public function testInvalidQueryType() {
$entity = new Facet([], 'facets_facet');
$entity
->setWidget('links');
$entity
->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
$this
->expectException(InvalidQueryTypeException::class);
$entity
->getQueryType();
}
public function testQueryType() {
$entity = new Facet([], 'facets_facet');
$entity
->setWidget('links');
$entity
->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
$entity
->setFieldIdentifier('name');
$selectedQueryType = $entity
->getQueryType();
$this
->assertEquals('search_api_string', $selectedQueryType);
}
public function testQueryTypeJugglingInvalidWidget() {
$entity = new Facet([], 'facets_facet');
$entity
->setWidget('widget_invalid_qt');
$entity
->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
$entity
->setFieldIdentifier('name');
$this
->expectException(InvalidQueryTypeException::class);
$entity
->getQueryType();
}
public function testQueryTypeJugglingInvalidProcessor() {
$entity = new Facet([], 'facets_facet');
$entity
->setWidget('links');
$entity
->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
$entity
->setFieldIdentifier('name');
$entity
->addProcessor([
'processor_id' => 'invalid_qt',
'weights' => [],
'settings' => [],
]);
$this
->expectException(InvalidQueryTypeException::class);
$entity
->getQueryType();
}
public function testQueryTypeJugglingInvalidCombo() {
$entity = new Facet([], 'facets_facet');
$entity
->setWidget('widget_date_qt');
$entity
->setFacetSourceId('search_api:views_page__search_api_test_view__page_1');
$entity
->setFieldIdentifier('name');
$processor = [
'processor_id' => 'test_pre_query',
'weights' => [],
'settings' => [],
];
$entity
->addProcessor($processor);
$this
->expectException(InvalidQueryTypeException::class);
$entity
->getQueryType();
}
public function testDataDefinitions() {
$entity = new Facet([], 'facets_facet');
$display_name = 'search_api:views_page__search_api_test_view__page_1';
$entity
->setFacetSourceId($display_name);
$this
->assertInstanceOf(DataDefinitionInterface::class, $entity
->getFacetSource()
->getDataDefinition('id'));
$this
->assertInstanceOf(DataDefinitionInterface::class, $entity
->getFacetSource()
->getDataDefinition('name'));
$this
->assertInstanceOf(DataDefinitionInterface::class, $entity
->getFacetSource()
->getDataDefinition('category'));
$this
->expectException(Exception::class);
$entity
->getFacetSource()
->getDataDefinition('llama');
}
}