You are here

public function FormatterTest::testRenderOutput in Scheduled Publish 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/FormatterTest.php \Drupal\Tests\scheduled_publish\Kernel\FormatterTest::testRenderOutput()

File

tests/src/Kernel/FormatterTest.php, line 92

Class

FormatterTest
Class FormatterTest

Namespace

Drupal\Tests\scheduled_publish\Kernel

Code

public function testRenderOutput() {
  $display = EntityViewDisplay::create([
    'targetEntityType' => 'node',
    'bundle' => 'page',
    'mode' => 'default',
    'status' => TRUE,
  ]);
  $display_options = [
    'label' => 'above',
    'type' => 'scheduled_publish_generic_formatter',
    'settings' => [
      'date_format' => 'html_date',
      'text_pattern' => '%moderation_state% ------ %date%',
    ],
  ];
  $display
    ->setComponent('field_scheduled_publish', $display_options);
  $display
    ->save();
  $page = Node::create([
    'type' => 'page',
    'title' => 'A',
    'uid' => 1,
  ]);
  $page->moderation_state->value = 'draft';
  $page
    ->set('field_scheduled_publish', [
    'moderation_state' => 'published',
    'value' => '2021-01-31T00:00:00',
  ]);
  $page
    ->save();
  $nodeID = $page
    ->id();
  $view_builder = \Drupal::entityTypeManager()
    ->getViewBuilder('node');
  $storage = \Drupal::entityTypeManager()
    ->getStorage('node');
  $node = $storage
    ->load($nodeID);
  $build = $view_builder
    ->view($node, 'default');
  $output = \Drupal::service('renderer')
    ->renderPlain($build);
  self::assertTrue($nodeID);
  self::assertTrue(strpos($output, '<div>published ------ 2021-01-31</div>') !== FALSE);
  $this->scheduledUpdateService
    ->doUpdate();
  $loadedNode = Node::load($nodeID);
}