/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installSchema('search_api', array('search_api_item', 'search_api_task')); // Create a test server. $this->server = Server::create(array( 'name' => $this->randomString(), 'id' => $this->randomMachineName(), 'status' => 1, 'backend' => 'search_api_test_backend', )); $this->server->save(); // Create a test index. $this->index = Index::create(array( 'name' => $this->randomString(), 'id' => $this->randomMachineName(), 'status' => 1, 'datasources' => array('entity:' . $this->testEntityTypeId), 'tracker' => 'default', 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE), )); $this->index->save(); Utility::getIndexTaskManager()->addItemsAll($this->index); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); // Enable translation for the entity_test module. \Drupal::state()->set('entity_test.translation', TRUE); $this->installSchema('search_api', array('search_api_item', 'search_api_task')); $this->installEntitySchema('entity_test_mul'); // Create the default languages. $this->installConfig(array('language')); $this->langcodes = array(); for ($i = 0; $i < 3; ++$i) { /** @var \Drupal\language\Entity\ConfigurableLanguage $language */ $language = ConfigurableLanguage::create(array('id' => 'l' . $i, 'label' => 'language - ' . $i, 'weight' => $i)); $this->langcodes[$i] = $language->getId(); $language->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); // Create a test server. $this->server = Server::create(array('name' => 'Test Server', 'id' => 'test_server', 'status' => 1, 'backend' => 'search_api_test_backend')); $this->server->save(); // Create a test index. $this->index = Index::create(array('name' => 'Test Index', 'id' => 'test_index', 'status' => 1, 'datasource_settings' => array('entity:' . $this->testEntityTypeId => array('plugin_id' => 'entity:' . $this->testEntityTypeId, 'settings' => array())), 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array())), 'server' => $this->server->id(), 'options' => array('index_directly' => FALSE))); $this->index->save(); }
/** * Tests whether module dependencies are handled correctly. */ public function testModuleDependency() { // Test with all types of plugins at once. $datasources['search_api_test_dependencies'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('search_api_test_dependencies', array('index' => $this->index)); $datasources['entity:user'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('entity:user', array('index' => $this->index)); $this->index->setDatasources($datasources); $processor = \Drupal::getContainer()->get('plugin.manager.search_api.processor')->createInstance('search_api_test_dependencies'); $this->index->addProcessor($processor); $tracker = \Drupal::getContainer()->get('plugin.manager.search_api.tracker')->createInstance('search_api_test_dependencies'); $this->index->setTracker($tracker); $this->index->save(); // Check the dependencies were calculated correctly. $dependencies = $this->index->getDependencies(); $this->assertContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency correctly inserted'); // When the index resets the tracker, it needs to know the ID of the default // tracker. \Drupal::configFactory()->getEditable('search_api.settings')->set('default_tracker', 'default')->save(); // Disabling modules in Kernel tests normally doesn't trigger any kind of // reaction, just removes it from the list of modules (e.g., to avoid // calling of a hook). Therefore, we have to trigger that behavior // ourselves. \Drupal::getContainer()->get('config.manager')->uninstall('module', 'search_api_test_dependencies'); // Reload the index and check it's still there. $this->reloadIndex(); $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed'); // Make sure the dependency has been removed. $dependencies = $this->index->getDependencies(); $dependencies += array('module' => array()); $this->assertNotContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency removed from index'); // Make sure all the plugins have been removed. $this->assertNotContains('search_api_test_dependencies', $this->index->getDatasources(), 'Datasource was removed'); $this->assertArrayNotHasKey('search_api_test_dependencies', $this->index->getProcessors(), 'Processor was removed'); $this->assertEquals('default', $this->index->getTrackerId(), 'Tracker was reset'); }
/** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $values = $form_state->getValues(); $new_settings = array(); // Store processor settings. // @todo Go through all available processors, enable/disable with method on // processor plugin to allow reaction. /** @var \Drupal\search_api\Processor\ProcessorInterface $processor */ $processors = $this->entity->getProcessors(FALSE); foreach ($processors as $processor_id => $processor) { if (empty($values['status'][$processor_id])) { continue; } $new_settings[$processor_id] = array('processor_id' => $processor_id, 'weights' => array(), 'settings' => array()); $processor_values = $values['processors'][$processor_id]; if (!empty($processor_values['weights'])) { $new_settings[$processor_id]['weights'] = $processor_values['weights']; } if (isset($form['settings'][$processor_id])) { $processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings')); $processor->submitConfigurationForm($form['settings'][$processor_id], $processor_form_state); $new_settings[$processor_id]['settings'] = $processor->getConfiguration(); } } // Sort the processors so we won't have unnecessary changes. ksort($new_settings); if (!$this->entity->getOption('processors', array()) !== $new_settings) { $this->entity->setOption('processors', $new_settings); $this->entity->save(); $this->entity->reindex(); drupal_set_message($this->t('The indexing workflow was successfully edited. All content was scheduled for reindexing so the new settings can take effect.')); } else { drupal_set_message($this->t('No values were changed.')); } }
/** * 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')); $server_name = $this->randomMachineName(); $this->server = Server::create(array( 'id' => strtolower($server_name), 'name' => $server_name, 'status' => TRUE, 'backend' => 'search_api_db', 'backend_config' => array( 'min_chars' => 3, 'database' => 'default:default', ), )); $this->server->save(); $index_name = $this->randomMachineName(); $this->index = Index::create(array( 'id' => strtolower($index_name), 'name' => $index_name, 'status' => TRUE, 'datasources' => array('entity:comment', 'entity:node'), 'server' => $server_name, 'tracker' => 'default', )); $this->index->setServer($this->server); $this->index->setOption('fields', array( 'entity:comment/subject' => array( 'type' => 'text', ), 'entity:node/title' => array( 'type' => 'text', ), )); if ($processor) { $this->index->setOption('processors', array( $processor => array( 'processor_id' => $processor, 'weights' => array(), 'settings' => array(), ), )); /** @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->save(); \Drupal::configFactory() ->getEditable('search_api.settings') ->set('tracking_page_size', 100) ->save(); Utility::getIndexTaskManager()->addItemsAll($this->index); }
/** * Tests task system integration for the server's addIndex() method. */ public function testAddIndex() { // Since we want to add the index, we should first remove it (even though it // shouldn't matter – just for logic consistency). $this->index->setServer(NULL); $this->index->save(); // Set exception for addIndex() and reset the list of successful backend // method calls. $this->state->set('search_api_test_backend.exception.addIndex', TRUE); $this->getCalledServerMethods(); // Try to add the index. $this->server->addIndex($this->index); $this->assertEqual($this->getCalledServerMethods(), array(), 'addIndex correctly threw an exception.'); $tasks = $this->getServerTasks(); if (count($tasks) == 1) { $task_created = $tasks[0]->type === 'addIndex'; } $this->assertTrue(!empty($task_created), 'The addIndex task was successfully added.'); if ($tasks) { $this->assertEqual($tasks[0]->index_id, $this->index->id(), 'The right index ID was used for the addIndex task.'); } // Check whether other task-system-integrated methods now fail, too. $this->server->updateIndex($this->index); $this->assertEqual($this->getCalledServerMethods(), array(), 'updateIndex was not executed.'); $tasks = $this->getServerTasks(); if (count($tasks) == 2) { $this->pass("Second task ('updateIndex') was added."); $this->assertEqual($tasks[0]->type, 'addIndex', 'First task stayed the same.'); $this->assertEqual($tasks[1]->type, 'updateIndex', 'New task was queued as last.'); } else { $this->fail("Second task (updateIndex) was not added."); } // Let addIndex() succeed again, then trigger the task execution with a cron // run. $this->state->set('search_api_test_backend.exception.addIndex', FALSE); search_api_cron(); $this->assertEqual($this->getServerTasks(), array(), 'Server tasks were correctly executed.'); $this->assertEqual($this->getCalledServerMethods(), array('addIndex', 'updateIndex'), 'Right methods were called during task execution.'); }
/** * 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(); }
/** * Removes a field from a search index. * * @param \Drupal\search_api\IndexInterface $search_api_index * The search index. * @param string $field_id * The ID of the field to remove. * * @return \Symfony\Component\HttpFoundation\Response * The response to send to the browser. * * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * Thrown when the field was not found. */ public function removeField(IndexInterface $search_api_index, $field_id) { $fields = $search_api_index->getFields(); if (isset($fields[$field_id])) { try { $search_api_index->removeField($field_id); $search_api_index->save(); } catch (SearchApiException $e) { $args['%field'] = $fields[$field_id]->getLabel(); drupal_set_message($this->t('The field %field is locked and cannot be removed.', $args), 'error'); } } else { throw new NotFoundHttpException(); } // Redirect to the index's "View" page. $url = $search_api_index->toUrl('fields'); return $this->redirect($url->getRouteName(), $url->getRouteParameters()); }