You are here

public function PurchasedEntityConstraintValidatorTest::testAvailabilityConstraint in Commerce Core 8.2

Tests the availability constraint.

@dataProvider dataProviderCheckerData @covers ::validate

Parameters

string $sku: The SKU. SKUs prefixed with TEST_* will fail availability checks.

string $order_state: The order state.

\Drupal\commerce_order\AvailabilityResult $expected_check_result: The expected availability result.

bool $expected_constraint: The expected constraint count.

File

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

Class

PurchasedEntityConstraintValidatorTest
Tests the purchased entity constraint on order items.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

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