/tests/forms/DateFieldTest.php

https://github.com/sminnee/silverstripe-framework · PHP · 194 lines · 146 code · 35 blank · 13 comment · 2 complexity · e3bfa02a08ed0abcfbbbffcff4e58975 MD5 · raw file

  1. <?php
  2. /**
  3. * @package framework
  4. * @subpackage tests
  5. */
  6. class DateFieldTest extends SapphireTest {
  7. public function setUp() {
  8. parent::setUp();
  9. $this->originalLocale = i18n::get_locale();
  10. i18n::set_locale('en_NZ');
  11. $this->origDateFormat = DateField::$default_config['dateformat'];
  12. DateField::$default_config['dateformat'] = 'dd/MM/yyyy';
  13. }
  14. public function tearDown() {
  15. parent::tearDown();
  16. i18n::set_locale($this->originalLocale);
  17. DateField::$default_config['dateformat'] = $this->origDateFormat;
  18. }
  19. public function testValidateMinDate() {
  20. $f = new DateField('Date');
  21. $f->setConfig('min', '2009-03-31');
  22. $f->setValue('2010-03-31');
  23. $this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
  24. $f = new DateField('Date');
  25. $f->setConfig('min', '2009-03-31');
  26. $f->setValue('1999-03-31');
  27. $this->assertFalse($f->validate(new RequiredFields()), 'Date below min date');
  28. $f = new DateField('Date');
  29. $f->setConfig('min', '2009-03-31');
  30. $f->setValue('2009-03-31');
  31. $this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date');
  32. }
  33. public function testValidateMinDateStrtotime() {
  34. $f = new DateField('Date');
  35. $f->setConfig('min', '-7 days');
  36. $f->setValue(strftime('%Y-%m-%d', strtotime('-8 days')));
  37. $this->assertFalse($f->validate(new RequiredFields()), 'Date below min date, with strtotime');
  38. $f = new DateField('Date');
  39. $f->setConfig('min', '-7 days');
  40. $f->setValue(strftime('%Y-%m-%d', strtotime('-7 days')));
  41. $this->assertTrue($f->validate(new RequiredFields()), 'Date matching min date, with strtotime');
  42. }
  43. public function testValidateMaxDateStrtotime() {
  44. $f = new DateField('Date');
  45. $f->setConfig('max', '7 days');
  46. $f->setValue(strftime('%Y-%m-%d', strtotime('8 days')));
  47. $this->assertFalse($f->validate(new RequiredFields()), 'Date above max date, with strtotime');
  48. $f = new DateField('Date');
  49. $f->setConfig('max', '7 days');
  50. $f->setValue(strftime('%Y-%m-%d', strtotime('7 days')));
  51. $this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date, with strtotime');
  52. }
  53. public function testValidateMaxDate() {
  54. $f = new DateField('Date');
  55. $f->setConfig('max', '2009-03-31');
  56. $f->setValue('1999-03-31');
  57. $this->assertTrue($f->validate(new RequiredFields()), 'Date above min date');
  58. $f = new DateField('Date');
  59. $f->setConfig('max', '2009-03-31');
  60. $f->setValue('2010-03-31');
  61. $this->assertFalse($f->validate(new RequiredFields()), 'Date above max date');
  62. $f = new DateField('Date');
  63. $f->setConfig('max', '2009-03-31');
  64. $f->setValue('2009-03-31');
  65. $this->assertTrue($f->validate(new RequiredFields()), 'Date matching max date');
  66. }
  67. public function testConstructorWithoutArgs() {
  68. $f = new DateField('Date');
  69. $this->assertEquals($f->dataValue(), null);
  70. }
  71. public function testConstructorWithDateString() {
  72. $f = new DateField('Date', 'Date', '29/03/2003');
  73. $this->assertEquals($f->dataValue(), '2003-03-29');
  74. }
  75. public function testSetValueWithDateString() {
  76. $f = new DateField('Date', 'Date');
  77. $f->setValue('29/03/2003');
  78. $this->assertEquals($f->dataValue(), '2003-03-29');
  79. }
  80. public function testSetValueWithDateArray() {
  81. $f = new DateField('Date', 'Date');
  82. $f->setConfig('dmyfields', true);
  83. $f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003));
  84. $this->assertEquals($f->dataValue(), '2003-03-29');
  85. }
  86. public function testConstructorWithIsoDate() {
  87. // used by Form->loadDataFrom()
  88. $f = new DateField('Date', 'Date', '2003-03-29');
  89. $this->assertEquals($f->dataValue(), '2003-03-29');
  90. }
  91. public function testValidateDMY() {
  92. $f = new DateField('Date', 'Date', '29/03/2003');
  93. $this->assertTrue($f->validate(new RequiredFields()));
  94. $f = new DateField('Date', 'Date', 'wrong');
  95. $this->assertFalse($f->validate(new RequiredFields()));
  96. }
  97. public function testValidateArray() {
  98. $f = new DateField('Date', 'Date');
  99. $f->setConfig('dmyfields', true);
  100. $f->setValue(array('day' => 29, 'month' => 03, 'year' => 2003));
  101. $this->assertTrue($f->validate(new RequiredFields()));
  102. $f->setValue(null);
  103. $this->assertTrue($f->validate(new RequiredFields()), 'NULL values are validating TRUE');
  104. $f->setValue(array());
  105. $this->assertTrue($f->validate(new RequiredFields()), 'Empty array values are validating TRUE');
  106. $f->setValue(array('day' => null, 'month' => null, 'year' => null));
  107. $this->assertTrue($f->validate(new RequiredFields()), 'Empty array values with keys are validating TRUE');
  108. // TODO Fix array validation
  109. // $f = new DateField('Date', 'Date', array('day' => 9999, 'month' => 9999, 'year' => 9999));
  110. // $this->assertFalse($f->validate(new RequiredFields()));
  111. }
  112. public function testValidateEmptyArrayValuesSetsNullForValueObject() {
  113. $f = new DateField('Date', 'Date');
  114. $f->setConfig('dmyfields', true);
  115. $f->setValue(array('day' => '', 'month' => '', 'year' => ''));
  116. $this->assertNull($f->dataValue());
  117. $f->setValue(array('day' => null, 'month' => null, 'year' => null));
  118. $this->assertNull($f->dataValue());
  119. }
  120. public function testValidateArrayValue() {
  121. $f = new DateField('Date', 'Date');
  122. $this->assertTrue($f->validateArrayValue(array('day' => 29, 'month' => 03, 'year' => 2003)));
  123. $this->assertFalse($f->validateArrayValue(array('month' => 03, 'year' => 2003)));
  124. $this->assertFalse($f->validateArrayValue(array('day' => 99, 'month' => 99, 'year' => 2003)));
  125. }
  126. public function testFormatEnNz() {
  127. /* We get YYYY-MM-DD format as the data value for DD/MM/YYYY input value */
  128. $f = new DateField('Date', 'Date', '29/03/2003');
  129. $this->assertEquals($f->dataValue(), '2003-03-29');
  130. }
  131. public function testSetLocale() {
  132. // should get en_NZ by default through setUp()
  133. $f = new DateField('Date', 'Date', '29/03/2003');
  134. $f->setLocale('de_DE');
  135. $f->setValue('29.06.2006');
  136. $this->assertEquals($f->dataValue(), '2006-06-29');
  137. }
  138. /**
  139. * Note: This is mostly tested for legacy reasons
  140. */
  141. public function testMDYFormat() {
  142. $dateField = new DateField('Date', 'Date');
  143. $dateField->setConfig('dateformat', 'd/M/Y');
  144. $dateField->setValue('31/03/2003');
  145. $this->assertEquals(
  146. $dateField->dataValue(),
  147. '2003-03-31',
  148. "We get MM-DD-YYYY format as the data value for YYYY-MM-DD input value"
  149. );
  150. $dateField2 = new DateField('Date', 'Date');
  151. $dateField2->setConfig('dateformat', 'd/M/Y');
  152. $dateField2->setValue('04/3/03');
  153. $this->assertEquals(
  154. $dateField2->dataValue(),
  155. '2003-03-04',
  156. "Even if input value hasn't got leading 0's in it we still get the correct data value"
  157. );
  158. }
  159. }