public function OrderReportGeneratorTest::testRefreshSinglePluginReports in Commerce Reporting 8
Tests the refresh reports method for a single plugin method.
File
- tests/
src/ Kernel/ OrderReportGeneratorTest.php, line 319
Class
- OrderReportGeneratorTest
- Test order report generator service.
Namespace
Drupal\Tests\commerce_reports\KernelCode
public function testRefreshSinglePluginReports() {
/** @var \Drupal\commerce_order\Entity\OrderInterface $order */
$order = Order::create([
'type' => 'default',
'store_id' => $this->store
->id(),
'cart' => TRUE,
'mail' => $this
->randomString() . '@example.com',
'uid' => User::getAnonymousUser(),
'ip_address' => '127.0.0.1',
'order_number' => '6',
'billing_profile' => $this->profile,
]);
$order_item_storage = $this->container
->get('entity_type.manager')
->getStorage('commerce_order_item');
$order_item = $order_item_storage
->createFromPurchasableEntity($this->variation, [
'quantity' => 1,
]);
$order_item
->save();
$order
->addItem($order_item);
$order
->set('state', 'completed');
$order
->save();
/** @var \Drupal\commerce_reports\OrderReportGeneratorInterface $orderReportGenerator */
$orderReportGenerator = $this->container
->get('commerce_reports.order_report_generator');
// Verify that no reports are created for an invalid plugin type.
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
], 'none');
$this
->assertEquals($processed, 0);
$order_reports = OrderReport::loadMultiple();
$this
->assertEmpty($order_reports);
// Verify that only order reports are generated.
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
], 'order_report');
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertEquals(count($order_reports), 1);
/** @var \Drupal\commerce_reports\Entity\OrderReport $order_report */
$order_report = reset($order_reports);
// Verify order report data.
$this
->assertEquals('order_report', $order_report
->bundle());
$this
->assertEquals($order_report
->getOrderId(), $order
->id());
$this
->assertTrue($order_report
->hasField('amount'), 'Default order report has the amount field');
$this
->assertEquals($order_report
->get('amount')
->first()
->toPrice(), $order
->getTotalPrice());
// Verify that existing order reports are not deleted when order item reports generated.
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
], 'order_items_report');
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
$has_order_reports = FALSE;
$has_order_items_reports = FALSE;
foreach ($order_reports as $order_report) {
if ($order_report
->bundle() == 'order_report') {
$has_order_reports = TRUE;
}
if ($order_report
->bundle() == 'order_items_report') {
$has_order_items_reports = TRUE;
}
}
$this
->assertTrue($has_order_reports);
$this
->assertTrue($has_order_items_reports);
}