123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Tests\Component\Form;
- require_once __DIR__ . '/DateTimeTestCase.php';
- use Symfony\Component\Form\DateField;
- use Symfony\Component\Form\FormContext;
- class DateFieldTest extends DateTimeTestCase
- {
- protected function setUp()
- {
- \Locale::setDefault('de_AT');
- }
- public function testSubmit_fromInput_dateTime()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'type' => DateField::DATETIME,
- ));
- $field->submit('2.6.2010');
- $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
- $this->assertEquals('02.06.2010', $field->getDisplayedData());
- }
- public function testSubmit_fromInput_string()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'type' => DateField::STRING,
- ));
- $field->submit('2.6.2010');
- $this->assertEquals('2010-06-02', $field->getData());
- $this->assertEquals('02.06.2010', $field->getDisplayedData());
- }
- public function testSubmit_fromInput_timestamp()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'type' => DateField::TIMESTAMP,
- ));
- $field->submit('2.6.2010');
- $dateTime = new \DateTime('2010-06-02 UTC');
- $this->assertEquals($dateTime->format('U'), $field->getData());
- $this->assertEquals('02.06.2010', $field->getDisplayedData());
- }
- public function testSubmit_fromInput_raw()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'type' => DateField::RAW,
- ));
- $field->submit('2.6.2010');
- $output = array(
- 'day' => '2',
- 'month' => '6',
- 'year' => '2010',
- );
- $this->assertEquals($output, $field->getData());
- $this->assertEquals('02.06.2010', $field->getDisplayedData());
- }
- public function testSubmit_fromChoice()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => DateField::CHOICE,
- ));
- $input = array(
- 'day' => '2',
- 'month' => '6',
- 'year' => '2010',
- );
- $field->submit($input);
- $dateTime = new \DateTime('2010-06-02 UTC');
- $this->assertDateTimeEquals($dateTime, $field->getData());
- $this->assertEquals($input, $field->getDisplayedData());
- }
- public function testSubmit_fromChoice_empty()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => DateField::CHOICE,
- 'required' => false,
- ));
- $input = array(
- 'day' => '',
- 'month' => '',
- 'year' => '',
- );
- $field->submit($input);
- $this->assertSame(null, $field->getData());
- $this->assertEquals($input, $field->getDisplayedData());
- }
- public function testSetData_differentTimezones()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'America/New_York',
- 'user_timezone' => 'Pacific/Tahiti',
- // don't do this test with DateTime, because it leads to wrong results!
- 'type' => DateField::STRING,
- 'widget' => 'input',
- ));
- $field->setData('2010-06-02');
- $this->assertEquals('01.06.2010', $field->getDisplayedData());
- }
- public function testIsYearWithinRange_returnsTrueIfWithin()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'years' => array(2010, 2011),
- ));
- $field->submit('2.6.2010');
- $this->assertTrue($field->isYearWithinRange());
- }
- public function testIsYearWithinRange_returnsTrueIfEmpty()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'years' => array(2010, 2011),
- ));
- $field->submit('');
- $this->assertTrue($field->isYearWithinRange());
- }
- public function testIsYearWithinRange_returnsTrueIfEmpty_choice()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'choice',
- 'years' => array(2010, 2011),
- ));
- $field->submit(array(
- 'day' => '1',
- 'month' => '2',
- 'year' => '',
- ));
- $this->assertTrue($field->isYearWithinRange());
- }
- public function testIsYearWithinRange_returnsFalseIfNotContained()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'years' => array(2010, 2012),
- ));
- $field->submit('2.6.2011');
- $this->assertFalse($field->isYearWithinRange());
- }
- public function testIsMonthWithinRange_returnsTrueIfWithin()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'months' => array(6, 7),
- ));
- $field->submit('2.6.2010');
- $this->assertTrue($field->isMonthWithinRange());
- }
- public function testIsMonthWithinRange_returnsTrueIfEmpty()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'months' => array(6, 7),
- ));
- $field->submit('');
- $this->assertTrue($field->isMonthWithinRange());
- }
- public function testIsMonthWithinRange_returnsTrueIfEmpty_choice()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'choice',
- 'months' => array(6, 7),
- ));
- $field->submit(array(
- 'day' => '1',
- 'month' => '',
- 'year' => '2011',
- ));
- $this->assertTrue($field->isMonthWithinRange());
- }
- public function testIsMonthWithinRange_returnsFalseIfNotContained()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'months' => array(6, 8),
- ));
- $field->submit('2.7.2010');
- $this->assertFalse($field->isMonthWithinRange());
- }
- public function testIsDayWithinRange_returnsTrueIfWithin()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'days' => array(6, 7),
- ));
- $field->submit('6.6.2010');
- $this->assertTrue($field->isDayWithinRange());
- }
- public function testIsDayWithinRange_returnsTrueIfEmpty()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'days' => array(6, 7),
- ));
- $field->submit('');
- $this->assertTrue($field->isDayWithinRange());
- }
- public function testIsDayWithinRange_returnsTrueIfEmpty_choice()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'choice',
- 'days' => array(6, 7),
- ));
- $field->submit(array(
- 'day' => '',
- 'month' => '1',
- 'year' => '2011',
- ));
- $this->assertTrue($field->isDayWithinRange());
- }
- public function testIsDayWithinRange_returnsFalseIfNotContained()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- 'days' => array(6, 8),
- ));
- $field->submit('7.6.2010');
- $this->assertFalse($field->isDayWithinRange());
- }
- public function testIsPartiallyFilled_returnsFalseIfInput()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'input',
- ));
- $field->submit('7.6.2010');
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyEmpty()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'choice',
- ));
- $field->submit(array(
- 'day' => '',
- 'month' => '',
- 'year' => '',
- ));
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsFalseIfChoiceAndCompletelyFilled()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'choice',
- ));
- $field->submit(array(
- 'day' => '2',
- 'month' => '6',
- 'year' => '2010',
- ));
- $this->assertFalse($field->isPartiallyFilled());
- }
- public function testIsPartiallyFilled_returnsTrueIfChoiceAndDayEmpty()
- {
- $field = new DateField('name', array(
- 'data_timezone' => 'UTC',
- 'user_timezone' => 'UTC',
- 'widget' => 'choice',
- ));
- $field->submit(array(
- 'day' => '',
- 'month' => '6',
- 'year' => '2010',
- ));
- $this->assertTrue($field->isPartiallyFilled());
- }
- }
|