You are here

public function MonitoringCommerceTest::testTurnoverSensor in Monitoring 8

Tests the commerce turnover sensor.

File

tests/src/Functional/MonitoringCommerceTest.php, line 77

Class

MonitoringCommerceTest
Tests the commerce turnover sensor.

Namespace

Drupal\Tests\monitoring\Functional

Code

public function testTurnoverSensor() {

  // Create commerce turnover sensor.
  $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();

  // Assert there is no value if there are no orders.
  $result = $this
    ->runSensor('commerce_total_turnover');
  $this
    ->assertEqual($result
    ->getMessage(), 'No value');
  $this
    ->assertNull($result
    ->getValue());

  // Create some orders with different states and currencies.
  $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');

  // Now only consider completed orders.
  $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');

  // Change currency.
  $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');
}