Commit 67112f9e authored by borisson_'s avatar borisson_ Committed by borisson_
Browse files

Issue #2903527 by borisson_: Clean up tests + coding standards

parent 64c56021
......@@ -17,7 +17,6 @@ No other modules required, we're supporting drupal core's search as a source for
creating facets. Though we recommend using Search API, as that integration is
better tested.
INSTALLATION
------------
* Install as you would normally install a contributed drupal module. See:
......
......@@ -22,7 +22,6 @@ function facets_update_8001() {
// This only works for the 3 default plugins that we previously shipped. So
// only views that have a page, block, or rest display. The id will get
// replaced from views_page:foo to search_api:views_page__foo.
$old_ids = ['views_page', 'views_block', 'views_rest'];
/** @var \Drupal\facets\FacetInterface[] $entities */
......@@ -79,9 +78,9 @@ function facets_update_8002() {
}
$database->update('config')
->fields(array(
->fields([
'data' => serialize($data),
))
])
->condition('name', $result->name)
->execute();
}
......
......@@ -68,9 +68,6 @@ function facets_theme($existing, $type, $theme, $path) {
/**
* Implements hook_search_api_query_alter().
*
* @param \Drupal\search_api\Query\QueryInterface $query
* Generates query for facet search.
*/
function facets_search_api_query_alter(QueryInterface &$query) {
if ($query->getIndex()->getServerInstance()->supportsFeature('search_api_facets')) {
......@@ -214,7 +211,7 @@ function facets_entity_predelete(EntityInterface $entity) {
*
* @see https://www.drupal.org/node/1842756
*/
function facets_preprocess_facets_item_list(&$variables) {
function facets_preprocess_facets_item_list(array &$variables) {
template_preprocess_item_list($variables);
}
......@@ -322,7 +319,7 @@ function facets_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInter
// Because we can't get the desired url trough a url processor method
// we iterate each result url and remove the facet params that haven't
// been used on previous crumbs
// been used on previous crumbs.
foreach ($facet_results as $res) {
$facet_url = $res->getUrl();
/** @var \Drupal\Core\Url $facet_url */
......
......@@ -17,9 +17,7 @@ abstract class CoreSearchFacetsTestBase extends BrowserTestBase {
use TestHelperTrait;
/**
* Modules to enable for this test.
*
* @var string[]
* {@inheritdoc}
*/
public static $modules = [
'field',
......
......@@ -51,7 +51,7 @@ class HooksTest extends CoreSearchFacetsTestBase {
// Verify that hook_facets_core_allowed_field_types was triggered.
$facet_add_page = 'admin/config/search/facets/add-facet';
$this->drupalGet($facet_add_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
// Select the node_search facet source.
$this->drupalGet($facet_add_page);
......@@ -62,7 +62,7 @@ class HooksTest extends CoreSearchFacetsTestBase {
);
// The field appears as expected.
$this->assertText('Float Field Label');
$this->assertSession()->pageTextContains('Float Field Label');
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\core_search_facets\Functional;
use Drupal\facets\Entity\Facet;
use Drupal\Tests\facets\Functional\BlockTestTrait;
use Drupal\node\Entity\Node;
......@@ -85,8 +86,8 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Verify that the "empty_text" appears as expected.
$this->setEmptyBehaviorFacetText($facet_name);
$this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
$this->assertRaw('block-test-facet-name');
$this->assertRaw('No results found for this block!');
$this->assertSession()->responseContains('block-test-facet-name');
$this->assertSession()->responseContains('No results found for this block!');
// Delete the block.
$this->deleteBlock($facet_id);
......@@ -100,10 +101,9 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
* Tests the "Post date" facets.
*/
public function testPostDate() {
$facet_name = 'Tardigrade';
$facet_id = 'tardigrade';
$this->addFacet($facet_id, $facet_name, 'created');
$this->addFacet($facet_id, 'Tardigrade', 'created');
$this->blocks[$facet_id] = $this->createBlock($facet_id);
$this->setShowAmountOfResults($facet_id, TRUE);
......@@ -115,22 +115,22 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
$this->assertFacetLabel('February 2016 (9)');
$this->assertFacetLabel('March 2016 (10)');
$this->assertFacetLabel('April 2016 (10)');
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$this->clickPartialLink('March 2016');
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$this->assertFacetLabel('March 8, 2016 (1)');
$this->assertFacetLabel('March 9, 2016 (2)');
$this->clickPartialLink('March 9');
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$this->assertFacetLabel('10 AM (1)');
$this->assertFacetLabel('12 PM (1)');
$this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
$this->assertFacetLabel('April 2016 (10)');
$this->clickPartialLink('April 2016');
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$this->assertFacetLabel('April 1, 2016 (1)');
$this->assertFacetLabel('April 2, 2016 (1)');
}
......@@ -139,10 +139,9 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
* Tests the "Updated date" facets.
*/
public function testUpdatedDate() {
$facet_name = 'Tardigrade';
$facet_id = 'tardigrade';
$this->addFacet($facet_id, $facet_name, 'changed');
$this->addFacet($facet_id, 'Tardigrade', 'changed');
$this->blocks[$facet_id] = $this->createBlock($facet_id);
$this->setShowAmountOfResults($facet_id, TRUE);
......@@ -200,15 +199,24 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
* The field type.
*/
protected function addFacet($id, $name, $type = 'type') {
$this->drupalGet('admin/config/search/facets/add-facet');
$form_values = [
/** @var \Drupal\facets\FacetInterface $facet */
$facet = Facet::create([
'id' => $id,
'name' => $name,
'facet_source_id' => 'core_node_search:node_search',
'facet_source_configs[core_node_search:node_search][field_identifier]' => $type,
];
$this->drupalPostForm(NULL, ['facet_source_id' => 'core_node_search:node_search'], $this->t('Configure facet source'));
$this->drupalPostForm(NULL, $form_values, $this->t('Save'));
'weight' => 0,
]);
$facet->setFacetSourceId('core_node_search:node_search');
$facet->setFieldIdentifier($type);
$facet->setUrlAlias($id);
$facet->setWidget('links', ['show_numbers' => TRUE]);
$facet->addProcessor([
'processor_id' => 'url_processor_handler',
'weights' => ['pre_query' => -10, 'build' => -10],
'settings' => [],
]);
$facet->setEmptyBehavior(['behavior' => 'none']);
$facet->setOnlyVisibleWhenFacetSourceIsVisible(TRUE);
$facet->save();
}
/**
......@@ -223,12 +231,12 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$edit = [
'widget_config[show_numbers]' => $show,
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
$this->drupalPostForm(NULL, $edit, 'Save');
}
/**
......@@ -244,14 +252,14 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
// Configure the text for empty results behavior.
$edit = [
'facet_settings[empty_behavior]' => 'text',
'facet_settings[empty_behavior_container][empty_behavior_text][value]' => 'No results found for this block!',
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
$this->drupalPostForm(NULL, $edit, 'Save');
}
/**
......@@ -265,14 +273,14 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$edit = [
'facet_settings[only_visible_when_facet_source_is_visible]' => TRUE,
'widget' => 'links',
'widget_config[show_numbers]' => '0',
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
$this->drupalPostForm(NULL, $edit, 'Save');
}
/**
......@@ -281,11 +289,11 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
protected function checkEmptyOverview() {
$facet_overview = '/admin/config/search/facets';
$this->drupalGet($facet_overview);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
// The list overview has Field: field_name as description. This tests on the
// absence of that.
$this->assertNoText('Field:');
$this->assertSession()->pageTextNotContains('Field:');
}
/**
......@@ -302,7 +310,7 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Go to the Add facet page and make sure that returns a 200.
$facet_add_page = '/admin/config/search/facets/add-facet';
$this->drupalGet($facet_add_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
$form_values = [
'name' => '',
......@@ -311,34 +319,34 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Try filling out the form, but without having filled in a name for the
// facet to test for form errors.
$this->drupalPostForm($facet_add_page, $form_values, $this->t('Save'));
$this->assertText($this->t('Name field is required.'));
$this->assertText($this->t('Facet source field is required.'));
$this->drupalPostForm($facet_add_page, $form_values, 'Save');
$this->assertSession()->pageTextContains('Name field is required.');
$this->assertSession()->pageTextContains('Facet source field is required.');
// Make sure that when filling out the name, the form error disappears.
$form_values['name'] = $facet_name;
$this->drupalPostForm(NULL, $form_values, $this->t('Save'));
$this->assertNoText($this->t('Facet name field is required.'));
$this->drupalPostForm(NULL, $form_values, 'Save');
$this->assertSession()->pageTextNotContains('Facet name field is required.');
// Configure the facet source by selecting one of the Search API views.
$this->drupalGet($facet_add_page);
$this->drupalPostForm(NULL, ['facet_source_id' => 'core_node_search:node_search'], $this->t('Configure facet source'));
$this->drupalPostForm(NULL, ['facet_source_id' => 'core_node_search:node_search'], 'Configure facet source');
// The facet field is still required.
$this->drupalPostForm(NULL, $form_values, $this->t('Save'));
$this->assertText($this->t('Field field is required.'));
$this->drupalPostForm(NULL, $form_values, 'Save');
$this->assertSession()->pageTextContains('Field field is required.');
// Fill in all fields and make sure the 'field is required' message is no
// longer shown.
$facet_source_form = [
'facet_source_configs[core_node_search:node_search][field_identifier]' => $type,
];
$this->drupalPostForm(NULL, $form_values + $facet_source_form, $this->t('Save'));
$this->assertNoText('field is required.');
$this->drupalPostForm(NULL, $form_values + $facet_source_form, 'Save');
$this->assertSession()->pageTextNotContains('field is required.');
// Make sure that the redirection to the display page is correct.
$this->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
$this->assertUrl('admin/config/search/facets/' . $facet_id . '/edit');
$this->assertSession()->responseContains(t('Facet %name has been created.', ['%name' => $facet_name]));
$this->assertSession()->addressEquals('admin/config/search/facets/' . $facet_id . '/edit');
$this->drupalGet('admin/config/search/facets');
}
......@@ -356,28 +364,28 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
$this->assertRaw($this->t('Facet settings for @facet facet', ['@facet' => $facet_name]));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->responseContains($this->t('Facet settings for @facet facet', ['@facet' => $facet_name]));
// Change the facet name to add in "-2" to test editing of a facet works.
$form_values = ['name' => $facet_name . ' - 2'];
$this->drupalPostForm($facet_edit_page, $form_values, $this->t('Save'));
$this->drupalPostForm($facet_edit_page, $form_values, 'Save');
// Make sure that the redirection back to the overview was successful and
// the edited facet is shown on the overview page.
$this->assertRaw(t('Facet %name has been updated.', ['%name' => $facet_name . ' - 2']));
$this->assertSession()->responseContains(t('Facet %name has been updated.', ['%name' => $facet_name . ' - 2']));
// Make sure the "-2" suffix is still on the facet when editing a facet.
$this->drupalGet($facet_edit_page);
$this->assertRaw($this->t('Facet settings for @facet facet', ['@facet' => $facet_name . ' - 2']));
$this->assertSession()->responseContains($this->t('Facet settings for @facet facet', ['@facet' => $facet_name . ' - 2']));
// Edit the form and change the facet's name back to the initial name.
$form_values = ['name' => $facet_name];
$this->drupalPostForm($facet_edit_page, $form_values, $this->t('Save'));
$this->drupalPostForm($facet_edit_page, $form_values, 'Save');
// Make sure that the redirection back to the overview was successful and
// the edited facet is shown on the overview page.
$this->assertRaw(t('Facet %name has been updated.', ['%name' => $facet_name]));
$this->assertSession()->responseContains(t('Facet %name has been updated.', ['%name' => $facet_name]));
}
/**
......@@ -393,11 +401,11 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Go to the facet delete page and make the warning is shown.
$this->drupalGet($facet_delete_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
// Check that the facet by testing for the message and the absence of the
// facet name on the overview.
$this->assertRaw($this->t("The facet is currently used in a block and thus can't be removed. Remove the block first."));
$this->assertSession()->responseContains("The facet is currently used in a block and thus can't be removed. Remove the block first.");
}
/**
......@@ -413,34 +421,21 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Go to the facet delete page and make the warning is shown.
$this->drupalGet($facet_delete_page);
$this->assertResponse(200);
$this->assertText($this->t('This action cannot be undone'));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('This action cannot be undone');
// Actually submit the confirmation form.
$this->drupalPostForm(NULL, [], $this->t('Delete'));
// Check that the facet by testing for the message and the absence of the
// facet name on the overview.
$this->assertRaw($this->t('The facet %facet has been deleted.', ['%facet' => $facet_name]));
$this->assertSession()->responseContains($this->t('The facet %facet has been deleted.', ['%facet' => $facet_name]));
// Refresh the page because on the previous page the $facet_name is still
// visible (in the message).
$facet_overview = '/admin/config/search/facets';
$this->drupalGet($facet_overview);
$this->assertResponse(200);
$this->assertNoText($facet_name);
}
/**
* Convert facet name to machine name.
*
* @param string $facet_name
* The name of the facet.
*
* @return string
* The facet name changed to a machine name.
*/
protected function convertNameToMachineName($facet_name) {
return preg_replace('@[^a-zA-Z0-9_]+@', '_', strtolower($facet_name));
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextNotContains($facet_name);
}
/**
......@@ -456,7 +451,7 @@ class IntegrationTest extends CoreSearchFacetsTestBase {
// Go to the facet delete page and make the warning is shown.
$this->drupalGet($facet_delete_page);
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
}
}
......@@ -36,8 +36,10 @@ class SliderProcessor extends ProcessorPluginBase implements PostQueryProcessorI
'count' => (int) $result->getCount(),
];
}
uasort($simple_results, function($a, $b) {
if ($a['value'] === $b['value']) { return 0; }
uasort($simple_results, function ($a, $b) {
if ($a['value'] === $b['value']) {
return 0;
}
return $a['value'] < $b['value'] ? -1 : 1;
});
......
......@@ -48,8 +48,7 @@ class SliderIntegrationTest extends FacetsTestBase {
public function testSliderWidget() {
$this->createIntegerField();
$id = 'owl';
$name = 'Owl widget.';
$this->createFacet($name, $id, 'field_integer');
$this->createFacet('Owl widget.', $id, 'field_integer');
$this->drupalGet('admin/config/search/facets/' . $id . '/edit');
......
......@@ -22,6 +22,8 @@ use Drupal\Tests\UnitTestCase;
class RangeSliderProcessorTest extends UnitTestCase {
/**
* The processor we're testing.
*
* @var \Drupal\facets_range_widget\Plugin\facets\processor\RangeSliderProcessor
*/
protected $processor;
......
......@@ -19,6 +19,8 @@ use Drupal\Tests\UnitTestCase;
class SliderProcessorTest extends UnitTestCase {
/**
* The processor we're testing.
*
* @var \Drupal\facets\Processor\ProcessorInterface
*/
protected $processor;
......@@ -46,7 +48,7 @@ class SliderProcessorTest extends UnitTestCase {
$result_higher = new Result(150, '150', 1);
$facet->setResults([$result_lower, $result_higher]);
// Process the data
// Process the data.
$startTime = microtime();
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
......@@ -81,7 +83,7 @@ class SliderProcessorTest extends UnitTestCase {
$original_results[] = new Result(150000, 'Big', 5);
$facet->setResults($original_results);
// Process the data
// Process the data.
$startTime = microtime();
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
......@@ -110,7 +112,7 @@ class SliderProcessorTest extends UnitTestCase {
}
$facet->setResults($original_results);
// Process the data
// Process the data.
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
......@@ -139,7 +141,7 @@ class SliderProcessorTest extends UnitTestCase {
$result_higher = new Result(150, '150', 1);
$facet->setResults([$result_lower, $result_higher]);
// Process the data
// Process the data.
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
......@@ -161,7 +163,7 @@ class SliderProcessorTest extends UnitTestCase {
$result_higher = new Result(15, '15', 4);
$facet->setResults([$result_lower, $result_higher]);
// Process the data
// Process the data.
$this->processor->postQuery($facet);
$new_results = $facet->getResults();
......@@ -178,7 +180,7 @@ class SliderProcessorTest extends UnitTestCase {
* Configures the container.
*
* @param array $config
* The config for the widget.
* The config for the widget.
*/
protected function configureContainer(array $config = []) {
$widget = $this->prophesize(ArrayWidget::class);
......
......@@ -80,7 +80,7 @@ class SliderWidgetTest extends WidgetTestBase {
$originalResults = [];
foreach (range(3, 20000, 2) as $rv) {
$res = new Result($rv, 'Value: ' . $rv, ceil($rv/2));
$res = new Result($rv, 'Value: ' . $rv, ceil($rv / 2));
$res->setUrl(new Url('test'));
$originalResults[] = $res;
}
......
......@@ -93,6 +93,6 @@ function facets_summary_theme_suggestions_facets_summary_facet_result(array $var
*
* @see https://www.drupal.org/node/1842756
*/
function facets_summary_preprocess_facets_summary_item_list(&$variables) {
function facets_summary_preprocess_facets_summary_item_list(array &$variables) {
template_preprocess_item_list($variables);
}
......@@ -7,12 +7,10 @@ use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\facets\FacetManager\DefaultFacetManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\FacetSource\FacetSourcePluginInterface;
use Drupal\facets\FacetSource\FacetSourcePluginManager;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\facets_summary\Processor\ProcessorPluginManager;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\views\Views;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\facets_summary\Functional;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\search_api\Item\Field;
use Drupal\taxonomy\Entity\Term;
......@@ -35,7 +34,7 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase {
protected $vocabulary;
/**
* The fieldname for the referenced term.
* The field name for the referenced term.
*
* @var string
*/
......@@ -87,8 +86,8 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase {
$this->setUpExampleStructure();
// Create a taxonomy_term_reference field on the article and item.
$this->fieldName = Unicode::strtolower($this->randomMachineName());
$fieldLabel = $this->randomString();
$this->fieldName = 'hierarchy_field';
$fieldLabel = 'Hierarchy field';
$this->createEntityReferenceField('entity_test_mulrev_changed', 'article', $this->fieldName, $fieldLabel, 'taxonomy_term');
$this->createEntityReferenceField('entity_test_mulrev_changed', 'item', $this->fieldName, $fieldLabel, 'taxonomy_term');
......@@ -123,7 +122,7 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase {
// Verify that the link to the index processors settings page is available.
$this->drupalGet($this->facetEditPage);
$this->clickLink('Search api index processor configuration');
$this->assertResponse(200);
$this->assertSession()->statusCodeEquals(200);
// Enable hierarchical facets and translation of entity ids to its names for
// a better readability.
......@@ -154,12 +153,12 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase {
$this->assertFacetBlocksAppear();
$this->assertFacetLabel('Parent 1');
$this->assertFacetLabel('Parent 2');
$this->assertNoLink('Child 1');
$this->assertNoLink('Child 2');
$this->assertNoLink('Child 3');
$this->assertNoLink('Child 4');
$this->assertSession()->linkNotExists('Child 1');
$this->assertSession()->linkNotExists('Child 2');
$this->assertSession()->linkNotExists('Child 3');
$this->assertSession()->linkNotExists('Child 4');
$this->assertText($block->label());
$this->assertSession()->pageTextContains($block->label());
// Click the first parent and make sure its children are visible.
$this->clickLink('Parent 1');
......@@ -167,10 +166,10 @@ class HierarchicalFacetIntegrationTest extends FacetsTestBase {
$this->checkFacetIsActive('Parent 1');