You are here

public function ContentEntityNormalizerTest::testNormalizeOneFieldMultiValued in Acquia Content Hub 8

Tests the normalize() method.

Tests 1 field with multiple values and checks if it appears in the normalized result. Also adds multiple languages to see if it properly combines them.

@covers ::normalize @covers ::addFieldsToContentHubEntity @covers ::appendToAttribute

File

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

Class

ContentEntityNormalizerTest
PHPUnit test for the ContentEntityNormalizer class.

Namespace

Drupal\Tests\acquia_contenthub\Unit\Normalizer

Code

public function testNormalizeOneFieldMultiValued() {
  $this
    ->mockContainerResponseForNormalize();
  $this->container
    ->expects($this
    ->at(2))
    ->method('get')
    ->with('entity_type.manager')
    ->willReturn($this->entityTypeManager);
  $definitions = [
    'field_1' => $this
      ->createMockFieldListItem('field_1', 'string', TRUE, $this->userContext, [
      [
        'value' => 'test',
      ],
      [
        'value' => 'test2',
      ],
    ]),
  ];

  // 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',
    'nl',
  ]);

  // 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 the UUID property.
  $this
    ->assertEquals('custom-uuid', $normalized_entity
    ->getUuid());

  // Check if there was a created date set.
  $this
    ->assertNotEmpty($normalized_entity
    ->getCreated());

  // Check if there was a modified date set.
  $this
    ->assertNotEmpty($normalized_entity
    ->getModified());

  // Check if there was an origin property set.
  $this
    ->assertEquals('test-origin', $normalized_entity
    ->getOrigin());

  // Check if there was a type property set to the entity type.
  $this
    ->assertEquals('node', $normalized_entity
    ->getType());

  // Check if the field has the given value.
  $expected_output = [
    'en' => [
      'test',
      'test2',
    ],
    'nl' => [
      'test',
      'test2',
    ],
  ];
  $this
    ->assertEquals($normalized_entity
    ->getAttribute('field_1')
    ->getValues(), $expected_output);
}