Changeset 144 for trunk/tests


Ignore:
Timestamp:
Jun 4, 2006 8:34:32 AM (18 years ago)
Author:
scdev
Message:

Q - Added lib/Validator.inc.php as a backend to FormValidator? and also to be used directly for non-form validation and assertion.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/FormValidatorTest.php

    r42 r144  
    3333    function test_geterrorlist()
    3434    {
    35         $this->FormValidator->geterrorlist();
     35        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     36        $err_list = $this->FormValidator->geterrorlist();
     37        $this->assertTrue($err_list[0]['message'] === _("This is an error."));
    3638    }
    3739
    3840    function test_adderror()
    3941    {
    40         $this->FormValidator->addError('name', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     42        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     43        $this->assertTrue($this->FormValidator->errors[0]['message'] === _("This is an error."));
    4144    }
    4245
     
    4548        $result = $this->FormValidator->anyerrors();
    4649        $this->assertFalse($result);
     50        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     51        $result = $this->FormValidator->anyerrors();
     52        $this->assertTrue($result);
    4753    }
    4854
    4955    function test_reseterrorlist()
    5056    {
    51         $this->FormValidator->addError('name', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     57        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
    5258        $this->FormValidator->reseterrorlist();
    5359        $this->assertFalse($this->FormValidator->anyerrors());
     
    5662    function test_printerrormessages()
    5763    {
    58         $this->FormValidator->addError('name', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     64        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
    5965        ob_start();
    6066        $this->FormValidator->printerrormessages();
     
    6571    function test_err()
    6672    {
    67         $this->FormValidator->addError('name', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
     73        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
    6874        ob_start();
    69         $this->FormValidator->err('name', 'printthis');
     75        $this->FormValidator->err('some_field', 'printthis');
    7076        $result = ob_get_clean();
    7177        $this->assertContains('printthis', $result);
     
    7480    function test_notempty()
    7581    {
    76         $this->FormValidator->isEmpty('name', _("Error message"));
     82        // This should not generate any error.
     83        $_POST['some_field'] = 'a non empty string';
     84        $this->FormValidator->isempty('some_field', _("Error message"));
     85        $this->assertFalse($this->FormValidator->anyerrors());
     86       
     87        // This one is an error!
     88        $_POST['some_field'] = '';
     89        $this->FormValidator->isempty('some_field', _("Error message"));
     90        $this->assertTrue($this->FormValidator->anyerrors());
    7791    }
    7892
    7993    function test_isempty()
    8094    {
    81         $result = $this->FormValidator->isempty('name', _("Error message"));
     95        // This should not generate any error.
     96        $_POST['some_field'] = 'a non empty string';
     97        $this->FormValidator->isempty('some_field', _("Error message"));
     98        $this->assertFalse($this->FormValidator->anyerrors());
     99       
     100        // This one is an error!
     101        $_POST['some_field'] = '';
     102        $this->FormValidator->isempty('some_field', _("Error message"));
     103        $this->assertTrue($this->FormValidator->anyerrors());
    82104    }
    83105
    84106    function test_isstring()
    85107    {
    86         $result = $this->FormValidator->isstring('name', _("Error message"));
     108        // This should not generate any error.
     109        $_POST['some_field'] = 'this is a string';
     110        $this->FormValidator->isstring('some_field', _("Error message"));
     111        $this->assertFalse($this->FormValidator->anyerrors());
     112       
     113        // This one is an error!
     114        $_POST['some_field'] = 12.3248; // not a string.
     115        $this->FormValidator->isstring('some_field', _("Error message"));
     116        $this->assertTrue($this->FormValidator->anyerrors());
    87117    }
    88118
    89119    function test_isnumber()
    90120    {
    91         $result = $this->FormValidator->isnumber('name', _("Error message"));
     121        // This should not generate any error.
     122        $_POST['some_field'] = '1234.453';
     123        $this->FormValidator->isnumber('some_field', _("Error message"));
     124        $this->assertFalse($this->FormValidator->anyerrors());
     125       
     126        // This one is an error!
     127        $_POST['some_field'] = 'not a number';
     128        $this->FormValidator->isnumber('some_field', _("Error message"));
     129        $this->assertTrue($this->FormValidator->anyerrors());
    92130    }
    93131
    94132    function test_isinteger()
    95133    {
    96         $result = $this->FormValidator->isinteger('name', _("Error message"));
     134        // This should not generate any error.
     135        $_POST['some_field'] = '1234';
     136        $this->FormValidator->isinteger('some_field', _("Error message"));
     137        $this->assertFalse($this->FormValidator->anyerrors());
     138       
     139        // This one is an error!
     140        $_POST['some_field'] = '1234.1';
     141        $this->FormValidator->isinteger('some_field', _("Error message"));
     142        $this->assertTrue($this->FormValidator->anyerrors());
    97143    }
    98144
    99145    function test_isfloat()
    100146    {
    101         $result = $this->FormValidator->isfloat('name', _("Error message"));
     147        // This should not generate any error.
     148        $_POST['some_field'] = '123.1234';
     149        $this->FormValidator->isfloat('some_field', _("Error message"));
     150        $this->assertFalse($this->FormValidator->anyerrors());
     151       
     152        // This one is an error!
     153        $_POST['some_field'] = 'some falsity';
     154        $this->FormValidator->isfloat('some_field', _("Error message"));
     155        $this->assertTrue($this->FormValidator->anyerrors());
    102156    }
    103157
    104158    function test_isarray()
    105159    {
    106         $result = $this->FormValidator->isarray('name', _("Error message"));
     160        // This should not generate any error.
     161        $_POST['some_field'] = array('asdf', 123);
     162        $this->FormValidator->isarray('some_field', _("Error message"));
     163        $this->assertFalse($this->FormValidator->anyerrors());
     164       
     165        // This one is an error!
     166        $_POST['some_field'] = 'some falsity';
     167        $this->FormValidator->isarray('some_field', _("Error message"));
     168        $this->assertTrue($this->FormValidator->anyerrors());
    107169    }
    108170
    109171    function test_checkregex()
    110172    {
    111         $result = $this->FormValidator->checkregex('name', '/\w/', true, _("Error message"));
     173        // This should not generate any error.
     174        $_POST['some_field'] = '1234abcd';
     175        $this->FormValidator->checkregex('some_field', '/\d{4}[a-d]{4}/', true, _("Error message"));
     176        $this->assertFalse($this->FormValidator->anyerrors());
     177       
     178        // This should not generate any error.
     179        $_POST['some_field'] = 'no digits here';
     180        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
     181        $this->assertFalse($this->FormValidator->anyerrors());
     182       
     183        // This one is an error!
     184        $_POST['some_field'] = 'oops, a d1git';
     185        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
     186        $this->assertTrue($this->FormValidator->anyerrors());
    112187    }
    113188
    114189    function test_stringlength()
    115190    {
    116         $this->FormValidator->stringLength('name', 0, 255, _("Error message"));
     191        // This should not generate any error.
     192        $_POST['some_field'] = 'some truth';
     193        $this->FormValidator->stringLength('some_field', 0, 255, _("Error message"));
     194        $this->assertFalse($this->FormValidator->anyerrors());
     195       
     196        // This one is an error!
     197        $_POST['some_field'] = 'some falsity';
     198        $this->FormValidator->stringLength('some_field', 0, 4, _("Error message"));
     199        $this->assertTrue($this->FormValidator->anyerrors());
    117200    }
    118201
    119202    function test_numericrange()
    120203    {
    121         $this->FormValidator->numericrange('name', 0, 255, _("Error message"));
     204        // This should not generate any error.
     205        $_POST['some_field'] = '12';
     206        $this->FormValidator->numericrange('some_field', 3, 22, _("Error message"));
     207        $this->assertFalse($this->FormValidator->anyerrors());
     208       
     209        // This one is an error!
     210        $_POST['some_field'] = '12';
     211        $this->FormValidator->numericrange('some_field', 300, 2200, _("Error message"));
     212        $this->assertTrue($this->FormValidator->anyerrors());
    122213    }
    123214
    124215    function test_validateemail()
    125216    {
    126         $this->FormValidator->validateemail('name');
     217        // This should not generate any error.
     218        $_POST['some_field'] = 'Quinn the Kook <quinn@strangecode.com>';
     219        $this->FormValidator->validateemail('some_field', _("Error message"));
     220        $this->assertFalse($this->FormValidator->anyerrors());
     221               
     222        // This one is an error!
     223        $_POST['some_field'] = 'quinn@kook.com.';
     224        $this->FormValidator->validateemail('some_field', _("Error message"));
     225        $this->assertTrue($this->FormValidator->anyerrors());
    127226    }
    128227
    129228    function test_validatephone()
    130229    {
    131         $this->FormValidator->validatephone('name');
     230        // This should not generate any error.
     231        $_POST['some_field'] = '+1 (530) 555-1212';
     232        $this->FormValidator->validatephone('some_field', _("Error message"));
     233        $this->assertFalse($this->FormValidator->anyerrors());
     234       
     235        // This one is an error!
     236        $_POST['some_field'] = 'd321/654*9875 ';
     237        $this->FormValidator->validatephone('some_field', _("Error message"));
     238        $this->assertTrue($this->FormValidator->anyerrors());
    132239    }
    133240
    134241    function test_validatestrdate()
    135242    {
    136         $this->FormValidator->validatestrdate('name');
     243        // This should not generate any error.
     244        $_POST['some_field'] = 'next tuesday';
     245        $this->FormValidator->validatestrdate('some_field', _("Error message"));
     246        $this->assertFalse($this->FormValidator->anyerrors());
     247       
     248        // This one is an error!
     249        $_POST['some_field'] = 'in a galaxy far far away';
     250        $this->FormValidator->validatestrdate('some_field', _("Error message"));
     251        $this->assertTrue($this->FormValidator->anyerrors());
    137252    }
    138253
    139254    function test_validateccnumber()
    140255    {
    141         $this->FormValidator->validateccnumber('name');
    142     }
    143 
    144     function test_validatefile()
    145     {
    146         $this->FormValidator->validatefile('name');
     256        // This should not generate any error.
     257        $_POST['some_field'] = '2323-2005-7766-3554';
     258        $this->FormValidator->validateccnumber('some_field', null);
     259        $this->assertFalse($this->FormValidator->anyerrors());
     260       
     261        // This one is an error!
     262        $_POST['some_field'] = '1234 1234 1234 1234';
     263        $this->FormValidator->validateccnumber('some_field', null);
     264        $this->assertTrue($this->FormValidator->anyerrors());
     265    }
     266
     267    function test_fileuploaded()
     268    {
     269        // This one is an error!
     270        $this->FormValidator->fileUploaded('some_field', _("Error message"));
     271        $this->assertTrue($this->FormValidator->anyerrors());
    147272    }
    148273
Note: See TracChangeset for help on using the changeset viewer.