You are here

public function ParagraphsTest::checkCdfOutput in Acquia Content Hub 8

Ensures the CDF output is what we expect it to be.

1 call to ParagraphsTest::checkCdfOutput()
ParagraphsTest::testParagraphsCdf in tests/src/Functional/ParagraphsTest.php
Tests acquia_contenthub_cdf format for paragraphs entities inside a node.

File

tests/src/Functional/ParagraphsTest.php, line 122

Class

ParagraphsTest
Tests paragraphs support in Acquia Content Hub module.

Namespace

Drupal\Tests\acquia_contenthub\Functional

Code

public function checkCdfOutput() {
  $output = $this
    ->drupalGetCdf('acquia-contenthub-cdf/' . $this->node
    ->getEntityTypeId() . '/' . $this->node
    ->id());
  $this
    ->assertResponse(200);

  // Obtaining paragraphs' IDs and UUIDs to verify later.
  $paragraphs = $this->node
    ->get('field_client_data')
    ->getValue();
  $paragraphs_id_0 = $paragraphs[0]['target_id'];
  $paragraphs_uuid_0 = $output['entities']['0']['attributes']['field_client_data']['value']['en']['0'];

  // Check that paragraphs are included in the CDF.
  $this
    ->assertTrue(isset($output['entities']['0']['metadata']), 'Metadata is present');
  $this
    ->assertTrue(isset($output['entities']['0']['metadata']['view_modes']['default']), $this
    ->t('View mode %view_mode is present', [
    '%view_mode' => 'default',
  ]));
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_client_data']['type'], 'array<reference>');
  $this
    ->assertTrue(Uuid::isValid($paragraphs_uuid_0));
  $output = $this
    ->drupalGetCdf('acquia-contenthub-cdf/entity/paragraph/' . $paragraphs_id_0, [
    'query' => [
      'entity_type' => 'paragraph',
      'entity_id' => $paragraphs_id_0,
    ],
  ]);
  $this
    ->assertResponse(200);

  // Obtaining nested paragraph's IDs and UUIDs to verify later.
  $client_data = \Drupal::entityTypeManager()
    ->getStorage("paragraph")
    ->load($paragraphs_id_0);
  $field_client_location = $client_data
    ->get('field_client_location')
    ->getValue();
  $paragraphs_id_1 = $field_client_location[0]['target_id'];
  $paragraphs_id_2 = $field_client_location[1]['target_id'];
  $paragraphs_uuid_1 = $output['entities']['0']['attributes']['field_client_location']['value']['en']['0'];
  $paragraphs_uuid_2 = $output['entities']['0']['attributes']['field_client_location']['value']['en']['1'];

  // Check that paragraphs have the correct values in the CDF.
  $this
    ->assertEqual($output['entities']['0']['uuid'], $paragraphs_uuid_0);
  $this
    ->assertEqual($output['entities']['0']['type'], 'paragraph');
  $this
    ->assertEqual($output['entities']['0']['attributes']['type']['value']['en'], 'client_data');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_type']['type'], 'array<string>');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_type']['value']['en']['0'], 'node');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_uuid']['type'], 'string');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_uuid']['value']['en'], $this->node
    ->uuid());
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_field_name']['value']['en']['0'], 'field_client_data');
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_client_email']['value']['en']['0'], 'test@test.com');
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_client_name']['value']['en']['0'], 'Client Name');
  $this
    ->assertTrue(Uuid::isValid($output['entities']['0']['attributes']['field_client_tags']['value']['en']['0']));
  $this
    ->assertTrue(Uuid::isValid($paragraphs_uuid_1));
  $this
    ->assertTrue(Uuid::isValid($paragraphs_uuid_2));
  $output = $this
    ->drupalGetCdf('acquia-contenthub-cdf/entity/paragraph/' . $paragraphs_id_1, [
    'query' => [
      'entity_type' => 'paragraph',
      'entity_id' => $paragraphs_id_1,
    ],
  ]);
  $this
    ->assertResponse(200);

  // Check that paragraphs have the correct values in the CDF.
  $this
    ->assertEqual($output['entities']['0']['uuid'], $paragraphs_uuid_1);
  $this
    ->assertEqual($output['entities']['0']['type'], 'paragraph');
  $this
    ->assertEqual($output['entities']['0']['attributes']['type']['value']['en'], 'addresses');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_uuid']['type'], 'string');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_uuid']['value']['en'], $paragraphs_uuid_0);
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_type']['value']['en']['0'], 'paragraph');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_type']['type'], 'array<string>');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_field_name']['value']['en']['0'], 'field_client_location');
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_address']['value']['en']['0'], 'Test Address');
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_address_type']['value']['en']['0'], 'Work Address');
  $output = $this
    ->drupalGetCdf('acquia-contenthub-cdf/entity/paragraph/' . $paragraphs_id_2, [
    'query' => [
      'entity_type' => 'paragraph',
      'entity_id' => $paragraphs_id_2,
    ],
  ]);
  $this
    ->assertResponse(200);

  // Check that paragraphs have the correct values in the CDF.
  $this
    ->assertEqual($output['entities']['0']['uuid'], $paragraphs_uuid_2);
  $this
    ->assertEqual($output['entities']['0']['type'], 'paragraph');
  $this
    ->assertEqual($output['entities']['0']['attributes']['type']['value']['en'], 'addresses');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_uuid']['type'], 'string');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_uuid']['value']['en'], $paragraphs_uuid_0);
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_type']['value']['en']['0'], 'paragraph');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_type']['type'], 'array<string>');
  $this
    ->assertEqual($output['entities']['0']['attributes']['parent_field_name']['value']['en']['0'], 'field_client_location');
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_address']['value']['en']['0'], 'Another Test Address');
  $this
    ->assertEqual($output['entities']['0']['attributes']['field_address_type']['value']['en']['0'], 'Shipping Address');
}