/** * 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'); } }
/** * {@inheritdoc} */ public function disable() { return $this->entity->disable(); }