diff --git a/facets.plugin_type.yml b/facets.plugin_type.yml index 1fd9cbf..8db8901 100644 --- a/facets.plugin_type.yml +++ b/facets.plugin_type.yml @@ -3,6 +3,11 @@ facets_processor: plugin_manager_service_id: plugin.manager.facets.processor plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator +facets_url_processor: + label: Facets URL processor + plugin_manager_service_id: plugin.manager.facets.url_processor + plugin_definition_decorator_class: \Drupal\plugin\PluginDefinition\ArrayPluginDefinitionDecorator + facets_facet_source: label: Facets source plugin_manager_service_id: plugin.manager.facets.facet_source diff --git a/src/Entity/FacetSource.php b/src/Entity/FacetSource.php index 14c792e..0ed4cc8 100644 --- a/src/Entity/FacetSource.php +++ b/src/Entity/FacetSource.php @@ -77,13 +77,6 @@ class FacetSource extends ConfigEntityBase implements FacetSourceInterface { protected $urlProcessor = 'query_string'; /** - * The url processor instance. - * - * @var \Drupal\facets\UrlProcessor\UrlProcessorInterface - */ - protected $urlProcessorInstance = NULL; - - /** * {@inheritdoc} */ public function id() { diff --git a/src/Form/FacetSourceEditForm.php b/src/Form/FacetSourceEditForm.php index 1eaba54..bdc7779 100644 --- a/src/Form/FacetSourceEditForm.php +++ b/src/Form/FacetSourceEditForm.php @@ -33,13 +33,13 @@ class FacetSourceEditForm extends EntityForm { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $etm */ - $etm = $container->get('entity_type.manager'); + /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ + $entity_type_manager = $container->get('entity_type.manager'); - /** @var \Drupal\facets\UrlProcessor\UrlProcessorPluginManager $uppm */ - $uppm = $container->get('plugin.manager.facets.url_processor'); + /** @var \Drupal\facets\UrlProcessor\UrlProcessorPluginManager $url_processor_plugin_manager */ + $url_processor_plugin_manager = $container->get('plugin.manager.facets.url_processor'); - return new static($etm, $uppm); + return new static($entity_type_manager, $url_processor_plugin_manager); } /** diff --git a/src/Tests/UrlIntegrationTest.php b/src/Tests/UrlIntegrationTest.php index 62339d3..565392e 100644 --- a/src/Tests/UrlIntegrationTest.php +++ b/src/Tests/UrlIntegrationTest.php @@ -32,20 +32,26 @@ class UrlIntegrationTest extends FacetWebTestBase { ]; /** - * Tests various url integration things. + * {@inheritdoc} */ - public function testUrlIntegration() { + public function setUp() { + parent::setUp(); + $this->drupalLogin($this->adminUser); $this->insertExampleContent(); $this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.'); + } + /** + * Tests various url integration things. + */ + public function testUrlIntegration() { $id = 'facet'; $name = '&^Facet@#1'; - $facet_add_page = 'admin/config/search/facets/add-facet'; + $this->drupalGet($facet_add_page); - $this->assertResponse(200); $form_values = [ 'id' => $id, @@ -57,47 +63,38 @@ class UrlIntegrationTest extends FacetWebTestBase { ]; $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_views_fulltext:page_1'], $this->t('Configure facet source')); $this->drupalPostForm(NULL, $form_values, $this->t('Save')); - $this->assertNoText('field is required.'); - - $this->assertRaw(t('Facet %name has been created.', ['%name' => $name])); + // Go to the only enabled facet source's config. $this->drupalGet('admin/config/search/facets'); + $this->clickLink($this->t('Configure')); $edit = [ 'filterKey' => 'y', 'urlProcessor' => 'dummy_query', ]; - $this->clickLink($this->t('Configure')); $this->drupalPostForm(NULL, $edit, $this->t('Save')); /** @var \Drupal\facets\FacetInterface $facet */ $facet = \Drupal::service('entity_type.manager')->getStorage('facets_facet')->load($id); $block_values = [ - [ - 'label' => 'Facet Block', - 'tr' => '16', - 'plugin_id' => 'facet_block', - 'settings' => [ - 'region' => 'footer', - 'id' => str_replace('_', '-', $id), - 'context_mapping' => [ - 'facet' => '@facets.facet_context:' . $facet->uuid(), - ], - ], - 'test_weight' => '0', + 'region' => 'footer', + 'id' => str_replace('_', '-', $id), + 'context_mapping' => [ + 'facet' => '@facets.facet_context:' . $facet->uuid(), ], ]; - foreach ($block_values as $values) { - $this->drupalPlaceBlock($values['plugin_id'], $values['settings']); - } + $this->drupalPlaceBlock('facet_block', $block_values); $this->drupalGet('search-api-test-fulltext'); + $this->assertResponse(200); $this->assertLink('item'); $this->assertLink('article'); $this->clickLink('item'); + $this->assertResponse(200); $this->assertLink('(-) item'); + $this->assertNoLink('article'); $this->assertUrl('search-api-test-fulltext?y[0]=facet||item'); } diff --git a/src/UrlProcessor/UrlProcessorInterface.php b/src/UrlProcessor/UrlProcessorInterface.php index e040db4..3bc51a1 100644 --- a/src/UrlProcessor/UrlProcessorInterface.php +++ b/src/UrlProcessor/UrlProcessorInterface.php @@ -22,8 +22,27 @@ use Drupal\facets\FacetInterface; */ interface UrlProcessorInterface { + /** + * Adds urls to the results. + * + * @param \Drupal\facets\FacetInterface $facet + * The facet. + * @param \Drupal\facets\Result\ResultInterface[] $results + * An array of results. + * + * @return \Drupal\facets\Result\ResultInterface[] + * An array of results with added urls. + */ public function buildUrls(FacetInterface $facet, array $results); + /** + * Sets active items. + * + * Makes sure that the items that are already set in the current request are + * remembered when building the facet's urls. + * + * @param \Drupal\facets\FacetInterface $facet + */ public function setActiveItems(FacetInterface $facet); /** diff --git a/src/UrlProcessor/UrlProcessorPluginManager.php b/src/UrlProcessor/UrlProcessorPluginManager.php index d3f3dcf..84008fd 100644 --- a/src/UrlProcessor/UrlProcessorPluginManager.php +++ b/src/UrlProcessor/UrlProcessorPluginManager.php @@ -11,7 +11,6 @@ use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\StringTranslation\StringTranslationTrait; -use Drupal\Core\StringTranslation\TranslationInterface; /** * Manages URL processor plugins. diff --git a/tests/src/Unit/Plugin/url_processor/QueryStringTest.php b/tests/src/Unit/Plugin/url_processor/QueryStringTest.php index 9698c84..df418fb 100644 --- a/tests/src/Unit/Plugin/url_processor/QueryStringTest.php +++ b/tests/src/Unit/Plugin/url_processor/QueryStringTest.php @@ -55,6 +55,14 @@ class QueryStringTest extends UnitTestCase { } /** + * Tests that the processor correctly throws an exception. + */ + public function testEmptyProcessorConfiguration() { + $this->setExpectedException('\Drupal\facets\Exception\InvalidProcessorException', "The url processor doesn't have the required 'facet' in the configuration array."); + new QueryString([], 'test', [], new Request()); + } + + /** * Basic test with one active item. */ public function testSetSingleActiveItem() {