You are here

public static function ContentProcessorTest::assertRadioFieldEqualsOptions in GatherContent 8.4

Assertion for radio elements.

1 call to ContentProcessorTest::assertRadioFieldEqualsOptions()
ContentProcessorTest::assertFieldEqualsElement in tests/src/Kernel/ContentProcessorTest.php
Assertion for Drupal field and GC element.

File

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

Class

ContentProcessorTest
Class for testing core node import functionality.

Namespace

Drupal\Tests\gathercontent\Kernel

Code

public static function assertRadioFieldEqualsOptions(array $field, array $options) {
  $selectedOptions = array_filter($options, function ($option) {
    return $option['selected'];
  });

  // No radios selected.
  if (empty($field)) {
    static::assertEmpty($selectedOptions, "No taxonomy term inserted in node.\nExpected radio item:\n" . print_r($selectedOptions, TRUE));
    return;
  }
  static::assertEquals(1, count($field), 'There must only be one radio taxonomy term imported.');
  $termId = reset($field)['target_id'];
  static::assertTrue(is_string($termId), 'Term id should be a string, but it is a(n) "' . gettype($termId) . '".');
  $term = Term::load($termId);
  static::assertEquals(MockData::RADIO_TAXONOMY_NAME, $term
    ->get('vid')
    ->getValue()[0]['target_id'], 'Term was imported in the wrong taxonomy.');
  static::assertNotEmpty($selectedOptions, 'No taxonomy term should be imported.');
  $optionsMatchingThisTerm = array_filter($selectedOptions, function ($option) use ($term) {
    $isSameName = $term
      ->get('name')
      ->getValue()[0]['value'] == $option['label'];
    $termOptionIdsMatchingThisOption = array_filter($term
      ->get('gathercontent_option_ids')
      ->getValue(), function ($optionId) use ($option) {
      return $optionId['value'] === $option['name'];
    });
    $isSameOptionId = 1 === count($termOptionIdsMatchingThisOption);
    return $isSameName && $isSameOptionId;
  });
  static::assertEquals(1, count($optionsMatchingThisTerm), 'Term was incorrectly imported.');
}