EntityTypeInfoTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\content_moderation\Entity\Handler\ModerationHandler;
  4. use Drupal\content_moderation\EntityTypeInfo;
  5. use Drupal\KernelTests\KernelTestBase;
  6. /**
  7. * @coversDefaultClass \Drupal\content_moderation\EntityTypeInfo
  8. *
  9. * @group content_moderation
  10. */
  11. class EntityTypeInfoTest extends KernelTestBase {
  12. /**
  13. * Modules to enable.
  14. *
  15. * @var array
  16. */
  17. public static $modules = [
  18. 'content_moderation',
  19. 'workflows',
  20. 'entity_test',
  21. ];
  22. /**
  23. * The entity type manager.
  24. *
  25. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  26. */
  27. protected $entityTypeManager;
  28. /**
  29. * The entity type info class.
  30. *
  31. * @var \Drupal\content_moderation\EntityTypeInfo
  32. */
  33. protected $entityTypeInfo;
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function setUp() {
  38. parent::setUp();
  39. $this->entityTypeInfo = $this->container->get('class_resolver')->getInstanceFromDefinition(EntityTypeInfo::class);
  40. $this->entityTypeManager = $this->container->get('entity_type.manager');
  41. }
  42. /**
  43. * @covers ::entityBaseFieldInfo
  44. */
  45. public function testEntityBaseFieldInfo() {
  46. $definition = $this->entityTypeManager->getDefinition('entity_test');
  47. $definition->setHandlerClass('moderation', ModerationHandler::class);
  48. $base_fields = $this->entityTypeInfo->entityBaseFieldInfo($definition);
  49. $this->assertFalse($base_fields['moderation_state']->isReadOnly());
  50. $this->assertTrue($base_fields['moderation_state']->isComputed());
  51. $this->assertTrue($base_fields['moderation_state']->isTranslatable());
  52. }
  53. /**
  54. * Test the correct entity types have moderation added.
  55. *
  56. * @covers ::entityTypeAlter
  57. *
  58. * @dataProvider providerTestEntityTypeAlter
  59. */
  60. public function testEntityTypeAlter($entity_type_id, $moderatable) {
  61. $entity_types = $this->entityTypeManager->getDefinitions();
  62. $this->assertSame($moderatable, $entity_types[$entity_type_id]->hasHandlerClass('moderation'));
  63. }
  64. /**
  65. * Provides test data for testEntityTypeAlter().
  66. *
  67. * @return array
  68. * An array of test cases, where each test case is an array with the
  69. * following values:
  70. * - An entity type ID.
  71. * - Whether the entity type is moderatable or not.
  72. */
  73. public function providerTestEntityTypeAlter() {
  74. $tests = [];
  75. $tests['non_internal_non_revisionable'] = ['entity_test', FALSE];
  76. $tests['non_internal_revisionable'] = ['entity_test_rev', TRUE];
  77. $tests['internal_non_revisionable'] = ['entity_test_no_label', FALSE];
  78. $tests['internal_revisionable'] = ['content_moderation_state', FALSE];
  79. return $tests;
  80. }
  81. }