You are here

public function InvoicePrintBuilderTest::testSavePrintable in Commerce Invoice 8.2

@covers ::generateFilename @covers ::savePrintable

File

tests/src/Kernel/InvoicePrintBuilderTest.php, line 70

Class

InvoicePrintBuilderTest
Tests the invoice print builder.

Namespace

Drupal\Tests\commerce_invoice\Kernel

Code

public function testSavePrintable() {
  $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' => 'paid',
    'uid' => $this->user
      ->id(),
  ]);
  $invoice
    ->save();
  $print_engine = $this->container
    ->get('plugin.manager.entity_print.print_engine')
    ->createInstance('testprintengine');
  $file = $this->printBuilder
    ->savePrintable($invoice, $print_engine);
  $this
    ->assertNotEmpty($file);
  $this
    ->assertRegExp('#private://(.*)\\.pdf#', $file
    ->getFileUri());
  $this
    ->assertEquals('10-en-paid.pdf', $file
    ->getFilename());
  $this
    ->assertEquals('application/pdf', $file
    ->getMimeType());

  // Tests the filename alteration via an event subscriber.
  $invoice
    ->setData('alter_filename', TRUE);
  $file = $this->printBuilder
    ->savePrintable($invoice, $print_engine);
  $this
    ->assertRegExp('#private://(.*)\\.pdf#', $file
    ->getFileUri());
  $this
    ->assertEquals('10-en-paid-altered.pdf', $file
    ->getFilename());
  $this
    ->assertEquals('application/pdf', $file
    ->getMimeType());
}