InitialStateTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\entity_test\Entity\EntityTestRev;
  4. use Drupal\KernelTests\KernelTestBase;
  5. use Drupal\node\Entity\Node;
  6. use Drupal\node\Entity\NodeType;
  7. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  8. /**
  9. * Tests the correct initial states are set on install.
  10. *
  11. * @group content_moderation
  12. */
  13. class InitialStateTest extends KernelTestBase {
  14. use ContentModerationTestTrait;
  15. /**
  16. * {@inheritdoc}
  17. */
  18. public static $modules = [
  19. 'entity_test',
  20. 'node',
  21. 'user',
  22. 'system',
  23. ];
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function setUp() {
  28. parent::setUp();
  29. $this->installSchema('node', 'node_access');
  30. $this->installEntitySchema('node');
  31. $this->installEntitySchema('user');
  32. $this->installEntitySchema('entity_test_rev');
  33. }
  34. /**
  35. * Tests the correct initial state.
  36. */
  37. public function testInitialState() {
  38. $node_type = NodeType::create([
  39. 'type' => 'example',
  40. ]);
  41. $node_type->save();
  42. // Test with an entity type that implements EntityPublishedInterface.
  43. $unpublished_node = Node::create([
  44. 'type' => 'example',
  45. 'title' => 'Unpublished node',
  46. 'status' => 0,
  47. ]);
  48. $unpublished_node->save();
  49. $published_node = Node::create([
  50. 'type' => 'example',
  51. 'title' => 'Published node',
  52. 'status' => 1,
  53. ]);
  54. $published_node->save();
  55. // Test with an entity type that doesn't implement EntityPublishedInterface.
  56. $entity_test = EntityTestRev::create();
  57. $entity_test->save();
  58. \Drupal::service('module_installer')->install(['content_moderation'], TRUE);
  59. $workflow = $this->createEditorialWorkflow();
  60. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
  61. $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
  62. $workflow->save();
  63. $loaded_unpublished_node = Node::load($unpublished_node->id());
  64. $loaded_published_node = Node::load($published_node->id());
  65. $loaded_entity_test = EntityTestRev::load($entity_test->id());
  66. $this->assertEquals('draft', $loaded_unpublished_node->moderation_state->value);
  67. $this->assertEquals('published', $loaded_published_node->moderation_state->value);
  68. $this->assertEquals('draft', $loaded_entity_test->moderation_state->value);
  69. $presave_node = Node::create([
  70. 'type' => 'example',
  71. 'title' => 'Presave node',
  72. ]);
  73. $this->assertEquals('draft', $presave_node->moderation_state->value);
  74. }
  75. }