You are here

public function DiffPluginVariousTest::testCorePluginTimestampField in Diff 8

Tests the Core plugin with a timestamp field.

@covers \Drupal\diff\Plugin\diff\Field\CoreFieldBuilder

File

tests/src/Functional/DiffPluginVariousTest.php, line 152

Class

DiffPluginVariousTest
Tests the Diff module plugins.

Namespace

Drupal\Tests\diff\Functional

Code

public function testCorePluginTimestampField() {

  // Add a timestamp field (supported by the Diff core plugin) to the Article
  // content type.
  $field_name = 'field_timestamp';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => 'node',
    'type' => 'timestamp',
  ]);
  $field_storage
    ->save();
  FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => 'article',
    'label' => 'Timestamp test',
  ])
    ->save();

  // Add the timestamp field to the article form.
  $this->formDisplay
    ->load('node.article.default')
    ->setComponent($field_name, [
    'type' => 'datetime_timestamp',
  ])
    ->save();

  // Add the timestamp field to the default display.
  $this->viewDisplay
    ->load('node.article.default')
    ->setComponent($field_name, [
    'type' => 'timestamp',
  ])
    ->save();
  $old_timestamp = '321321321';
  $new_timestamp = '123123123';

  // Create an article with an timestamp.
  $this
    ->drupalCreateNode([
    'title' => 'timestamp_test',
    'type' => 'article',
    'field_timestamp' => $old_timestamp,
  ]);

  // Create a new revision with an updated timestamp.
  $node = $this
    ->drupalGetNodeByTitle('timestamp_test');
  $node->field_timestamp = $new_timestamp;
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();

  // Compare the revisions.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/revisions');
  $this
    ->drupalPostForm(NULL, NULL, t('Compare selected revisions'));

  // Assert that the timestamp field does not show a unix time format.
  $this
    ->assertText('Timestamp test');
  $date_formatter = \Drupal::service('date.formatter');
  $this
    ->assertText($date_formatter
    ->format($old_timestamp));
  $this
    ->assertText($date_formatter
    ->format($new_timestamp));
}