Completed
Push — 3.2 ( 6e29d3...46cbe8 )
by Damian
12:12
created

ConfirmedPasswordFieldTest::testFormValidation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 19
rs 9.4285
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
/**
3
 * @package framework
4
 * @subpackage tests
5
 */
6
class ConfirmedPasswordFieldTest extends SapphireTest {
7
8
	public function testSetValue() {
9
		$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA');
10
		$this->assertEquals('valueA', $field->Value());
11
		$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
12
		$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
13
		$field->setValue('valueB');
14
		$this->assertEquals('valueB', $field->Value());
15
		$this->assertEquals('valueB', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
16
		$this->assertEquals('valueB', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
17
	}
18
19
	public function testHashHidden() {
20
		$field = new ConfirmedPasswordField('Password', 'Password', 'valueA');
21
		$field->setCanBeEmpty(true);
22
23
		$this->assertEquals('valueA', $field->Value());
24
		$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
25
		$this->assertEquals('valueA', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
26
27
		$member = new Member();
28
		$member->Password = "valueB";
29
		$member->write();
30
31
		$form = new Form($this, 'Form', new FieldList($field), new FieldList());
32
		$form->loadDataFrom($member);
33
34
		$this->assertEquals('', $field->Value());
35
		$this->assertEquals('', $field->children->fieldByName($field->getName() . '[_Password]')->Value());
36
		$this->assertEquals('', $field->children->fieldByName($field->getName() . '[_ConfirmPassword]')->Value());
37
	}
38
39
	public function testSetShowOnClick() {
40
		//hide by default and display show/hide toggle button
41
		$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA', null, true);
42
		$fieldHTML = $field->Field();
43
		$this->assertContains("showOnClickContainer", $fieldHTML,
44
			"Test class for hiding/showing the form contents is set");
45
		$this->assertContains("showOnClick", $fieldHTML,
46
			"Test class for hiding/showing the form contents is set");
47
48
		//show all by default
49
		$field = new ConfirmedPasswordField('Test', 'Testing', 'valueA', null, false);
50
		$fieldHTML = $field->Field();
51
		$this->assertNotContains("showOnClickContainer", $fieldHTML,
52
			"Test class for hiding/showing the form contents is set");
53
		$this->assertNotContains("showOnClick", $fieldHTML,
54
			"Test class for hiding/showing the form contents is set");
55
	}
56
57
	public function testValidation() {
58
		$field = new ConfirmedPasswordField('Test', 'Testing', array(
59
			"_Password" => "abc123",
60
			"_ConfirmPassword" => "abc123"
61
		));
62
		$validator = new RequiredFields();
63
		$form = new Form($this, 'Form', new FieldList($field), new FieldList(), $validator);
64
		$this->assertTrue(
65
			$field->validate($validator),
66
			"Validates when both passwords are the same"
67
		);
68
		$field->setName("TestNew"); //try changing name of field
69
		$this->assertTrue(
70
			$field->validate($validator),
71
			"Validates when field name is changed"
72
		);
73
		//non-matching password should make the field invalid
74
		$field->setValue(array(
75
			"_Password" => "abc123",
76
			"_ConfirmPassword" => "123abc"
77
		));
78
		$this->assertFalse(
79
			$field->validate($validator),
80
			"Does not validate when passwords differ"
81
		);
82
	}
83
	
84
    public function testFormValidation() {
85
        $form = new Form(
86
            new Controller(),
87
            'Form',
88
            new FieldList($field = new ConfirmedPasswordField('Password')),
89
            new FieldList()
90
        );
91
92
        $form->loadDataFrom(array(
93
            'Password' => array(
94
                '_Password' => '123',
95
                '_ConfirmPassword' => '999',
96
            )
97
        ));
98
99
        $this->assertEquals('123', $field->children->first()->Value());
100
        $this->assertEquals('999', $field->children->last()->Value());
101
        $this->assertNotEquals($field->children->first()->Value(), $field->children->last()->Value());
102
    }
103
104
}
105