You are here

protected function EntityRenderHandlerTest::assertCdfAttributes in Acquia Lift Connector 8.4

Asserts that entity related cdf list has the correct values and attributes.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity to test.

\Acquia\ContentHubClient\CDF\CDFObject[] $cdfs: The cdf list to compare.

1 call to EntityRenderHandlerTest::assertCdfAttributes()
EntityRenderHandlerTest::testOnCreateCdfBlockContent in modules/acquia_lift_publisher/tests/src/Kernel/EventSubscriber/Cdf/EntityRenderHandlerTest.php
@covers ::onCreateCdf

File

modules/acquia_lift_publisher/tests/src/Kernel/EventSubscriber/Cdf/EntityRenderHandlerTest.php, line 322

Class

EntityRenderHandlerTest
Class EntityRenderHandlerTest.

Namespace

Drupal\Tests\acquia_lift_publisher\Kernel\EventSubscriber\Cdf

Code

protected function assertCdfAttributes(ContentEntityInterface $entity, array $cdfs) : void {
  $original_languages = [];
  foreach ($entity
    ->getTranslationLanguages() as $translation_language) {
    $original_languages[] = $translation_language
      ->getId();
  }

  // The attributes to check in rendered entities.
  $cdf_languages = [];
  $source_entities = [];
  $contents = [];
  foreach ($cdfs as $cdf) {
    $language = $cdf
      ->getAttribute('language');
    $this
      ->assertNotNull($language, 'Entity translation has a corresponding cdf.');
    $language = $language
      ->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED];
    $cdf_languages[] = $language;
    $source_entities[] = $cdf
      ->getAttribute('source_entity')
      ->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED];
    $contents[$language] = $cdf
      ->getAttribute('content')
      ->getValue()[LanguageInterface::LANGCODE_NOT_SPECIFIED];
  }

  // These entities must come from the same source.
  $entity_uuid = $entity
    ->uuid();
  $this
    ->assertTrue($source_entities[0] === $entity_uuid, 'Source uuid and original uuid match.');
  $this
    ->assertTrue($source_entities[1] === $entity_uuid, 'Source uuid and original uuid match.');
  $this
    ->assertEqual($cdf_languages, $original_languages, 'All the translations have been rendered.');

  // Check if the content are translation specific.
  foreach ($original_languages as $original_language) {
    $translation = $entity
      ->getTranslation($original_language);
    $orig_label = $translation
      ->label();
    $this
      ->assertNotFalse(strpos($contents[$original_language], htmlspecialchars($orig_label, ENT_QUOTES)), 'Cdf contains the translated content.');
  }
}