You are here

clientside_validation_testswarm.module in Clientside Validation 7

Testswarm tests for Clientside Validation.

File

clientside_validation_testswarm/clientside_validation_testswarm.module
View source
<?php

/**
 * @file
 * Testswarm tests for Clientside Validation.
 */

/**
 * Implements hook_menu().
 */
function clientside_validation_testswarm_menu() {
  $path = 'clientsidevalidation/tests';
  $items[$path] = array(
    'title' => 'Clientside Validation Testswarm Tests',
    'description' => 'All forms for Clientside Validation Testswarm needed to run the tests',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'access content',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items[$path . '/fapi'] = array(
    'title' => 'Clientside Validation Fapi Test',
    'description' => 'Test Clientside Validation on normal FAPI elements',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clientside_validation_testswarm_fapi',
    ),
    'access arguments' => array(
      'access content',
    ),
    'file' => 'clientside_validation_testswarm.forms.inc',
  );
  $items[$path . '/html5'] = array(
    'title' => 'Clientside Validation HTML5 Test',
    'description' => 'Test Clientside Validation on HTML5 FAPI elements provided by the Elements module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clientside_validation_testswarm_html5',
    ),
    'access arguments' => array(
      'access content',
    ),
    'file' => 'clientside_validation_testswarm.forms.inc',
  );
  $items[$path . '/fapivalidation'] = array(
    'title' => 'Clientside Validation FAPI Validation Test',
    'description' => 'Test Clientside Validation on FAPI elements with FAPI Validation rules',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'clientside_validation_testswarm_fapi_validation',
    ),
    'access arguments' => array(
      'access content',
    ),
    'file' => 'clientside_validation_testswarm.forms.inc',
  );
  return $items;
}

/**
 * Implements hook_enable().
 */
function clientside_validation_testswarm_enable() {
  module_load_include('inc', 'clientside_validation_testswarm', 'clientside_validation_testswarm.default_content');
  $content = _clientside_validation_testswarm_default_content();
  $nodenames = variable_get('clientside_validation_testswarm_nodenames', array());
  variable_set('node_content_enabled', TRUE);
  foreach ($content as $newnode) {
    if (!array_key_exists($newnode->machine_name, $nodenames)) {
      defaultcontent_import_node($newnode);
      $nid = defaultcontent_get_default($newnode->machine_name);
      $nodenames[$newnode->machine_name] = $nid;
    }
  }
  variable_set('clientside_validation_testswarm_nodenames', $nodenames);
  if (module_exists('path')) {
    foreach (variable_get('clientside_validation_testswarm_nodenames', array()) as $machine_name => $nid) {
      $node = node_load($nid);
      $path = array(
        'source' => 'node/' . $nid,
        'alias' => 'clientsidevalidation/tests/' . $machine_name,
      );
      if (isset($node->language)) {
        $path['language'] = $node->language;
      }
      path_save($path);
    }
  }
  menu_rebuild();
  $pid = db_select('menu_links', 'ml')
    ->fields('ml')
    ->condition('link_path', 'clientsidevalidation/tests')
    ->execute()
    ->fetchObject()->mlid;
  $link = array(
    'menu_name' => 'navigation',
    'link_path' => 'node-name/webform',
    'router_path' => 'node/%',
    'link_title' => 'Clientside Validation Webform Tests',
    'options' => array(
      'attributes' => array(
        'title' => 'Test Clientside Validation on form elements provided by the Webform module',
      ),
    ),
    'module' => 'menu',
    'hidden' => '0',
    'external' => '0',
    'has_children' => '0',
    'expanded' => '0',
    'weight' => '0',
    'parent_path' => 'clientsidevalidation/tests',
    'identifier' => 'navigation:node-name/webform',
    'pid' => $pid,
  );
  defaultcontent_import_menu_link($link['identifier'], $link);
  $link = array(
    'menu_name' => 'navigation',
    'link_path' => 'node-name/webformvalidation',
    'router_path' => 'node/%',
    'link_title' => 'Clientside Validation Webform Validation Tests',
    'options' => array(
      'attributes' => array(
        'title' => 'Test Clientside Validation Webform Validation',
      ),
    ),
    'module' => 'menu',
    'hidden' => '0',
    'external' => '0',
    'has_children' => '0',
    'expanded' => '0',
    'weight' => '0',
    'parent_path' => 'clientsidevalidation/tests',
    'identifier' => 'navigation:node-name/webformvalidation',
    'pid' => $pid,
  );
  defaultcontent_import_menu_link($link['identifier'], $link);
  $link = array(
    'menu_name' => 'navigation',
    'link_path' => 'node/add/cv-test-field-validation',
    'router_path' => 'node/add/cv-test-field-validation',
    'link_title' => 'Clientside Validation Test Field Validation',
    'options' => array(
      'attributes' => array(
        'title' => 'Test Clientside Validation Field Validation',
      ),
    ),
    'module' => 'menu',
    'hidden' => '0',
    'external' => '0',
    'has_children' => '0',
    'expanded' => '0',
    'weight' => '0',
    'parent_path' => 'clientsidevalidation/tests',
    'pid' => $pid,
    'identifier' => 'navigation:node/add/cv-test-field-validation',
  );
  defaultcontent_import_menu_link($link['identifier'], $link);
  _clientside_validation_testswarm_create_webform_validation_rules();
}

/**
 * Implements hook_testswarm_tests().
 */
function clientside_validation_testswarm_testswarm_tests() {
  $path = drupal_get_path('module', 'clientside_validation_testswarm') . '/tests/';
  $tests = array(
    'clientside_validation_testswarm_fapi' => array(
      'module' => 'Clientside Validation',
      'description' => 'Test Clientside Validation on normal FAPI elements',
      'js' => array(
        $path . 'clientside_validation_fapi.test.js' => array(),
      ),
      'path' => 'clientsidevalidation/tests/fapi',
      'query' => array(),
    ),
    'clientside_validation_testswarm_html5' => array(
      'module' => 'Clientside Validation',
      'description' => 'Test Clienstide Valiation on HTML5 FAPI elements provided by the Elements module',
      'js' => array(
        $path . 'clientside_validation_html5.test.js' => array(),
      ),
      'path' => 'clientsidevalidation/tests/html5',
      'query' => array(),
    ),
    'clientside_validation_testswarm_fapi_validation' => array(
      'module' => 'Clientside Validation',
      'description' => 'Test Clientside Validation on FAPI elements with FAPI Validation rules',
      'js' => array(
        $path . 'clientside_validation_fapi_validation.test.js' => array(),
      ),
      'path' => 'clientsidevalidation/tests/fapivalidation',
      'query' => array(),
    ),
    'clientside_validation_testswarm_field_validation' => array(
      'module' => 'Clientside Validation',
      'description' => 'Test Clientside Validation Field Validation',
      'js' => array(
        $path . 'clientside_validation_field_validation.test.js' => array(),
      ),
      'path' => 'clientsidevalidation/tests/fieldvalidation',
      'query' => array(),
    ),
  );
  $nodenames = variable_get('clientside_validation_testswarm_nodenames', array());
  foreach ($nodenames as $machine_name => $nid) {
    if ($nid) {
      $tests['clientside_validation_testswarm_' . $machine_name] = array(
        'module' => 'Clientside Validation',
        'js' => array(
          $path . 'clientside_validation_' . $machine_name . '.test.js' => array(),
          array(
            'data' => array(
              'clientsideValidationTestswarm' => array(
                'formID' => 'webform-client-form-' . $nid,
              ),
            ),
            'type' => 'setting',
          ),
        ),
        'path' => 'clientsidevalidation/tests/' . $machine_name,
        'query' => array(),
      );
    }
  }
  return $tests;
}
function _clientside_validation_testswarm_create_webform_validation_rules() {
  $added = variable_get('clientside_validation_rules_added', 0);
  $nodenames = variable_get('clientside_validation_testswarm_nodenames', array());
  $nid = isset($nodenames['webformvalidation']) ? $nodenames['webformvalidation'] : FALSE;
  if (!$nid || $added) {
    return;
  }
  $transaction = db_transaction();
  try {
    $values = array(
      array(
        'cv_regex',
        $nid,
        'regex',
        '[a-z0-9]+$',
        'Must be a valid machine readable name',
        drupal_map_assoc(array(
          1,
        )),
      ),
      array(
        'cv_min_length',
        $nid,
        'min_length',
        '5',
        NULL,
        drupal_map_assoc(array(
          2,
        )),
      ),
      array(
        'cv_max_length',
        $nid,
        'max_length',
        '10',
        NULL,
        drupal_map_assoc(array(
          3,
        )),
      ),
      array(
        'cv_min_words',
        $nid,
        'min_words',
        '5',
        NULL,
        drupal_map_assoc(array(
          4,
        )),
      ),
      array(
        'cv_max_words',
        $nid,
        'max_words',
        '10',
        NULL,
        drupal_map_assoc(array(
          5,
        )),
      ),
      array(
        'cv_numeric',
        $nid,
        'numeric',
        '',
        NULL,
        drupal_map_assoc(array(
          6,
        )),
      ),
      array(
        'cv_numeric_min',
        $nid,
        'numeric',
        '5',
        NULL,
        drupal_map_assoc(array(
          7,
        )),
      ),
      array(
        'cv_numeric_max',
        $nid,
        'numeric',
        '|10',
        NULL,
        drupal_map_assoc(array(
          8,
        )),
      ),
      array(
        'cv_oneof',
        $nid,
        'oneofseveral',
        NULL,
        NULL,
        drupal_map_assoc(array(
          10,
          11,
          12,
        )),
      ),
      array(
        'cv_oneoftwo',
        $nid,
        'oneoftwo',
        NULL,
        NULL,
        drupal_map_assoc(array(
          14,
          15,
        )),
      ),
      array(
        'cv_plain_text',
        $nid,
        'plain_text',
        NULL,
        NULL,
        drupal_map_assoc(array(
          16,
        )),
      ),
      array(
        'cv_blacklist',
        $nid,
        'blacklist',
        'black,list',
        "Field can not contain the words 'black or list'",
        drupal_map_assoc(array(
          17,
        )),
      ),
      array(
        'cv_equal',
        $nid,
        'equal',
        NULL,
        NULL,
        drupal_map_assoc(array(
          19,
          20,
        )),
      ),
      array(
        'cv_unique',
        $nid,
        'unique',
        NULL,
        NULL,
        drupal_map_assoc(array(
          22,
          23,
          24,
        )),
      ),
      array(
        'cv_specific_value',
        $nid,
        'specific_value',
        'abc-123',
        "Field must equal 'abc-123'",
        drupal_map_assoc(array(
          25,
        )),
      ),
      array(
        'cv_select_min',
        $nid,
        'select_min',
        '2',
        NULL,
        drupal_map_assoc(array(
          26,
        )),
      ),
      array(
        'cv_select_max',
        $nid,
        'select_max',
        '5',
        NULL,
        drupal_map_assoc(array(
          27,
        )),
      ),
      array(
        'cv_select_exact',
        $nid,
        'select_exact',
        '3',
        NULL,
        drupal_map_assoc(array(
          28,
        )),
      ),
      array(
        'cv_must_be_empty',
        $nid,
        'must_be_empty',
        NULL,
        NULL,
        drupal_map_assoc(array(
          29,
        )),
      ),
    );
    foreach ($values as $value) {
      $keys = array(
        'rulename',
        'nid',
        'validator',
        'data',
        'error_message',
        'rule_components',
      );
      $rule = array_combine($keys, $value);
      $rule['action'] = 'add';
      webform_validation_rule_save($rule);
    }
    variable_set('clientside_validation_rules_added', 1);
  } catch (Exception $e) {
    $transaction
      ->rollback();
    watchdog('cv_testswarm', $e
      ->getMessage());
  }
}