View source
<?php
namespace Drupal\Tests\commerce_cart_api\Kernel\Normalizer;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductAttribute;
use Drupal\commerce_product\Entity\ProductAttributeValue;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Tests\commerce_order\Kernel\OrderKernelTestBase;
use Symfony\Component\Routing\Route;
class EntityReferenceNormalizerTest extends OrderKernelTestBase implements ServiceModifierInterface {
protected $order;
public static $modules = [
'serialization',
'commerce_product',
'commerce_cart',
'commerce_cart_api',
];
public function register(ContainerBuilder $container) {
parent::register($container);
$params = $this->container
->getParameter('commerce_cart_api');
if ($this
->getName() === 'testDefaults') {
}
elseif ($this
->getName() === 'testWithProductId') {
$params['normalized_entity_references'] = [
'order_items',
'purchased_entity',
'product_id',
];
}
elseif ($this
->getName() === 'testWithAttributeColor') {
$params['normalized_entity_references'] = [
'order_items',
'purchased_entity',
'attribute_color',
];
}
elseif ($this
->getName() === 'testWithProductIdAttributeColor') {
$params['normalized_entity_references'] = [
'order_items',
'purchased_entity',
'product_id',
'attribute_color',
];
}
$this->container
->setParameter('commerce_cart_api', $params);
}
public function alter(ContainerBuilder $container) {
$container
->removeDefinition('commerce_order.order_store_resolver');
$mocked_route_match = $this
->prophesize(RouteMatchInterface::class);
$mocked_route = $this
->prophesize(Route::class);
$mocked_route
->hasRequirement('_cart_api')
->willReturn(TRUE);
$mocked_route_match
->getRouteObject()
->willReturn($mocked_route
->reveal());
$container
->set('current_route_match', $mocked_route_match
->reveal());
}
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->installEntitySchema('commerce_product_attribute_value');
EntityFormMode::create([
'id' => 'commerce_order_item.add_to_cart',
'label' => 'Add to cart',
'targetEntityType' => 'commerce_order_item',
])
->save();
$this
->installConfig([
'commerce_product',
'commerce_order',
]);
$color_attribute = ProductAttribute::create([
'id' => 'color',
'label' => 'Color',
]);
$this->container
->get('commerce_product.attribute_field_manager')
->createField($color_attribute, 'default');
$color_blue = ProductAttributeValue::create([
'attribute' => 'color',
'name' => 'Blue',
]);
$color_blue
->save();
$order = Order::create([
'type' => 'default',
]);
$order_item = OrderItem::create([
'type' => 'default',
]);
$product = Product::create([
'type' => 'default',
]);
$product_variation = ProductVariation::create([
'type' => 'default',
'attribute_color' => $color_blue
->id(),
]);
$product_variation
->save();
$product
->addVariation($product_variation);
$product
->save();
$order_item
->get('purchased_entity')
->appendItem($product_variation);
$order
->addItem($order_item);
$this->order = $order;
}
public function testDefaults() {
$this
->assertEntityReferenceNormalization([], [
[
'order_items',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'uuid',
],
], [
[
'order_items',
0,
'purchased_entity',
0,
'product_id',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'attribute_color',
0,
'uuid',
],
]);
}
public function testWithProductId() {
$this
->assertEntityReferenceNormalization([
'order_items',
'purchased_entity',
'product_id',
], [
[
'order_items',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'product_id',
0,
'uuid',
],
], [
[
'order_items',
0,
'purchased_entity',
0,
'attribute_color',
0,
'uuid',
],
]);
}
public function testWithAttributeColor() {
$this
->assertEntityReferenceNormalization([
'order_items',
'purchased_entity',
'attribute_color',
], [
[
'order_items',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'attribute_color',
0,
'uuid',
],
], [
[
'order_items',
0,
'purchased_entity',
0,
'product_id',
0,
'uuid',
],
]);
}
public function testWithProductIdAttributeColor() {
$this
->assertEntityReferenceNormalization([
'order_items',
'purchased_entity',
'product_id',
'attribute_color',
], [
[
'order_items',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'product_id',
0,
'uuid',
],
[
'order_items',
0,
'purchased_entity',
0,
'attribute_color',
0,
'uuid',
],
], []);
}
protected function assertEntityReferenceNormalization(array $field_overrides, array $keys_exists, array $keys_not_exist) {
$params = $this->container
->getParameter('commerce_cart_api');
if ($field_overrides === []) {
$field_overrides = [
'order_items',
'purchased_entity',
];
}
$this
->assertEquals($field_overrides, $params['normalized_entity_references']);
$serializer = $this->container
->get('serializer');
$data = $serializer
->normalize($this->order, 'json');
foreach ($keys_exists as $parents) {
$this
->assertTrue(NestedArray::keyExists($data, $parents), sprintf('Parent keys %s not found.', implode('.', $parents)));
}
foreach ($keys_not_exist as $parents) {
$this
->assertFalse(NestedArray::keyExists($data, $parents), sprintf('Parent keys %s should not be found.', implode('.', $parents)));
}
}
}