コード例 #1
0
 /**
  * Performs setup tasks before each individual test method is run.
  *
  * Installs commonly used schemas and sets up a search server and an index,
  * with the specified processor enabled.
  *
  * @param string|null $processor
  *   (optional) The plugin ID of the processor that should be set up for
  *   testing.
  */
 public function setUp($processor = NULL)
 {
     parent::setUp();
     $this->installSchema('node', array('node_access'));
     $this->installSchema('search_api', array('search_api_item', 'search_api_task'));
     $this->installEntitySchema('user');
     $this->installEntitySchema('node');
     $this->installEntitySchema('comment');
     $this->installConfig(array('field'));
     Action::create(['id' => 'foo', 'label' => 'Foobaz', 'plugin' => 'comment_publish_action'])->save();
     \Drupal::configFactory()->getEditable('search_api.settings')->set('tracking_page_size', 100)->save();
     // Do not use a batch for tracking the initial items after creating an
     // index when running the tests via the GUI. Otherwise, it seems Drupal's
     // Batch API gets confused and the test fails.
     \Drupal::state()->set('search_api_use_tracking_batch', FALSE);
     $this->server = Server::create(array('id' => 'server', 'name' => 'Server & Name', 'status' => TRUE, 'backend' => 'search_api_db', 'backend_config' => array('min_chars' => 3, 'database' => 'default:default')));
     $this->server->save();
     $this->index = Index::create(array('id' => 'index', 'name' => 'Index name', 'status' => TRUE, 'datasource_settings' => array('entity:comment' => array('plugin_id' => 'entity:comment', 'settings' => array()), 'entity:node' => array('plugin_id' => 'entity:node', 'settings' => array())), 'server' => 'server', 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array()))));
     $this->index->setServer($this->server);
     $field_subject = new Field($this->index, 'subject');
     $field_subject->setType('text');
     $field_subject->setPropertyPath('subject');
     $field_subject->setDatasourceId('entity:comment');
     $field_subject->setLabel('Subject');
     $field_title = new Field($this->index, 'title');
     $field_title->setType('text');
     $field_title->setPropertyPath('title');
     $field_title->setDatasourceId('entity:node');
     $field_title->setLabel('Title');
     $this->index->addField($field_subject);
     $this->index->addField($field_title);
     if ($processor) {
         /** @var \Drupal\search_api\Processor\ProcessorPluginManager $plugin_manager */
         $plugin_manager = \Drupal::service('plugin.manager.search_api.processor');
         $this->processor = $plugin_manager->createInstance($processor, array('index' => $this->index));
         $this->index->addProcessor($this->processor);
     }
     $this->index->save();
 }
コード例 #2
0
 /**
  * Form submission handler for adding a new field to the index.
  *
  * @param array $form
  *   An associative array containing the structure of the form.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  */
 public function addField(array $form, FormStateInterface $form_state)
 {
     $button = $form_state->getTriggeringElement();
     if (!$button) {
         return;
     }
     /** @var \Drupal\Core\TypedData\DataDefinitionInterface $property */
     $property = $button['#property'];
     list($datasource_id, $property_path) = Utility::splitCombinedId($button['#name']);
     $field = Utility::createFieldFromProperty($this->entity, $property, $datasource_id, $property_path, NULL, $button['#data_type']);
     $field->setLabel($button['#prefixed_label']);
     $this->entity->addField($field);
     $args['%label'] = $field->getLabel();
     drupal_set_message($this->t('Field %label was added to the index.', $args));
 }
コード例 #3
0
 /**
  * Tests custom data types integration.
  */
 public function testCustomDataTypes()
 {
     $original_value = $this->entities[1]->get('name')->getValue()[0]['value'];
     $original_type = $this->index->getField('name')->getType();
     $item = $this->index->loadItem('entity:entity_test/1:en');
     $item = Utility::createItemFromObject($this->index, $item, 'entity:entity_test/1:en');
     $name_field = $item->getField('name');
     $processed_value = $name_field->getValues()[0];
     $processed_type = $name_field->getType();
     $label = $name_field->getLabel();
     $this->assertEquals($original_value, $processed_value, 'The processed value matches the original value');
     $this->assertEquals($original_type, $processed_type, 'The processed type matches the original type.');
     $this->assertEquals('Name', $label, 'The label is correctly set.');
     // Reset the fields on the item and change to the supported data type.
     $item->setFieldsExtracted(FALSE);
     $item->setFields(array());
     $field = $this->index->getField('name')->setType('search_api_test_data_type')->setLabel("Test");
     $this->index->addField($field);
     $name_field = $item->getField('name');
     $processed_value = $name_field->getValues()[0];
     $processed_type = $name_field->getType();
     $this->assertEquals($original_value, $processed_value, 'The processed value matches the original value');
     $this->assertEquals('search_api_test_data_type', $processed_type, 'The processed type matches the new type.');
     $this->assertEquals('Test', $name_field->getLabel(), 'The label is correctly set.');
     // Reset the fields on the item and change to the non-supported data type.
     $item->setFieldsExtracted(FALSE);
     $item->setFields(array());
     $field = $this->index->getField('name')->setType('search_api_unsupported_test_data_type');
     $this->index->addField($field);
     $name_field = $item->getField('name');
     $processed_value = $name_field->getValues()[0];
     $processed_type = $name_field->getType();
     $this->assertEquals($original_value, $processed_value, 'The processed value matches the original value');
     $this->assertEquals('integer', $processed_type, 'The processed type matches the fallback type.');
     // Reset the fields on the item and change to the data altering data type.
     $item->setFieldsExtracted(FALSE);
     $item->setFields(array());
     $field = $this->index->getField('name')->setType('search_api_altering_test_data_type');
     $this->index->addField($field);
     $name_field = $item->getField('name');
     $processed_value = $name_field->getValues()[0];
     $processed_type = $name_field->getType();
     $this->assertEquals(strlen($original_value), $processed_value, 'The processed value matches the altered original value');
     $this->assertEquals('search_api_altering_test_data_type', $processed_type, 'The processed type matches the defined type.');
 }