You are here

public function InvoiceFileManagerTest::testGetInvoiceFile in Commerce Invoice 8.2

@covers ::getInvoiceFile

File

tests/src/Kernel/InvoiceFileManagerTest.php, line 69

Class

InvoiceFileManagerTest
Tests the invoice file manager.

Namespace

Drupal\Tests\commerce_invoice\Kernel

Code

public function testGetInvoiceFile() {
  $order_item = OrderItem::create([
    'type' => 'test',
    'quantity' => 1,
    'title' => $this
      ->randomString(),
    'unit_price' => new Price('12.00', 'USD'),
  ]);
  $order_item
    ->save();
  $order = Order::create([
    'type' => 'default',
    'state' => 'completed',
    'store_id' => $this->store,
    'billing_profile' => $this->profile,
    'uid' => $this->user
      ->id(),
    'order_items' => [
      $order_item,
    ],
  ]);
  $order
    ->save();
  $order = $this
    ->reloadEntity($order);
  $invoice = Invoice::create([
    'type' => 'default',
    'invoice_number' => '10',
    'orders' => [
      $order,
    ],
    'store_id' => $this->store
      ->id(),
    'billing_profile' => $this->profile,
    'state' => 'draft',
    'uid' => $this->user
      ->id(),
  ]);
  $invoice
    ->save();
  $invoice = $this
    ->reloadEntity($invoice);
  $file = $this->fileManager
    ->getInvoiceFile($invoice);
  $this
    ->assertNotEmpty($file);
  $this
    ->assertRegExp('#private://(.*)\\.pdf#', $file
    ->getFileUri());
  $this
    ->assertEquals('10-en-draft.pdf', $file
    ->getFilename());
  $this
    ->assertEquals('application/pdf', $file
    ->getMimeType());
  $invoice = $this
    ->reloadEntity($invoice);
  $invoice_file = $invoice
    ->getFile();
  $this
    ->assertNotEmpty($invoice_file);
  $this
    ->assertEquals($file
    ->id(), $invoice_file
    ->id());

  // Assert that updating the invoice state clears the invoice file
  // reference.
  $invoice
    ->getState()
    ->applyTransitionById('confirm');
  $invoice
    ->save();
  $file = $this->fileManager
    ->getInvoiceFile($invoice);
  $this
    ->assertNotEmpty($file);
  $this
    ->assertRegExp('#private://(.*)\\.pdf#', $file
    ->getFileUri());
  $this
    ->assertEquals('10-en-pending.pdf', $file
    ->getFilename());
  $this
    ->assertEquals('application/pdf', $file
    ->getMimeType());
  $this
    ->assertEquals($file
    ->id(), $invoice
    ->getFile()
    ->id());
}