/** * {@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(); $this->installSchema('search_api', array('search_api_item', 'search_api_task')); $this->installSchema('system', array('router')); $this->installSchema('user', array('users_data')); $this->setUpExampleStructure(); $this->installConfig(array('search_api_test_db')); $this->insertExampleContent(); // 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(); $this->index = Index::load('database_search_index'); $this->index->setServer($this->server); }
/** * {@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(); }
/** * Enables a search server without a confirmation form. * * @param \Drupal\search_api\ServerInterface $search_api_server * The server to be enabled. * * @return \Symfony\Component\HttpFoundation\Response * The response to send to the browser. */ public function serverBypassEnable(ServerInterface $search_api_server) { $search_api_server->setStatus(TRUE)->save(); // Notify the user about the status change. drupal_set_message($this->t('The search server %name has been enabled.', array('%name' => $search_api_server->label()))); // Redirect to the server's "View" page. $url = $search_api_server->urlInfo('canonical'); return $this->redirect($url->getRouteName(), $url->getRouteParameters()); }
/** * 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); }
/** * Implements the magic __sleep() method. * * Prevents the server entity from being serialized. */ public function __sleep() { if ($this->server) { $this->serverId = $this->server->id(); } $properties = array_flip(parent::__sleep()); unset($properties['server']); return array_keys($properties); }
/** * Tests tracking of items when saving an index through the CLI. */ public function testItemTracking() { EntityTest::create(array('name' => 'foo bar baz föö smile' . json_decode('"\\u1F601"'), 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('Orange', 'orange', 'örange', 'Orange'), 'category' => 'item_category'))->save(); EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category'))->save(); // Create a test index. /** @var \Drupal\search_api\IndexInterface $index */ $index = Index::create(array('name' => 'Test index', 'id' => 'index', 'status' => 1, 'datasource_settings' => array('entity:entity_test' => array('plugin_id' => 'entity:entity_test', 'settings' => array())), 'tracker_settings' => array('default' => array('plugin_id' => 'default', 'settings' => array())), 'server' => $this->server->id(), 'options' => array('index_directly' => TRUE))); $index->save(); $total_items = $index->getTrackerInstance()->getTotalItemsCount(); $indexed_items = $index->getTrackerInstance()->getIndexedItemsCount(); $this->assertEquals(2, $total_items, 'The 2 items are tracked.'); $this->assertEquals(0, $indexed_items, 'No items are indexed'); EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category'))->save(); EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category'))->save(); $total_items = $index->getTrackerInstance()->getTotalItemsCount(); $indexed_items = $index->getTrackerInstance()->getIndexedItemsCount(); $this->assertEquals(4, $total_items, 'All 4 items are tracked.'); $this->assertEquals(2, $indexed_items, '2 items are indexed'); }
/** * Retrieves the tasks set on the test server. * * @return object[] * All tasks read from the database for the test server, with numeric keys * starting with 0. */ protected function getServerTasks() { $tasks = array(); $select = \Drupal::database()->select('search_api_task', 't'); $select->fields('t')->orderBy('id')->condition('server_id', $this->server->id()); foreach ($select->execute() as $task) { if ($task->data) { $task->data = unserialize($task->data); } $tasks[] = $task; } return $tasks; }
/** * 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(); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $this->installSchema('search_api', array('search_api_item', 'search_api_task')); $this->installSchema('system', array('router')); $this->installSchema('user', array('users_data')); $this->installEntitySchema('entity_test'); // 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->installConfig(array('search_api_test_db')); // Create test entities. $this->entities[1] = EntityTest::create(array('name' => 'foo bar baz föö smile' . json_decode('"\\u1F601"'), 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('Orange', 'orange', 'örange', 'Orange'), 'category' => 'item_category')); $this->entities[2] = EntityTest::create(array('name' => 'foo bar baz föö smile', 'body' => 'test test case Case casE', 'type' => 'item', 'keywords' => array('strawberry', 'llama'), 'category' => 'item_category')); $this->entities[1]->save(); $this->entities[2]->save(); // Create a test server. $this->server = Server::create(array('name' => 'Server test ~', 'id' => 'test', 'status' => 1, 'backend' => 'search_api_test_backend')); $this->server->save(); $this->index = Index::load('database_search_index'); $this->index->setServer($this->server); }
/** * Asserts enable/disable operations for a search server or index. * * @param \Drupal\search_api\ServerInterface|\Drupal\search_api\IndexInterface $entity * A search server or index. */ protected function assertEntityStatusChange($entity) { $this->drupalGet($this->overviewPageUrl); $row_class = Html::cleanCssIdentifier($entity->getEntityTypeId() . '-' . $entity->id()); $this->assertFieldByXPath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-enabled")]', NULL, 'The newly created entity is enabled by default.'); // The first "Disable" link on the page belongs to our server, the second // one to our index. $this->clickLink('Disable', $entity instanceof ServerInterface ? 0 : 1); // Submit the confirmation form and test that the entity has been disabled. $this->drupalPostForm(NULL, array(), 'Disable'); $this->assertFieldByXPath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-disabled")]', NULL, 'The entity has been disabled.'); // Now enable the entity and verify that the operation succeeded. $this->clickLink('Enable'); $this->drupalGet($this->overviewPageUrl); $this->assertFieldByXPath('//tr[contains(@class,"' . $row_class . '") and contains(@class, "search-api-list-enabled")]', NULL, 'The entity has benn enabled.'); }
/** * Builds the backend-specific configuration form. * * @param \Drupal\search_api\ServerInterface $server * The server that is being created or edited. */ public function buildBackendConfigForm(array &$form, FormStateInterface $form_state, ServerInterface $server) { $form['backend_config'] = array('#type' => 'container', '#attributes' => array('id' => 'search-api-backend-config-form'), '#tree' => TRUE); if ($server->hasValidBackend()) { $backend = $server->getBackend(); if ($backend_form = $backend->buildConfigurationForm(array(), $form_state)) { // If the backend plugin changed, notify the user. if (!empty($form_state->getValues()['backend'])) { drupal_set_message($this->t('Please configure the used backend.'), 'warning'); } // Modify the backend plugin configuration container element. $form['backend_config']['#type'] = 'details'; $form['backend_config']['#title'] = $this->t('Configure %plugin backend', array('%plugin' => $backend->label())); $form['backend_config']['#description'] = $backend->getDescription(); $form['backend_config']['#open'] = TRUE; // Attach the backend plugin configuration form. $form['backend_config'] += $backend_form; } } elseif (!$server->isNew()) { drupal_set_message($this->t('The backend plugin is missing or invalid.'), 'error'); } }
/** * {@inheritdoc} */ public function setServer(ServerInterface $server = NULL) { $this->serverInstance = $server; $this->server = $server ? $server->id() : NULL; }
/** * Tests whether deleting a server works correctly. * * @param \Drupal\search_api\ServerInterface $server * The server used for this test. */ public function serverDelete(ServerInterface $server) { $this->storage->delete(array($server)); $loaded_server = $this->storage->load($server->id()); $this->assertNull($loaded_server); }
/** * Retrieves a list of all config files of a server's Solr backend. * * @param \Drupal\search_api\ServerInterface $server * The Solr server whose files should be retrieved. * @param string $dir_name * (optional) The directory that should be searched for files. Defaults to the * root config directory. * * @return array * An associative array of all config files in the given directory. The keys * are the file names, values are arrays with information about the file. The * files are returned in alphabetical order and breadth-first. * * @throws \Drupal\search_api\SearchApiException * If a problem occurred while retrieving the files. */ public static function getServerFiles(ServerInterface $server, $dir_name = NULL) { $response = $server->getBackend()->getFile($dir_name); // Search for directories and recursively merge directory files. $files_data = json_decode($response->getBody(), TRUE); $files_list = $files_data['files']; $dir_length = strlen($dir_name) + 1; $result = array('' => array()); foreach ($files_list as $file_name => $file_info) { // Annoyingly, Solr 4.7 changed the way the admin/file handler returns // the file names when listing directory contents: the returned name is now // only the base name, not the complete path from the config root directory. // We therefore have to check for this case. if ($dir_name && substr($file_name, 0, $dir_length) !== "{$dir_name}/") { $file_name = "{$dir_name}/" . $file_name; } if (empty($file_info['directory'])) { $result[''][$file_name] = $file_info; } else { $result[$file_name] = static::getServerFiles($server, $file_name); } } ksort($result); ksort($result['']); return array_reduce($result, 'array_merge', array()); }
/** * {@inheritdoc} */ public function delete(array $ids = NULL, ServerInterface $server = NULL, $index = NULL) { $delete = $this->database->delete('search_api_task'); if ($ids) { $delete->condition('id', $ids, 'IN'); } if ($server) { $delete->condition('server_id', $server->id()); } if ($index) { $delete->condition('index_id', $index instanceof IndexInterface ? $index->id() : $index); } $delete->execute(); }