StateFormatterTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\Core\Render\RenderContext;
  4. use Drupal\entity_test\Entity\EntityTestRev;
  5. use Drupal\KernelTests\KernelTestBase;
  6. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  7. /**
  8. * Test the state field formatter.
  9. *
  10. * @group content_moderation
  11. */
  12. class StateFormatterTest extends KernelTestBase {
  13. use ContentModerationTestTrait;
  14. /**
  15. * Modules to enable.
  16. *
  17. * @var array
  18. */
  19. public static $modules = [
  20. 'workflows',
  21. 'content_moderation',
  22. 'entity_test',
  23. 'user',
  24. ];
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function setUp() {
  29. parent::setUp();
  30. $this->installEntitySchema('entity_test_rev');
  31. $this->installEntitySchema('content_moderation_state');
  32. $this->installConfig('content_moderation');
  33. $workflow = $this->createEditorialWorkflow();
  34. $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
  35. $workflow->save();
  36. }
  37. /**
  38. * Test the embed field.
  39. *
  40. * @dataProvider formatterTestCases
  41. */
  42. public function testStateFieldFormatter($field_value, $formatter_settings, $expected_output) {
  43. $entity = EntityTestRev::create([
  44. 'moderation_state' => $field_value,
  45. ]);
  46. $entity->save();
  47. $field_output = $this->container->get('renderer')->executeInRenderContext(new RenderContext(), function () use ($entity, $formatter_settings) {
  48. return $entity->moderation_state->view($formatter_settings);
  49. });
  50. $this->assertEquals($expected_output, $field_output[0]);
  51. }
  52. /**
  53. * Test cases for ::
  54. */
  55. public function formatterTestCases() {
  56. return [
  57. 'Draft State' => [
  58. 'draft',
  59. [
  60. 'type' => 'content_moderation_state',
  61. 'settings' => [],
  62. ],
  63. [
  64. '#markup' => 'Draft',
  65. ],
  66. ],
  67. 'Published State' => [
  68. 'published',
  69. [
  70. 'type' => 'content_moderation_state',
  71. 'settings' => [],
  72. ],
  73. [
  74. '#markup' => 'Published',
  75. ],
  76. ],
  77. ];
  78. }
  79. }