diff --git a/src/Form/DropdownWidgetForm.php b/src/Form/DropdownWidgetForm.php index c355c78..cd739e1 100644 --- a/src/Form/DropdownWidgetForm.php +++ b/src/Form/DropdownWidgetForm.php @@ -87,7 +87,7 @@ class DropdownWidgetForm implements BaseFormIdInterface { $form[$this->facet->getFieldAlias()]['#options'] = $options; - $form['submit'] = [ + $form[$this->facet->getFieldAlias() . '_submit'] = [ '#type' => 'submit', '#value' => 'submit', ]; @@ -104,7 +104,9 @@ class DropdownWidgetForm implements BaseFormIdInterface { * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { - $form_state->setResponse(new RedirectResponse($form_state->getValue('dropdown'))); + $field_alias = $this->facet->getFieldAlias(); + $form_value = $form_state->getValue($field_alias); + $form_state->setResponse(new RedirectResponse($form_value)); } } diff --git a/src/Tests/WidgetIntegrationTest.php b/src/Tests/WidgetIntegrationTest.php index b943316..e83fd60 100644 --- a/src/Tests/WidgetIntegrationTest.php +++ b/src/Tests/WidgetIntegrationTest.php @@ -47,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'; @@ -83,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'; @@ -157,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'; @@ -226,14 +226,20 @@ class WidgetIntegrationTest extends FacetWebTestBase { $this->drupalGet('search-api-test-fulltext'); $this->assertField('edit-type', 'Dropdown is visible.'); + $this->assertText('Displaying 5 search results'); - $this->drupalPostForm(NULL, ['type' => 'item'], $this->t('submit')); + $url = Url::fromUserInput('/search-api-test-fulltext', ['query' => ['f[0]' => 'select_widget:item']]); + $url->setAbsolute(); + + $this->drupalPostForm(NULL, ['type' => $url->toString()], $this->t('submit')); + $this->assertResponse(200); + $this->assertText('Displaying 3 search results'); } /** * 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 index 5c5f130..9510fcc 100644 --- a/tests/src/Unit/Form/SelectWidgetFormTest.php +++ b/tests/src/Unit/Form/SelectWidgetFormTest.php @@ -37,28 +37,32 @@ class SelectWidgetFormTest extends UnitTestCase { protected function setUp() { parent::setUp(); - /** @var \Drupal\facets\Result\Result[] $original_results */ - $original_results = [ - new Result('llama', 'Llama', 10), - new Result('badger', 'Badger', 20), - new Result('duck', 'Duck', 15), - new Result('alpaca', 'Alpaca', 9), + $result = new Result('llama', 'Llama', 10); + $result->setUrl(new Url('llama')); + $result2 = new Result('badger', 'Badger', 20); + $result2->setUrl(new Url('badger')); + $result3 = new Result('duck', 'Duck', 15); + $result3->setUrl(new Url('duck')); + $result4 = new Result('alpaca', 'Alpaca', 9); + $result4->setUrl(new Url('alpaca')); + + $this->originalResults = [ + $result, + $result2, + $result3, + $result4, ]; - foreach ($original_results as $original_result) { - $original_result->setUrl(new Url('test')); - } - $original_results[1]->setActiveState(TRUE); - - $this->originalResults = $original_results; - - $url_gen = $this->getMock(UrlGeneratorInterface::class); + $url_generator = $this->getMock(UrlGeneratorInterface::class); + $url_generator->expects($this->any()) + ->method('generateFromRoute') + ->willReturnCallback(function ($param) { return 'http://test/' . $param; }); $string_translation = $this->getMockBuilder(TranslationManager::class) ->disableOriginalConstructor() ->getMock(); $container_builder = new ContainerBuilder(); - $container_builder->set('url_generator', $url_gen); + $container_builder->set('url_generator', $url_generator); $container_builder->set('string_translation', $string_translation); \Drupal::setContainer($container_builder); } @@ -79,14 +83,14 @@ class SelectWidgetFormTest extends UnitTestCase { $built_form = $widget_form->buildForm($form, $form_state); $this->assertInternalType('array', $built_form); - $this->assertCount(4, $built_form['zoo_animal']['#options']); + $this->assertCount(5, $built_form['zoo_animal']['#options']); $this->assertEquals('select', $built_form['zoo_animal']['#type']); $expected_links = [ - 'llama' => 'Llama', - 'badger' => 'Badger', - 'duck' => 'Duck', - 'alpaca' => 'Alpaca', + 'http://test/llama' => 'Llama', + 'http://test/badger' => 'Badger', + 'http://test/duck' => 'Duck', + 'http://test/alpaca' => 'Alpaca', ]; foreach ($expected_links as $index => $value) { $this->assertEquals($value, $built_form['zoo_animal']['#options'][$index]); @@ -111,12 +115,12 @@ class SelectWidgetFormTest extends UnitTestCase { $built_form = $widget_form->buildForm($form, $form_state); $this->assertInternalType('array', $built_form); - $this->assertCount(4, $built_form['zoo__animal']['#options']); + $this->assertCount(5, $built_form['zoo__animal']['#options']); $expected_links = [ - 'llama' => 'Llama', - 'badger' => 'Badger', - 'duck' => 'Duck', - 'alpaca' => 'Alpaca', + 'http://test/llama' => 'Llama', + 'http://test/badger' => 'Badger', + 'http://test/duck' => 'Duck', + 'http://test/alpaca' => 'Alpaca', ]; foreach ($expected_links as $index => $value) { $this->assertEquals($value, $built_form['zoo__animal']['#options'][$index]); @@ -128,13 +132,13 @@ class SelectWidgetFormTest extends UnitTestCase { $built_form = $widget_form->buildForm($form, $form_state); $this->assertInternalType('array', $built_form); - $this->assertCount(4, $built_form['zoo__animal']['#options']); + $this->assertCount(5, $built_form['zoo__animal']['#options']); $expected_links = [ - 'llama' => 'Llama (10)', - 'badger' => 'Badger (20)', - 'duck' => 'Duck (15)', - 'alpaca' => 'Alpaca (9)', + 'http://test/llama' => 'Llama (10)', + 'http://test/badger' => 'Badger (20)', + 'http://test/duck' => 'Duck (15)', + 'http://test/alpaca' => 'Alpaca (9)', ]; foreach ($expected_links as $index => $value) { $this->assertEquals($value, $built_form['zoo__animal']['#options'][$index]);