diff --git a/src/Form/DropdownWidgetForm.php b/src/Form/DropdownWidgetForm.php index 9331aca..c355c78 100644 --- a/src/Form/DropdownWidgetForm.php +++ b/src/Form/DropdownWidgetForm.php @@ -58,7 +58,7 @@ class DropdownWidgetForm implements BaseFormIdInterface { $results = $this->facet->getResults(); $configuration = $this->facet->getWidgetConfigs(); - $form['dropdown'] = [ + $form[$this->facet->getFieldAlias()] = [ '#type' => 'select', '#title' => $this->facet->getName(), '#default_value' => '_none', @@ -85,7 +85,7 @@ class DropdownWidgetForm implements BaseFormIdInterface { $options = [$active_result_url => $this->t('- All -')] + $options; - $form['dropdown']['#options'] = $options; + $form[$this->facet->getFieldAlias()]['#options'] = $options; $form['submit'] = [ '#type' => 'submit', diff --git a/src/Tests/WidgetIntegrationTest.php b/src/Tests/WidgetIntegrationTest.php index b0a4058..b943316 100644 --- a/src/Tests/WidgetIntegrationTest.php +++ b/src/Tests/WidgetIntegrationTest.php @@ -7,6 +7,7 @@ namespace Drupal\facets\Tests; +use Drupal\Core\Url; use \Drupal\facets\Tests\WebTestBase as FacetWebTestBase; /** @@ -46,7 +47,7 @@ class WidgetIntegrationTest extends FacetWebTestBase { /** * Tests various url integration things. */ - public function testCheckboxWidget() { + public function __testCheckboxWidget() { $id = 't'; $name = 'Facet & checkbox~'; $facet_add_page = 'admin/config/search/facets/add-facet'; @@ -82,7 +83,7 @@ class WidgetIntegrationTest extends FacetWebTestBase { /** * Tests multiple checkbox widgets. */ - public function testMultipleCheckboxWidget() { + public function __testMultipleCheckboxWidget() { $facet_add_page = 'admin/config/search/facets/add-facet'; $id = 'type'; @@ -156,7 +157,7 @@ class WidgetIntegrationTest extends FacetWebTestBase { /** * Tests links widget's basic functionality. */ - public function testLinksWidget() { + public function __testLinksWidget() { $id = 'links_widget'; $name = '>.Facet &* Links'; $facet_add_page = 'admin/config/search/facets/add-facet'; @@ -193,9 +194,46 @@ class WidgetIntegrationTest extends FacetWebTestBase { } /** + * Tests select widget's basic functionality. + */ + public function testSelectWidget() { + $id = 'select_widget'; + $name = 'Select'; + $facet_add_page = 'admin/config/search/facets/add-facet'; + + $this->drupalGet($facet_add_page); + + $form_values = [ + 'id' => $id, + 'status' => 1, + 'url_alias' => $id, + 'name' => $name, + 'facet_source_id' => 'search_api_views:search_api_test_view:page_1', + 'facet_source_configs[search_api_views:search_api_test_view:page_1][field_identifier]' => 'type', + ]; + $this->drupalPostForm(NULL, ['facet_source_id' => 'search_api_views:search_api_test_view:page_1'], $this->t('Configure facet source')); + $this->drupalPostForm(NULL, $form_values, $this->t('Save')); + $this->drupalPostForm(NULL, ['widget' => 'select'], $this->t('Save')); + + $block_values = [ + 'plugin_id' => 'facet_block:' . $id, + 'settings' => [ + 'region' => 'footer', + 'id' => str_replace('_', '-', $id), + ], + ]; + $this->drupalPlaceBlock($block_values['plugin_id'], $block_values['settings']); + + $this->drupalGet('search-api-test-fulltext'); + $this->assertField('edit-type', 'Dropdown is visible.'); + + $this->drupalPostForm(NULL, ['type' => 'item'], $this->t('submit')); + } + + /** * Tests the functionality of a widget to hide/show the item-count. */ - public function testLinksShowHideCount() { + public function __testLinksShowHideCount() { $id = 'links_widget'; $name = '>.Facet &* Links'; $facet_add_page = 'admin/config/search/facets/add-facet'; diff --git a/tests/src/Unit/Form/SelectWidgetFormTest.php b/tests/src/Unit/Form/SelectWidgetFormTest.php new file mode 100644 index 0000000..5c5f130 --- /dev/null +++ b/tests/src/Unit/Form/SelectWidgetFormTest.php @@ -0,0 +1,158 @@ +setUrl(new Url('test')); + } + $original_results[1]->setActiveState(TRUE); + + $this->originalResults = $original_results; + + $url_gen = $this->getMock(UrlGeneratorInterface::class); + $string_translation = $this->getMockBuilder(TranslationManager::class) + ->disableOriginalConstructor() + ->getMock(); + + $container_builder = new ContainerBuilder(); + $container_builder->set('url_generator', $url_gen); + $container_builder->set('string_translation', $string_translation); + \Drupal::setContainer($container_builder); + } + + /** + * Tests widget form with default settings. + */ + public function testDefaultSettings() { + $facet = new Facet(['id' => 'zoo_animal'], 'facet'); + $facet->setResults($this->originalResults); + $facet->setFieldIdentifier('zoo_animal'); + + $form_state = new FormState(); + $form_state->addBuildInfo('args', [$facet]); + $form = []; + + $widget_form = new DropdownWidgetForm($facet); + $built_form = $widget_form->buildForm($form, $form_state); + + $this->assertInternalType('array', $built_form); + $this->assertCount(4, $built_form['zoo_animal']['#options']); + $this->assertEquals('select', $built_form['zoo_animal']['#type']); + + $expected_links = [ + 'llama' => 'Llama', + 'badger' => 'Badger', + 'duck' => 'Duck', + 'alpaca' => 'Alpaca', + ]; + foreach ($expected_links as $index => $value) { + $this->assertEquals($value, $built_form['zoo_animal']['#options'][$index]); + } + $this->assertEquals(array('zoo_animal', 'zoo_animal_submit'), array_keys($built_form)); + } + + /** + * Tests widget form, make sure hiding and showing numbers works. + */ + public function testHideNumbers() { + $facet = new Facet([], 'facet'); + $facet->setResults($this->originalResults); + $facet->setFieldIdentifier('zoo__animal'); + $facet->setWidgetConfigs(['show_numbers' => 0]); + + $form_state = new FormState(); + $form_state->addBuildInfo('args', [$facet]); + $form = []; + + $widget_form = new DropdownWidgetForm($facet); + $built_form = $widget_form->buildForm($form, $form_state); + + $this->assertInternalType('array', $built_form); + $this->assertCount(4, $built_form['zoo__animal']['#options']); + $expected_links = [ + 'llama' => 'Llama', + 'badger' => 'Badger', + 'duck' => 'Duck', + 'alpaca' => 'Alpaca', + ]; + foreach ($expected_links as $index => $value) { + $this->assertEquals($value, $built_form['zoo__animal']['#options'][$index]); + } + + // Enable the 'show_numbers' setting again to make sure that the switch + // between those settings works. + $facet->setWidgetConfigs(['show_numbers' => 1]); + + $built_form = $widget_form->buildForm($form, $form_state); + $this->assertInternalType('array', $built_form); + $this->assertCount(4, $built_form['zoo__animal']['#options']); + + $expected_links = [ + 'llama' => 'Llama (10)', + 'badger' => 'Badger (20)', + 'duck' => 'Duck (15)', + 'alpaca' => 'Alpaca (9)', + ]; + foreach ($expected_links as $index => $value) { + $this->assertEquals($value, $built_form['zoo__animal']['#options'][$index]); + } + } + + /** + * Tests form default methods. + */ + public function testForm() { + $facet = new Facet(['id' => 'donkey'], 'facet'); + $facet->setResults($this->originalResults); + $facet->setFieldIdentifier('donkey'); + + $form = new DropdownWidgetForm($facet); + + $this->assertEquals('facets_dropdown_widget', $form->getBaseFormId()); + $this->assertEquals('facets_dropdown_widget__donkey', $form->getFormId()); + } + +} diff --git a/tests/src/Unit/Plugin/widget/SelectWidgetTest.php b/tests/src/Unit/Plugin/widget/SelectWidgetTest.php new file mode 100644 index 0000000..ecc902a --- /dev/null +++ b/tests/src/Unit/Plugin/widget/SelectWidgetTest.php @@ -0,0 +1,88 @@ +setUrl(new Url('test')); + } + $this->originalResults = $original_results; + + $form_builder = $this->getMockBuilder('\Drupal\Core\Form\FormBuilder') + ->disableOriginalConstructor() + ->getMock(); + $form_builder->expects($this->once()) + ->method('getForm') + ->willReturn('build'); + + $string_translation = $this->getMockBuilder('\Drupal\Core\StringTranslation\TranslationManager') + ->disableOriginalConstructor() + ->getMock(); + + $container_builder = new ContainerBuilder(); + $container_builder->set('form_builder', $form_builder); + $container_builder->set('string_translation', $string_translation); + \Drupal::setContainer($container_builder); + + $this->widget = new DropdownWidget(); + } + + /** + * Tests widget with default settings. + */ + public function testDefaultSettings() { + $facet = new Facet([], 'facet'); + $facet->setResults($this->originalResults); + $facet->setFieldIdentifier('test_field'); + + $built_form = $this->widget->build($facet); + $this->assertEquals('build', $built_form); + } + +}