View source
<?php
namespace Drupal\Tests\commerce_order\Kernel;
use Drupal\commerce\Context;
use Drupal\commerce_order\AvailabilityResult;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Symfony\Component\Validator\ConstraintViolationInterface;
final class PurchasedEntityConstraintValidatorTest extends OrderKernelTestBase {
public static $modules = [
'commerce_order_test',
];
public function testAvailabilityConstraint($sku, $order_state, AvailabilityResult $expected_check_result, $expected_constraint) {
$context = new Context($this
->createUser(), $this->store);
$availability_manager = $this->container
->get('commerce_order.availability_manager');
$product_variation = $this
->createTestProductVariation([
'sku' => $sku,
'price' => new Price('10.0', 'USD'),
]);
$order = Order::create([
'type' => 'default',
'state' => $order_state,
'store_id' => $this->store,
]);
$order_item = OrderItem::create([
'type' => 'default',
'order_id' => $order,
'quantity' => '1',
'unit_price' => $product_variation
->getPrice(),
'purchased_entity' => $product_variation
->id(),
]);
assert($order_item instanceof OrderItem);
$constraints = $order_item
->validate();
$this
->assertEquals($expected_check_result, $availability_manager
->check($order_item, $context));
if ($expected_constraint) {
$this
->assertCount(1, $constraints);
$this
->assertEquals('<em class="placeholder">test product</em> is not available with a quantity of <em class="placeholder">1</em>.', $constraints
->offsetGet(0)
->getMessage());
}
else {
$this
->assertCount(0, $constraints);
}
}
public function testValidateOrderItemWithoutPurchasedEntity() {
$order = Order::create([
'type' => 'default',
'state' => 'draft',
'store_id' => $this->store,
]);
$order_item = OrderItem::create([
'type' => 'test',
'title' => 'Test order item',
'order_id' => $order,
'quantity' => '1',
'unit_price' => new Price('10.00', 'USD'),
]);
$constraints = $order_item
->validate();
$this
->assertCount(0, $constraints);
}
public function testPurchasedEntityNoLongerExists() {
$product_variation = $this
->createTestProductVariation([
'sku' => 'SKU123',
'price' => new Price('10.0', 'USD'),
]);
$order = Order::create([
'type' => 'default',
'state' => 'draft',
'store_id' => $this->store,
]);
$order_item = OrderItem::create([
'type' => 'default',
'order_id' => $order,
'quantity' => '1',
'unit_price' => $product_variation
->getPrice(),
'purchased_entity' => $product_variation
->id(),
]);
assert($order_item instanceof OrderItem);
$constraints = $order_item
->validate();
$this
->assertCount(0, $constraints);
$product_variation
->delete();
$constraints = $order_item
->validate();
$this
->assertCount(1, $constraints);
$constraint_messages = array_map(static function (ConstraintViolationInterface $item) {
return $item
->getMessage();
}, \iterator_to_array($constraints
->getIterator()));
$this
->assertEquals([
'The referenced entity (<em class="placeholder">commerce_product_variation</em>: <em class="placeholder">1</em>) does not exist.',
], $constraint_messages);
}
public function testSelectStoresViolations() {
$product_variation = $this
->createTestProductVariation([
'sku' => 'SKU123',
'price' => new Price('10.0', 'USD'),
]);
$order_item = OrderItem::create([
'type' => 'default',
'quantity' => '1',
'unit_price' => $product_variation
->getPrice(),
'purchased_entity' => $product_variation
->id(),
]);
assert($order_item instanceof OrderItem);
$constraints = $order_item
->validate();
$this
->assertCount(0, $constraints);
$product_variation
->getProduct()
->setStoreIds([])
->save();
$this
->assertEquals([], $product_variation
->getStores());
$constraints = $order_item
->validate();
$this
->assertCount(1, $constraints);
$this
->assertEquals('The given entity is not assigned to any store.', $constraints
->offsetGet(0)
->getMessage());
$new_store1 = $this
->createStore(NULL, NULL, 'online', FALSE);
$new_store2 = $this
->createStore(NULL, NULL, 'online', FALSE);
$product_variation
->getProduct()
->setStores([
$new_store1,
$new_store2,
])
->save();
$constraints = $order_item
->validate();
$this
->assertCount(1, $constraints);
$this
->assertEquals("The given entity can't be purchased from the current store.", $constraints
->offsetGet(0)
->getMessage());
$product_variation
->getProduct()
->setStoreIds([
$this->store
->id(),
])
->save();
$constraints = $order_item
->validate();
$this
->assertCount(0, $constraints);
}
public function dataProviderCheckerData() {
(yield [
'SKU1234',
'draft',
AvailabilityResult::neutral(),
FALSE,
]);
(yield [
'TEST_SKU1234',
'draft',
AvailabilityResult::unavailable(),
TRUE,
]);
(yield [
'SKU1234',
'complete',
AvailabilityResult::neutral(),
FALSE,
]);
(yield [
'TEST_SKU1234',
'complete',
AvailabilityResult::unavailable(),
FALSE,
]);
}
protected function createTestProductVariation(array $variation_data) {
$product = Product::create([
'title' => 'test product',
'type' => 'default',
'stores' => [
$this->store
->id(),
],
]);
$product_variation = ProductVariation::create($variation_data + [
'type' => 'default',
]);
$product_variation
->save();
$product
->addVariation($product_variation);
$product
->save();
return $this
->reloadEntity($product_variation);
}
}