diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index 9241521..50f663c 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -52,6 +52,8 @@ public function setUp() { 'administer search_api', 'access administration pages', 'administer nodes', + 'bypass node access', + 'administer content types', 'administer node fields', )); $this->drupalLogin($this->adminUser); @@ -100,7 +102,6 @@ public function testFramework() { * server also has an integer as id/label. */ public function testIntegerIndex() { - $this->drupalLogin($this->adminUser); $this->getTestServer(789, 456); $this->drupalCreateNode(array('type' => 'article')); @@ -560,14 +561,6 @@ public function enableAllProcessors() { * Tests that field labels are always properly escaped. */ protected function checkFieldLabels() { - $permissions = array( - 'administer search_api', - 'access administration pages', - 'administer content types', - 'administer node fields', - ); - $this->drupalLogin($this->createUser($permissions)); - $content_type_name = '&%@Content()_='; // Add a new content type with funky chars. @@ -710,15 +703,20 @@ protected function checkDataTypesTable() { protected function addField($datasource_id, $property_path, $label = NULL) { $path = $this->getIndexPath('fields/add'); $url_options = array('query' => array('datasource' => $datasource_id)); - if ($this->getUrl() === $this->buildUrl($path, $url_options)) { - $path = NULL; + list($parent_path) = Utility::splitPropertyPath($property_path); + if ($parent_path) { + $url_options['query']['property_path'] = $parent_path; + } + if ($this->getUrl() !== $this->buildUrl($path, $url_options)) { + $this->drupalGet($path, $url_options); } // Unfortunately it doesn't seem possible to specify the clicked button by // anything other than label, so we have to pass it as extra POST data. $combined_property_path = Utility::createCombinedId($datasource_id, $property_path); + $this->assertRaw('name="' . $combined_property_path . '"'); $post = '&' . $this->serializePostValues(array($combined_property_path => $this->t('Add'))); - $this->drupalPostForm($path, array(), NULL, $url_options, array(), NULL, $post); + $this->drupalPostForm(NULL, array(), NULL, array(), array(), NULL, $post); if ($label) { $args['%label'] = $label; $this->assertRaw($this->t('Field %label was added to the index.', $args)); @@ -820,7 +818,7 @@ protected function removeFieldsFromIndex() { } /** - * Tests if non-base fields of reference fields are displayed in Add fields. + * Tests if non-base fields of referenced entities can be added. */ protected function checkReferenceFieldsNonBaseFields() { // Add a entity_reference field. @@ -830,19 +828,18 @@ protected function checkReferenceFieldsNonBaseFields() { 'label' => $field_label, 'field_name' => '_reference_field_', ); - $this->drupalGet('admin/structure/types/manage/_content_/fields/add-field'); - $this->assertResponse(200); - $this->drupalPostForm(NULL, $edit, $this->t('Save and continue')); - $this->drupalPostForm(NULL, ['cardinality' => -1], $this->t('Save field settings')); - - $url_options['query'] = array( - 'datasource' => 'entity:node', - 'property_path' => 'field__reference_field_:entity', - ); - $this->drupalGet($this->getIndexPath('fields/add'), $url_options); - - // Check if the field is displayed to be indexed. - $this->assertRaw('name="entity:node/field__reference_field_:entity:field__reference_field_"'); + $this->drupalPostForm('admin/structure/types/manage/article/fields/add-field', $edit, $this->t('Save and continue')); + $this->drupalPostForm(NULL, array('cardinality' => -1), $this->t('Save field settings')); + + $node_label = $this->getIndex()->getDatasource('entity:node')->label(); + $field_label = "$field_label » $node_label » $field_label"; + $this->addField('entity:node', 'field__reference_field_:entity:field__reference_field_', $field_label); + + $this->drupalGet('node/2/edit'); + $edit = array('field__reference_field_[0][target_id]' => 'Something (2)'); + $this->drupalPostForm('node/2/edit', $edit, $this->t('Save and keep published')); + $indexed_values = \Drupal::state()->get("search_api_test_backend.indexed.{$this->indexId}", array()); + $this->assertEqual(array(2), $indexed_values['entity:node/2:en']['field__reference_field_'], 'Correct value indexed for nested non-base field.'); } /** diff --git a/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php b/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php index e5e46f5..3900557 100644 --- a/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php +++ b/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php @@ -90,6 +90,18 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta */ public function indexItems(IndexInterface $index, array $items) { $this->checkError(__FUNCTION__); + + $state = \Drupal::state(); + $key = 'search_api_test_backend.indexed.' . $index->id(); + $indexed_values = $state->get($key, array()); + /** @var \Drupal\search_api\Item\ItemInterface $item */ + foreach ($items as $id => $item) { + foreach ($item->getFields() as $field_id => $field) { + $indexed_values[$id][$field_id] = $field->getValues(); + } + } + $state->set($key, $indexed_values); + return array_keys($items); }