You are here

public static function QuailApiBase::save_test in Quail API 8

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

src/QuailApiBase.php, line 123

Class

QuailApiBase
Class QuailApiBase.

Namespace

Drupal\quail_api

Code

public static function save_test($test_data) {
  if (!is_array($test_data)) {
    return FALSE;
  }
  $result = FALSE;
  $columns = [
    'machine_name',
    'severity',
    'human_name',
    'description',
  ];
  foreach ($columns as $key) {
    if (empty($test_data[$key])) {
      return FALSE;
    }
  }
  $results = self::load_tests([
    'machine_name' => $test_data['machine_name'],
  ], NULL);
  if (empty($results)) {
    $data = [];
    $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::database()
      ->insert('quail_api_tests')
      ->fields($data)
      ->execute();
  }
  return $result;
}