You are here

monitoring.commerce.test in Monitoring 7

File

test/tests/monitoring.commerce.test
View source
<?php

/**
 * @file
 * Contains \MonitoringCommerceTest.
 */

/**
 * Tests for commerce order turnover sensor.
 */
class MonitoringCommerceTest extends MonitoringTestBase {
  public static function getInfo() {
    return array(
      'name' => 'Monitoring commerce',
      'description' => 'Drupal commerce sensors tests.',
      'group' => 'Monitoring',
      'dependencies' => array(
        'commerce_order',
      ),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp(array(
      'commerce_order',
    ));
  }

  /**
   * Test commerce turnover sensors.
   */
  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");
  }

  /**
   * Test commerce order count sensors with different order status.
   */
  public function testOrderStatusSensors() {
    $this
      ->createEmptyOrderWithPrice('pending');
    $this
      ->createEmptyOrderWithPrice('pending');
    $this
      ->createEmptyOrderWithPrice('completed');
    $result = $this
      ->runSensor('commerce_order_status_pending');
    $this
      ->assertEqual($result
      ->getValue(), 2);
    $this
      ->assertEqual($result
      ->getMessage(), '2 orders in 1 day');
    $result = $this
      ->runSensor('commerce_order_status_completed');
    $this
      ->assertEqual($result
      ->getValue(), 1);
    $this
      ->assertEqual($result
      ->getMessage(), '1 orders in 1 day');
  }

  /**
   * Create an order for testing purposes.
   *
   * The value of the total amount of the resulting order will be 1 (100 in db).
   *
   * @param string $status
   *   Status in which the order should be created.
   * @param string $currency
   *   Currency of the total amount.
   */
  protected function createEmptyOrderWithPrice($status, $currency = 'CHF', $amount = 100) {
    $order = commerce_order_new(0, $status);
    commerce_order_save($order);

    // Make life easier to directly update the total so we do not need to
    // create line items.
    db_update('field_data_commerce_order_total')
      ->fields(array(
      'commerce_order_total_amount' => $amount,
      'commerce_order_total_currency_code' => $currency,
    ))
      ->condition('entity_id', $order->order_id)
      ->execute();
  }

}

Classes

Namesort descending Description
MonitoringCommerceTest Tests for commerce order turnover sensor.