View source
<?php
namespace Drupal\Tests\commerce_reports\Kernel\ReportType;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_price\Price;
use Drupal\commerce_reports\Entity\OrderReport;
use Drupal\profile\Entity\Profile;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class OrderReportTest extends CommerceKernelTestBase {
public static $modules = [
'entity_reference_revisions',
'profile',
'state_machine',
'commerce_order',
'commerce_reports',
];
protected $reportTypeManager;
protected $user;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('profile');
$this
->installEntitySchema('commerce_order');
$this
->installEntitySchema('commerce_order_item');
$this
->installEntitySchema('commerce_order_report');
$this
->installConfig('commerce_order');
$this->reportTypeManager = $this->container
->get('plugin.manager.commerce_report_type');
$user = $this
->createUser([
'mail' => $this
->randomString() . '@example.com',
]);
$this->user = $this
->reloadEntity($user);
}
public function testFields() {
$order_report = OrderReport::create([
'type' => 'order_report',
]);
$this
->assertTrue($order_report
->hasField('order_type_id'));
$this
->assertTrue($order_report
->hasField('amount'));
$this
->assertTrue($order_report
->hasField('mail'));
$this
->assertTrue($order_report
->hasField('billing_address'));
}
public function testOrderReport() {
$report_type_plugin = $this->reportTypeManager
->createInstance('order_report');
$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' => $this->user
->id(),
]);
$profile
->save();
$order = Order::create([
'order_id' => 1234,
'type' => 'default',
'state' => 'completed',
'placed' => '1518491883',
'mail' => $this->user
->getEmail(),
'uid' => $this->user
->id(),
'ip_address' => '127.0.0.1',
'order_number' => '6',
'billing_profile' => $profile,
'store_id' => $this->store
->id(),
]);
$order
->get('total_price')
->setValue(new Price('100.00', 'USD'));
$report_type_plugin
->generateReports($order);
$order_reports = OrderReport::loadMultiple();
$this
->assertEquals(1, count($order_reports));
$order_report = reset($order_reports);
$this
->assertEquals('1518491883', $order_report
->getCreatedTime());
$this
->assertEquals(1234, $order_report
->getOrderId());
$this
->assertFalse($order_report
->get('order_type_id')
->isEmpty());
$this
->assertEquals($order
->bundle(), $order_report
->get('order_type_id')
->first()->target_id);
$this
->assertFalse($order_report
->get('amount')
->isEmpty());
$this
->assertEquals(new Price('100.00', 'USD'), $order_report
->get('amount')
->first()
->toPrice());
$this
->assertFalse($order_report
->get('mail')
->isEmpty());
$this
->assertEquals($this->user
->getEmail(), $order_report
->get('mail')
->first()->value);
$this
->assertFalse($order_report
->get('billing_address')
->isEmpty());
$this
->assertEquals($profile
->get('address')
->first()
->toArray(), $order_report
->get('billing_address')
->first()
->toArray());
}
}