diff options
Diffstat (limited to 'tests/units')
-rw-r--r-- | tests/units/Core/DateParserTest.php | 182 | ||||
-rw-r--r-- | tests/units/Helper/DatetimeHelperTest.php | 52 | ||||
-rw-r--r-- | tests/units/Model/TaskCreationTest.php | 4 | ||||
-rw-r--r-- | tests/units/Model/TaskDuplicationTest.php | 6 |
4 files changed, 178 insertions, 66 deletions
diff --git a/tests/units/Core/DateParserTest.php b/tests/units/Core/DateParserTest.php index 0d345784..dc3366b3 100644 --- a/tests/units/Core/DateParserTest.php +++ b/tests/units/Core/DateParserTest.php @@ -6,79 +6,167 @@ use Kanboard\Core\DateParser; class DateParserTest extends Base { + public function testGetTimeFormats() + { + $dateParser = new DateParser($this->container); + $this->assertCount(2, $dateParser->getTimeFormats()); + $this->assertContains('H:i', $dateParser->getTimeFormats()); + $this->assertContains('g:i a', $dateParser->getTimeFormats()); + } + + public function testGetDateFormats() + { + $dateParser = new DateParser($this->container); + $this->assertCount(4, $dateParser->getDateFormats()); + $this->assertCount(6, $dateParser->getDateFormats(true)); + $this->assertContains('d/m/Y', $dateParser->getDateFormats()); + $this->assertNotContains('Y-m-d', $dateParser->getDateFormats()); + $this->assertContains('Y-m-d', $dateParser->getDateFormats(true)); + } + + public function testGetDateTimeFormats() + { + $dateParser = new DateParser($this->container); + $this->assertCount(8, $dateParser->getDateTimeFormats()); + $this->assertCount(12, $dateParser->getDateTimeFormats(true)); + $this->assertContains('d/m/Y H:i', $dateParser->getDateTimeFormats()); + $this->assertNotContains('Y-m-d H:i', $dateParser->getDateTimeFormats()); + $this->assertContains('Y-m-d g:i a', $dateParser->getDateTimeFormats(true)); + } + + public function testGetAllDateFormats() + { + $dateParser = new DateParser($this->container); + $this->assertCount(12, $dateParser->getAllDateFormats()); + $this->assertCount(18, $dateParser->getAllDateFormats(true)); + $this->assertContains('d/m/Y', $dateParser->getAllDateFormats()); + $this->assertContains('d/m/Y H:i', $dateParser->getAllDateFormats()); + $this->assertNotContains('Y-m-d H:i', $dateParser->getAllDateFormats()); + $this->assertContains('Y-m-d g:i a', $dateParser->getAllDateFormats(true)); + $this->assertContains('Y-m-d', $dateParser->getAllDateFormats(true)); + } + + public function testGetAllAvailableFormats() + { + $dateParser = new DateParser($this->container); + + $formats = $dateParser->getAvailableFormats($dateParser->getDateFormats()); + $this->assertArrayHasKey('d/m/Y', $formats); + $this->assertContains(date('d/m/Y'), $formats); + + $formats = $dateParser->getAvailableFormats($dateParser->getDateTimeFormats()); + $this->assertArrayHasKey('d/m/Y H:i', $formats); + $this->assertContains(date('d/m/Y H:i'), $formats); + + $formats = $dateParser->getAvailableFormats($dateParser->getAllDateFormats()); + $this->assertArrayHasKey('d/m/Y', $formats); + $this->assertContains(date('d/m/Y'), $formats); + $this->assertArrayHasKey('d/m/Y H:i', $formats); + $this->assertContains(date('d/m/Y H:i'), $formats); + } + + public function testGetTimestamp() + { + $dateParser = new DateParser($this->container); + + $this->assertEquals(1393995600, $dateParser->getTimestamp(1393995600)); + $this->assertEquals('2014-03-05', date('Y-m-d', $dateParser->getTimestamp('2014-03-05'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $dateParser->getTimestamp('2014_03_05'))); + $this->assertEquals('2014-03-05', date('Y-m-d', $dateParser->getTimestamp('03/05/2014'))); + $this->assertEquals('2014-03-25 17:18', date('Y-m-d H:i', $dateParser->getTimestamp('03/25/2014 5:18 pm'))); + $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $dateParser->getTimestamp('03/25/2014 5:18 am'))); + $this->assertEquals('2014-03-25 17:18', date('Y-m-d H:i', $dateParser->getTimestamp('03/25/2014 5:18pm'))); + $this->assertEquals('2014-03-25 23:14', date('Y-m-d H:i', $dateParser->getTimestamp('03/25/2014 23:14'))); + $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $dateParser->getTimestamp('2014_03_29 23:14'))); + $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $dateParser->getTimestamp('2014-03-29 23:14'))); + } + public function testDateRange() { - $d = new DateParser($this->container); + $dateParser = new DateParser($this->container); - $this->assertTrue($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); - $this->assertFalse($d->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 16:00:00'), new DateTime('2015-03-14 17:00:00'))); + $this->assertTrue($dateParser->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); + $this->assertFalse($dateParser->withinDateRange(new DateTime('2015-03-14 15:30:00'), new DateTime('2015-03-14 16:00:00'), new DateTime('2015-03-14 17:00:00'))); + } + + public function testGetHours() + { + $dateParser = new DateParser($this->container); + + $this->assertEquals(1, $dateParser->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); + $this->assertEquals(2.5, $dateParser->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:30:00'))); + $this->assertEquals(2.75, $dateParser->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:45:00'))); + $this->assertEquals(3, $dateParser->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 17:58:00'))); + $this->assertEquals(3, $dateParser->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 11:58:00'))); } public function testRoundSeconds() { - $d = new DateParser($this->container); - $this->assertEquals('16:30', date('H:i', $d->getRoundedSeconds(strtotime('16:28')))); - $this->assertEquals('16:00', date('H:i', $d->getRoundedSeconds(strtotime('16:02')))); - $this->assertEquals('16:15', date('H:i', $d->getRoundedSeconds(strtotime('16:14')))); - $this->assertEquals('17:00', date('H:i', $d->getRoundedSeconds(strtotime('16:58')))); + $dateParser = new DateParser($this->container); + $this->assertEquals('16:30', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:28')))); + $this->assertEquals('16:00', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:02')))); + $this->assertEquals('16:15', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:14')))); + $this->assertEquals('17:00', date('H:i', $dateParser->getRoundedSeconds(strtotime('16:58')))); } - public function testGetHours() + public function testGetIsoDate() { - $d = new DateParser($this->container); + $dateParser = new DateParser($this->container); + + $this->assertEquals('2016-02-06', $dateParser->getIsoDate(1454786217)); + $this->assertEquals('2014-03-05', $dateParser->getIsoDate('2014-03-05')); + $this->assertEquals('2014-03-05', $dateParser->getIsoDate('2014_03_05')); + $this->assertEquals('2014-03-05', $dateParser->getIsoDate('03/05/2014')); + $this->assertEquals('2014-03-25', $dateParser->getIsoDate('03/25/2014 5:18 pm')); + $this->assertEquals('2014-03-25', $dateParser->getIsoDate('03/25/2014 5:18 am')); + $this->assertEquals('2014-03-25', $dateParser->getIsoDate('03/25/2014 5:18pm')); + $this->assertEquals('2014-03-25', $dateParser->getIsoDate('03/25/2014 23:14')); + $this->assertEquals('2014-03-29', $dateParser->getIsoDate('2014_03_29 23:14')); + $this->assertEquals('2014-03-29', $dateParser->getIsoDate('2014-03-29 23:14')); + } - $this->assertEquals(1, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 16:00:00'))); - $this->assertEquals(2.5, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:30:00'))); - $this->assertEquals(2.75, $d->getHours(new DateTime('2015-03-14 15:00:00'), new DateTime('2015-03-14 17:45:00'))); - $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 17:58:00'))); - $this->assertEquals(3, $d->getHours(new DateTime('2015-03-14 14:57:00'), new DateTime('2015-03-14 11:58:00'))); + public function testGetTimestampFromIsoFormat() + { + $dateParser = new DateParser($this->container); + $this->assertEquals('2014-03-05 00:00', date('Y-m-d H:i', $dateParser->getTimestampFromIsoFormat('2014-03-05'))); + $this->assertEquals(date('Y-m-d 00:00', strtotime('+2 days')), date('Y-m-d H:i', $dateParser->getTimestampFromIsoFormat(strtotime('+2 days')))); } - public function testValidDate() + public function testRemoveTimeFromTimestamp() { - $d = new DateParser($this->container); - - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('2014-03-05', 'Y-m-d'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('2014_03_05', 'Y_m_d'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('05/03/2014', 'd/m/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('03/05/2014', 'm/d/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('3/5/2014', 'm/d/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('5/3/2014', 'd/m/Y'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getValidDate('5/3/14', 'd/m/y'))); - $this->assertEquals(0, $d->getValidDate('5/3/14', 'd/m/Y')); - $this->assertEquals(0, $d->getValidDate('5-3-2014', 'd/m/Y')); + $dateParser = new DateParser($this->container); + $this->assertEquals('2016-02-06 00:00', date('Y-m-d H:i', $dateParser->removeTimeFromTimestamp(1454786217))); } - public function testGetTimestamp() + public function testFormat() { - $d = new DateParser($this->container); - - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('2014-03-05'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('2014_03_05'))); - $this->assertEquals('2014-03-05', date('Y-m-d', $d->getTimestamp('03/05/2014'))); - $this->assertEquals('2014-03-25 17:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18 pm'))); - $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18 am'))); - $this->assertEquals('2014-03-25 05:18', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 5:18am'))); - $this->assertEquals('2014-03-25 23:14', date('Y-m-d H:i', $d->getTimestamp('03/25/2014 23:14'))); - $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $d->getTimestamp('2014_03_29 23:14'))); - $this->assertEquals('2014-03-29 23:14', date('Y-m-d H:i', $d->getTimestamp('2014-03-29 23:14'))); + $dateParser = new DateParser($this->container); + $values['date'] = '1454787006'; + + $this->assertEquals(array('date' => '06/02/2016'), $dateParser->format($values, array('date'), 'd/m/Y')); + $this->assertEquals(array('date' => '02/06/2016 7:30 pm'), $dateParser->format($values, array('date'), 'm/d/Y g:i a')); } public function testConvert() { - $d = new DateParser($this->container); - + $dateParser = new DateParser($this->container); $values = array( 'date_due' => '2015-01-25', - 'date_started' => '2015_01_25', + 'date_started' => '2015-01-25 17:25', ); - $d->convert($values, array('date_due', 'date_started')); + $this->assertEquals( + array('date_due' => 1422144000, 'date_started' => 1422144000), + $dateParser->convert($values, array('date_due', 'date_started')) + ); - $this->assertEquals(mktime(0, 0, 0, 1, 25, 2015), $values['date_due']); - $this->assertEquals('2015-01-25', date('Y-m-d', $values['date_due'])); + $values = array( + 'date_started' => '2015-01-25 17:25', + ); - $this->assertEquals(mktime(0, 0, 0, 1, 25, 2015), $values['date_started']); - $this->assertEquals('2015-01-25', date('Y-m-d', $values['date_started'])); + $this->assertEquals( + array('date_started' => 1422206700), + $dateParser->convert($values, array('date_due', 'date_started'), true) + ); } } diff --git a/tests/units/Helper/DatetimeHelperTest.php b/tests/units/Helper/DatetimeHelperTest.php index 8e9c461b..f27a2eb9 100644 --- a/tests/units/Helper/DatetimeHelperTest.php +++ b/tests/units/Helper/DatetimeHelperTest.php @@ -6,23 +6,45 @@ use Kanboard\Helper\Dt; class DatetimeHelperTest extends Base { + public function testGetTime() + { + $helper = new Dt($this->container); + $this->assertEquals('17:25', $helper->time(1422206700)); + } + + public function testGetDate() + { + $helper = new Dt($this->container); + $this->assertEquals('01/25/2015', $helper->date(1422206700)); + $this->assertEquals('01/25/2015', $helper->date('2015-01-25')); + $this->assertEquals('', $helper->date('0')); + $this->assertEquals('', $helper->date(0)); + $this->assertEquals('', $helper->date('')); + } + + public function testGetDatetime() + { + $helper = new Dt($this->container); + $this->assertEquals('01/25/2015 17:25', $helper->datetime(1422206700)); + } + public function testAge() { - $h = new Dt($this->container); - - $this->assertEquals('<15m', $h->age(0, 30)); - $this->assertEquals('<30m', $h->age(0, 1000)); - $this->assertEquals('<1h', $h->age(0, 3000)); - $this->assertEquals('~2h', $h->age(0, 2*3600)); - $this->assertEquals('1d', $h->age(0, 30*3600)); - $this->assertEquals('2d', $h->age(0, 65*3600)); + $helper = new Dt($this->container); + + $this->assertEquals('<15m', $helper->age(0, 30)); + $this->assertEquals('<30m', $helper->age(0, 1000)); + $this->assertEquals('<1h', $helper->age(0, 3000)); + $this->assertEquals('~2h', $helper->age(0, 2*3600)); + $this->assertEquals('1d', $helper->age(0, 30*3600)); + $this->assertEquals('2d', $helper->age(0, 65*3600)); } public function testGetDayHours() { - $h = new Dt($this->container); + $helper = new Dt($this->container); - $slots = $h->getDayHours(); + $slots = $helper->getDayHours(); $this->assertNotEmpty($slots); $this->assertCount(48, $slots); @@ -36,9 +58,9 @@ class DatetimeHelperTest extends Base public function testGetWeekDays() { - $h = new Dt($this->container); + $helper = new Dt($this->container); - $slots = $h->getWeekDays(); + $slots = $helper->getWeekDays(); $this->assertNotEmpty($slots); $this->assertCount(7, $slots); @@ -48,9 +70,9 @@ class DatetimeHelperTest extends Base public function testGetWeekDay() { - $h = new Dt($this->container); + $helper = new Dt($this->container); - $this->assertEquals('Monday', $h->getWeekDay(1)); - $this->assertEquals('Sunday', $h->getWeekDay(7)); + $this->assertEquals('Monday', $helper->getWeekDay(1)); + $this->assertEquals('Sunday', $helper->getWeekDay(7)); } } diff --git a/tests/units/Model/TaskCreationTest.php b/tests/units/Model/TaskCreationTest.php index 19b3b0d1..781a7147 100644 --- a/tests/units/Model/TaskCreationTest.php +++ b/tests/units/Model/TaskCreationTest.php @@ -295,7 +295,7 @@ class TaskCreationTest extends Base $task = $tf->getById(2); $this->assertNotEmpty($task); $this->assertEquals(2, $task['id']); - $this->assertEquals($timestamp, $task['date_due']); + $this->assertEquals(date('Y-m-d 00:00', $timestamp), date('Y-m-d 00:00', $task['date_due'])); $task = $tf->getById(3); $this->assertEquals(3, $task['id']); @@ -422,6 +422,6 @@ class TaskCreationTest extends Base $task = $tf->getById(1); $this->assertNotEmpty($task); - $this->assertEquals('2050-01-10 12:30', date('Y-m-d H:i', $task['date_due'])); + $this->assertEquals('2050-01-10 00:00', date('Y-m-d H:i', $task['date_due'])); } } diff --git a/tests/units/Model/TaskDuplicationTest.php b/tests/units/Model/TaskDuplicationTest.php index 798b3835..8649c6b0 100644 --- a/tests/units/Model/TaskDuplicationTest.php +++ b/tests/units/Model/TaskDuplicationTest.php @@ -2,6 +2,7 @@ require_once __DIR__.'/../Base.php'; +use Kanboard\Core\DateParser; use Kanboard\Model\Task; use Kanboard\Model\TaskCreation; use Kanboard\Model\TaskDuplication; @@ -665,6 +666,7 @@ class TaskDuplicationTest extends Base $tf = new TaskFinder($this->container); $p = new Project($this->container); $c = new Category($this->container); + $dp = new DateParser($this->container); $this->assertEquals(1, $p->create(array('name' => 'test1'))); @@ -685,7 +687,7 @@ class TaskDuplicationTest extends Base $this->assertNotEmpty($task); $this->assertEquals(Task::RECURRING_STATUS_PROCESSED, $task['recurrence_status']); $this->assertEquals(2, $task['recurrence_child']); - $this->assertEquals(1436561776, $task['date_due'], '', 2); + $this->assertEquals(1436486400, $task['date_due'], '', 2); $task = $tf->getById(2); $this->assertNotEmpty($task); @@ -695,6 +697,6 @@ class TaskDuplicationTest extends Base $this->assertEquals(Task::RECURRING_BASEDATE_TRIGGERDATE, $task['recurrence_basedate']); $this->assertEquals(1, $task['recurrence_parent']); $this->assertEquals(2, $task['recurrence_factor']); - $this->assertEquals(strtotime('+2 days'), $task['date_due'], '', 2); + $this->assertEquals($dp->removeTimeFromTimestamp(strtotime('+2 days')), $task['date_due'], '', 2); } } |