View source
<?php
namespace Drupal\Tests\commerce_cart\Kernel;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductVariation;
class OrderItemMatcherTest extends CartKernelTestBase {
protected $orderItemMatcher;
protected $variation1;
protected $variation2;
public static $modules = [
'extra_order_item_field',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'extra_order_item_field',
]);
$this->variation1 = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'price' => new Price('1.00', 'USD'),
'status' => 1,
]);
$this->variation2 = ProductVariation::create([
'type' => 'default',
'sku' => strtolower($this
->randomMachineName()),
'title' => $this
->randomString(),
'price' => new Price('2.00', 'USD'),
'status' => 1,
]);
$this->orderItemMatcher = $this->container
->get('commerce_cart.order_item_matcher');
}
public function testOrderItemMatcher() {
$order_item1 = OrderItem::create([
'type' => 'default',
'quantity' => 2,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
]);
$order_item1
->save();
$order_item2 = OrderItem::create([
'type' => 'default',
'quantity' => 6,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
]);
$order_item2
->save();
$order_item3 = OrderItem::create([
'type' => 'default',
'quantity' => 6,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation2,
]);
$order_item3
->save();
$matches = $this->orderItemMatcher
->matchAll($order_item1, [
$order_item2,
$order_item3,
]);
$this
->assertNotEmpty($matches);
$this
->assertEquals($matches, [
$order_item2,
]);
$match = $this->orderItemMatcher
->match($order_item1, [
$order_item2,
$order_item3,
]);
$this
->assertNotEmpty($match);
$this
->assertEquals($match, $order_item2);
$matches = $this->orderItemMatcher
->matchAll($order_item3, [
$order_item1,
$order_item2,
]);
$this
->assertEmpty($matches);
$match = $this->orderItemMatcher
->match($order_item1, [
$order_item3,
$order_item2,
]);
$this
->assertNotEmpty($match);
$this
->assertEquals($match, $order_item2);
$order_item4 = OrderItem::create([
'type' => 'test',
'quantity' => 2,
'unit_price' => new Price('1.00', 'USD'),
'purchased_entity' => $this->variation1,
]);
$order_item4
->save();
$matches = $this->orderItemMatcher
->matchAll($order_item4, [
$order_item1,
$order_item2,
$order_item3,
]);
$this
->assertEmpty($matches);
}
public function testNoPurchasedEntity() {
$order_item = OrderItem::create([
'type' => 'default',
'quantity' => 2,
'unit_price' => new Price('12.00', 'USD'),
]);
$order_item
->save();
$order_item2 = OrderItem::create([
'type' => 'default',
'quantity' => 3,
'unit_price' => new Price('1.00', 'USD'),
]);
$order_item2
->save();
$match = $this->orderItemMatcher
->match($order_item, [
$order_item2,
]);
$this
->assertEmpty($match);
}
public function testCustomField() {
$form_display = \Drupal::entityTypeManager()
->getStorage('entity_form_display')
->load('commerce_order_item.default.add_to_cart');
$this
->assertNotEmpty($form_display);
$form_display
->setComponent('field_custom_text', [
'type' => 'string_textfield',
]);
$form_display
->save();
$order_item1 = OrderItem::create([
'type' => 'default',
'quantity' => 2,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
'field_custom_text' => 'Blue',
]);
$order_item1
->save();
$order_item2 = OrderItem::create([
'type' => 'default',
'quantity' => 6,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
'field_custom_text' => 'Red',
]);
$order_item2
->save();
$order_item3 = OrderItem::create([
'type' => 'default',
'quantity' => 4,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
'field_custom_text' => 'Blue',
]);
$order_item3
->save();
$order_item4 = OrderItem::create([
'type' => 'default',
'quantity' => 4,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
'field_custom_text' => '',
]);
$order_item4
->save();
$matches = $this->orderItemMatcher
->matchAll($order_item1, [
$order_item2,
]);
$this
->assertEmpty($matches);
$match = $this->orderItemMatcher
->match($order_item1, [
$order_item2,
$order_item3,
]);
$this
->assertNotEmpty($match);
$this
->assertEquals($match, $order_item3);
$order_item5 = OrderItem::create([
'type' => 'default',
'quantity' => 5,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
]);
$matches = $this->orderItemMatcher
->matchAll($order_item5, [
$order_item1,
$order_item2,
$order_item3,
]);
$this
->assertEmpty($matches);
$order_item6 = OrderItem::create([
'type' => 'default',
'quantity' => 5,
'unit_price' => new Price('12.00', 'USD'),
'purchased_entity' => $this->variation1,
'field_custom_text' => '',
]);
$match = $this->orderItemMatcher
->match($order_item6, [
$order_item4,
]);
$this
->assertNotEmpty($match);
$this
->assertEquals($match, $order_item4);
}
}