123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685 |
- <?php
- /**
- * Part of the Fuel framework.
- *
- * @package Fuel
- * @version 1.5
- * @author Fuel Development Team
- * @license MIT License
- * @copyright 2010 - 2013 Fuel Development Team
- * @link http://fuelphp.com
- */
- namespace Fuel\Core;
- /**
- * Validation class tests
- *
- * @group Core
- * @group Validation
- */
- class Test_Validation extends TestCase
- {
- public static function form_provider()
- {
- return array(
- array(
- array(
- 'foo' => 'bar',
- 'bar' => 'foo',
- 'boo' => 'bar',
- 'cat' => '',
- 'dog' => '245abc',
- 'php' => 1,
- 'sky' => 'blue',
- 'one' => '[email protected]',
- 'two' => '[email protected], [email protected]',
- 'owt' => '[email protected]; [email protected]',
- 'six' => 6,
- 'ten' => 10,
- 'dns' => '127.0.0.1',
- 'snd' => '127.0.0',
- 'url' => 'http://www.google.com',
- )
- )
- );
- }
- /**
- * Validation: required
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_required_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('foo', 'Foo', 'required');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: required
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_required_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('cat', 'Cat', 'required');
- $val->run($input);
- $output = $val->error('cat', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_value
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_value_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('foo', 'Foo', 'match_value[bar]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_value
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_value_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('foo', 'Foo', "match_value['foo']");
- $val->run($input);
- $output = $val->error('foo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_value (strict)
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_value_strict_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('php', 'PHP', '')->add_rule('match_value', 1, 1);
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_value (strict)
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_value_strict_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('php', 'PHP', '')->add_rule('match_value', '1', 1);
- $val->run($input);
- $output = $val->error('php', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_pattern
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_pattern_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('dog', 'Dog', '')->add_rule('match_pattern', '/\d{3}\w{3}/');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_pattern
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_pattern_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('dog', 'Dog', '')->add_rule('match_pattern', '/^\d{2}[abc]{3}/');
- $val->run($input);
- $output = $val->error('dog', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_field
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_field_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('foo', 'Foo', 'valid_string');
- $val->add_field('boo', 'Boo', 'match_field[foo]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: match_field
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_match_field_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('bar', 'Bar', 'valid_string');
- $val->add_field('boo', 'Boo', 'match_field[bar]');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: min_length
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_min_length_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Boo', 'min_length[2]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: min_length
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_min_length_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Boo', 'min_length[4]');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: max_length
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_max_length_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Boo', 'max_length[4]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: max_length
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_max_length_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Boo', 'max_length[2]');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: exact_length
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_exact_length_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Boo', 'exact_length[3]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: exact_length
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_exact_length_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Boo', 'max_length[2]');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_email
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_email_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('one', 'Email', 'valid_email');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_email
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_email_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Email', 'valid_email');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_emails
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_emails_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('two', 'Emails', 'valid_emails');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_emails (different separator)
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_emails_separator_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add('owt', 'Emails')->add_rule('valid_emails', ';');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_emails
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_emails_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Email', 'valid_emails');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_url
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_url_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('url', 'Url', 'valid_url');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_url
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_url_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('boo', 'Url', 'valid_url');
- $val->run($input);
- $output = $val->error('boo', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_ip
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_ip_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('dns', 'IP', 'valid_ip');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_ip
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_valid_ip_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('snd', 'IP', 'valid_ip');
- $val->run($input);
- $output = $val->error('snd', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: numeric_min
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_numeric_min_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('six', 'Number', 'numeric_min[4]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: numeric_min
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_numeric_min_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('six', 'Number', 'numeric_min[8]');
- $val->run($input);
- $output = $val->error('six', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: numeric_max
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_numeric_max_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('ten', 'Number', 'numeric_max[11]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: numeric_max
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_numeric_max_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('ten', 'Number', 'numeric_max[8]');
- $val->run($input);
- $output = $val->error('ten', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: numeric_between
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_numeric_between_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('ten', 'Number', 'numeric_between[9,11]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: numeric_between
- * Expecting: failure
- *
- * @dataProvider form_provider
- */
- public function test_validation_numeric_between_failure($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('ten', 'Number', 'numeric_between[7,8]');
- $val->run($input);
- $output = $val->error('ten', false) ? true : false;
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: required_with
- * Expecting: success
- *
- * @dataProvider form_provider
- */
- public function test_validation_required_with_success($input)
- {
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('foo', 'Foo', 'valid_string');
- $val->add_field('bar', 'Bar', 'required_with[foo]');
- $output = $val->run($input);
- $expected = true;
- $this->assertEquals($expected, $output);
- }
- /**
- * Validation: valid_string numeric
- * Expecting: success
- */
- public function test_validation_valid_string_numeric_success()
- {
- $post = array(
- 'f1' => '123456',
- );
- $val = Validation::forge(__FUNCTION__);
- $val->add('f1', 'F1')->add_rule('valid_string', 'numeric');
- $test = $val->run($post);
- $expected = true;
- $this->assertEquals($expected, $test);
- }
- /**
- * Validation: valid_string numeric
- * Expecting: failure
- */
- public function test_validation_valid_string_numeric_failure()
- {
- $post = array(
- 'f1' => 'a123456',
- );
- $val = Validation::forge(__FUNCTION__);
- $val->add('f1', 'F1')->add_rule('valid_string', 'numeric');
- $test = $val->run($post);
- $expected = false;
- $this->assertEquals($expected, $test);
- }
- /**
- * Validation: valid_string alpha,numeric
- * Expecting: failure
- */
- public function test_validation_valid_string_multiple_flags_error_message_add_rule() {
- $post = array(
- 'f1' => '123 abc',
- );
- $val = Validation::forge(__FUNCTION__);
- $val->add('f1', 'F1')->add_rule('valid_string', array('alpha', 'numeric'));
- $val->set_message('valid_string', 'The valid string rule :rule(:param:1) failed for field :label');
- $val->run($post);
- $test = $val->error('f1')->get_message();
- $expected = 'The valid string rule valid_string(alpha, numeric) failed for field F1';
- $this->assertEquals($expected, $test);
- }
- /**
- * Validation: valid_string alpha,numeric
- * Expecting: failure
- */
- public function test_validation_valid_string_multiple_flags_error_message_add_field() {
- $post = array(
- 'f1' => '123 abc',
- );
- $val = Validation::forge(__FUNCTION__);
- $val->add_field('f1', 'F1', 'valid_string[alpha,numeric]');
- $val->set_message('valid_string', 'The valid string rule :rule(:param:1) failed for field :label');
- $val->run($post);
- $test = $val->error('f1')->get_message();
- $expected = 'The valid string rule valid_string(alpha, numeric) failed for field F1';
- $this->assertEquals($expected, $test);
- }
- }
|