Commit ba4f21ec authored by borisson_'s avatar borisson_ Committed by borisson_

Issue #2717903 by borisson_: Cleanup for tests

parent e75d01a1
......@@ -29,23 +29,19 @@ class HooksTest extends WebTestBase {
parent::setUp();
// Create a field of type float.
FieldStorageConfig::create(
[
'field_name' => 'float',
'entity_type' => 'node',
'type' => 'float',
]
)->save();
FieldStorageConfig::create([
'field_name' => 'float',
'entity_type' => 'node',
'type' => 'float',
])->save();
// Create an instance of the float field on the "page" content type.
FieldConfig::create(
[
'field_name' => 'float',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Float Field Label',
]
)->save();
FieldConfig::create([
'field_name' => 'float',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Float Field Label',
])->save();
// Log in, so we can test all the things.
$this->drupalLogin($this->adminUser);
......
......@@ -2,7 +2,7 @@
namespace Drupal\core_search_facets\Tests;
use Drupal\facets\Tests\ExampleContentTrait;
use Drupal\facets\Tests\BlockTestTrait;
use Drupal\node\Entity\Node;
/**
......@@ -12,14 +12,7 @@ use Drupal\node\Entity\Node;
*/
class IntegrationTest extends WebTestBase {
use ExampleContentTrait;
/**
* The block entities used by this test.
*
* @var \Drupal\block\BlockInterface[]
*/
protected $blocks;
use BlockTestTrait;
/**
* {@inheritdoc}
......@@ -30,7 +23,9 @@ class IntegrationTest extends WebTestBase {
$this->drupalLogin($this->adminUser);
// Index the content.
\Drupal::service('plugin.manager.search')->createInstance('node_search')->updateIndex();
\Drupal::service('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
search_update_totals();
// Make absolutely sure the ::$blocks variable doesn't pass information
......@@ -42,7 +37,6 @@ class IntegrationTest extends WebTestBase {
* Tests various operations via the Facets' admin UI.
*/
public function testFramework() {
$facet_id = 'test_facet_name';
$facet_name = 'Test Facet Name';
......@@ -50,10 +44,10 @@ class IntegrationTest extends WebTestBase {
$this->checkEmptyOverview();
// Add a new facet and edit it.
$this->addFacet($facet_id, $facet_name, 'type');
$this->addFacet($facet_id, $facet_name);
// Create and place a block for "Test Facet name" facet.
$this->createFacetBlock($facet_id);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
// Verify that the facet results are correct.
$this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
......@@ -74,10 +68,10 @@ class IntegrationTest extends WebTestBase {
$this->clickLinkPartialName('page');
$this->assertRaw('<span class="facet-deactivate">(-)</span> page <span class="facet-count">(19)</span>');
// Do not show the block on empty behaviors.
// Truncate the search_index table because, for the moment, we don't have
// the possibility to clear the index from the API.
// See https://www.drupal.org/node/326062
// To make sure we have an empty result, we truncate the search_index table
// because, for the moment, we don't have the possibility to clear the index
// from the API.
// @see https://www.drupal.org/node/326062
\Drupal::database()->truncate('search_index')->execute();
// Verify that no facet blocks appear. Empty behavior "None" is selected by
......@@ -97,7 +91,6 @@ class IntegrationTest extends WebTestBase {
// Delete the facet and make sure the overview is empty again.
$this->deleteUnusedFacet($facet_id, $facet_name);
$this->checkEmptyOverview();
}
/**
......@@ -108,7 +101,7 @@ class IntegrationTest extends WebTestBase {
$facet_id = 'tardigrade';
$this->addFacet($facet_id, $facet_name, 'created');
$this->createFacetBlock($facet_id);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
$this->setShowAmountOfResults($facet_id, TRUE);
// Assert date facets.
......@@ -144,7 +137,7 @@ class IntegrationTest extends WebTestBase {
$facet_id = 'tardigrade';
$this->addFacet($facet_id, $facet_name, 'changed');
$this->createFacetBlock($facet_id);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
$this->setShowAmountOfResults($facet_id, TRUE);
// Update the changed date. The nodes were created on February/March 2016
......@@ -155,7 +148,9 @@ class IntegrationTest extends WebTestBase {
$node->save();
// Index the content.
\Drupal::service('plugin.manager.search')->createInstance('node_search')->updateIndex();
\Drupal::service('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
search_update_totals();
$this->drupalGet('search/node', ['query' => ['keys' => 'test']]);
......@@ -165,30 +160,22 @@ class IntegrationTest extends WebTestBase {
$this->assertRaw('December 3, 2016 <span class="facet-count">(1)</span>');
$this->clickLinkPartialName('December 3, 2016');
$this->assertResponse(200);
}
/**
* Tests for CRUD operations.
* Tests for CRUD operations in the admin UI.
*/
public function testCrudFacet() {
$facet_name = "Test Facet name";
$facet_id = 'test_facet_name';
// Check if the overview is empty.
$this->checkEmptyOverview();
// Add a new facet and edit it.
$this->addFacetCheck($facet_id, $facet_name, 'type');
$this->editFacetCheck($facet_id, $facet_name);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
// Create and place a block.
$this->createFacetBlock($facet_id);
// Delete the block.
$this->deleteBlock($facet_id);
// Delete the facet.
$this->deleteUnusedFacet($facet_id, $facet_name);
}
......@@ -202,7 +189,7 @@ class IntegrationTest extends WebTestBase {
* @param string $type
* The field type.
*/
public function addFacet($id, $name, $type) {
protected function addFacet($id, $name, $type = 'type') {
$this->drupalGet('admin/config/search/facets/add-facet');
$form_values = [
'id' => $id,
......@@ -216,7 +203,7 @@ class IntegrationTest extends WebTestBase {
}
/**
* Configures the possibility to show the amount of results for facet blocks.
* Configures the possibility to show the amount of results for facet items.
*
* @param string $facet_id
* The id of the facet.
......@@ -224,62 +211,17 @@ class IntegrationTest extends WebTestBase {
* Boolean to determine if we want to show the amount of results.
*/
protected function setShowAmountOfResults($facet_id, $show = TRUE) {
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
// Go to the facet edit page and make sure "edit facet %facet" is present.
$this->drupalGet($facet_edit_page);
$this->assertResponse(200);
// Configure the text for empty results behavior.
$edit = [
'widget_configs[show_numbers]' => $show,
];
$this->drupalPostForm(NULL, $edit, $this->t('Save'));
}
/**
* Deletes a facet block by id.
*
* @param string $id
* The id of the block.
*/
protected function deleteBlock($id) {
$this->drupalGet('admin/structure/block/manage/' . $this->blocks[$id]->id(), array('query' => array('destination' => 'admin')));
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $this->blocks[$id]->label())));
}
/**
* Asserts that a facet block does not appear.
*/
protected function assertNoFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this->assertNoBlockAppears($block);
}
}
/**
* Asserts that a facet block appears.
*/
protected function assertFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this->assertBlockAppears($block);
}
}
/**
* Creates a facet block by id.
*
* @param string $id
* The id of the block.
*/
protected function createFacetBlock($id) {
$block_values = ['region' => 'footer', 'id' => str_replace('_', '-', $id)];
$this->blocks[$id] = $this->drupalPlaceBlock('facet_block:' . $id, $block_values);
}
/**
* Configures empty behavior option to show a text on empty results.
*
......@@ -401,7 +343,7 @@ class IntegrationTest extends WebTestBase {
* @param string $facet_name
* The name of the facet.
*/
public function editFacetCheck($facet_id, $facet_name) {
protected function editFacetCheck($facet_id, $facet_name) {
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/settings';
// Go to the facet edit page and make sure "edit facet %facet" is present.
......
......@@ -70,7 +70,7 @@ abstract class WebTestBase extends SimpletestWebTestBase {
$field_storage->setTranslatable(TRUE);
$field_storage->save();
// Adding 10 pages.
// Add 9 nodes of the type page.
for ($i = 1; $i <= 9; $i++) {
// Adding a different created time per language to avoid to have exactly
// the same value per nid and langcode.
......@@ -102,7 +102,7 @@ abstract class WebTestBase extends SimpletestWebTestBase {
'langcode' => 'en',
));
// Adding 10 articles.
// Add 10 nodes of the type article.
for ($i = 1; $i <= 10; $i++) {
$created_time = new \DateTime('April ' . $i . ' 2016 ' . str_pad($i, 2, STR_PAD_LEFT, 0) . 'PM');
$this->drupalCreateNode(array(
......
<?php
namespace Drupal\facets\Tests;
/**
* Shared test methods for facet blocks.
*/
trait BlockTestTrait {
/**
* The block entities used by this test.
*
* @var \Drupal\block\BlockInterface[]
*/
protected $blocks;
/**
* 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,
'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'));
$this->blocks[$id] = $this->createBlock($id);
}
/**
* Creates a facet block by id.
*
* @param string $id
* The id of the block.
*
* @return \Drupal\block\Entity\Block
* The block entity.
*/
protected function createBlock($id) {
$block = [
'region' => 'footer',
'id' => str_replace('_', '-', $id),
];
return $this->drupalPlaceBlock('facet_block:' . $id, $block);
}
/**
* Asserts that a facet block does not appear.
*/
protected function assertNoFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this->assertNoBlockAppears($block);
}
}
/**
* Asserts that a facet block appears.
*/
protected function assertFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this->assertBlockAppears($block);
}
}
/**
* Deletes a facet block by id.
*
* @param string $id
* The id of the block.
*/
protected function deleteBlock($id) {
$this->drupalGet('admin/structure/block/manage/' . $this->blocks[$id]->id(), array('query' => array('destination' => 'admin')));
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $this->blocks[$id]->label())));
}
}
......@@ -15,7 +15,7 @@ trait ExampleContentTrait {
*
* @var \Drupal\entity_test\Entity\EntityTest[]
*/
protected $entities = array();
protected $entities = [];
/**
* Sets up the necessary bundles on the test entity type.
......@@ -90,32 +90,6 @@ 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,
'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.
*
......
......@@ -22,18 +22,25 @@ class FacetSourceTest extends WebTestBase {
];
/**
* Tests the facet source editing.
* {@inheritdoc}
*/
public function testEditFilterKey() {
public function setUp() {
parent::setUp();
// Make sure we're logged in with a user that has sufficient permissions.
$this->drupalLogin($this->adminUser);
// Test the overview.
// Go to the overview and click the first configure link.
$this->drupalGet('admin/config/search/facets');
$this->assertLink($this->t('Configure'));
$this->clickLink($this->t('Configure'));
}
// Test the edit page.
/**
* Tests the facet source editing.
*/
public function testEditFilterKey() {
// Change the filter key.
$edit = array(
'filter_key' => 'fq',
);
......@@ -52,15 +59,7 @@ class FacetSourceTest extends WebTestBase {
* Tests editing the url processor.
*/
public function testEditUrlProcessor() {
// Make sure we're logged in with a user that has sufficient permissions.
$this->drupalLogin($this->adminUser);
// Test the overview.
$this->drupalGet('admin/config/search/facets');
$this->assertLink($this->t('Configure'));
$this->clickLink($this->t('Configure'));
// Test the edit page.
// Change the url processor.
$edit = array(
'url_processor' => 'dummy_query',
);
......
......@@ -12,13 +12,6 @@ use Drupal\facets\Entity\Facet;
*/
class IntegrationTest extends WebTestBase {
/**
* The block entities used by this test.
*
* @var \Drupal\block\BlockInterface[]
*/
protected $blocks;
/**
* {@inheritdoc}
*/
......@@ -37,7 +30,7 @@ class IntegrationTest extends WebTestBase {
}
/**
* Tests Facets' permissions.
* Tests permissions.
*/
public function testOverviewPermissions() {
$facet_overview = '/admin/config/search/facets';
......@@ -76,7 +69,7 @@ class IntegrationTest extends WebTestBase {
$this->assertText('Displaying 5 search results', 'The search view displays the correct number of results.');
// Create and place a block for "Test Facet name" facet.
$this->createFacetBlock($facet_id);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
// Verify that the facet results are correct.
$this->drupalGet('search-api-test-fulltext');
......@@ -152,7 +145,7 @@ class IntegrationTest extends WebTestBase {
$this->assertText('Fulltext test index', 'The search view displays the correct number of results.');
// Create and place a block for the test facet.
$this->createFacetBlock($facet_id);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
// Verify that the facet results are correct displayed.
$this->drupalGet('<front>');
......@@ -170,15 +163,10 @@ class IntegrationTest extends WebTestBase {
* @see https://www.drupal.org/node/2629504
*/
public function testRenameFacet() {
// Set names.
$facet_id = 'ab_facet';
$new_facet_id = 'facet__ab';
$facet_name = 'ab>Facet';
// Make sure we're logged in with a user that has sufficient permissions.
$this->drupalLogin($this->adminUser);
// Add a new facet.
$this->addFacet($facet_name);
......@@ -201,28 +189,8 @@ class IntegrationTest extends WebTestBase {
public function testUrlAlias() {
$facet_id = 'ab_facet';
$facet_name = 'ab>Facet';
// Make sure we're logged in with a user that has sufficient permissions.
$this->drupalLogin($this->adminUser);
$facet_add_page = '/admin/config/search/facets/add-facet';
$facet_edit_page = '/admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalGet($facet_add_page);
$this->assertResponse(200);
$form_values = [
'name' => $facet_name,
'id' => $facet_id,
'status' => 1,
'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->assertRaw(t('Facet %name has been created.', ['%name' => $facet_name]));
$this->createFacetBlock($facet_id);
$this->createFacet($facet_name, $facet_id);
$this->drupalGet('search-api-test-fulltext');
$this->assertLink('item');
......@@ -250,15 +218,16 @@ class IntegrationTest extends WebTestBase {
public function testFacetDependencies() {
$facet_name = "DependableFacet";
$facet_id = 'dependablefacet';
$this->addFacet($facet_name);
$depending_facet_name = "DependingFacet";
$depending_facet_id = "dependingfacet";
$this->addFacet($facet_name);
$this->addFacet($depending_facet_name, 'keywords');
// Create both facets as blocks and add them on the page.
$this->createFacetBlock($facet_id);
$this->createFacetBlock($depending_facet_id);
$this->blocks[$facet_id] = $this->createBlock($facet_id);
$this->blocks[$depending_facet_id] = $this->createBlock($depending_facet_id);
// Go the the view and test that both facets are shown. Item and article
// come from the DependableFacet, orange and grape come from DependingFacet.
......@@ -334,16 +303,11 @@ class IntegrationTest extends WebTestBase {
$facet_id = 'test_facet';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->drupalLogin($this->adminUser);
$this->addFacet($facet_name);
$this->createFacetBlock('test_facet');
$this->createFacet($facet_name, $facet_id);
$this->drupalGet($facet_edit_page);
$this->drupalPostForm(NULL, ['facet_settings[query_operator]' => 'AND'], $this->t('Save'));
$this->insertExampleContent();
$this->assertEqual($this->indexItems($this->indexId), 5, '5 items were indexed.');
$this->drupalGet('search-api-test-fulltext');
$this->assertLink('item');
$this->assertLink('article');
......@@ -364,7 +328,7 @@ class IntegrationTest extends WebTestBase {
}
/**
* Tests that we disallow unwanted values.
* Tests that we disallow unwanted values when creating a facet trough the UI.
*/
public function testUnwantedValues() {
// Go to the Add facet page and make sure that returns a 200.
......@@ -409,9 +373,7 @@ class IntegrationTest extends WebTestBase {
$facet_name = 'test & facet';
$facet_id = 'test_facet';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->addFacet($facet_name);
$this->createFacetBlock($facet_id);
$this->createFacet($facet_name, $facet_id);
$this->drupalGet($facet_edit_page);
$this->assertNoFieldChecked('edit-facet-settings-exclude');
......@@ -456,8 +418,7 @@ class IntegrationTest extends WebTestBase {
$facet_id = 'spotted_wood_owl';
$facet_edit_page = 'admin/config/search/facets/' . $facet_id;
$this->addFacet($facet_name, 'keywords');
$this->createFacetBlock($facet_id);
$this->createFacet($facet_name, $facet_id, 'keywords');
$this->drupalGet($facet_edit_page . '/edit');
$edit = ['facet_settings[show_only_one_result]' => TRUE];
......@@ -485,7 +446,7 @@ class IntegrationTest extends WebTestBase {
public function testWeight() {
$facet_name = "Forest owlet";
$id = "forest_owlet";
$this->addFacet($facet_name);
$this->createFacet($facet_name, $id);
/** @var \Drupal\facets\FacetInterface $facet */
$facet = Facet::load($id);
......@@ -493,54 +454,6 @@ class IntegrationTest extends WebTestBase {
$this->assertEqual(10, $facet->getWeight());
}
/**
* Deletes a facet block by id.
*
* @param string $id
* The id of the block.
*/
protected function deleteBlock($id) {
$this->drupalGet('admin/structure/block/manage/' . $this->blocks[$id]->id(), array('query' => array('destination' => 'admin')));
$this->clickLink(t('Delete'));
$this->drupalPostForm(NULL, array(), t('Delete'));
$this->assertRaw(t('The block %name has been deleted.', array('%name' => $this->blocks[$id]->label())));
}
/**
* Asserts that a facet block does not appear.
*/
protected function assertNoFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this->assertNoBlockAppears($block);
}
}
/**
* Asserts that a facet block appears.
*/
protected function assertFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this->assertBlockAppears($block);
}
}
/**
* Creates a facet block by id.
*
* @param string $id
* The id of the block.
*/
protected function createFacetBlock($id) {
$block = [
'plugin_id' => 'facet_block:' . $id,
'settings' => [
'region' => 'footer',
'id' => str_replace('_', '-', $id),
],
];
$this->blocks[$id] = $this->drupalPlaceBlock($block['plugin_id'], $block['settings']);
}
/**
* Configures empty behavior option to show a text on empty results.
*
......
......@@ -97,7 +97,6 @@ class ProcessorIntegrationTest extends WebTestBase {
$this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->createFacet($facet_name, $facet_id, 'keywords');
$this->createFacetBlock($facet_id);
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
......@@ -122,7 +121,6 @@ class ProcessorIntegrationTest extends WebTestBase {
$this->editForm = 'admin/config/search/facets/' . $facet_id . '/edit';
$this->createFacet($facet_name, $facet_id, 'keywords');
$this->createFacetBlock($facet_id);
$this->checkSortByActive();
$this->checkSortByCount();
......@@ -133,7 +131,7 @@ class ProcessorIntegrationTest extends WebTestBase {
/**
* Tests the count limit processor.
*/
private function checkCountLimitProcessor() {
protected function checkCountLimitProcessor() {
$this->drupalGet($this->editForm);
$form = [
......@@ -185,7 +183,7 @@ class ProcessorIntegrationTest extends WebTestBase {
/**
* Tests the exclude items.
*/
private function checkExcludeItems() {
protected function checkExcludeItems() {
$form = [
'facet_settings[exclude_specified_items][status]' => TRUE,
];
......@@ -223,7 +221,7 @@ class ProcessorIntegrationTest extends WebTestBase {
/**
* Tests hiding non-narrowing results.
*/
private function checkHideNonNarrowingProcessor() {
protected function checkHideNonNarrowingProcessor() {
$this->drupalGet('search-api-test-fulltext');
$this->assertText('Displaying 10 search results');
$this->assertLink('grape');
......@@ -254,7 +252,7 @@ class ProcessorIntegrationTest extends WebTestBase {
/**
* Tests hiding active results.
*/
private function checkHideActiveItems() {
protected function checkHideActiveItems() {
$form = [
'facet_settings[hide_active_items_processor][status]' => TRUE,
];
......@@ -341,10 +339,8 @@ class ProcessorIntegrationTest extends WebTestBase {
/**
* Tests the display order.
*/
public function checkSortByDisplay() {
$form = [
'facet_sorting[display_value_widget_order][status]' => TRUE,
];
protected 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');
......@@ -361,16 +357,14 @@ class ProcessorIntegrationTest extends WebTestBase {
$this->assertStringPosition('strawberry', 'grape');
$this->assertStringPosition('banana', 'apple');