Completed
Push — 3.2 ( 9dabf1...378749 )
by Damian
09:42
created

OptionsetFieldTest::testValidation()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 28
rs 8.8571
cc 1
eloc 17
nc 1
nop 0
1
<?php
2
/**
3
 * @package framework
4
 * @subpackage tests
5
 */
6
class OptionsetFieldTest extends SapphireTest {
7
	public function testSetDisabledItems() {
8
		$f = new OptionsetField(
9
			'Test',
10
			false,
11
			array(0 => 'Zero', 1 => 'One')
12
		);
13
14
		$f->setDisabledItems(array(0));
15
		$p = new CSSContentParser($f->Field());
16
		$item0 = $p->getBySelector('#Test_0');
17
		$item1 = $p->getBySelector('#Test_1');
18
		$this->assertEquals(
19
			(string)$item0[0]['disabled'],
20
			'disabled'
21
		);
22
		$this->assertEquals(
23
			(string)$item1[0]['disabled'],
24
			''
25
		);
26
	}
27
28
	public function testValidation() {
29
		$field = OptionsetField::create('Test', 'Testing', array(
30
			"One" => "One",
31
			"Two" => "Two",
32
			"Five" => "Five"
33
		));
34
		$validator = new RequiredFields('Test');
35
		$form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator);
36
37
		$field->setValue("One");
38
		$this->assertTrue($field->validate($validator));
39
40
		//non-existent value should make the field invalid
41
		$field->setValue("Three");
42
		$this->assertFalse($field->validate($validator));
43
44
		//empty string should pass field-level validation...
45
		$field->setValue('');
46
		$this->assertTrue($field->validate($validator));
47
48
		// ... but should not pass "RequiredFields" validation
49
		$this->assertFalse($form->validate());
50
51
		//disabled items shouldn't validate
52
		$field->setDisabledItems(array('Five'));
53
		$field->setValue('Five');
54
		$this->assertFalse($field->validate($validator));
55
	}
56
57
	public function testReadonlyField() {
58
		$sourceArray = array(0 => 'No', 1 => 'Yes');
59
		$field = new OptionsetField('FeelingOk', 'are you feeling ok?', $sourceArray, 1);
60
		$field->setEmptyString('(Select one)');
61
		$field->setValue(1);
62
		$readonlyField = $field->performReadonlyTransformation();
63
		preg_match('/Yes/', $field->Field(), $matches);
64
		$this->assertEquals($matches[0], 'Yes');
65
	}
66
}
67