You are here

public function TaxonomyTermTest::testTaxonomyTermFields in Acquia Content Hub 8

Tests that adding "type" fields that does not contain bundle should work.

This tests adds an entity_reference field to a taxonomy term and tests that this is converted to 'array<reference>' pointing to a UUID.

File

tests/src/Functional/TaxonomyTermTest.php, line 78

Class

TaxonomyTermTest
Test that Acquia Content Hub respects Taxonomy Term.

Namespace

Drupal\Tests\acquia_contenthub\Functional

Code

public function testTaxonomyTermFields() {

  // Create and enable Publish options for target vocabulary.
  $vocabulary_target = $this
    ->createVocabulary();

  // Create a taxonomy term entity reference field that points to an entity
  // from the other vocabulary.
  $field_name = 'type';
  $handler_settings = [
    'target_bundles' => [
      $vocabulary_target
        ->id() => $vocabulary_target
        ->id(),
    ],
  ];
  $this
    ->createEntityReferenceField('taxonomy_term', $this->vocabulary
    ->id(), $field_name, $this
    ->randomString(), 'taxonomy_term', 'default', $handler_settings);
  \Drupal::service('entity_display.repository')
    ->getViewDisplay('taxonomy_term', $this->vocabulary
    ->id(), 'default')
    ->setComponent('type')
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('taxonomy_term', $this->vocabulary
    ->id(), 'default')
    ->setComponent($field_name, [
    'type' => 'entity_reference_autocomplete',
  ])
    ->save();

  // Create term1.
  $term1 = $this
    ->createTerm($this->vocabulary);
  $term2 = $this
    ->createTerm($vocabulary_target);
  $term1
    ->set('type', $term2
    ->id());
  $term1
    ->save();
  $this
    ->configureContentHubContentTypes('taxonomy_term', [
    $vocabulary_target
      ->get('vid'),
  ]);

  // Check CH cdf response.
  $output = $this
    ->drupalGetCdf('acquia-contenthub-cdf/taxonomy/term/' . $term1
    ->id(), [
    'query' => [
      'include_references' => 'true',
    ],
  ]);
  $this
    ->assertResponse(200);

  // Check that main taxonomy_term exists, with 'type' attribute.
  $this
    ->assertTrue(isset($output['entities']['0']['attributes']['type']), 'Type attribute is present.');
  $this
    ->assertEqual($term1
    ->uuid(), $output['entities']['0']['uuid']);
  $this
    ->assertEqual($this->vocabulary
    ->id(), $output['entities']['0']['attributes']['vocabulary']['value'][LANGUAGE_NONE]);
  $this
    ->assertEqual('array<reference>', $output['entities']['0']['attributes']['type']['type']);
  $this
    ->assertEqual($term2
    ->uuid(), $output['entities']['0']['attributes']['type']['value'][LANGUAGE_NONE][0]);

  // Check that the taxonomy_term dependency has been included in the CDF.
  $this
    ->assertEqual($term2
    ->uuid(), $output['entities']['1']['uuid']);
  $this
    ->assertEqual($vocabulary_target
    ->id(), $output['entities']['1']['attributes']['vocabulary']['value'][LANGUAGE_NONE]);
}