You are here

public function ContentEntityNormalizerTest::testNormalizeWithRevisionId in Acquia Content Hub 8

Tests the normalize() method for node revisions.

Tests 2 fields given a passed user context. Field 1 is accessible, field 2 is a node revision id 'vid' which should be stripped out.

@covers ::normalize @covers ::addFieldsToContentHubEntity

File

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

Class

ContentEntityNormalizerTest
PHPUnit test for the ContentEntityNormalizer class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer

Code

public function testNormalizeWithRevisionId() {
  $this
    ->mockContainerResponseForNormalize();
  $mock_account = $this
    ->createMock('Drupal\\Core\\Session\\AccountInterface');
  $context = [
    'account' => $mock_account,
  ];

  // The mock account should get passed directly into the access() method on
  // field items from $context['account'].
  $definitions = [
    'field_1' => $this
      ->createMockFieldListItem('field_1', 'string', TRUE, $mock_account, [
      '0' => [
        'value' => 'test',
      ],
    ]),
    'vid' => $this
      ->createMockFieldListItem('vid', 'string', TRUE, $mock_account, [
      '0' => [
        'value' => '1',
      ],
    ]),
  ];

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

  // Create our Content Entity with English support.
  $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', $context);

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

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

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

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