Projet

Général

Profil

Paste
Télécharger (2,65 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform_validation / tests / WebformValidationUnitTestCase.test @ 8916d7ec

1
<?php
2

    
3
/**
4
 * Unit test Webform Validation module.
5
 */
6
class WebformValidationUnitTestCase extends DrupalUnitTestCase {
7

    
8
  /**
9
   * {@inheritdoc}
10
   */
11
  public static function getInfo() {
12
    return array(
13
      'name' => t('Webform Validation Unit Tests'),
14
      'description' => t('Unit tests for Webform Validation module.'),
15
      'group' => t('Webform'),
16
    );
17
  }
18

    
19
  /**
20
   * The tests.
21
   */
22
  public function test() {
23
    require_once __DIR__ . '/../webform_validation.validators.inc';
24

    
25
    $validator_name = 'comparison';
26
    $items = array(
27
      'one' => array(
28
        'hour' => 12,
29
        'minute' => 1,
30
        'ampm' => 'AM',
31
      ),
32
      'two' => array(
33
        'hour' => 12,
34
        'minute' => 4,
35
        'ampm' => 'AM',
36
      ),
37
    );
38
    $components = array(
39
      'one' => array(
40
        'type' => 'time',
41
      ),
42
      'two' => array(
43
        'type' => 'time',
44
      ),
45
    );
46
    $rule = array(
47
      'data' => '<',
48
      'components' => $components,
49
      'error_message' => 'Error message.',
50
    );
51
    $test_value = webform_validation_webform_validation_validate($validator_name, $items, $components, $rule);
52
    $value = array();
53
    $this->assertIdentical($test_value, $value, 'No error for passing validation.');
54

    
55
    $rule['data'] = '>';
56
    $test_value = webform_validation_webform_validation_validate($validator_name, $items, $components, $rule);
57
    $value = array('two' => 'Error message.');
58
    $this->assertIdentical($test_value, $value, 'Error for failing validation.');
59

    
60
    // Test the 'pattern' validator.
61
    $validator_name = 'pattern';
62
    $items = [];
63
    $components = [];
64
    $rule = [
65
      'data' => 'D-25##|(###) ###-####|@# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA',
66
      'error_message' => 'Invalid value.',
67
    ];
68

    
69
    $items['item_1'] = 'D-25AA';
70
    $test = webform_validation_webform_validation_validate($validator_name, $items, $components, $rule);
71
    $this->assertIdentical(count($test), 1, $items['item_1'] . ' fails validation.');
72

    
73
    $items['item_1'] = 'D-2500';
74
    $test = webform_validation_webform_validation_validate($validator_name, $items, $components, $rule);
75
    $this->assertIdentical(count($test), 0, $items['item_1'] . ' passes validation.');
76

    
77
    $items['item_1'] = '(123)-456-7890';
78
    $test = webform_validation_webform_validation_validate($validator_name, $items, $components, $rule);
79
    $this->assertIdentical(count($test), 1, $items['item_1'] . ' fails validation.');
80

    
81
    $items['item_1'] = '(123) 456-7890';
82
    $test = webform_validation_webform_validation_validate($validator_name, $items, $components, $rule);
83
    $this->assertIdentical(count($test), 0, $items['item_1'] . ' passes validation.');
84
  }
85

    
86
}