You are here

function MonitoringCoreTest::testGenericDBAggregate in Monitoring 7

Tests the watchdog entries aggregator.

File

test/tests/monitoring.core.test, line 545
Contains \MonitoringCoreTest.

Class

MonitoringCoreTest
Tests for cron sensor.

Code

function testGenericDBAggregate() {

  // Aggregate by watchdog type.
  monitoring_sensor_settings_save('watchdog_aggregate_test', array(
    'conditions' => array(
      array(
        'field' => 'type',
        'value' => 'test_type',
      ),
    ),
  ));
  watchdog('test_type', $this
    ->randomName());
  watchdog('test_type', $this
    ->randomName());
  watchdog('other_test_type', $this
    ->randomName());
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Aggregate by watchdog message.
  monitoring_sensor_settings_save('watchdog_aggregate_test', array(
    'conditions' => array(
      array(
        'field' => 'message',
        'value' => 'test_message',
      ),
    ),
  ));
  watchdog($this
    ->randomName(), 'test_message');
  watchdog($this
    ->randomName(), 'another_test_message');
  watchdog($this
    ->randomName(), 'another_test_message');
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 1);

  // Aggregate by watchdog severity.
  monitoring_sensor_settings_save('watchdog_aggregate_test', array(
    'conditions' => array(
      array(
        'field' => 'severity',
        'value' => WATCHDOG_CRITICAL,
      ),
    ),
  ));
  watchdog($this
    ->randomName(), $this
    ->randomName(), array(), WATCHDOG_CRITICAL);
  watchdog($this
    ->randomName(), $this
    ->randomName(), array(), WATCHDOG_CRITICAL);
  watchdog($this
    ->randomName(), $this
    ->randomName(), array(), WATCHDOG_CRITICAL);
  watchdog($this
    ->randomName(), $this
    ->randomName(), array(), WATCHDOG_CRITICAL);
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 4);

  // Aggregate by watchdog location.
  monitoring_sensor_settings_save('watchdog_aggregate_test', array(
    'conditions' => array(
      array(
        'field' => 'location',
        'value' => 'http://some.url.dev',
      ),
    ),
  ));

  // Update the two test_type watchdog entries with a custom location.
  db_update('watchdog')
    ->fields(array(
    'location' => 'http://some.url.dev',
  ))
    ->condition('type', 'test_type')
    ->execute();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Filter for time period.
  monitoring_sensor_settings_save('watchdog_aggregate_test', array(
    'time_interval_value' => 10,
    'time_interval_field' => 'timestamp',
  ));

  // Make all system watchdog messages older than the configured time period.
  db_update('watchdog')
    ->fields(array(
    'timestamp' => REQUEST_TIME - 20,
  ))
    ->condition('type', 'system')
    ->execute();
  $count_latest = db_query('SELECT COUNT(*) FROM {watchdog} WHERE timestamp > :timestamp', array(
    ':timestamp' => REQUEST_TIME - 10,
  ))
    ->fetchField();
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), $count_latest);

  // Test for thresholds and statuses.
  monitoring_sensor_settings_save('watchdog_aggregate_test', array(
    'conditions' => array(
      array(
        'field' => 'type',
        'value' => 'test_watchdog_aggregate_sensor',
      ),
    ),
  ));
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertTrue($result
    ->isOk());
  $this
    ->assertEqual($result
    ->getValue(), 0);
  watchdog('test_watchdog_aggregate_sensor', 'testing');
  watchdog('test_watchdog_aggregate_sensor', 'testing');
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertTrue($result
    ->isWarning());
  $this
    ->assertEqual($result
    ->getValue(), 2);
  watchdog('test_watchdog_aggregate_sensor', 'testing');
  $result = $this
    ->runSensor('watchdog_aggregate_test');
  $this
    ->assertTrue($result
    ->isCritical());
  $this
    ->assertEqual($result
    ->getValue(), 3);

  // Test with different db table.
  $info = $this->sensorManager
    ->getSensorInfoByName('db_aggregate_test');
  $this
    ->drupalCreateNode(array(
    'promote' => '1',
  ));
  $this
    ->drupalCreateNode(array(
    'promote' => '0',
  ));
  $this
    ->drupalCreateNode(array(
    'promote' => '0',
  ));

  // Create one node that should not meet the time_interval condition.
  $node = $this
    ->drupalCreateNode(array(
    'promote' => '0',
  ));
  db_update('node')
    ->fields(array(
    'created' => REQUEST_TIME - ($info
      ->getTimeIntervalValue() + 10),
  ))
    ->condition('nid', $node->nid)
    ->execute();

  // Based on default sensor settings we should get the promoted node.
  $result = $this
    ->runSensor('db_aggregate_test');
  $this
    ->assertEqual($result
    ->getValue(), 1);

  // Test with settings updated.
  $settings = monitoring_sensor_settings_get('db_aggregate_test');
  $settings['conditions'] = array(
    'test' => array(
      'field' => 'promote',
      'value' => '0',
    ),
  );
  monitoring_sensor_settings_save('db_aggregate_test', $settings);
  $result = $this
    ->runSensor('db_aggregate_test');

  // There should be two nodes with promote 0 and created in last 24 hours.
  $this
    ->assertEqual($result
    ->getValue(), 2);

  // Test support for configurable fields, create a taxonomy reference field.
  $type = $this
    ->drupalCreateContentType();
  $vocabulary = $this
    ->createVocabulary();
  $field = array(
    'field_name' => 'term_reference',
    'type' => 'taxonomy_term_reference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);

  // Create a second field.
  $instance = array(
    'field_name' => 'term_reference',
    'bundle' => $type->type,
    'entity_type' => 'node',
    'widget' => array(
      'type' => 'options_select',
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
      ),
    ),
  );
  field_create_instance($instance);
  $field = array(
    'field_name' => 'term_reference2',
    'type' => 'taxonomy_term_reference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED,
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name,
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'term_reference2',
    'bundle' => $type->type,
    'entity_type' => 'node',
    'widget' => array(
      'type' => 'options_select',
    ),
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link',
      ),
    ),
  );
  field_create_instance($instance);

  // Create some terms.
  $term1 = $this
    ->createTerm($vocabulary);
  $term2 = $this
    ->createTerm($vocabulary);

  // Create node that only references the first term.
  $node1 = $this
    ->drupalCreateNode(array(
    'created' => REQUEST_TIME,
    'type' => $type->type,
    'term_reference' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term1->tid,
        ),
      ),
    ),
  ));

  // Create node that only references both terms.
  $node2 = $this
    ->drupalCreateNode(array(
    'created' => REQUEST_TIME,
    'type' => $type->type,
    'term_reference' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term1->tid,
        ),
        array(
          'tid' => $term2->tid,
        ),
      ),
    ),
  ));

  // Create a third node that references both terms but in different fields.
  $node2 = $this
    ->drupalCreateNode(array(
    'created' => REQUEST_TIME,
    'type' => $type->type,
    'term_reference' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term1->tid,
        ),
      ),
    ),
    'term_reference2' => array(
      LANGUAGE_NONE => array(
        array(
          'tid' => $term2->tid,
        ),
      ),
    ),
  ));

  // Update the sensor to look for nodes with a reference to term1 in the
  // first field.
  $settings = monitoring_sensor_settings_get('db_aggregate_test');
  $settings['conditions'] = array(
    'test' => array(
      'field' => 'term_reference.tid',
      'value' => $term1->tid,
    ),
  );
  monitoring_sensor_settings_save('db_aggregate_test', $settings);
  $result = $this
    ->runSensor('db_aggregate_test');

  // There should be three nodes with that reference.
  $this
    ->assertEqual($result
    ->getValue(), 3);

  // Update the sensor to look for nodes with a reference to term1 in the
  // first field and term2 in the second.
  $settings = monitoring_sensor_settings_get('db_aggregate_test');
  $settings['conditions'] = array(
    'test' => array(
      'field' => 'term_reference.tid',
      'value' => $term1->tid,
    ),
    'test2' => array(
      'field' => 'term_reference2.tid',
      'value' => $term2->tid,
    ),
  );
  monitoring_sensor_settings_save('db_aggregate_test', $settings);
  $result = $this
    ->runSensor('db_aggregate_test');

  // There should be one nodes with those references.
  $this
    ->assertEqual($result
    ->getValue(), 1);
}