Commit 8474b9f6 authored by borisson_'s avatar borisson_ Committed by borisson_
Browse files

Issue #2680301 by borisson_: Expand processor test coverage

parent 8cde322f
......@@ -14,6 +14,17 @@ plugin.plugin_configuration.facets_processor.display_value_widget_order:
type: string
label: sort order
plugin.plugin_configuration.facets_processor.exclude_specified_items:
type: mapping
label: 'Exclude specified items'
mapping:
exclude:
type: string
label: Exclude
regex:
type: boolean
label: Regex
plugin.plugin_configuration.facets_processor.raw_value_widget_order:
type: mapping
label: 'Raw value widget order'
......@@ -30,6 +41,14 @@ plugin.plugin_configuration.facets_processor.active_widget_order:
type: string
label: sort order
plugin.plugin_configuration.facets_processor.count_widget_order:
type: mapping
label: 'Active widget order'
mapping:
sort:
type: string
label: sort order
plugin.plugin_configuration.facets_processor.count_limit:
type: mapping
label: 'Count limit widget'
......@@ -46,3 +65,6 @@ plugin.plugin_configuration.facets_processor.url_processor_handler:
plugin.plugin_configuration.facets_processor.hide_non_narrowing_result_processor:
type: config_object
plugin.plugin_configuration.facets_processor.hide_active_items_processor:
type: config_object
......@@ -7,6 +7,7 @@
namespace Drupal\facets\Tests;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\search_api\Entity\Index;
/**
......@@ -94,4 +95,54 @@ trait ExampleContentTrait {
return $index->indexItems();
}
/**
* Add a facet trough the UI.
*
* @param string $name
* The facet name.
* @param string $id
* The facet id.
* @param string $field
* The facet field.
*/
protected function createFacet($name, $id, $field = 'type') {
$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]' => $field,
];
$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'));
}
/**
* Asserts that a string is found before another string in the source.
*
* This uses the simpletest's getRawContent method to search in the source of
* the page for the position of 2 strings and that the first argument is
* before the second argument's position.
*
* @param string $x
* A string.
* @param string $y
* Another string.
*/
protected function assertStringPosition($x, $y) {
$this->assertText($x);
$this->assertText($y);
$x_position = strpos($this->getRawContent(), $x);
$y_position = strpos($this->getRawContent(), $y);
$message = new FormattableMarkup('Assert that %x is before %y in the source', ['%x' => $x, '%y' => $y]);
$this->assertTrue($x_position < $y_position, $message);
}
}
......@@ -385,62 +385,6 @@ class IntegrationTest extends FacetWebTestBase {
$this->assertNoText($this->t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
}
/**
* Tests for processor integration.
*/
public function testProcessorIntegration() {
$facet_name = "Snowy owl~";
$facet_id = "snowy_owl_";
$this->addFacet($facet_name);
// Go to the processors form and check that the count limit processor is not
// checked.
$this->drupalGet('admin/config/search/facets/' . $facet_id . '/display');
$this->assertNoFieldChecked('edit-facet-settings-count-limit-status');
$form = ['facet_settings[count_limit][status]' => TRUE];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
// Enable the sort processor and change sort direction.
// Test that the sort settings change persisted.
$form = [
'facet_sorting[active_widget_order][status]' => TRUE,
'facet_sorting[active_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-sorting-active-widget-order-status');
$this->assertFieldChecked('edit-facet-sorting-active-widget-order-settings-sort-desc');
// Add an extra processor so we can test the weights as well.
$form = [
'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
$this->assertFieldChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
$this->assertOptionSelected('edit-processors-count-limit-weights-build', -10);
$this->assertOptionSelected('edit-processors-hide-non-narrowing-result-processor-weights-build', -10);
// Change the weight of one of the processors and test that the weight
// change persisted.
$form = [
'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
'processors[hide_non_narrowing_result_processor][weights][build]' => 5,
];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
$this->assertFieldChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
$this->assertOptionSelected('edit-processors-count-limit-weights-build', -10);
$this->assertOptionSelected('edit-processors-hide-non-narrowing-result-processor-weights-build', 5);
}
/**
* Tests the facet's exclude functionality.
*/
......
<?php
/**
* @file
* Contains \Drupal\facets\Tests\ProcessorIntegrationTest.
*/
namespace Drupal\facets\Tests;
use Drupal\facets\Tests\WebTestBase as FacetWebTestBase;
/**
* Tests the processor functionality.
*
* @group facets
*/
class ProcessorIntegrationTest extends FacetWebTestBase {
/**
* The url of the edit form.
*
* @var string
*/
protected $editForm;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
// Set up example content types and insert 10 new content items.
$this->setUpExampleStructure();
$this->insertExampleContent();
$this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.');
$this->insertExampleContent();
$this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.');
}
/**
* Tests for the processors behavior in the backend.
*/
public function testProcessorAdmin() {
$facet_name = "Guanaco";
$facet_id = "guanaco";
$this->createFacet($facet_name, $facet_id);
// Go to the processors form and check that the count limit processor is not
// checked.
$this->drupalGet('admin/config/search/facets/' . $facet_id . '/display');
$this->assertNoFieldChecked('edit-facet-settings-count-limit-status');
$form = ['facet_settings[count_limit][status]' => TRUE];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
// Enable the sort processor and change sort direction, check that the
// change is persisted.
$form = [
'facet_sorting[active_widget_order][status]' => TRUE,
'facet_sorting[active_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-sorting-active-widget-order-status');
$this->assertFieldChecked('edit-facet-sorting-active-widget-order-settings-sort-desc');
// Add an extra processor so we can test the weights as well.
$form = [
'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
$this->assertFieldChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
$this->assertOptionSelected('edit-processors-count-limit-weights-build', -10);
$this->assertOptionSelected('edit-processors-hide-non-narrowing-result-processor-weights-build', -10);
// Change the weight of one of the processors and test that the weight
// change persisted.
$form = [
'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
'processors[hide_non_narrowing_result_processor][weights][build]' => 5,
];
$this->drupalPostForm(NULL, $form, $this->t('Save'));
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
$this->assertFieldChecked('edit-facet-settings-hide-non-narrowing-result-processor-status');
$this->assertOptionSelected('edit-processors-count-limit-weights-build', -10);
$this->assertOptionSelected('edit-processors-hide-non-narrowing-result-processor-weights-build', 5);
}
/**
* Tests the for processors in the frontend with a 'keywords' facet.
*/
public function testProcessorIntegration() {
$facet_name = "Vicuña";
$facet_id = "vicuna";
$this->editForm = 'admin/config/search/facets/' . $facet_id . '/display';
$this->createFacet($facet_name, $facet_id, 'keywords');
$this->createFacetBlock($facet_id);
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertText('grape');
$this->assertText('orange');
$this->assertText('apple');
$this->assertText('strawberry');
$this->assertText('banana');
$this->checkCountLimitProcessor();
$this->checkExcludeItems();
$this->checkHideNonNarrowingProcessor();
$this->checkHideActiveItems();
}
/**
* Tests the for sorting processors in the frontend with a 'keywords' facet.
*/
public function testSortingWidgets() {
$facet_name = "Huacaya alpaca";
$facet_id = "huacaya_alpaca";
$this->editForm = 'admin/config/search/facets/' . $facet_id . '/display';
$this->createFacet($facet_name, $facet_id, 'keywords');
$this->createFacetBlock($facet_id);
$this->checkSortByActive();
$this->checkSortByCount();
$this->checkSortByDisplay();
$this->checkSortByRaw();
}
/**
* Tests the count limit processor.
*/
private function checkCountLimitProcessor() {
$this->drupalGet($this->editForm);
$form = [
'widget_configs[show_numbers]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->assertResponse(200);
$this->assertFieldChecked('edit-facet-settings-count-limit-status');
$form = [
'widget_configs[show_numbers]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$form = [
'widget_configs[show_numbers]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
'facet_settings[count_limit][settings][minimum_items]' => 5,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertText('grape (6)');
$this->assertNoText('apple (4)');
$form = [
'widget_configs[show_numbers]' => TRUE,
'facet_settings[count_limit][status]' => TRUE,
'facet_settings[count_limit][settings][minimum_items]' => 1,
'facet_settings[count_limit][settings][maximum_items]' => 5,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertNoText('grape (6)');
$this->assertText('apple (4)');
$form = [
'widget_configs[show_numbers]' => FALSE,
'facet_settings[count_limit][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests the exclude items.
*/
private function checkExcludeItems() {
$form = [
'facet_settings[exclude_specified_items][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$form = [
'facet_settings[exclude_specified_items][status]' => TRUE,
'facet_settings[exclude_specified_items][settings][exclude]' => 'banana',
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertText('grape');
$this->assertNoText('banana');
$form = [
'facet_settings[exclude_specified_items][status]' => TRUE,
'facet_settings[exclude_specified_items][settings][exclude]' => '(.*)berry',
'facet_settings[exclude_specified_items][settings][regex]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertNoText('strawberry');
$this->assertText('grape');
$form = [
'facet_settings[exclude_specified_items][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests hiding non-narrowing results.
*/
private function checkHideNonNarrowingProcessor() {
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertLink('grape');
$this->clickLink('grape');
$this->assertText('Displaying 6 search results');
$this->assertLink('orange');
$form = [
'facet_settings[hide_non_narrowing_result_processor][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertLink('grape');
$this->clickLink('grape');
$this->assertText('Displaying 6 search results');
$this->assertNoLink('orange');
$form = [
'facet_settings[hide_non_narrowing_result_processor][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests hiding active results.
*/
private function checkHideActiveItems() {
$form = [
'facet_settings[hide_active_items_processor][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertLink('grape');
$this->assertLink('banana');
$this->clickLink('grape');
$this->assertText('Displaying 6 search results');
$this->assertNoLink('grape');
$this->assertLink('banana');
$form = [
'facet_settings[hide_active_items_processor][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests the active widget order.
*/
protected function checkSortByActive() {
$form = [
'facet_sorting[active_widget_order][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->clickLink('strawberry');
$this->assertStringPosition('(-) strawberry', 'grape');
$form = [
'facet_sorting[active_widget_order][status]' => TRUE,
'facet_sorting[active_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->clickLink('strawberry');
$this->assertStringPosition('grape', '(-) strawberry');
$form = [
'facet_sorting[active_widget_order][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests the active widget order.
*/
protected function checkSortByCount() {
$form = [
'widget_configs[show_numbers]' => TRUE,
'facet_sorting[count_widget_order][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('banana', 'apple');
$this->assertStringPosition('banana', 'strawberry');
$this->assertStringPosition('apple', 'orange');
$form = [
'facet_sorting[count_widget_order][status]' => TRUE,
'facet_sorting[count_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('apple', 'banana');
$this->assertStringPosition('strawberry', 'banana');
$this->assertStringPosition('orange', 'apple');
$form = [
'widget_configs[show_numbers]' => FALSE,
'facet_sorting[count_widget_order][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests the display order.
*/
public function checkSortByDisplay() {
$form = [
'facet_sorting[display_value_widget_order][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('grape', 'strawberry');
$this->assertStringPosition('apple', 'banana');
$form = [
'facet_sorting[display_value_widget_order][status]' => TRUE,
'facet_sorting[display_value_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('strawberry', 'grape');
$this->assertStringPosition('banana', 'apple');
$form = [
'facet_sorting[display_value_widget_order][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Tests the display order.
*/
public function checkSortByRaw() {
$form = [
'facet_sorting[raw_value_widget_order][status]' => TRUE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('grape', 'strawberry');
$this->assertStringPosition('apple', 'banana');
$form = [
'facet_sorting[raw_value_widget_order][status]' => TRUE,
'facet_sorting[raw_value_widget_order][settings][sort]' => 'DESC',
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
$this->drupalGet('search-api-test-fulltext');
$this->assertStringPosition('strawberry', 'grape');
$this->assertStringPosition('banana', 'apple');
$form = [
'facet_sorting[raw_value_widget_order][status]' => FALSE,
];
$this->drupalPostForm($this->editForm, $form, $this->t('Save'));
}
/**
* Creates a facet block by id.
*
* @param string $id
* The id of the block.
*/
protected function createFacetBlock($id) {
$plugin_id = 'facet_block:' . $id;
$settings = [
'region' => 'footer',
'id' => str_replace('_', '-', $id),
];
$this->blocks[$id] = $this->drupalPlaceBlock($plugin_id, $settings);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment