View source
<?php
namespace Drupal\Tests\commerce_reports\Kernel;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_reports\Entity\OrderReport;
use Drupal\profile\Entity\Profile;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
use Drupal\user\Entity\User;
class OrderReportGeneratorTest extends CommerceKernelTestBase {
public static $modules = [
'path',
'entity_reference_revisions',
'profile',
'state_machine',
'commerce_order',
'commerce_product',
'commerce_reports',
];
protected $variation;
protected $product;
protected $profile;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('profile');
$this
->installEntitySchema('commerce_order');
$this
->installEntitySchema('commerce_order_item');
$this
->installEntitySchema('commerce_product');
$this
->installEntitySchema('commerce_product_variation');
$this
->installConfig('commerce_order');
$this
->installConfig('commerce_product');
$this
->installEntitySchema('commerce_order_report');
$this->variation = ProductVariation::create([
'type' => 'default',
'sku' => $this
->randomMachineName(),
'price' => [
'number' => 999,
'currency_code' => 'USD',
],
]);
$this->variation
->save();
$this->product = Product::create([
'type' => 'default',
'title' => $this
->randomMachineName(),
'stores' => [
$this->store,
],
'variations' => [
$this->variation,
],
]);
$this->product
->save();
$this->profile = Profile::create([
'type' => 'customer',
'address' => [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
],
'uid' => 0,
]);
$this->profile
->save();
}
public function testGenerateReports() {
$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
->save();
$orderReportGenerator = $this->container
->get('commerce_reports.order_report_generator');
$processed = $orderReportGenerator
->generateReports([
$order
->id(),
]);
$this
->assertEquals($processed, 0);
$order_reports = OrderReport::loadMultiple();
$this
->assertEmpty($order_reports);
$order
->set('state', 'completed');
$order
->save();
$processed = $orderReportGenerator
->generateReports([
$order
->id(),
]);
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
foreach ($order_reports as $order_report) {
$this
->assertEquals($order_report
->getOrderId(), $order
->id());
}
$order_report = OrderReport::load(1);
$order_2 = 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_2 = $order_item_storage
->createFromPurchasableEntity($this->variation, [
'quantity' => 1,
]);
$order_item_2
->save();
$order_2
->addItem($order_item_2);
$order_2
->save();
$processed = $orderReportGenerator
->generateReports([
$order
->id(),
$order_2
->id(),
]);
$this
->assertEquals($processed, 1);
$order_2
->set('state', 'completed');
$order_2
->save();
$processed = $orderReportGenerator
->generateReports([
$order
->id(),
$order_2
->id(),
]);
$this
->assertEquals($processed, 2);
}
public function testRefreshReports() {
$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();
$orderReportGenerator = $this->container
->get('commerce_reports.order_report_generator');
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
]);
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
$reports_created = count($order_reports);
foreach (OrderReport::loadMultiple() as $order_report) {
$this
->assertEquals($order_report
->getOrderId(), $order
->id());
}
$order_item
->setQuantity(2);
$order_item
->save();
$order
->setItems([
$order_item,
]);
$order
->save();
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
]);
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
for ($index = 1; $index <= $reports_created; $index++) {
$this
->assertEmpty(OrderReport::load($index));
}
$order_2 = 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_2 = $order_item_storage
->createFromPurchasableEntity($this->variation, [
'quantity' => 1,
]);
$order_item_2
->save();
$order_2
->addItem($order_item_2);
$order_2
->save();
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
$order_2
->id(),
]);
$this
->assertEquals($processed, 1);
$order_2
->set('state', 'completed');
$order_2
->save();
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
$order_2
->id(),
]);
$this
->assertEquals($processed, 2);
}
public function testGenerateSinglePluginReports() {
$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
->save();
$order
->set('state', 'completed');
$order
->save();
$orderReportGenerator = $this->container
->get('commerce_reports.order_report_generator');
$processed = $orderReportGenerator
->generateReports([
$order
->id(),
], 'none');
$this
->assertEquals($processed, 0);
$order_reports = OrderReport::loadMultiple();
$this
->assertEmpty($order_reports);
$processed = $orderReportGenerator
->generateReports([
$order
->id(),
], 'order_items_report');
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertNotEmpty($order_reports);
foreach ($order_reports as $order_report) {
$this
->assertEquals('order_items_report', $order_report
->bundle());
}
}
public function testRefreshSinglePluginReports() {
$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();
$orderReportGenerator = $this->container
->get('commerce_reports.order_report_generator');
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
], 'none');
$this
->assertEquals($processed, 0);
$order_reports = OrderReport::loadMultiple();
$this
->assertEmpty($order_reports);
$processed = $orderReportGenerator
->refreshReports([
$order
->id(),
], 'order_report');
$this
->assertEquals($processed, 1);
$order_reports = OrderReport::loadMultiple();
$this
->assertEquals(count($order_reports), 1);
$order_report = reset($order_reports);
$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());
$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);
}
}