ProcessFieldTest.php 4.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
<?php

namespace Drupal\Tests\field\Unit\Plugin\migrate\process;

use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\field\Plugin\migrate\process\ProcessField;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\MigrateCckFieldPluginManagerInterface;
use Drupal\migrate_drupal\Plugin\MigrateFieldInterface;
use Drupal\migrate_drupal\Plugin\MigrateFieldPluginManagerInterface;
use Drupal\Tests\migrate\Unit\MigrateTestCase;

/**
 * Tests the ProcessField migrate process plugin.
 *
 * @coversDefaultClass \Drupal\field\Plugin\migrate\process\ProcessField
 * @group field
 * @group legacy
 */
class ProcessFieldTest extends MigrateTestCase {

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->cckFieldManager = $this->prophesize(MigrateCckFieldPluginManagerInterface::class);
    $this->fieldManager = $this->prophesize(MigrateFieldPluginManagerInterface::class);
    $this->fieldPlugin = $this->prophesize(MigrateFieldInterface::class);
    $this->migrateExecutable = $this->prophesize(MigrateExecutable::class);
    $this->migration = $this->prophesize(MigrationInterface::class);
    $this->row = $this->prophesize(Row::class);

    $this->fieldManager->getPluginIdFromFieldType('foo', [], $this->migration->reveal())->willReturn('foo');
    $this->fieldManager->createInstance('foo', [], $this->migration->reveal())->willReturn($this->fieldPlugin);

    parent::setUp();
  }

  /**
   * Tests the transform method.
   *
   * @param string $method
   *   The method to call.
   * @param string $value
   *   The value to process.
   * @param mixed $expected_value
   *   The expected transformed value.
   * @param string $migrate_exception
   *   The MigrateException message to expect.
   * @param bool $plugin_not_found
   *   Whether the field plugin is not found.
   *
   * @covers ::transform
   * @dataProvider providerTestTransform
   */
  public function testTransform($method, $value, $expected_value, $migrate_exception = '', $plugin_not_found = FALSE) {
    if ($method) {
      $this->fieldPlugin->$method($this->row->reveal())->willReturn($expected_value);
    }
    $this->plugin = new ProcessField(['method' => $method], $value, [], $this->cckFieldManager->reveal(), $this->fieldManager->reveal(), $this->migration->reveal());

    if ($migrate_exception) {
66 67
      $this->expectException(MigrateException::class);
      $this->expectExceptionMessage($migrate_exception);
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    }

    if ($plugin_not_found) {
      $exception = new PluginNotFoundException('foo');
      $this->cckFieldManager->getPluginIdFromFieldType()->willThrow($exception);
      $this->fieldManager->getPluginIdFromFieldType()->willThrow($exception);
    }

    $transformed_value = $this->plugin->transform($value, $this->migrateExecutable->reveal(), $this->row->reveal(), 'foo');
    $this->assertSame($transformed_value, $expected_value);
  }

  /**
   * Provides data for the transform method test.
   *
   * @return array
   *   - The method to call.
   *   - The value to process.
   *   - The expected transformed value.
   *   - The MigrateException message to expect.
   *   - Whether the field plugin is not found.
   */
  public function providerTestTransform() {
    return [
      // Tests the getFieldType() method.
      [
        'method' => 'getFieldType',
        'value' => 'foo',
        'expected_value' => 'bar',
      ],
      // Tests the getFieldFormatterMap() method.
      [
        'method' => 'getFieldFormatterMap',
        'value' => 'foo',
        'expected_value' => ['foo' => 'bar'],
      ],
      // Tests the getFieldWidgetMap() method.
      [
        'method' => 'getFieldWidgetMap',
        'value' => 'foo',
        'expected_value' => ['foo' => 'bar'],
      ],
      // Tests that an exception is thrown if the value is not a string.
      [
        'method' => 'getFieldType',
        'value' => ['foo'],
        'expected_value' => '',
        'migrate_exception' => 'The input value must be a string.',
      ],
      // Tests that an exception is thrown if no method name is provided.
      [
        'method' => '',
        'value' => '',
        'expected_value' => '',
        'migrate_exception' => 'You need to specify the name of a method to be called on the Field plugin.',
      ],
      // Tests that NULL is returned if no field plugin is found.
      [
        'method' => 'getFieldType',
        'value' => 'foo',
        'expected_value' => NULL,
        'migrate_exception' => '',
        'plugin_not_found' => TRUE,
      ],
    ];
  }

}