protected function IntegrationTest::addFieldsToIndex in Search API 8
Tests whether adding fields to the index works correctly.
2 calls to IntegrationTest::addFieldsToIndex()
- IntegrationTest::testFramework in tests/
src/ Functional/ IntegrationTest.php - Tests various operations via the Search API's admin UI.
- IntegrationTest::testIntegerIndex in tests/
src/ Functional/ IntegrationTest.php - Tests what happens when an index has an integer as id/label.
File
- tests/
src/ Functional/ IntegrationTest.php, line 817
Class
- IntegrationTest
- Tests the overall functionality of the Search API framework and admin UI.
Namespace
Drupal\Tests\search_api\FunctionalCode
protected function addFieldsToIndex() {
// Make sure that hidden properties are not displayed.
$url_options['query']['datasource'] = '';
$this
->drupalGet($this
->getIndexPath('fields/add/nojs'), $url_options);
$this
->assertSession()
->pageTextNotContains('Node access information');
$fields = [
'nid' => 'ID',
'title' => 'Title',
'body' => 'Body',
'revision_log' => 'Revision log message',
'uid:entity:name' => 'Authored by » User » Name',
];
foreach ($fields as $property_path => $label) {
$this
->addField('entity:node', $property_path, $label);
}
$this
->assertSession()
->pageTextNotContains('No UI data type');
$index = $this
->getIndex(TRUE);
$fields = $index
->getFields();
$this
->assertArrayNotHasKey('nid', $fields, 'Field changes have not been persisted.');
$this
->drupalGet($this
->getIndexPath('fields'));
$this
->submitForm([], 'Save changes');
$this
->assertSession()
->pageTextContains('The changes were successfully saved.');
$index = $this
->getIndex(TRUE);
$fields = $index
->getFields();
$this
->assertArrayHasKey('nid', $fields, 'nid field is indexed.');
// Ensure that we aren't offered to index properties of the "Content type"
// property.
$path = $this
->getIndexPath('fields/add/nojs');
$url_options = [
'query' => [
'datasource' => 'entity:node',
],
];
$this
->drupalGet($path, $url_options);
$this
->assertSession()
->responseNotContains('property_path=type');
// The "Content access" processor correctly marked fields as locked.
$this
->assertArrayHasKey('uid', $fields, 'uid field is indexed.');
$this
->assertTrue($fields['uid']
->isIndexedLocked(), 'uid field is locked.');
$this
->assertTrue($fields['uid']
->isTypeLocked(), 'uid field is type-locked.');
$this
->assertEquals('integer', $fields['uid']
->getType(), 'uid field has type integer.');
$this
->assertArrayHasKey('status', $fields, 'status field is indexed.');
$this
->assertTrue($fields['status']
->isIndexedLocked(), 'status field is locked.');
$this
->assertTrue($fields['status']
->isTypeLocked(), 'status field is type-locked.');
$this
->assertEquals('boolean', $fields['status']
->getType(), 'status field has type boolean.');
// Check that a 'parent_data_type.data_type' Search API field type => data
// type mapping relationship works.
$this
->assertArrayHasKey('body', $fields, 'body field is indexed.');
$this
->assertEquals('text', $fields['body']
->getType(), 'Complex field mapping relationship works.');
// Test renaming of fields.
$edit = [
'fields[title][title]' => 'new_title',
'fields[title][id]' => 'new_id',
'fields[title][type]' => 'text',
'fields[title][boost]' => Utility::formatBoostFactor(21),
'fields[revision_log][type]' => 'search_api_test',
];
$this
->drupalGet($this
->getIndexPath('fields'));
$this
->submitForm($edit, 'Save changes');
$this
->assertSession()
->pageTextContains('The changes were successfully saved.');
$index = $this
->getIndex(TRUE);
$fields = $index
->getFields();
$this
->assertArrayHasKey('new_id', $fields, 'title field is indexed.');
$this
->assertEquals($edit['fields[title][title]'], $fields['new_id']
->getLabel(), 'title field title is saved.');
$this
->assertEquals($edit['fields[title][id]'], $fields['new_id']
->getFieldIdentifier(), 'title field id value is saved.');
$this
->assertEquals($edit['fields[title][type]'], $fields['new_id']
->getType(), 'title field type is text.');
$this
->assertEquals($edit['fields[title][boost]'], $fields['new_id']
->getBoost(), 'title field boost value is 21.');
$this
->assertArrayHasKey('revision_log', $fields, 'revision_log field is indexed.');
$this
->assertEquals($edit['fields[revision_log][type]'], $fields['revision_log']
->getType(), 'revision_log field type is search_api_test.');
// Reset field values to original.
$edit = [
'fields[new_id][title]' => 'Title',
'fields[new_id][id]' => 'title',
];
$this
->drupalGet($this
->getIndexPath('fields'));
$this
->submitForm($edit, 'Save changes');
$this
->assertSession()
->pageTextContains('The changes were successfully saved.');
// Make sure that property paths are correctly displayed.
$this
->assertSession()
->pageTextContains('uid:entity:name');
// Verify that custom boost values set directly in the config won't be
// overwritten when saving the "Fields" form in the UI.
$index = $this
->getIndex(TRUE);
$index
->getField('title')
->setBoost(4.0);
$index
->save();
$this
->drupalGet($this
->getIndexPath('fields'));
$this
->submitForm([], 'Save changes');
$this
->assertSession()
->pageTextContains('The changes were successfully saved.');
$index = $this
->getIndex(TRUE);
$this
->assertEquals(4.0, $index
->getField('title')
->getBoost());
}