/** * Creates or deletes a server. * * @param string $name * (optional) The name of the server. * @param string $id * (optional) The ID of the server. * @param string $backend_id * (optional) The ID of the backend to set for the server. * @param array $backend_config * (optional) The backend configuration to set for the server. * @param bool $reset * (optional) If TRUE, delete the server instead of creating it. (Only the * server's ID is required in that case). * * @return \Drupal\search_api\ServerInterface * A search server. */ public function getTestServer($name = 'WebTest server', $id = 'webtest_server', $backend_id = 'search_api_test_backend', $backend_config = array(), $reset = FALSE) { if ($reset) { $server = Server::load($id); if ($server) { $server->delete(); } } else { $server = Server::create(array('id' => $id, 'name' => $name, 'description' => $name, 'backend' => $backend_id, 'backend_config' => $backend_config)); $server->save(); } return $server; }
/** * Tests whether removing the configuration again works as it should. */ protected function checkModuleUninstall() { // See whether clearing the server works. // Regression test for #2156151. $server = Server::load($this->serverId); $index = Index::load($this->indexId); $server->deleteAllIndexItems($index); $query = $this->buildSearch(); $results = $query->execute(); $this->assertEqual($results->getResultCount(), 0, 'Clearing the server worked correctly.'); $table = 'search_api_db_' . $this->indexId; $this->assertTrue(db_table_exists($table), 'The index tables were left in place.'); // Remove first the index and then the server. $index->setServer(); $index->save(); $server = Server::load($this->serverId); $this->assertEqual($server->getBackendConfig()['field_tables'], array(), 'The index was successfully removed from the server.'); $this->assertFalse(db_table_exists($table), 'The index tables were deleted.'); $server->delete(); // Uninstall the module. \Drupal::service('module_installer')->uninstall(array('search_api_db'), FALSE); #\Drupal::moduleHandler()->uninstall(array('search_api_db'), FALSE); $this->assertFalse(\Drupal::moduleHandler()->moduleExists('search_api_db'), 'The Database Search module was successfully disabled.'); $prefix = \Drupal::database()->prefixTables('{search_api_db_}') . '%'; $this->assertEqual(\Drupal::database()->schema()->findTables($prefix), array(), 'The Database Search module was successfully uninstalled.'); }
/** * Retrieves the search server used by this test. * * @return \Drupal\search_api\ServerInterface * The search server. */ protected function getServer() { return Server::load($this->serverId); }
/** * Implements the magic __wakeup() method. * * Reloads the server entity. */ public function __wakeup() { parent::__wakeup(); if ($this->serverId) { $this->server = Server::load($this->serverId); $this->serverId = NULL; } }
/** * {@inheritdoc} */ protected function checkModuleUninstall() { // See whether clearing the server works. // Regression test for #2156151. $server = Server::load($this->serverId); $index = Index::load($this->indexId); $server->deleteAllItems($index); // Deleting items take at least 1 second for Solr to parse it so that drupal // doesn't get timeouts while waiting for Solr. Lets give it 2 seconds to // make sure we are in bounds. sleep(2); $query = $this->buildSearch(); $results = $query->execute(); $this->assertEqual($results->getResultCount(), 0, 'Clearing the server worked correctly.'); }
/** * {@inheritdoc} */ public function getServerInstance() { if (!$this->serverInstance && $this->server) { $this->serverInstance = Server::load($this->server); if (!$this->serverInstance) { $args['@server'] = $this->server; $args['%index'] = $this->label(); throw new SearchApiException(new FormattableMarkup('The server with ID "@server" could not be retrieved for index %index.', $args)); } } return $this->serverInstance; }
/** * Tests deleting a search server via the UI. */ protected function deleteServer() { $server = Server::load($this->serverId); // Load confirmation form. $this->drupalGet('admin/config/search/search-api/server/' . $this->serverId . '/delete'); $this->assertResponse(200, 'Server delete page exists'); $this->assertRaw(t('Are you sure you want to delete the search server %name?', array('%name' => $server->label())), 'Deleting a server sks for confirmation.'); $this->assertText(t('Deleting a server will disable all its indexes and their searches.'), 'Correct warning is displayed when deleting a server.'); // Confirm deletion. $this->drupalPostForm(NULL, NULL, t('Delete')); $this->assertRaw(t('The search server %name has been deleted.', array('%name' => $server->label())), 'The server was deleted.'); $this->assertFalse(Server::load($this->serverId), 'Server could not be found anymore.'); $this->assertUrl('admin/config/search/search-api', array(), 'Correct redirect to search api overview page.'); // Confirm that the index hasn't been deleted. $this->indexStorage->resetCache(array($this->indexId)); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->indexStorage->load($this->indexId); if ($this->assertTrue($index, 'The index associated with the server was not deleted.')) { $this->assertFalse($index->status(), 'The index associated with the server was disabled.'); $this->assertNull($index->getServerId(), 'The index was removed from the server.'); } }
/** * Tests whether the default search was correctly installed. */ protected function testInstallAndDefaultSetupWorking() { $this->drupalLogin($this->adminUser); // Install the search_api_db_defaults module. $edit_enable = array('modules[Search][search_api_db_defaults][enable]' => TRUE); $this->drupalPostForm('admin/modules', $edit_enable, t('Install')); $this->assertText(t('Some required modules must be enabled'), 'Dependencies required.'); $this->drupalPostForm(NULL, NULL, t('Continue')); $args = array('@count' => 3, '%names' => 'Database Search Defaults, Database search, Search API'); $success_text = strip_tags(t('@count modules have been enabled: %names.', $args)); $this->assertText($success_text, 'Modules have been installed.'); $this->rebuildContainer(); $server = Server::load('default_server'); $this->assertTrue($server, 'Server can be loaded'); $index = Index::load('default_index'); $this->assertTrue($index, 'Index can be loaded'); $this->drupalLogin($this->authenticatedUser); $this->drupalGet('search/content'); $this->assertResponse(200, 'Authenticated user can access the search view.'); $this->drupalLogin($this->adminUser); // Uninstall the module. $edit_disable = array('uninstall[search_api_db_defaults]' => TRUE); $this->drupalPostForm('admin/modules/uninstall', $edit_disable, t('Uninstall')); $this->drupalPostForm(NULL, array(), t('Uninstall')); $this->rebuildContainer(); $this->assertFalse($this->container->get('module_handler')->moduleExists('search_api_db_defaults'), 'Search API DB Defaults module uninstalled.'); // Check if the server is found in the Search API admin UI. $this->drupalGet('admin/config/search/search-api/server/default_server'); $this->assertResponse(200, 'Server was not removed.'); // Check if the index is found in the Search API admin UI. $this->drupalGet('admin/config/search/search-api/index/default_index'); $this->assertResponse(200, 'Index was not removed.'); $this->drupalLogin($this->authenticatedUser); $this->drupalGet('search/content'); $this->assertResponse(200, 'Authenticated user can access the search view.'); $this->drupalLogin($this->adminUser); // Enable the module again. This should fail because the either the index // or the server or the view was found. $this->drupalPostForm('admin/modules', $edit_enable, t('Install')); $this->assertText(t('It looks like the default setup provided by this module already exists on your site. Cannot re-install module.')); // Delete all the entities that we would fail on if they exist. $entities_to_remove = array('search_api_index' => 'default_index', 'search_api_server' => 'default_server', 'view' => 'search_content'); /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ $entity_type_manager = \Drupal::service('entity_type.manager'); foreach ($entities_to_remove as $entity_type => $entity_id) { /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */ $entity_storage = $entity_type_manager->getStorage($entity_type); $entity_storage->resetCache(); $entities = $entity_storage->loadByProperties(array('id' => $entity_id)); if (!empty($entities[$entity_id])) { $entities[$entity_id]->delete(); } } // Delete the article content type. $this->drupalGet('admin/structure/types/manage/article'); $this->clickLink(t('Delete')); $this->assertResponse(200); $this->drupalPostForm(NULL, array(), t('Delete')); // Try to install search_api_db_defaults module and test if it failed // because there was no content type "article". $this->drupalPostForm('admin/modules', $edit_enable, t('Install')); $success_text = t('Content type @content_type not found. Database Search Defaults module could not be installed.', array('@content_type' => 'article')); $this->assertText($success_text); }
/** * Tests whether the default search was correctly installed. */ protected function testDefaultSetupWorking() { $server = Server::load('default_server'); $this->assertTrue($server, 'Server can be loaded'); $index = Index::load('default_index'); $this->assertTrue($index, 'Index can be loaded'); $this->drupalGet('search/content'); $this->assertResponse(200, 'Anonymous user can access the search page.'); $this->drupalLogin($this->authenticatedUser); $this->drupalGet('search/content'); $this->assertResponse(200, 'Authenticated user can access the search page.'); }
/** * Tests Acquia Search environment creation. * * Tests executed: * - Acquia Search environment is saved and loaded. * - Acquia Search environment is set as the default environment when created. * - The service class is set to AcquiaSearchService. * - The environment's URL is built as expected. */ public function testEnvironment() { // Connect site on key and id. $this->drupalGet('admin/config/search/search-api'); $environment = Server::load('acquia_search_server'); // Check if the environment is a valid variable. $this->assertTrue($environment, t('Acquia Search environment saved.'), 'Acquia Search'); }