You are here

public function PurchasedEntityConstraintValidatorTest::testPurchasedEntityNoLongerExists in Commerce Core 8.2

Tests the constraint when the purchased entity no longer exists.

@covers ::validate

File

modules/order/tests/src/Kernel/PurchasedEntityConstraintValidatorTest.php, line 106

Class

PurchasedEntityConstraintValidatorTest
Tests the purchased entity constraint on order items.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

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);
}