You are here

public function PurchasedEntityConstraintValidatorTest::testSelectStoresViolations in Commerce Core 8.2

Tests the constraint when there is a problem selecting the store.

@covers ::validate

File

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

Class

PurchasedEntityConstraintValidatorTest
Tests the purchased entity constraint on order items.

Namespace

Drupal\Tests\commerce_order\Kernel

Code

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