/AegisBorn/lib/vendor/symfony-1.4.8/test/unit/widget/sfWidgetFormDateTest.php

https://github.com/aarontrett/AegisBorn · PHP · 142 lines · 100 code · 23 blank · 19 comment · 0 complexity · 10cb07d0ab1bdedc3952fe124598dfaa MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. require_once(dirname(__FILE__).'/../../bootstrap/unit.php');
  10. $t = new lime_test(43);
  11. $w = new sfWidgetFormDate();
  12. $dom = new DomDocument('1.0', 'utf-8');
  13. $dom->validateOnParse = true;
  14. // ->render()
  15. $t->diag('->render()');
  16. foreach (array(
  17. '2005-10-15' => array('year' => 2005, 'month' => 10, 'day' => 15),
  18. time() => array('year' => date('Y'), 'month' => date('m'), 'day' => date('d')),
  19. 'tomorrow' => array('year' => date('Y', time() + 86400), 'month' => date('m', time() + 86400), 'day' => date('d', time() + 86400)),
  20. ) as $date => $values)
  21. {
  22. $dom->loadHTML($w->render('foo', $date));
  23. $css = new sfDomCssSelector($dom);
  24. // selected date
  25. $t->is($css->matchSingle('#foo_year option[value="'.$values['year'].'"][selected="selected"]')->getValue(), $values['year'], '->render() renders a select tag for the year');
  26. $t->is($css->matchSingle('#foo_month option[value="'.$values['month'].'"][selected="selected"]')->getValue(), $values['month'], '->render() renders a select tag for the month');
  27. $t->is($css->matchSingle('#foo_day option[value="'.$values['day'].'"][selected="selected"]')->getValue(), $values['day'], '->render() renders a select tag for the day');
  28. }
  29. // pre-epoch date
  30. $t->diag('pre-epoch date');
  31. $years = range(1901, 1920);
  32. $w1 = new sfWidgetFormDate(array('years' => array_combine($years, $years)));
  33. $dom->loadHTML($w1->render('foo', '1910-01-15'));
  34. $css = new sfDomCssSelector($dom);
  35. $t->is($css->matchSingle('#foo_year option[value="1910"][selected="selected"]')->getValue(), 1910, '->render() renders a select tag for the year');
  36. $t->is($css->matchSingle('#foo_month option[value="01"][selected="selected"]')->getValue(), 01, '->render() renders a select tag for the month');
  37. $t->is($css->matchSingle('#foo_day option[value="15"][selected="selected"]')->getValue(), 15, '->render() renders a select tag for the day');
  38. // date as an array
  39. $t->diag('date as an array');
  40. $values = array('year' => 2005, 'month' => 10, 'day' => 15);
  41. $dom->loadHTML($w->render('foo', $values));
  42. $css = new sfDomCssSelector($dom);
  43. $t->is($css->matchSingle('#foo_year option[value="'.$values['year'].'"][selected="selected"]')->getValue(), $values['year'], '->render() renders a select tag for the year');
  44. $t->is($css->matchSingle('#foo_month option[value="'.$values['month'].'"][selected="selected"]')->getValue(), $values['month'], '->render() renders a select tag for the month');
  45. $t->is($css->matchSingle('#foo_day option[value="'.$values['day'].'"][selected="selected"]')->getValue(), $values['day'], '->render() renders a select tag for the day');
  46. // invalid date
  47. $t->diag('invalid date');
  48. $dom->loadHTML($w->render('foo', array('year' => null, 'month' => 10)));
  49. $css = new sfDomCssSelector($dom);
  50. $t->is($css->matchSingle('#foo_year option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the year');
  51. $t->is($css->matchSingle('#foo_month option[selected="selected"]')->getValue(), 10, '->render() renders a select tag for the month');
  52. $t->is($css->matchSingle('#foo_day option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the day');
  53. $dom->loadHTML($w->render('foo', 'invaliddate'));
  54. $css = new sfDomCssSelector($dom);
  55. $t->is($css->matchSingle('#foo_year option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the year');
  56. $t->is($css->matchSingle('#foo_month option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the month');
  57. $t->is($css->matchSingle('#foo_day option[selected="selected"]')->getValue(), '', '->render() renders a select tag for the day');
  58. // number of options in each select
  59. $t->diag('number of options in each select');
  60. $dom->loadHTML($w->render('foo', '2005-10-15'));
  61. $css = new sfDomCssSelector($dom);
  62. $t->is(count($css->matchAll('#foo_year option')->getNodes()), 12, '->render() renders a select tag for the 10 years around the current one');
  63. $t->is(count($css->matchAll('#foo_month option')->getNodes()), 13, '->render() renders a select tag for the 12 months in a year');
  64. $t->is(count($css->matchAll('#foo_day option')->getNodes()), 32, '->render() renders a select tag for the 31 days in a month');
  65. // can_be_empty option
  66. $t->diag('can_be_empty option');
  67. $w->setOption('can_be_empty', false);
  68. $dom->loadHTML($w->render('foo', '2005-10-15'));
  69. $css = new sfDomCssSelector($dom);
  70. $t->is(count($css->matchAll('#foo_year option')->getNodes()), 11, '->render() renders a select tag for the 10 years around the current one');
  71. $t->is(count($css->matchAll('#foo_month option')->getNodes()), 12, '->render() renders a select tag for the 12 months in a year');
  72. $t->is(count($css->matchAll('#foo_day option')->getNodes()), 31, '->render() renders a select tag for the 31 days in a month');
  73. $w->setOption('can_be_empty', true);
  74. // empty_values
  75. $t->diag('empty_values option');
  76. $w->setOption('empty_values', array('year' => 'YEAR', 'month' => 'MONTH', 'day' => 'DAY'));
  77. $dom->loadHTML($w->render('foo', '2005-10-15'));
  78. $css = new sfDomCssSelector($dom);
  79. $t->is($css->matchSingle('#foo_year option')->getNode()->nodeValue, 'YEAR', '->configure() can change the empty values');
  80. $t->is($css->matchSingle('#foo_month option')->getNode()->nodeValue, 'MONTH', '->configure() can change the empty values');
  81. $t->is($css->matchSingle('#foo_day option')->getNode()->nodeValue, 'DAY', '->configure() can change the empty values');
  82. $w->setOption('empty_values', array('year' => '', 'month' => '', 'day' => ''));
  83. // format option
  84. $t->diag('format option');
  85. $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '/', '->render() renders 3 selects with a default / as a separator');
  86. $t->like($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '#^/#', '->render() renders 3 selects with a default / as a separator');
  87. $w->setOption('format', '%month%#%day%#%year%');
  88. $dom->loadHTML($w->render('foo', '2005-10-15'));
  89. $css = new sfDomCssSelector($dom);
  90. $t->is($css->matchSingle('#foo_day')->getNode()->nextSibling->nodeValue, '#', '__construct() can change the default date format');
  91. $t->like($css->matchSingle('#foo_month')->getNode()->nextSibling->nodeValue, '/^#/', '__construct() can change the default date format');
  92. $w->setOption('format', '%day%/%month%/%year%');
  93. $dom->loadHTML($w->render('foo', '2005-10-15'));
  94. $css = new sfDomCssSelector($dom);
  95. $t->is($css->matchSingle('select')->getNode()->getAttribute('name'), 'foo[day]', '__construct() can change the default date format');
  96. // days / months / years options
  97. $t->diag('days / months / years options');
  98. $w->setOption('years', array(1998 => 1998, 1999 => 1999, 2000 => 2000, 2001 => 2001));
  99. $w->setOption('months', array(1 => 1, 2 => 2, 3 => 3));
  100. $w->setOption('days', array(1 => 1, 2 => 2));
  101. $dom->loadHTML($w->render('foo', '2005-10-15'));
  102. $css = new sfDomCssSelector($dom);
  103. $t->is(count($css->matchAll('#foo_year option')->getNodes()), 5, '__construct() can change the default array used for years');
  104. $t->is(count($css->matchAll('#foo_month option')->getNodes()), 4, '__construct() can change the default array used for months');
  105. $t->is(count($css->matchAll('#foo_day option')->getNodes()), 3, '__construct() can change the default array used for days');
  106. // attributes
  107. $t->diag('attributes');
  108. $dom->loadHTML($w->render('foo', '2005-10-15', array('disabled' => 'disabled')));
  109. $t->is(count($css->matchAll('select[disabled="disabled"]')->getNodes()), 3, '->render() takes the attributes into account for all the three embedded widgets');
  110. $w->setAttribute('disabled', 'disabled');
  111. $dom->loadHTML($w->render('foo', '2005-10-15'));
  112. $t->is(count($css->matchAll('select[disabled="disabled"]')->getNodes()), 3, '->render() takes the attributes into account for all the three embedded widgets');
  113. // id_format
  114. $t->diag('id_format');
  115. $w->setIdFormat('id_%s');
  116. $dom->loadHTML($w->render('foo'));
  117. $t->is(count($css->matchAll('#id_foo_day')), 1, '->render() uses the id_format');
  118. $t->is(count($css->matchAll('#id_foo_month')), 1, '->render() uses the id_format');
  119. $t->is(count($css->matchAll('#id_foo_year')), 1, '->render() uses the id_format');