You are here

function MonitoringCommerceTest::testTurnoverSensors in Monitoring 7

Test commerce turnover sensors.

File

test/tests/monitoring.commerce.test, line 31
Contains \MonitoringCommerceTest.

Class

MonitoringCommerceTest
Tests for commerce order turnover sensor.

Code

function testTurnoverSensors() {

  // Enable two currencies with different decimal handling, to verify
  // correct rounding. We don't test actual conversion rates as the different
  // decimal should be enough to ensure that the right commerce API's are
  // used.
  variable_set('commerce_enabled_currencies', array(
    'CHF' => 'CHF',
    'TND' => 'TND',
  ));
  variable_set('commerce_default_currency', 'CHF');

  // Create some orders with different status and currencies.
  $this
    ->createEmptyOrderWithPrice('canceled');
  $this
    ->createEmptyOrderWithPrice('pending');
  $this
    ->createEmptyOrderWithPrice('pending', 'CHF', 200);
  $this
    ->createEmptyOrderWithPrice('pending', 'TND', 10000);
  $this
    ->createEmptyOrderWithPrice('completed', 'CHF', 100000);

  // Sensor commerce_order_turnover_chf testing, verify default configuration.
  $result = $this
    ->runSensor('commerce_order_turnover_chf');
  $this
    ->assertEqual($result
    ->getSensorInfo()
    ->getValueLabel(), 'CHF');
  $this
    ->assertEqual($result
    ->getValue(), 3);
  $this
    ->assertEqual($result
    ->getMessage(), 'CHF 3.00 in 1 day');

  // Now only consider completed orders.
  $account = $this
    ->drupalCreateUser(array(
    'administer monitoring',
  ));
  $this
    ->drupalLogin($account);
  $edit = array(
    'monitoring_commerce_order_turnover_chf_settings[conditions][status][value][]' => array(
      'completed',
    ),
  );
  $this
    ->drupalPost('admin/config/system/monitoring/sensors/commerce_order_turnover_chf', $edit, t('Save'));
  monitoring_sensor_manager()
    ->resetCache();
  $sensor_info = monitoring_sensor_manager()
    ->getSensorInfoByName('commerce_order_turnover_chf');
  $conditions = $sensor_info
    ->getSetting('conditions');
  $this
    ->assertTrue(!in_array('pending', $conditions['status']['value']));
  $this
    ->assertTrue(in_array('completed', $conditions['status']['value']));
  $result = $this
    ->runSensor('commerce_order_turnover_chf');
  $this
    ->assertEqual($result
    ->getValue(), 1000);
  $this
    ->assertEqual($result
    ->getMessage(), "CHF 1'000.00 in 1 day");

  // Sensor commerce_order_turnover_tnd testing.
  $result = $this
    ->runSensor('commerce_order_turnover_tnd');
  $this
    ->assertEqual($result
    ->getValue(), 10);
  $this
    ->assertEqual($result
    ->getMessage(), '10.000  TND in 1 day');

  // Sensor commerce_order_turnover_total testing.
  $edit = array(
    'monitoring_commerce_order_turnover_total_settings[conditions][status][value][]' => array(
      'pending',
      'completed',
    ),
    // Configure the sensor to threshold type "falls" to not escalate.
    // @todo - thresholds should be fixed, that if the threshold value is an
    // empty string it will not do the assessment.
    'monitoring_commerce_order_turnover_total_settings[thresholds][type]' => 'falls',
  );
  $this
    ->drupalPost('admin/config/system/monitoring/sensors/commerce_order_turnover_total', $edit, t('Save'));
  monitoring_sensor_manager()
    ->resetCache();
  $result = $this
    ->runSensor('commerce_order_turnover_total');
  $this
    ->assertEqual($result
    ->getValue(), 1013);
  $this
    ->assertEqual($result
    ->getMessage(), "CHF 1'013.00 in 1 day");
}