You are here

protected function FeedsParaMapperWebTestCase::multiImport in Feeds Paragraphs 7

Tests Multi-valued fields importing.

Parameters

array $expected_values: The values that the entities should have.

bool $update: Whether we are updating or creating new node.

2 calls to FeedsParaMapperWebTestCase::multiImport()
FeedsParaMapperMultiValuedImportingTestCase::testMultiValuedImport in tests/importing.test
Test importing nodes with multi-valued paragraphs fields.
FeedsParaMapperUpdatingTestCase::testMultiValuedUpdate in tests/importing.test
Test importing nodes with multi-valued paragraphs fields.

File

tests/FeedsParaMapperWebTestCase.test, line 573
Common functionality for all Paragraphs Mapper tests.

Class

FeedsParaMapperWebTestCase
Test basic functionality via DrupalWebTestCase.

Code

protected function multiImport(array $expected_values, $update = FALSE) {
  $this
    ->import();
  $this
    ->topHostParagraphsFieldExists();
  $this
    ->topHostParagraphsEntityExists();
  $entities_count = 2;
  if ($update) {
    $entities_count = 3;
    $path = '/tests/assets/updated_content.csv';
    $this
      ->import($path, TRUE, FALSE);
  }
  $items = $this
    ->getTopHostParagraphsEntities();
  krsort($this->bundles);
  $this->bundles = array_values($this->bundles);

  // Check to see if the total of the created entities is correct:
  $paragraphs = NULL;
  if (count($this->bundles) > 1) {
    $parent = 'field_' . $this->bundles[0]['fields'][0]['name'];
    $paragraphs = $this
      ->getValues($items[0], $parent);
  }
  else {
    $paragraphs = $items;
  }
  $message = "The created Paragraphs entities are 2";
  $this
    ->assertEqual(count($paragraphs), $entities_count, $message, "Multi-valued Importing");

  // Test the entities values:
  $lastKey = count($this->bundles) - 1;
  foreach ($this->bundles[$lastKey]['fields'] as $field) {
    $machine_name = "field_" . $field['name'];
    $values = array();
    $values_count = array();
    $total_count = 0;
    foreach ($paragraphs as $paragraph) {
      $p_values = $this
        ->getValues($paragraph, $machine_name);
      $values_count[] = count($p_values);
      $total_count += count($p_values);
      $values = array_merge($values, $p_values);
    }

    // Test if each entity has total field values of 2:
    if ($total_count !== count($expected_values[$machine_name])) {
      for ($i = 0; $i < count($values_count); $i++) {
        $item_num = $i + 1;
        $message = format_string("Paragraph entity #@num has 2 values", array(
          "@num" => $item_num,
        ));
        $this
          ->assertEqual($values_count[$i], 2, $message, "Multi-valued Importing");
      }
    }
    $found_values = array();
    $expected_field_values = NULL;
    foreach ($expected_values as $values_field => $valuesArr) {
      if ($values_field === $machine_name) {
        $expected_field_values = $valuesArr;
        foreach ($valuesArr as $expected_value) {
          foreach ($values as $value) {
            if (is_array($expected_value)) {
              $total_found = 0;
              foreach ($expected_value as $subVal) {
                if (in_array($subVal, $value)) {
                  $total_found++;
                }
              }
              if (count($expected_value) === $total_found) {
                $found_values[] = $expected_value;
              }
            }
            elseif ($value === $expected_value) {
              $found_values[] = $expected_value;
            }
          }
        }
      }
    }
    $message = "All field values are found";
    $this
      ->assertEqual(count($found_values), count($expected_field_values), $message);
  }
}