View source
<?php
namespace Drupal\Tests\commerce\Kernel;
use Drupal\commerce_order\Plugin\Commerce\Condition\OrderTotalPrice;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\user\Entity\Role;
class PluginItemTest extends CommerceKernelTestBase {
public static $modules = [
'entity_reference_revisions',
'profile',
'state_machine',
'commerce_order',
'commerce_test',
];
protected function setUp() : void {
parent::setUp();
Role::create([
'id' => 'test_role',
'name' => $this
->randomString(),
])
->save();
$field_storage = FieldStorageConfig::create([
'field_name' => 'test_conditions',
'entity_type' => 'entity_test',
'type' => 'commerce_plugin_item:commerce_condition',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_name' => 'test_conditions',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
]);
$field
->save();
}
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();
$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());
$entity = EntityTest::create([
'test_conditions' => [
[
'target_plugin_id' => 'order_total_price',
],
],
]);
$entity
->save();
$condition_field = $entity
->get('test_conditions')
->first();
$condition = $condition_field
->getTargetInstance();
$this
->assertInstanceOf(OrderTotalPrice::class, $condition);
$this
->assertEquals($condition_field
->getTargetDefinition(), $condition
->getPluginDefinition());
}
}