You are here

public function InvoiceOrderAccessCheckTest::testAccessWithPartialInvoices in Commerce Invoice 8.2

@covers ::access

File

tests/src/Kernel/InvoiceOrderAccessCheckTest.php, line 62

Class

InvoiceOrderAccessCheckTest
Tests the 'access_check.invoice_order' access checker.

Namespace

Drupal\Tests\commerce_invoice\Kernel

Code

public function testAccessWithPartialInvoices() {

  // The parent test class creates an order containing a single order item
  // with a quantity of 1. Increase that quantity to 3 and add an order
  // adjustment so we can test multiple invoices generated for the same order.
  $order_item = $this->order
    ->getItems()[0];
  $order_item
    ->setQuantity(3);
  $this->order
    ->setItems([
    $order_item,
  ]);
  $adjustment = new Adjustment([
    'type' => 'custom',
    'label' => 'Random fee',
    'amount' => new Price('2.00', 'USD'),
  ]);
  $this->order
    ->addAdjustment($adjustment);
  $this->order
    ->getState()
    ->applyTransitionById('place');
  $this->order
    ->save();

  // Create a partial invoice which contains an invoice item with a quantity
  // of 1.

  /** @var \Drupal\commerce_invoice\Entity\InvoiceItemInterface $invoice_item */
  $invoice_item = InvoiceItem::create([
    'type' => 'default',
  ]);
  $invoice_item
    ->populateFromOrderItem($order_item);
  $invoice_item
    ->setQuantity(1);
  $invoice_item
    ->save();
  $invoice = Invoice::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'invoice_items' => [
      $invoice_item,
    ],
    'orders' => [
      $this->order,
    ],
  ]);
  $invoice
    ->save();

  // Check that access is still allowed to add invoices this order, since the
  // order contains an extra quantity of 2 for its order item, and the
  // adjustment.
  $access = $this->accessChecker
    ->access($this
    ->getRoute(), $this
    ->getRouteMatch($this->order), $this
    ->getAccount());
  $this
    ->assertTrue($access
    ->isAllowed());

  // Create another partial invoice containing an invoice item with a quantity
  // of 1.
  $invoice_item = InvoiceItem::create([
    'type' => 'default',
  ]);
  $invoice_item
    ->populateFromOrderItem($order_item);
  $invoice_item
    ->setQuantity(1);
  $invoice_item
    ->save();
  $invoice = Invoice::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'invoice_items' => [
      $invoice_item,
    ],
    'orders' => [
      $this->order,
    ],
  ]);
  $invoice
    ->save();

  // Check that access is still allowed to add invoices for this order, since
  // the order contains an extra quantity of 1 for its order item, and the
  // adjustment.
  $access = $this->accessChecker
    ->access($this
    ->getRoute(), $this
    ->getRouteMatch($this->order), $this
    ->getAccount());
  $this
    ->assertTrue($access
    ->isAllowed());

  // Create a "final" partial invoice containing an invoice item with the
  // remaining quantity of 1, as well as the adjustment.
  $invoice_item = InvoiceItem::create([
    'type' => 'default',
  ]);
  $invoice_item
    ->populateFromOrderItem($order_item);
  $invoice_item
    ->setQuantity(1);
  $invoice_item
    ->save();
  $invoice = Invoice::create([
    'type' => 'default',
    'store_id' => $this->store
      ->id(),
    'invoice_items' => [
      $invoice_item,
    ],
    'orders' => [
      $this->order,
    ],
    'adjustments' => [
      $adjustment,
    ],
  ]);
  $invoice
    ->save();

  // Check that access is no longer allowed to add invoices for this order,
  // since the three partial invoices now contain invoice items that match the
  // quantity of 3 of the order item, as well as the order adjustment.
  $access = $this->accessChecker
    ->access($this
    ->getRoute(), $this
    ->getRouteMatch($this->order), $this
    ->getAccount());
  $this
    ->assertFalse($access
    ->isAllowed());
}