You are here

importing.test in Feeds Paragraphs 7

Contains ImportingTestCase.

File

tests/importing.test
View source
<?php

/**
 * @file
 * Contains ImportingTestCase.
 */

/**
 * Tests importing a single value to a content type with a Paragraphs field.
 */
class FeedsParaMapperSingleValueTestCase extends FeedsParaMapperWebTestCase {

  /**
   * Lets SimpleTest knows about this test class.
   */
  public static function getInfo() {
    return array(
      'name' => 'Single Value Importing Test',
      'description' => 'Insures that importing a single value to a Paragraphs field works correctly',
      'group' => 'Paragraphs Mapper',
    );
  }

  /**
   * Test importing nodes with paragraphs field value.
   *
   * - Tests importing a node.
   * - Checks that the host Paragraphs field exists on the created node.
   * - Check that the paragraphs bundle field value matches the source.
   */
  public function testImport() {
    parent::import();
    parent::topHostParagraphsFieldExists();
    parent::topHostParagraphsEntityExists();
    $items = parent::getTopHostParagraphsEntities();
    $bundle_field = "field_" . $this->bundles[0]['fields'][0]['name'];
    $lang = $items[0]
      ->langcode();
    $value_exists = isset($items[0]->{$bundle_field}[$lang][0]['value']);
    $message = "The bundle field value exists";
    $this
      ->assertTrue($value_exists, $message, 'Importing');
    $source_value = "This is a dummy text";
    $value_matches = $items[0]->{$bundle_field}[$lang][0]['value'] === $source_value;
    $message = "The value of the bundle field matches the source value";
    $this
      ->assertTrue($value_matches, $message, 'Importing');
    $this
      ->checkFileFieldImport();
  }

  /**
   * Test importing nodes with image field in a Paragraphs bundle.
   *
   * The point of this test is to check if the module works,
   * when the a field module sets multiple targets to a field,
   * e.g: image:alt and image:title
   * - Tests importing a node.
   * - Checks that the host Paragraphs field exists on the created node.
   * - Check that the paragraphs bundle field value matches the source.
   */
  protected function checkFileFieldImport() {
    $items = parent::getTopHostParagraphsEntities();
    $last = count($this->bundles[0]['fields']) - 1;
    $image_field = "field_" . $this->bundles[0]['fields'][$last]['name'];
    $lang = $items[0]
      ->langcode();
    $image_field_exists = isset($items[0]->{$image_field});
    $message = format_string("The image field @field exists", array(
      '@field' => $image_field,
    ));
    $this
      ->assertTrue($image_field_exists, $message);
    $expected_values = array(
      'alt' => 'My image alt',
      'title' => "My image title",
      'uri' => 'public:///image.png',
    );
    foreach ($expected_values as $field => $expected_value) {
      $value_exists = isset($items[0]->{$image_field}[$lang][0][$field]);
      $message = format_string("The image sub-field @field has value", array(
        "@field" => $field,
      ));
      $this
        ->assertTrue($value_exists, $message, 'Importing');
      $value_matches = $items[0]->{$image_field}[$lang][0][$field] === $expected_value;
      $message = format_string("The value of the image sub-field @field matches the source value", array(
        "@field" => $field,
      ));
      $this
        ->assertTrue($value_matches, $message, 'Importing');
    }
  }

}

/**
 * Tests importing to a content type with a nested Paragraphs field.
 */
class FeedsParaMapperNestedImportTestCase extends FeedsParaMapperWebTestCase {

  /**
   * Lets SimpleTest knows about this test class.
   */
  public static function getInfo() {
    return array(
      'name' => 'Nested Importing Test',
      'description' => 'Insures that importing to a nested Paragraphs field works correctly',
      'group' => 'Paragraphs Mapper',
    );
  }

  /**
   * Prepares the test environment.
   */
  public function setUp() {
    parent::setUp(NULL, NULL, TRUE);
  }

  /**
   * Test importing nodes with nested paragraphs fields.
   *
   * - Tests importing a node.
   * - Checks that the host Paragraphs field exists on the created node.
   * - Check that the paragraphs bundle field value matches the source.
   */
  protected function testNestedImport() {
    parent::import();
    parent::topHostParagraphsFieldExists();
    parent::topHostParagraphsEntityExists();
    $items = parent::getTopHostParagraphsEntities();
    krsort($this->bundles);
    $this->bundles = array_values($this->bundles);
    foreach ($this->bundles as $bundle) {
      foreach ($bundle['fields'] as $field) {
        $machine_name = 'field_' . $field['name'];
        $value = $this
          ->getValues($items[0], $machine_name);
        $value = reset($value);
        $message = "Field has value";
        $this
          ->assertNotNull($value, $message, 'Nested Importing');
        if ($field['type'] === 'paragraphs') {
          $pi = new ParagraphsItemEntity();
          $isParagraph = get_class($value) === get_class($pi);
          $message = "Value is paragraph";
          $this
            ->assertTrue($isParagraph, $message, 'Nested Importing');
        }
        elseif ($field['type'] === "text") {
          $message = "Value is text";
          $this
            ->assertTrue(is_string($value), $message, 'Nested Importing');
          $source_value = "This is a dummy text";
          $value_matches = $value === $source_value;
          $message = "The value of the bundle field matches the source value";
          $this
            ->assertTrue($value_matches, $message, 'Nested Importing');
        }
        else {
          $source_value = array(
            "My image alt",
            "My image title",
            "public:///image.png",
          );
          $found = 0;
          foreach ($source_value as $item) {
            if (in_array($item, $value)) {
              $found++;
            }
          }
          $message = "The value of the bundle field matches the source value";
          $this
            ->assertEqual(count($source_value), $found, $message, "Nested Importing");
        }
      }
    }
  }

}

/**
 * Tests importing to a content type with a nested Paragraphs field.
 */
class FeedsParaMapperMultiValuedImportingTestCase extends FeedsParaMapperWebTestCase {

  /**
   * Lets SimpleTest knows about this test class.
   */
  public static function getInfo() {
    return array(
      'name' => 'Multi-valued Importing Test',
      'description' => 'Insures that importing to a multi-valued Paragraphs field works correctly',
      'group' => 'Paragraphs Mapper',
    );
  }

  /**
   * Prepares the test environment.
   */
  public function setUp() {
    parent::setUp(TRUE, TRUE, TRUE);
  }

  /**
   * Test importing nodes with multi-valued paragraphs fields.
   *
   * @see \FeedsParaMapperWebTestCase::multiImport()
   */
  protected function testMultiValuedImport() {
    $expected_values = array(
      'field_description' => array(
        'text 1',
        'text 2',
        'text 3',
        'text 4',
      ),
      'field_image' => array(
        array(
          'image 1 alt',
          'image 1 title',
          'public:///image.png',
        ),
        array(
          'image 2 alt',
          'image 2 title',
          'public:///image.png',
        ),
        array(
          'image 3 alt',
          'image 3 title',
          'public:///image.png',
        ),
        array(
          'image 4 alt',
          'image 4 title',
          'public:///image.png',
        ),
      ),
    );
    parent::multiImport($expected_values);
  }

}

/**
 * Tests updating Paragraphs fields values.
 */
class FeedsParaMapperUpdatingTestCase extends FeedsParaMapperWebTestCase {

  /**
   * Lets SimpleTest knows about this test class.
   */
  public static function getInfo() {
    return array(
      'name' => 'Updating Test',
      'description' => 'Insures that updating the values of Paragraphs fields works correctly',
      'group' => 'Paragraphs Mapper',
    );
  }

  /**
   * Prepares the test environment.
   */
  public function setUp() {
    parent::setUp(TRUE, TRUE, TRUE);
  }

  /**
   * Test importing nodes with multi-valued paragraphs fields.
   *
   * @see \FeedsParaMapperWebTestCase::multiImport()
   */
  protected function testMultiValuedUpdate() {
    $expected_values = array(
      'field_description' => array(
        'text 5',
        'text 6',
        'text 7',
        'text 8',
        'text 9',
        'text 10',
      ),
      'field_image' => array(
        array(
          'image 5 alt',
          'image 5 title',
          'public:///image.png',
        ),
        array(
          'image 6 alt',
          'image 6 title',
          'public:///image.png',
        ),
        array(
          'image 7 alt',
          'image 7 title',
          'public:///image.png',
        ),
        array(
          'image 8 alt',
          'image 8 title',
          'public:///image.png',
        ),
      ),
    );
    parent::multiImport($expected_values, TRUE);
  }

}

Classes

Namesort descending Description
FeedsParaMapperMultiValuedImportingTestCase Tests importing to a content type with a nested Paragraphs field.
FeedsParaMapperNestedImportTestCase Tests importing to a content type with a nested Paragraphs field.
FeedsParaMapperSingleValueTestCase Tests importing a single value to a content type with a Paragraphs field.
FeedsParaMapperUpdatingTestCase Tests updating Paragraphs fields values.