EntityDeriverTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. use Drupal\comment\Entity\CommentType;
  4. use Drupal\Component\Plugin\Exception\PluginNotFoundException;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\node\Entity\NodeType;
  7. /**
  8. * Tests EntityDeriver functionality.
  9. *
  10. * @coversDefaultClass \Drupal\Core\Entity\Plugin\DataType\Deriver\EntityDeriver
  11. *
  12. * @group Entity
  13. */
  14. class EntityDeriverTest extends KernelTestBase {
  15. /**
  16. * The typed data manager to use.
  17. *
  18. * @var \Drupal\Core\TypedData\TypedDataManagerInterface
  19. */
  20. protected $typedDataManager;
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public static $modules = [
  25. 'system',
  26. 'field',
  27. 'user',
  28. 'node',
  29. 'comment',
  30. 'entity_test',
  31. ];
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function setUp() {
  36. parent::setup();
  37. $this->installEntitySchema('comment');
  38. NodeType::create(['type' => 'article', 'name' => 'Article'])->save();
  39. CommentType::create([
  40. 'id' => 'comment',
  41. 'name' => 'Default comment',
  42. 'target_entity_type_id' => 'node',
  43. ])->save();
  44. entity_test_create_bundle('foo', NULL, 'entity_test_no_bundle');
  45. entity_test_create_bundle('entity_test_no_bundle', NULL, 'entity_test_no_bundle');
  46. $this->typedDataManager = $this->container->get('typed_data_manager');
  47. }
  48. /**
  49. * Tests that types are derived for entity types with and without bundles.
  50. *
  51. * @dataProvider derivativesProvider
  52. */
  53. public function testDerivatives($data_type, $expect_exception) {
  54. if ($expect_exception) {
  55. $this->expectException(PluginNotFoundException::class);
  56. }
  57. $this->typedDataManager->createDataDefinition($data_type);
  58. }
  59. /**
  60. * Provides test data for ::testDerivatives().
  61. */
  62. public function derivativesProvider() {
  63. return [
  64. 'unbundleable entity type with no bundle type' => ['entity:user', FALSE],
  65. 'unbundleable entity type with bundle type' => ['entity:user:user', TRUE],
  66. 'bundleable entity type with no bundle type' => ['entity:node', FALSE],
  67. 'bundleable entity type with bundle type' => [
  68. 'entity:node:article',
  69. FALSE,
  70. ],
  71. 'bundleable entity type with bundle type with matching name' => [
  72. 'entity:comment:comment',
  73. FALSE,
  74. ],
  75. 'unbundleable entity type with entity_test_entity_bundle_info()-generated bundle type' => [
  76. 'entity:entity_test_no_bundle:foo',
  77. FALSE,
  78. ],
  79. 'unbundleable entity type with entity_test_entity_bundle_info()-generated bundle type with matching name' => [
  80. 'entity:entity_test_no_bundle:entity_test_no_bundle',
  81. FALSE,
  82. ],
  83. ];
  84. }
  85. }