You are here

function quail_api_save_test in Quail API 7

This stores the quail test information in the drupal database.

The quail test data does not have a unique id associated with is so the machine name must be used. When the machine name is used, a unique id is associated. If the machine name already exists then no write is performed. If an update of the data is required then pass either the 'do_update' vairable.

Parameters

array $test_data: An array of test data with the following keys:

  • machine_name: This is the name of the test as defined by the quail library.
  • severity: This is the display level of the test as defined by the quail library.
  • human_name: This is a more title of the test as defiend by the quail library.
  • description: The detailed description of the test, which is defined by the quail library in the 'body' item.
  • id: (optional) The numeric id for existing data.
  • do_update: (optional) A boolean. When specified this tells forces an update if the machine name already exists.

Return value

int|false The return states of either FALSE, SAVED_NEW, or SAVED_UPDATED.

File

./quail_api.module, line 869
Module file for the quail api.

Code

function quail_api_save_test($test_data) {
  if (!is_array($test_data)) {
    if (class_exists('cf_error')) {
      cf_error::invalid_array('test_data');
    }
    return FALSE;
  }
  $result = FALSE;
  $columns = array(
    'machine_name',
    'severity',
    'human_name',
    'description',
  );
  foreach ($columns as $key) {
    if (empty($test_data[$key])) {
      if (class_exists('cf_error')) {
        cf_error::missing_array_key('problem_data', $key);
      }
      return FALSE;
    }
  }

  // @todo: implement 'id' and 'do_update'
  $results = quail_api_load_tests(array(
    'machine_name' => $test_data['machine_name'],
  ), NULL);
  if (empty($results)) {
    $data = array();
    $primary_key = array();
    $data['machine_name'] = $test_data['machine_name'];
    $data['severity'] = $test_data['severity'];
    $data['human_name'] = $test_data['human_name'];
    $data['description'] = $test_data['description'];
    $result = drupal_write_record('quail_api_tests', $data, $primary_key);
  }
  return $result;
}