You are here

public function InvoiceAdminTest::testOrderInvoices in Commerce Invoice 8.2

Tests the order "Invoices" tab and the invoice generate form.

File

tests/src/Functional/InvoiceAdminTest.php, line 130

Class

InvoiceAdminTest
Tests the invoice admin UI.

Namespace

Drupal\Tests\commerce_invoice\Functional

Code

public function testOrderInvoices() {

  // Ensure the "Invoices" operation is not shown for a draft order.
  $this
    ->drupalGet($this->order
    ->toUrl('collection'));
  $this
    ->assertSession()
    ->linkByHrefNotExists($this->orderInvoicesUrl);
  $order_edit_link = $this->order
    ->toUrl('edit-form')
    ->toString();
  $this
    ->assertSession()
    ->linkByHrefExists($order_edit_link);
  $this->order
    ->set('state', 'completed');
  $this->order
    ->save();
  $this
    ->getSession()
    ->reload();
  $this
    ->assertSession()
    ->linkByHrefExists($this->orderInvoicesUrl);
  $this
    ->drupalGet($this->orderInvoicesUrl);
  $this
    ->assertSession()
    ->pageTextContains('There are no invoices yet.');
  $this
    ->assertSession()
    ->linkByHrefExists($this->orderInvoiceAddUrl);
  $this
    ->drupalGet($this->orderInvoiceAddUrl);
  $this
    ->getSession()
    ->getPage()
    ->pressButton('Save');
  $this
    ->assertSession()
    ->pageTextContains('The invoice 1 has been successfully saved.');
  $this
    ->assertSession()
    ->pageTextNotContains('There are no invoices yet.');
  $this
    ->assertSession()
    ->linkExists('Download');
  $this
    ->assertSession()
    ->pageTextContains('Number');
  $this
    ->assertSession()
    ->linkByHrefNotExists($this->orderInvoiceAddUrl);

  /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $invoice_storage */
  $invoice_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('commerce_invoice');

  /** @var \Drupal\commerce_invoice\Entity\InvoiceInterface $invoice */
  $invoice = $invoice_storage
    ->load(1);
  $this
    ->drupalGet($invoice
    ->toUrl('download')
    ->toString());
  $invoice = $this
    ->reloadEntity($invoice);
  $file = $invoice
    ->getFile();
  $this
    ->assertNotNull($file);

  // Assert that re-downloading the invoice doesn't generate a new file.
  $this
    ->drupalGet($invoice
    ->toUrl('download')
    ->toString());
  $invoice = $this
    ->reloadEntity($invoice);
  $this
    ->assertEquals($file
    ->id(), $invoice
    ->getFile()
    ->id());

  /** @var \Drupal\file\FileStorageInterface $file_storage */
  $file_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('file');
  $this
    ->assertNull($file_storage
    ->load(2));
}