You are here

public function ContentEntityNormalizerTest::testNormalizeWithCreatedAndChanged in Acquia Content Hub 8

Tests the normalize() method.

Tests 1 field and the created and changed fields. Make sure there is no changed or created field in the final attributes as those are excluded.

@covers ::normalize @covers ::addFieldsToContentHubEntity @covers ::excludedProperties

File

tests/src/Unit/Normalizer/ContentEntityNormalizerTest.php, line 345

Class

ContentEntityNormalizerTest
PHPUnit test for the ContentEntityNormalizer class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer

Code

public function testNormalizeWithCreatedAndChanged() {
  $this
    ->mockContainerResponseForNormalize();
  $definitions = [
    'field_1' => $this
      ->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
      '0' => [
        'value' => 'test',
      ],
    ]),
    'created' => $this
      ->createMockFieldListItem('created', 'timestamp', TRUE, $this->userContext, [
      '0' => [
        'value' => '1458811508',
      ],
    ]),
    'changed' => $this
      ->createMockFieldListItem('changed', 'timestamp', TRUE, $this->userContext, [
      '0' => [
        'value' => '1458811509',
      ],
    ]),
  ];

  // Set our Serializer and expected serialized return value for the given
  // fields.
  $serializer = $this
    ->getFieldsSerializer($definitions, $this->userContext);
  $this->contentEntityNormalizer
    ->setSerializer($serializer);

  // Create our Content Entity.
  $content_entity_mock = $this
    ->createMockForContentEntity($definitions, [
    'en',
  ]);

  // Normalize the Content Entity with the class that we are testing.
  $normalized = $this->contentEntityNormalizer
    ->normalize($content_entity_mock, 'acquia_contenthub_cdf');

  // Check if valid result.
  $this
    ->doTestValidResultForOneEntity($normalized);

  // Get our Content Hub Entity out of the result.
  $normalized_entity = $this
    ->getContentHubEntityFromResult($normalized);

  // Check if there was a created date set.
  $this
    ->assertEquals($normalized_entity
    ->getCreated(), date('c', 1458811508));

  // Check if there was a modified date set.
  // We assure that the date set will be the one when the CDF was modified
  // and sent to Content Hub, not when the drupal entity was last modified.
  $this
    ->assertNotEquals($normalized_entity
    ->getModified(), date('c', 1458811509));

  // Check if field_1 has the correct values.
  $this
    ->assertEquals($normalized_entity
    ->getAttribute('field_1')
    ->getValues(), [
    'en' => [
      'test',
    ],
  ]);

  // Field created should not be part of the normalizer.
  $this
    ->assertFalse($normalized_entity
    ->getAttribute('created'));

  // Field changed should not be part of the normalizer.
  $this
    ->assertFalse($normalized_entity
    ->getAttribute('changed'));
}