View source
<?php
namespace Drupal\Tests\monitoring\Functional;
use Drupal\commerce_order\Entity\Order;
use Drupal\commerce_order\Entity\OrderItem;
use Drupal\commerce_price\Price;
use Drupal\monitoring\Entity\SensorConfig;
class MonitoringCommerceTest extends MonitoringTestBase {
public static $modules = [
'commerce',
'commerce_order',
'node',
];
protected $account;
protected function setUp() {
parent::setUp();
$this->account = $this
->drupalCreateUser([
'administer commerce_order',
'administer commerce_currency',
'administer commerce_store',
'administer commerce_order_type',
'administer monitoring',
'monitoring reports',
]);
$this
->drupalLogin($this->account);
$edit = [
'currency_codes[]' => [
'CHF',
'EUR',
],
];
$this
->drupalPostForm('admin/commerce/config/currencies/add', $edit, t('Add'));
$edit = [
'name[0][value]' => 'Default',
'mail[0][value]' => 'test@example.com',
'address[0][address][country_code]' => 'CH',
'address[0][address][address_line1]' => 'Demo street',
'address[0][address][locality]' => 'Demo city',
'address[0][address][postal_code]' => '1234',
'default_currency' => 'CHF',
];
$this
->drupalPostForm('store/add/online', $edit, t('Save'));
$edit = [
'id' => 'test',
'label' => 'Test',
'orderType' => 'default',
];
$this
->drupalPostForm('admin/commerce/config/order-item-types/add', $edit, t('Save'));
}
public function testTurnoverSensor() {
$sensor = SensorConfig::create([
'id' => 'commerce_total_turnover',
'label' => 'Total turnover',
'plugin_id' => 'commerce_turnover',
'status' => 1,
'settings' => [
'entity_type' => 'commerce_order',
'commerce_order_paid_states' => [],
'commerce_order_currency' => 'CHF',
'time_interval_field' => 'created',
'time_interval_value' => 86400,
],
]);
$sensor
->save();
$result = $this
->runSensor('commerce_total_turnover');
$this
->assertEqual($result
->getMessage(), 'No value');
$this
->assertNull($result
->getValue());
$this
->createEmptyOrderWithPrice('canceled');
$this
->createEmptyOrderWithPrice('draft');
$this
->createEmptyOrderWithPrice('draft', 200, 'CHF');
$this
->createEmptyOrderWithPrice('draft', 1000, 'EUR');
$this
->createEmptyOrderWithPrice('completed', 1500, 'CHF');
$result = $this
->runSensor('commerce_total_turnover');
$this
->assertEqual($result
->getMessage(), 'CHF 1’900.00 in 1 day');
$this
->assertEqual($result
->getValue(), '1900.000000');
$edit = [
'settings[commerce_order_paid_states][completed]' => TRUE,
];
$this
->drupalPostForm('admin/config/system/monitoring/sensors/commerce_total_turnover', $edit, t('Save'));
$sensor = SensorConfig::load('commerce_total_turnover');
$paid_states = $sensor
->getSetting('commerce_order_paid_states');
$this
->assertNotContains('draft', $paid_states);
$this
->assertContains('completed', $paid_states);
$result = $this
->runSensor('commerce_total_turnover');
$this
->assertEqual($result
->getMessage(), 'CHF 1’500.00 in 1 day');
$this
->assertEqual($result
->getValue(), '1500.000000');
$this
->createEmptyOrderWithPrice('completed', 250, 'EUR');
$edit = [
'settings[commerce_order_currency]' => 'EUR',
];
$this
->drupalPostForm('admin/config/system/monitoring/sensors/commerce_total_turnover', $edit, t('Save'));
$result = $this
->runSensor('commerce_total_turnover');
$this
->assertEqual($result
->getMessage(), '€ 250.00 in 1 day');
$this
->assertEqual($result
->getValue(), '250.000000');
$this
->drupalLogout();
$result = $this
->runSensor('commerce_total_turnover');
$this
->assertEqual($result
->getMessage(), '€ 250.00 in 1 day');
$this
->assertEqual($result
->getValue(), '250.000000');
}
protected function createEmptyOrderWithPrice($state, $amount = 100, $currency = 'CHF') {
$order_item = OrderItem::create([
'type' => 'test',
'quantity' => 1,
'unit_price' => new Price((string) $amount, $currency),
'overridden_unit_price' => TRUE,
]);
$order_item
->save();
$order = Order::create([
'type' => 'default',
'state' => $state,
'store_id' => \Drupal::service('commerce_store.current_store')
->getStore()
->id(),
'order_items' => [
$order_item,
],
]);
$order
->save();
}
}