/** * @covers Jam_Validator_Rule_Purchase_Promocode::valid_promo_code */ public function test_valid_promo_code() { $purchase = Jam::find('purchase', 1); $promo_code1 = Jam::find('promo_code', 1); $purchase->update_fields(array('promo_code_id' => $promo_code1->get('id'))); $promo_code2 = Jam::find('promo_code', 2); $promo_code3 = Jam::find('promo_code', 3); $validator_rule = Jam::validator_rule('purchase_promocode'); // Invalid code $result = $validator_rule->valid_promo_code('asd', $purchase); $this->assertNull($result); // Already used promocode $result = $validator_rule->valid_promo_code($promo_code1->get('code'), $purchase); $this->assertEquals($promo_code1, $result); // Available promocode $result = $validator_rule->valid_promo_code($promo_code2->get('code'), $purchase); $this->assertEquals($promo_code2, $result); // Check already used promocode Jam::find('purchase', 2)->set('promo_code', $promo_code2)->save(); $result = $validator_rule->valid_promo_code($promo_code2->get('code'), $purchase); $this->assertNull($result, 'Already used promocode'); // Check already used allow_multiple Jam::find('purchase', 2)->set('promo_code', $promo_code3)->save(); $result = $validator_rule->valid_promo_code($promo_code3->get('code'), $purchase); $this->assertEquals($promo_code3, $result, 'Should match because its an allow_multiple=TRUE promocode'); }
/** * @dataProvider data_validate */ public function test_validate($value, $options, $error, $is_valid) { $element = Jam::build('test_element'); Jam::validator_rule('count', $options)->validate($element, 'count', $value); if ($is_valid) { $this->assertNotHasError($element, 'count', $error); } else { $this->assertHasError($element, 'count', $error); } }
/** * @dataProvider data_validate */ public function test_validate($value, $accept, $is_valid) { $element = Jam::build('test_element'); Jam::validator_rule('accepted', array('accept' => $accept))->validate($element, 'name', $value); if ($is_valid) { $this->assertNotHasError($element, 'name', 'accepted'); } else { $this->assertHasError($element, 'name', 'accepted'); } }
public function test_validate() { $element = Jam::find('test_element', 1); Jam::validator_rule('unique', array())->validate($element, 'name', $element->name); $this->assertNotHasError($element, 'name', 'unique'); $element2 = Jam::find('test_element', 2); Jam::validator_rule('unique', array())->validate($element2, 'url', $element2->url); $this->assertHasError($element2, 'url', 'unique'); $element3 = Jam::find('test_element', 2); Jam::validator_rule('unique', array('scope' => 'name'))->validate($element3, 'url', $element3->url); $this->assertNotHasError($element3, 'url', 'unique'); }
/** * @dataProvider data_validate */ public function test_validate($value, $options, $error, $expected_attributes, $is_valid) { $element = Jam::build('test_element'); $validator_rule = Jam::validator_rule('format', $options); $this->assertEquals($expected_attributes, $validator_rule->html5_validation()); $validator_rule->validate($element, 'name', $value); if ($is_valid) { $this->assertNotHasError($element, 'name', $error); } else { $this->assertHasError($element, 'name', $error); } }
/** * @dataProvider data_validate */ public function test_validate($value, $allow_zero, $is_valid) { $element = Jam::build('test_element'); $validator_rule = Jam::validator_rule('present'); $validator_rule->allow_zero = $allow_zero; $this->assertEquals(array('required' => TRUE), $validator_rule->html5_validation()); $validator_rule->validate($element, 'url', $value); if ($is_valid) { $this->assertNotHasError($element, 'url', 'present'); } else { $this->assertHasError($element, 'url', 'present'); } }
public function test_validate() { $element1 = Jam::build('test_element')->load_fields(array('id' => 1, 'name' => 'Part 1')); Jam::validator_rule('confirmed', array())->validate($element1, 'name', $element1->name); $this->assertNotHasError($element1, 'name', 'confirmed'); $element2 = Jam::build('test_element')->load_fields(array('id' => 2, 'name' => 'Part 2')); $element2->name_confirmation = 'test'; Jam::validator_rule('confirmed', array())->validate($element2, 'name', $element2->name); $this->assertHasError($element2, 'name', 'confirmed'); $element3 = Jam::build('test_element')->load_fields(array('id' => 2, 'name' => 'Part 2')); $element3->name_confirmation = $element3->name; Jam::validator_rule('confirmed', array())->validate($element3, 'name', $element3->name); $this->assertNotHasError($element3, 'name', 'confirmed'); }
/** * @dataProvider data_validate */ public function test_validate($value, $options, $errors, $is_valid) { $element = Jam::build('test_element'); $validator_rule = Jam::validator_rule('range', $options); $validator_rule->validate($element, 'name', new Jam_Range($value)); if ($is_valid) { foreach ((array) $errors as $error) { $this->assertNotHasError($element, 'name', $error); } } else { foreach ((array) $errors as $error) { $this->assertHasError($element, 'name', $error); } } }
function __construct(array $attributes, array $options) { $this->attributes = $attributes; if (isset($options['if'])) { $this->condition = $options['if']; unset($options['if']); } if (isset($options['unless'])) { $this->condition = $options['unless']; $this->condition_negative = TRUE; unset($options['unless']); } foreach ($options as $rule => $params) { $this->rules[] = $params instanceof Jam_Validator_Rule ? $params : Jam::validator_rule($rule, $params); } }
public function data_constructor() { return array(array(array(), array(), array(), NULL, FALSE, array()), array(array('abc'), array(), array('abc'), NULL, FALSE, array()), array(array('abc'), array('present' => TRUE), array('abc'), NULL, FALSE, array(Jam::validator_rule('present'))), array(array('abc'), array('if' => 'abc'), array('abc'), 'abc', FALSE, array()), array(array('abc'), array('unless' => 'abc'), array('abc'), 'abc', TRUE, array()), array(array('abc'), array('unless' => 'abc', 'present' => TRUE, 'format' => array('email' => TRUE)), array('abc'), 'abc', TRUE, array(Jam::validator_rule('present'), Jam::validator_rule('format', array('email' => TRUE)))), array(array('abc', 'qwe'), array('unless' => 'qwe', 'if' => 'abc', 'present' => TRUE, 'format' => array('email' => TRUE)), array('abc', 'qwe'), 'qwe', TRUE, array(Jam::validator_rule('present'), Jam::validator_rule('format', array('email' => TRUE)))), array(array('abc', 'qwe'), array('unless' => 'qwe', 'if' => 'abc', 'present' => TRUE, Jam::validator_rule('numeric', array('greater_than' => 5)), 'format' => array('email' => TRUE)), array('abc', 'qwe'), 'qwe', TRUE, array(Jam::validator_rule('present'), Jam::validator_rule('numeric', array('greater_than' => 5)), Jam::validator_rule('format', array('email' => TRUE))))); }
public function test_multiple_rules() { // Logo is 127 x 34 $this->value->source(Upload_Util::combine($this->test_local, 'source', 'logo.gif'))->save_to_temp(); Jam::validator_rule('uploaded', array('only' => array('png'), 'maximum_size' => '1B', 'exact_height' => 20))->validate($this->model, 'file', $this->value); $this->assertHasError($this->model, 'file', 'uploaded_extension'); $this->assertHasError($this->model, 'file', 'uploaded_maximum_size'); $this->assertHasError($this->model, 'file', 'uploaded_exact_height'); }