diff --git a/core_search_facets/src/Tests/IntegrationTest.php b/core_search_facets/src/Tests/IntegrationTest.php new file mode 100644 index 0000000..c242ab0 --- /dev/null +++ b/core_search_facets/src/Tests/IntegrationTest.php @@ -0,0 +1,196 @@ +drupalLogin($this->adminUser); + $this->drupalCreateContentType(['type' => 'page']); + + $i = 0; + while ($i < 5) { + $this->drupalCreateNode(['title' => 'node ' . $i, 'type' => 'page']); + $i++; + } + } + + /** + * Tests various operations via the Facets' admin UI. + */ + public function testFramework() { + $facet_name = "Test Facet name"; + $facet_id = 'test_facet_name'; + + // Add a new facet and edit it. + $this->addFacet($facet_name); + $this->editFacet($facet_name); + + // Create and place a block for "Test Facet name" facet. + $this->createFacetBlock($facet_id); + + // Delete the block. + $this->deleteBlock($facet_id); + } + + /** + * 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()))); + } + + /** + * 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']); + } + + /** + * Tests adding a facet trough the interface. + * + * @param string $facet_name + * The name of the facet. + */ + protected function addFacet($facet_name) { + $facet_id = $this->convertNameToMachineName($facet_name); + + // 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); + + $form_values = [ + 'name' => '', + 'id' => $facet_id, + 'status' => 1, + 'url_alias' => $facet_id, + ]; + + // 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('Facet name field is required.')); + $this->assertText($this->t('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.')); + + // 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')); + + // The facet field is still required. + $this->drupalPostForm(NULL, $form_values, $this->t('Save')); + $this->assertText($this->t('Facet 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_id' => 'core_node_search:node_search', + '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.'); + + // 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 . '/display'); + + $this->drupalGet('admin/config/search/facets'); + } + + + /** + * Tests editing of a facet through the UI. + * + * @param string $facet_name + * The name of the facet. + */ + public function editFacet($facet_name) { + $facet_id = $this->convertNameToMachineName($facet_name); + + $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); + $this->assertRaw($this->t('Edit 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')); + + // 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'])); + + // Make sure the "-2" suffix is still on the facet when editing a facet. + $this->drupalGet($facet_edit_page); + $this->assertRaw($this->t('Edit 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')); + + // 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])); + } + + /** + * 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)); + } +} diff --git a/core_search_facets/src/Tests/WebTestBase.php b/core_search_facets/src/Tests/WebTestBase.php index e41426c..2ec7278 100644 --- a/core_search_facets/src/Tests/WebTestBase.php +++ b/core_search_facets/src/Tests/WebTestBase.php @@ -36,6 +36,7 @@ abstract class WebTestBase extends SimpletestWebTestBase { 'node', 'facets', 'block', + 'search', 'core_search_facets', ];