You are here

public static function ContentProcessorTest::assertNodeEqualsGcItem in GatherContent 8.4

Checks whether a node and a GC item contains the same data.

1 call to ContentProcessorTest::assertNodeEqualsGcItem()
ContentProcessorTest::createNodeTest in tests/src/Kernel/ContentProcessorTest.php
Test if entities are created correctly based on GC Items.

File

tests/src/Kernel/ContentProcessorTest.php, line 118

Class

ContentProcessorTest
Class for testing core node import functionality.

Namespace

Drupal\Tests\gathercontent\Kernel

Code

public static function assertNodeEqualsGcItem(NodeInterface $node, Item $gcItem, array $files) {

  /** @var \Drupal\gathercontent\Entity\Mapping $mapping */
  $mapping = MappingLoader::load($gcItem);
  $tabs = unserialize($mapping
    ->getData());
  $metatagTab = $tabs[MockData::METATAG_TAB]['elements'];
  unset($tabs[MockData::METATAG_TAB]);
  $fields = $node
    ->toArray();
  $translation = $node
    ->getTranslation('hu');
  $translatedFields = $translation
    ->toArray();
  foreach ($tabs as $tabId => $tab) {

    /** @var \Cheppers\GatherContent\DataTypes\Element[] $elements */
    $elements = $gcItem->config[$tabId]->elements;
    $itemMapping = $tab['elements'];
    foreach ($itemMapping as $gcId => $fieldId) {
      $ids = explode('||', $fieldId);
      $filesMatchingThisElement = array_filter($files, function ($file) use ($gcId) {
        return $file->field == $gcId;
      });
      if (count($ids) > 1) {

        // Paragraph.
        if ($tabId === MockData::TRANSLATED_TAB) {
          $field = static::loadFieldFromNode($translation, $ids, $tab['language']);
          static::assertFieldEqualsElement($field, $elements[$gcId], $filesMatchingThisElement);
        }
        else {
          $field = static::loadFieldFromNode($node, $ids, $tab['language']);
          static::assertFieldEqualsElement($field, $elements[$gcId], $filesMatchingThisElement);
        }
      }
      else {
        if ($fieldId === 'title') {
          static::assertTranslatedEquals($node
            ->getTitle(), $translation
            ->getTitle());
          static::assertEquals($node
            ->getTitle(), $gcItem->name);
        }
        else {

          // Basic fields.
          if ($tabId === MockData::TRANSLATED_TAB) {
            $fieldName = explode('.', $fieldId)[2];
            static::assertFieldEqualsElement($translatedFields[$fieldName], $elements[$gcId], $filesMatchingThisElement);
          }
          else {
            $fieldName = explode('.', $fieldId)[2];
            static::assertFieldEqualsElement($fields[$fieldName], $elements[$gcId], $filesMatchingThisElement);
          }
        }
      }
    }
  }

  // Metatags.
  $insertedMetatags = unserialize(reset($fields[MockData::METATAG_FIELD])['value']);
  $metatagElements = $gcItem->config[MockData::METATAG_TAB]->elements;
  foreach ($metatagTab as $gcId => $fieldName) {
    static::assertEquals($metatagElements[$gcId]->value, $insertedMetatags[$fieldName], 'Metatag was inserted incorrectly.');
  }
}