You are here

public function PluginItemTest::testField in Commerce Core 8.2

Tests the plugin item field.

File

tests/src/Kernel/PluginItemTest.php, line 55

Class

PluginItemTest
Tests the 'commerce_plugin_item' field type.

Namespace

Drupal\Tests\commerce\Kernel

Code

public function testField() {
  $plugin_configuration = [
    'operator' => '>',
    'amount' => [
      'number' => '9.99',
      'currency_code' => 'USD',
    ],
  ];
  $entity = EntityTest::create([
    'test_conditions' => [
      [
        'target_plugin_id' => 'order_total_price',
        'target_plugin_configuration' => $plugin_configuration,
      ],
    ],
  ]);
  $entity
    ->save();

  /** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $condition_field */
  $condition_field = $entity
    ->get('test_conditions')
    ->first();
  $condition = $condition_field
    ->getTargetInstance();
  $this
    ->assertInstanceOf(OrderTotalPrice::class, $condition);
  $this
    ->assertEquals($plugin_configuration, $condition
    ->getConfiguration());
  $this
    ->assertEquals($condition_field
    ->getTargetDefinition(), $condition
    ->getPluginDefinition());

  // Confirm that it is possible to save a plugin item without configuration.
  $entity = EntityTest::create([
    'test_conditions' => [
      [
        'target_plugin_id' => 'order_total_price',
      ],
    ],
  ]);
  $entity
    ->save();

  /** @var \Drupal\commerce\Plugin\Field\FieldType\PluginItem $condition_field */
  $condition_field = $entity
    ->get('test_conditions')
    ->first();
  $condition = $condition_field
    ->getTargetInstance();
  $this
    ->assertInstanceOf(OrderTotalPrice::class, $condition);
  $this
    ->assertEquals($condition_field
    ->getTargetDefinition(), $condition
    ->getPluginDefinition());
}