Completed
Push — master ( a45657...918fb9 )
by Daniel
13:32
created

DateFieldTest::testValidateArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package framework
4
 * @subpackage tests
5
 */
6
class DateFieldTest extends SapphireTest {
7
8
	public function setUp() {
9
		parent::setUp();
10
11
		$this->originalLocale = i18n::get_locale();
0 ignored issues
show
Bug introduced by
The property originalLocale does not seem to exist. Did you mean locale?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
12
		i18n::set_locale('en_NZ');
13
		$this->origConfig = Config::inst()->get('DateField', 'default_config');
0 ignored issues
show
Bug introduced by
The property origConfig does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
14
		Config::inst()->update('DateField', 'default_config', array('dateformat' => 'dd/MM/yyyy'));
15
	}
16
17
	public function tearDown() {
18
		parent::tearDown();
19
20
		i18n::set_locale($this->originalLocale);
0 ignored issues
show
Bug introduced by
The property originalLocale does not seem to exist. Did you mean locale?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
21
		Config::inst()->remove('DateField', 'default_config');
22
		Config::inst()->update('DateField', 'default_config', $this->origConfig);
23
	}
24
25
	public function testValidateMinDate() {
26
		$f = new DateField('Date');
27
		$f->setConfig('min', '2009-03-31');
28
		$f->setValue('2010-03-31');
29
		$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
30
31
		$f = new DateField('Date');
32
		$f->setConfig('min', '2009-03-31');
33
		$f->setValue('1999-03-31');
34
		$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date');
35
36
		$f = new DateField('Date');
37
		$f->setConfig('min', '2009-03-31');
38
		$f->setValue('2009-03-31');
39
		$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date');
40
	}
41
42
	public function testValidateMinDateStrtotime() {
43
		$f = new DateField('Date');
44
		$f->setConfig('min', '-7 days');
45
		$f->setValue(strftime('%Y-%m-%d', strtotime('-8 days')));
46
		$this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime');
47
48
		$f = new DateField('Date');
49
		$f->setConfig('min', '-7 days');
50
		$f->setValue(strftime('%Y-%m-%d', strtotime('-7 days')));
51
		$this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime');
52
	}
53
54
	public function testValidateMaxDateStrtotime() {
55
		$f = new DateField('Date');
56
		$f->setConfig('max', '7 days');
57
		$f->setValue(strftime('%Y-%m-%d', strtotime('8 days')));
58
		$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime');
59
60
		$f = new DateField('Date');
61
		$f->setConfig('max', '7 days');
62
		$f->setValue(strftime('%Y-%m-%d', strtotime('7 days')));
63
		$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime');
64
	}
65
66
	public function testValidateMaxDate() {
67
		$f = new DateField('Date');
68
		$f->setConfig('max', '2009-03-31');
69
		$f->setValue('1999-03-31');
70
		$this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
71
72
		$f = new DateField('Date');
73
		$f->setConfig('max', '2009-03-31');
74
		$f->setValue('2010-03-31');
75
		$this->assertFalse($f->validate(new RequiredFields()), 'Date above max date');
76
77
		$f = new DateField('Date');
78
		$f->setConfig('max', '2009-03-31');
79
		$f->setValue('2009-03-31');
80
		$this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date');
81
	}
82
83
	public function testConstructorWithoutArgs() {
84
		$f = new DateField('Date');
85
		$this->assertEquals($f->dataValue(), null);
86
	}
87
88
	public function testConstructorWithDateString() {
89
		$f = new DateField('Date', 'Date', '29/03/2003');
90
		$this->assertEquals($f->dataValue(), '2003-03-29');
91
	}
92
93
	public function testSetValueWithDateString() {
94
		$f = new DateField('Date', 'Date');
95
		$f->setValue('29/03/2003');
96
		$this->assertEquals($f->dataValue(), '2003-03-29');
97
	}
98
99
	public function testSetValueWithDateArray() {
100
		$f = new DateField('Date', 'Date');
101
		$f->setConfig('dmyfields', true);
102
		$f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003));
103
		$this->assertEquals($f->dataValue(), '2003-03-29');
104
	}
105
106
	public function testConstructorWithIsoDate() {
107
		// used by Form->loadDataFrom()
108
		$f = new DateField('Date', 'Date', '2003-03-29');
109
		$this->assertEquals($f->dataValue(), '2003-03-29');
110
	}
111
112
	public function testValidateDMY() {
113
		$f = new DateField('Date', 'Date', '29/03/2003');
114
		$this->assertTrue($f->validate(new RequiredFields()));
115
116
		$f = new DateField('Date', 'Date', 'wrong');
117
		$this->assertFalse($f->validate(new RequiredFields()));
118
119
	}
120
121
	public function testEmptyValueValidation() {
122
		$field = new DateField('Date');
123
		$validator = new RequiredFields();
124
		$this->assertTrue($field->validate($validator));
125
		$field->setConfig('dmyfields', true);
126
		$this->assertTrue($field->validate($validator));
127
		$field->setValue(array(
128
			'day' => '',
129
			'month' => '',
130
			'year' => '',
131
		));
132
		$this->assertTrue($field->validate($validator));
133
	}
134
135
	public function testValidateArray() {
136
		$f = new DateField('Date', 'Date');
137
		$f->setConfig('dmyfields', true);
138
		$f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003));
139
		$this->assertTrue($f->validate(new RequiredFields()));
140
141
		$f->setValue(null);
142
		$this->assertTrue($f->validate(new RequiredFields()), 'NULL values are validating TRUE');
143
144
		$f->setValue(array());
145
		$this->assertTrue($f->validate(new RequiredFields()), 'Empty array values are validating TRUE');
146
147
		$f->setValue(array('day' => null, 'month' => null, 'year' => null));
148
		$this->assertTrue($f->validate(new RequiredFields()), 'Empty array values with keys are validating TRUE');
149
150
		// TODO Fix array validation
151
		// $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999));
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
152
		// $this->assertFalse($f->validate(new RequiredFields()));
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
153
	}
154
155
	public function testValidateEmptyArrayValuesSetsNullForValueObject() {
156
		$f = new DateField('Date', 'Date');
157
		$f->setConfig('dmyfields', true);
158
159
		$f->setValue(array('day' => '', 'month' => '', 'year' => ''));
160
		$this->assertNull($f->dataValue());
161
162
		$f->setValue(array('day' => null, 'month' => null, 'year' => null));
163
		$this->assertNull($f->dataValue());
164
	}
165
166
	public function testValidateArrayValue() {
167
		$f = new DateField('Date', 'Date');
168
		$this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003)));
169
		$this->assertFalse($f->validateArrayValue(array('month' => 03, 'year' => 2003)));
170
		$this->assertFalse($f->validateArrayValue(array('day' => 99, 'month' => 99, 'year' => 2003)));
171
	}
172
173
	public function testFormatEnNz() {
174
		/* We get YYYY-MM-DD format as the data value for DD/MM/YYYY input value */
175
		$f = new DateField('Date', 'Date', '29/03/2003');
176
		$this->assertEquals($f->dataValue(), '2003-03-29');
177
	}
178
179
	public function testSetLocale() {
180
		// should get en_NZ by default through setUp()
181
		$f = new DateField('Date', 'Date', '29/03/2003');
182
		$f->setLocale('de_DE');
183
		$f->setValue('29.06.2006');
184
		$this->assertEquals($f->dataValue(), '2006-06-29');
185
	}
186
187
	/**
188
	 * Note: This is mostly tested for legacy reasons
189
	 */
190
	public function testMDYFormat() {
191
		$dateField = new DateField('Date', 'Date');
192
		$dateField->setConfig('dateformat', 'd/M/Y');
193
		$dateField->setValue('31/03/2003');
194
		$this->assertEquals(
195
			$dateField->dataValue(),
196
			'2003-03-31',
197
			"We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value"
198
		);
199
200
		$dateField2 = new DateField('Date', 'Date');
201
		$dateField2->setConfig('dateformat', 'd/M/Y');
202
		$dateField2->setValue('04/3/03');
203
		$this->assertEquals(
204
			$dateField2->dataValue(),
205
			'2003-03-04',
206
			"Even if input value hasn't got leading 0's in it we still get the correct data value"
207
		);
208
	}
209
210
}
211