View source
<?php
namespace Drupal\Tests\commerce_invoice\Kernel;
use Drupal\commerce_invoice\Entity\Invoice;
use Drupal\commerce_invoice\Entity\InvoiceItem;
use Drupal\commerce_invoice\Entity\InvoiceTypeInterface;
use Drupal\commerce_order\Adjustment;
use Drupal\commerce_order\Entity\OrderInterface;
use Drupal\commerce_price\Price;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\Routing\Route;
class InvoiceOrderAccessCheckTest extends OrderIntegrationTest {
protected $accessChecker;
protected function setUp() : void {
parent::setUp();
$this->accessChecker = $this->container
->get('access_check.invoice_order');
}
public function testAccessWithoutPermission() {
$access = $this->accessChecker
->access($this
->getRoute(FALSE), $this
->getRouteMatch(), $this
->getAccount(FALSE));
$this
->assertFalse($access
->isAllowed());
}
public function testAccessForDraftOrders() {
$this
->assertEquals('draft', $this->order
->getState()
->getId());
$access = $this->accessChecker
->access($this
->getRoute(), $this
->getRouteMatch($this->order), $this
->getAccount());
$this
->assertFalse($access
->isAllowed());
}
public function testAccessWithPartialInvoices() {
$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();
$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();
$access = $this->accessChecker
->access($this
->getRoute(), $this
->getRouteMatch($this->order), $this
->getAccount());
$this
->assertTrue($access
->isAllowed());
$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();
$access = $this->accessChecker
->access($this
->getRoute(), $this
->getRouteMatch($this->order), $this
->getAccount());
$this
->assertTrue($access
->isAllowed());
$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();
$access = $this->accessChecker
->access($this
->getRoute(), $this
->getRouteMatch($this->order), $this
->getAccount());
$this
->assertFalse($access
->isAllowed());
}
protected function getRoute($has_requirement = TRUE) {
if (!$has_requirement) {
$route = new Route('/foo');
}
else {
$route = new Route('/foo', [], [
'_invoice_generate_form_access' => 'TRUE',
]);
}
return $route;
}
protected function getAccount($has_permission = TRUE) {
$account = $this
->prophesize(AccountInterface::class);
$account
->hasPermission('administer commerce_invoice')
->willReturn($has_permission);
return $account
->reveal();
}
protected function getRouteMatch($order = NULL, $invoice_type = NULL) {
if (!$order) {
$order = $this
->prophesize(OrderInterface::class)
->reveal();
}
if (!$invoice_type) {
$invoice_type = $this
->prophesize(InvoiceTypeInterface::class);
$invoice_type
->id()
->willReturn('default');
$invoice_type = $invoice_type
->reveal();
}
$route_match = $this
->prophesize(RouteMatchInterface::class);
$route_match
->getRawParameter('commerce_order')
->willReturn($order
->id());
$route_match
->getParameter('commerce_order')
->willReturn($order);
$route_match
->getRawParameter('commerce_invoice_type')
->willReturn($invoice_type
->id());
$route_match
->getParameter('commerce_invoice_type')
->willReturn($invoice_type);
return $route_match
->reveal();
}
}