View source
<?php
namespace Drupal\Tests\elasticsearch_connector_autocomp\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class IndexFormTest extends WebDriverTestBase {
protected static $modules = [
'elasticsearch_connector_autocomp',
'search_api',
'elasticsearch_connector_autocomp_test',
'node',
'taxonomy',
'filter',
'options',
'text',
'elasticsearch_connector',
'system',
'user',
];
protected $strictConfigSchema = FALSE;
protected $defaultTheme = 'stark';
public function testFormBehaviour() {
$this
->drupalLogin($this
->drupalCreateUser([], NULL, TRUE));
$this
->internalTestSaveSearchApiIndexForm();
$this
->internalTestConfirmForm();
$this
->internalTestTaxonomyIndex();
}
public function internalTestSaveSearchApiIndexForm() {
$this
->drupalGet('/admin/config/search/search-api/add-index');
$this
->getSession()
->getPage()
->fillField('name', 'Search API Index');
$this
->getSession()
->wait(5000, "jQuery('#edit-name-machine-name-suffix').length");
$this
->getSession()
->getPage()
->checkField('datasources[entity:node]');
$this
->getSession()
->wait(5000, "jQuery('details[data-drupal-selector=edit-datasource-configs-entitynode]').length");
$this
->getSession()
->wait('10000');
$this
->submitForm([], 'Save');
$this
->getSession()
->getPage()
->fillField('id', 'search_api_index');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContainsOnce("The index was successfully saved.");
$this
->assertSession()
->pageTextContainsOnce("Search API Index");
}
public function internalTestConfirmForm() {
$indexStorage = $this->container
->get('entity_type.manager')
->getStorage('search_api_index');
$index = $indexStorage
->load('elasticsearch_index');
$this
->assertFalse($index
->getThirdPartySetting('elasticsearch_connector', 'ngram_filter_enabled', FALSE));
$this
->drupalGet($index
->toUrl('edit-form'));
$assert = $this
->assertSession();
$assert
->fieldExists('third_party_settings[elasticsearch_connector][ngram_filter_enabled]');
$node = $this
->getSession()
->getPage()
->find('css', "#edit-third-party-settings-elasticsearch-connector");
$node
->click();
$this
->submitForm([
'name' => 'A new name for the index',
'third_party_settings[elasticsearch_connector][ngram_filter_enabled]' => 1,
], 'Save');
$indexStorage
->resetCache();
$index = $indexStorage
->load('elasticsearch_index');
$this
->assertFalse($index
->getThirdPartySetting('elasticsearch_connector', 'ngram_filter_enabled', FALSE));
$this
->assertEquals('Test index using elasticsearch module', $index
->label());
$assert
->pageTextContains('You are changing the analyzer on an existing index.');
$assert
->pageTextContains('This will result in the index being deleted and rebuilt and you will have to reindex all items. Are you sure you want to continue?');
$this
->submitForm([], 'Confirm');
$indexStorage
->resetCache();
$index = $indexStorage
->load('elasticsearch_index');
$this
->assertNotEmpty($index
->getThirdPartySetting('elasticsearch_connector', 'ngram_filter_enabled', FALSE));
$this
->assertEquals('A new name for the index', $index
->label());
$this
->internalTestTaxonomyIndex($indexStorage);
}
public function internalTestTaxonomyIndex() {
$indexStorage = $this->container
->get('entity_type.manager')
->getStorage('search_api_index');
$index = $indexStorage
->load('taxonomy_term_index');
$this
->drupalGet($index
->toUrl('fields'));
$assert = $this
->assertSession();
$assert
->optionExists('fields[name][type]', 'text_ngram');
$field_name = $this
->xpath('//select[@id="edit-fields-name-boost"]');
$states = (array) json_decode($field_name[0]
->getAttribute('data-drupal-states'), TRUE);
$field_visible = $states['visible'][':input[name="fields[name][type]"]'];
$values = array_column($field_visible, 'value');
$this
->assertContains('text_ngram', $values, 'Field "name" updated in elasticsearch_connector_autocomp_form_search_api_index_fields_alter()');
}
}