public function BundlePluginTest::testPluginBundles in Entity API 8
Tests the bundle plugins.
File
- tests/
src/ Kernel/ BundlePluginTest.php, line 39
Class
- BundlePluginTest
- Tests the bundle plugin API.
Namespace
Drupal\Tests\entity\KernelCode
public function testPluginBundles() {
$bundled_entity_types = entity_get_bundle_plugin_entity_types();
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
$entity_type = $bundled_entity_types['entity_test_bundle_plugin'];
$this
->assertEquals('entity_test_bundle_plugin', $entity_type
->id());
$this
->assertTrue($entity_type
->hasHandlerClass('bundle_plugin'));
/** @var \Drupal\Core\Entity\EntityTypeBundleInfo $entity_type_bundle_info */
$entity_type_bundle_info = $this->container
->get('entity_type.bundle.info');
$bundle_info = $entity_type_bundle_info
->getBundleInfo('entity_test_bundle_plugin');
$this
->assertEquals(2, count($bundle_info));
$this
->assertArrayHasKey('first', $bundle_info);
$this
->assertArrayHasKey('second', $bundle_info);
$this
->assertEquals('First', $bundle_info['first']['label']);
$this
->assertEquals('Some description', $bundle_info['first']['description']);
/** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
$entity_field_manager = $this->container
->get('entity_field.manager');
$field_storage_definitions = $entity_field_manager
->getFieldStorageDefinitions('entity_test_bundle_plugin');
$this
->assertArrayHasKey('first_mail', $field_storage_definitions);
$this
->assertArrayHasKey('second_mail', $field_storage_definitions);
$first_field_definitions = $entity_field_manager
->getFieldDefinitions('entity_test_bundle_plugin', 'first');
$this
->assertArrayHasKey('first_mail', $first_field_definitions);
$this
->assertArrayNotHasKey('second_mail', $first_field_definitions);
$second_field_definitions = $entity_field_manager
->getFieldDefinitions('entity_test_bundle_plugin', 'second');
$this
->assertArrayNotHasKey('first_mail', $second_field_definitions);
$this
->assertArrayHasKey('second_mail', $second_field_definitions);
$first_entity = EntityTestBundlePlugin::create([
'type' => 'first',
'first_mail' => 'admin@test.com',
]);
$first_entity
->save();
$first_entity = EntityTestBundlePlugin::load($first_entity
->id());
$this
->assertEquals('admin@test.com', $first_entity->first_mail->value);
$second_entity = EntityTestBundlePlugin::create([
'type' => 'second',
'second_mail' => 'admin@example.com',
]);
$second_entity
->save();
$second_entity = EntityTestBundlePlugin::load($second_entity
->id());
$this
->assertEquals('admin@example.com', $second_entity->second_mail->value);
// Also test entity queries.
$result = $this->container
->get('entity_type.manager')
->getStorage('entity_test_bundle_plugin')
->getQuery()
->condition('second_mail', 'admin@example.com')
->execute();
$this
->assertEquals([
$second_entity
->id() => $second_entity
->id(),
], $result);
$result = $this->container
->get('entity_type.manager')
->getStorage('entity_test_bundle_plugin')
->getQuery()
->condition('type', 'first')
->execute();
$this
->assertEquals([
$first_entity
->id() => $first_entity
->id(),
], $result);
}