public function DependencyRemovalTest::testDataTypeDependency in Search API 8
Tests whether dependencies of used data types are handled correctly.
@dataProvider dataTypeDependencyTestDataProvider
Parameters
string $dependency_type: The type of dependency that should be set on the data type (and then removed): "module" or "config".
File
- tests/
src/ Kernel/ ConfigEntity/ DependencyRemovalTest.php, line 534
Class
- DependencyRemovalTest
- Tests what happens when an index's or a server's dependencies are removed.
Namespace
Drupal\Tests\search_api\Kernel\ConfigEntityCode
public function testDataTypeDependency($dependency_type) {
switch ($dependency_type) {
case 'module':
$type = 'search_api_test';
$config_dependency_key = 'module';
$config_dependency_name = 'search_api_test';
break;
case 'config':
$type = 'search_api_test_altering';
$config_dependency_key = $this->dependency
->getConfigDependencyKey();
$config_dependency_name = $this->dependency
->getConfigDependencyName();
\Drupal::state()
->set('search_api_test.data_type.dependencies', [
$config_dependency_key => [
$config_dependency_name,
],
]);
break;
default:
$this
->fail();
return;
}
// Use the "user" datasource (to not get a module dependency via that) and
// add a field with the given data type.
$datasources = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createDatasourcePlugins($this->index, [
'entity:user',
]);
$this->index
->setDatasources($datasources);
$field = \Drupal::getContainer()
->get('search_api.fields_helper')
->createField($this->index, 'uid', [
'label' => 'ID',
'datasource_id' => 'entity:user',
'property_path' => 'uid',
'type' => $type,
]);
$this->index
->addField($field);
// Set the server to NULL to not have a dependency on that by default.
$this->index
->setServer(NULL);
$this->index
->save();
// Check the dependencies were calculated correctly.
$dependencies = $this->index
->getDependencies();
$dependencies += [
$config_dependency_key => [],
];
$this
->assertContains($config_dependency_name, $dependencies[$config_dependency_key], 'Data type dependency correctly inserted');
switch ($dependency_type) {
case 'module':
// Disabling modules in Kernel tests normally doesn't trigger any kind of
// reaction, just removes it from the list of modules (for example, to
// avoid calling any of its hooks). Therefore, we have to trigger that
// behavior ourselves.
\Drupal::getContainer()
->get('config.manager')
->uninstall('module', 'search_api_test');
break;
case 'config':
$this->dependency
->delete();
break;
}
// 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 += [
$config_dependency_key => [],
];
$this
->assertNotContains($config_dependency_name, $dependencies[$config_dependency_key], 'Data type dependency correctly removed');
// Make sure the field type has changed.
$field = $this->index
->getField('uid');
$this
->assertNotNull($field, 'Field was not removed');
$this
->assertEquals('string', $field
->getType(), 'Field type was changed to fallback type');
}