/** * {@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); }
/** * 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 a backend with a dependency that gets removed. * * If the dependency does not get removed, proper cascading to the index is * also verified. * * @param bool $remove_dependency * Whether to remove the dependency from the backend when the object * depended on is deleted. * * @dataProvider dependencyTestDataProvider */ public function testBackendDependency($remove_dependency) { $dependency_key = $this->dependency->getConfigDependencyKey(); $dependency_name = $this->dependency->getConfigDependencyName(); // Create a server using the test backend, and set the dependency in the // configuration. /** @var \Drupal\search_api\ServerInterface $server */ $server = Server::create(array('id' => 'test_server', 'name' => 'Test server', 'backend' => 'search_api_test_backend', 'backend_config' => array('dependencies' => array($dependency_key => array($dependency_name))))); $server->save(); $server_dependency_key = $server->getConfigDependencyKey(); $server_dependency_name = $server->getConfigDependencyName(); // Set the server on the index and save that, too. However, we don't want // the index enabled, since that would lead to all kinds of overhead which // is completely irrelevant for this test. $this->index->setServer($server); $this->index->disable(); $this->index->save(); // Check that the dependencies were calculated correctly. $server_dependencies = $server->getDependencies(); $this->assertContains($dependency_name, $server_dependencies[$dependency_key], 'Backend dependency correctly inserted'); $index_dependencies = $this->index->getDependencies(); $this->assertContains($server_dependency_name, $index_dependencies[$server_dependency_key], 'Server dependency correctly inserted'); // Set our magic state key to let the test plugin know whether the // dependency should be removed or not. See // \Drupal\search_api_test_backend\Plugin\search_api\backend\TestBackend::onDependencyRemoval(). $key = 'search_api_test_backend.dependencies.remove'; \Drupal::state()->set($key, $remove_dependency); // Delete the backend's dependency. $this->dependency->delete(); // Reload the index and check it's still there. $this->reloadIndex(); $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed'); // Reload the server. $storage = \Drupal::entityTypeManager()->getStorage('search_api_server'); $storage->resetCache(); $server = $storage->load($server->id()); if ($remove_dependency) { $this->assertInstanceOf('Drupal\\search_api\\ServerInterface', $server, 'Server was not removed'); $this->assertArrayNotHasKey('dependencies', $server->get('backend_config'), 'Backend config was adapted'); // @todo Logically, this should not be changed: if the server does not get // removed, there is no need to adapt the index's configuration. // However, the way this config dependency cascading is actually // implemented in // \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval() // does not seem to follow that logic, but just computes the complete // tree of dependencies once and operates generally on the assumption // that all of them will be deleted. See #2642374. // $this->assertEquals($server->id(), $this->index->getServerId(), "Index's server was not changed"); } else { $this->assertNull($server, 'Server was removed'); $this->assertEquals(NULL, $this->index->getServerId(), 'Index server was changed'); } }
/** * 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(); }
/** * {@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); }
/** * {@inheritdoc} */ public function setServer(ServerInterface $server = NULL) { return $this->entity->setServer($server); }