diff --git a/tests/PHPUnit/Core/Period/DayTest.php b/tests/PHPUnit/Core/Period/DayTest.php new file mode 100644 index 0000000000000000000000000000000000000000..07f786c81934182ef8ceaddd55ad8bb21b3a4b5d --- /dev/null +++ b/tests/PHPUnit/Core/Period/DayTest.php @@ -0,0 +1,187 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id$ + */ +/** + * Testing Period_Day + */ +class Period_DayTest extends PHPUnit_Framework_TestCase +{ + /** + * today is NOT finished + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayIsFinishedToday() + { + $period = new Piwik_Period_Day( Piwik_Date::today()); + $this->assertEquals(date("Y-m-d"), $period->toString()); + $this->assertEquals(array(), $period->getSubperiods()); + $this->assertEquals(0, $period->getNumberOfSubperiods()); + } + + /** + * yesterday 23:59:59 is finished + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayIsFinishedYesterday() + { + + $period = new Piwik_Period_Day( Piwik_Date::yesterday()); + $this->assertEquals(date("Y-m-d", time()-86400), $period->toString()); + $this->assertEquals(array(), $period->getSubperiods()); + $this->assertEquals(0, $period->getNumberOfSubperiods()); + } + + /** + * tomorrow is not finished + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayIsFinishedTomorrow() + { + $period = new Piwik_Period_Day( Piwik_Date::factory(date("Y-m-d",time()+86400))); + $this->assertEquals(date("Y-m-d", time()+86400), $period->toString()); + $this->assertEquals(array(), $period->getSubperiods()); + $this->assertEquals(0, $period->getNumberOfSubperiods()); + } + + /** + * test day doesnt exist 31st feb + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayIsFinished31stfeb() + { + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-02-31")); + $this->assertEquals("2007-03-03", $period->toString()); + $this->assertEquals(array(), $period->getSubperiods()); + $this->assertEquals(0, $period->getNumberOfSubperiods()); + } + + /** + * test date that doesn't exist, should return the corresponding correct date + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayGetDateStart1() + { + // create the period + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-02-31")); + + // start date + $startDate = $period->getDateStart(); + + // expected string + $this->assertEquals("2007-03-03", $startDate->toString()); + + // check that for a day, getDateStart = getStartEnd + $this->assertEquals($startDate, $period->getDateEnd()); + } + + /** + * test normal date + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayGetDateStart2() + { + // create the period + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-01-03")); + + // start date + $startDate = $period->getDateStart(); + + // expected string + $this->assertEquals("2007-01-03", $startDate->toString()); + + // check that for a day, getDateStart = getStartEnd + $this->assertEquals($startDate, $period->getDateEnd() ); + } + + /** + * test last day of year + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayGetDateStart3() + { + // create the period + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-12-31")); + + // start date + $startDate = $period->getDateStart(); + + // expected string + $this->assertEquals("2007-12-31", $startDate->toString()); + + // check that for a day, getDateStart = getStartEnd + $this->assertEquals($startDate, $period->getDateEnd() ); + } + + /** + * test date that doesn't exist, should return the corresponding correct date + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayGetDateEnd1() + { + // create the period + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-02-31")); + + // end date + $endDate = $period->getDateEnd(); + + // expected string + $this->assertEquals("2007-03-03", $endDate->toString()); + } + + /** + * test normal date + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayGetDateEnd2() + { + // create the period + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-04-15")); + + // end date + $endDate = $period->getDateEnd(); + + // expected string + $this->assertEquals("2007-04-15", $endDate->toString()); + } + + /** + * test last day of year + * @group Core + * @group Period + * @group Period_Day + */ + public function testDayGetDateEnd3() + { + // create the period + $period = new Piwik_Period_Day( Piwik_Date::factory("2007-12-31")); + + // end date + $endDate = $period->getDateEnd(); + + // expected string + $this->assertEquals("2007-12-31", $endDate->toString()); + } +} \ No newline at end of file diff --git a/tests/PHPUnit/Core/Period/MonthTest.php b/tests/PHPUnit/Core/Period/MonthTest.php new file mode 100644 index 0000000000000000000000000000000000000000..6647034cdd1b9a8c58cbdd8d637a0410315d9ab1 --- /dev/null +++ b/tests/PHPUnit/Core/Period/MonthTest.php @@ -0,0 +1,277 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id$ + */ +/** + * Testing Period_Month + */ +class Period_MonthTest extends PHPUnit_Framework_TestCase +{ + /** + * testing december + * @group Core + * @group Period + * @group Period_Month + */ + public function testMonthDec() + { + $month = new Piwik_Period_Month( Piwik_Date::factory("2006-12-31")); + $correct=array( + "2006-12-01", + "2006-12-02", + "2006-12-03", + "2006-12-04", + "2006-12-05", + "2006-12-06", + "2006-12-07", + "2006-12-08", + "2006-12-09", + "2006-12-10", + "2006-12-11", + "2006-12-12", + "2006-12-13", + "2006-12-14", + "2006-12-15", + "2006-12-16", + "2006-12-17", + "2006-12-18", + "2006-12-19", + "2006-12-20", + "2006-12-21", + "2006-12-22", + "2006-12-23", + "2006-12-24", + "2006-12-25", + "2006-12-26", + "2006-12-27", + "2006-12-28", + "2006-12-29", + "2006-12-30", + "2006-12-31",); + $this->assertEquals($correct, $month->toString()); + $this->assertEquals(31, $month->getNumberOfSubperiods()); + } + + /** + * testing month feb leap year + * @group Core + * @group Period + * @group Period_Month + */ + public function testMonthFebLeap() + { + $month = new Piwik_Period_Month( Piwik_Date::factory("2024-02-11")); + $correct=array( + "2024-02-01", + "2024-02-02", + "2024-02-03", + "2024-02-04", + "2024-02-05", + "2024-02-06", + "2024-02-07", + "2024-02-08", + "2024-02-09", + "2024-02-10", + "2024-02-11", + "2024-02-12", + "2024-02-13", + "2024-02-14", + "2024-02-15", + "2024-02-16", + "2024-02-17", + "2024-02-18", + "2024-02-19", + "2024-02-20", + "2024-02-21", + "2024-02-22", + "2024-02-23", + "2024-02-24", + "2024-02-25", + "2024-02-26", + "2024-02-27", + "2024-02-28", + "2024-02-29",); + $this->assertEquals($correct, $month->toString()); + $this->assertEquals(29, $month->getNumberOfSubperiods()); + } + + /** + * testing month feb non-leap year + * @group Core + * @group Period + * @group Period_Month + */ + public function testMonthFebNonLeap() + { + $month = new Piwik_Period_Month( Piwik_Date::factory("2023-02-11")); + $correct=array( + "2023-02-01", + "2023-02-02", + "2023-02-03", + "2023-02-04", + "2023-02-05", + "2023-02-06", + "2023-02-07", + "2023-02-08", + "2023-02-09", + "2023-02-10", + "2023-02-11", + "2023-02-12", + "2023-02-13", + "2023-02-14", + "2023-02-15", + "2023-02-16", + "2023-02-17", + "2023-02-18", + "2023-02-19", + "2023-02-20", + "2023-02-21", + "2023-02-22", + "2023-02-23", + "2023-02-24", + "2023-02-25", + "2023-02-26", + "2023-02-27", + "2023-02-28",); + $this->assertEquals($correct, $month->toString()); + $this->assertEquals(28, $month->getNumberOfSubperiods()); + } + + /** + * testing jan + * @group Core + * @group Period + * @group Period_Month + */ + public function testMonthJan() + { + $month = new Piwik_Period_Month( Piwik_Date::factory("2007-01-01")); + $correct=array( + "2007-01-01", + "2007-01-02", + "2007-01-03", + "2007-01-04", + "2007-01-05", + "2007-01-06", + "2007-01-07", + "2007-01-08", + "2007-01-09", + "2007-01-10", + "2007-01-11", + "2007-01-12", + "2007-01-13", + "2007-01-14", + "2007-01-15", + "2007-01-16", + "2007-01-17", + "2007-01-18", + "2007-01-19", + "2007-01-20", + "2007-01-21", + "2007-01-22", + "2007-01-23", + "2007-01-24", + "2007-01-25", + "2007-01-26", + "2007-01-27", + "2007-01-28", + "2007-01-29", + "2007-01-30", + "2007-01-31",); + $this->assertEquals($correct, $month->toString()); + $this->assertEquals(31, $month->getNumberOfSubperiods()); + } + + /** + * testing month containing a time change (DST) + * @group Core + * @group Period + * @group Period_Month + */ + public function testMonthDSTChangeMarch() + { + $month = new Piwik_Period_Month( Piwik_Date::factory("2007-02-31")); + $correct=array( + "2007-03-01", + "2007-03-02", + "2007-03-03", + "2007-03-04", + "2007-03-05", + "2007-03-06", + "2007-03-07", + "2007-03-08", + "2007-03-09", + "2007-03-10", + "2007-03-11", + "2007-03-12", + "2007-03-13", + "2007-03-14", + "2007-03-15", + "2007-03-16", + "2007-03-17", + "2007-03-18", + "2007-03-19", + "2007-03-20", + "2007-03-21", + "2007-03-22", + "2007-03-23", + "2007-03-24", + "2007-03-25", + "2007-03-26", + "2007-03-27", + "2007-03-28", + "2007-03-29", + "2007-03-30", + "2007-03-31",); + $this->assertEquals($correct, $month->toString()); + $this->assertEquals(31, $month->getNumberOfSubperiods()); + } + + /** + * @group Core + * @group Period + * @group Period_Month + */ + public function testMonthDSTChangeOct() + { + $month = new Piwik_Period_Month( Piwik_Date::factory("2017-10-31")); + $correct=array( + "2017-10-01", + "2017-10-02", + "2017-10-03", + "2017-10-04", + "2017-10-05", + "2017-10-06", + "2017-10-07", + "2017-10-08", + "2017-10-09", + "2017-10-10", + "2017-10-11", + "2017-10-12", + "2017-10-13", + "2017-10-14", + "2017-10-15", + "2017-10-16", + "2017-10-17", + "2017-10-18", + "2017-10-19", + "2017-10-20", + "2017-10-21", + "2017-10-22", + "2017-10-23", + "2017-10-24", + "2017-10-25", + "2017-10-26", + "2017-10-27", + "2017-10-28", + "2017-10-29", + "2017-10-30", + "2017-10-31",); + $this->assertEquals($correct, $month->toString()); + $this->assertEquals(31, $month->getNumberOfSubperiods()); + } +} \ No newline at end of file diff --git a/tests/PHPUnit/Core/Period/WeekTest.php b/tests/PHPUnit/Core/Period/WeekTest.php new file mode 100644 index 0000000000000000000000000000000000000000..bdb570b2f2b34efc3f64764f5409770954ef561f --- /dev/null +++ b/tests/PHPUnit/Core/Period/WeekTest.php @@ -0,0 +1,141 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id$ + */ +/** + * Testing Period_Week + */ +class Period_WeekTest extends PHPUnit_Framework_TestCase +{ + /** + * test week between 2 years + * @group Core + * @group Period + * @group Period_Week + */ + function testWeekZendSetWeekday() + { + $this->markTestSkipped('see http://framework.zend.com/issues/browse/ZF-1832'); + $date = Piwik_Date::factory('2006-01-01', 'YYYY-MM-dd', 'en'); + $date->setWeekday(1); + $this->assertEquals('2005-12-26', $date->toString("Y-m-d")); + } + + /** + * test week between 2 years + * @group Core + * @group Period + * @group Period_Week + */ + public function testWeekBetween2years() + { + $week = new Piwik_Period_Week( Piwik_Date::factory("2006-01-01")); + $correct=array( + "2005-12-26", + "2005-12-27", + "2005-12-28", + "2005-12-29", + "2005-12-30", + "2005-12-31", + "2006-01-01",); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + } + + /** + * test week between 2 months Week Mai 29 To Mai 31 2006 + * @group Core + * @group Period + * @group Period_Week + */ + public function testWeekBetween2month() + { + $week = new Piwik_Period_Week( Piwik_Date::factory("2006-05-29")); + $correct=array( + "2006-05-29", + "2006-05-30", + "2006-05-31", + "2006-06-01", + "2006-06-02", + "2006-06-03", + "2006-06-04",); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + } + + /** + * test week between feb and march for leap year + * @group Core + * @group Period + * @group Period_Week + */ + public function testWeekFebLeapyear() + { + $correct=array( + '2023-02-27', + '2023-02-28', + '2023-03-01', + '2023-03-02', + '2023-03-03', + '2023-03-04', + '2023-03-05',); + + $week = new Piwik_Period_Week( Piwik_Date::factory('2023-02-27')); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + $week = new Piwik_Period_Week( Piwik_Date::factory('2023-03-01')); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + } + + /** + * test week between feb and march for no leap year + * @group Core + * @group Period + * @group Period_Week + */ + public function testWeekFebnonLeapyear() + { + $correct=array( + '2024-02-26', + '2024-02-27', + '2024-02-28', + '2024-02-29', + '2024-03-01', + '2024-03-02', + '2024-03-03',); + + $week = new Piwik_Period_Week( Piwik_Date::factory('2024-02-27')); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + $week = new Piwik_Period_Week( Piwik_Date::factory('2024-03-01')); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + } + + /** + * test week normal middle of the month + * @group Core + * @group Period + * @group Period_Week + */ + public function testWeekMiddleofmonth() + { + $correct=array( + '2024-10-07', + '2024-10-08', + '2024-10-09', + '2024-10-10', + '2024-10-11', + '2024-10-12', + '2024-10-13',); + + $week = new Piwik_Period_Week( Piwik_Date::factory('2024-10-09')); + $this->assertEquals($correct, $week->toString()); + $this->assertEquals(7, $week->getNumberOfSubperiods()); + } +} diff --git a/tests/PHPUnit/Core/Period/YearTest.php b/tests/PHPUnit/Core/Period/YearTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f524df629dbed59c60d7cdd9ba639be082f50ef9 --- /dev/null +++ b/tests/PHPUnit/Core/Period/YearTest.php @@ -0,0 +1,68 @@ +<?php +/** + * Piwik - Open source web analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + * @version $Id$ + */ +/** + * Testing Period_Year + */ +class Period_YearTest extends PHPUnit_Framework_TestCase +{ + /** + * test normal case + * @group Core + * @group Period + * @group Period_Year + */ + function testYearNormalcase() + { + $correct=array( + '2024-01-01', + '2024-02-01', + '2024-03-01', + '2024-04-01', + '2024-05-01', + '2024-06-01', + '2024-07-01', + '2024-08-01', + '2024-09-01', + '2024-10-01', + '2024-11-01', + '2024-12-01',); + + $year = new Piwik_Period_Year( Piwik_Date::factory('2024-10-09')); + $this->assertEquals(12, $year->getNumberOfSubperiods()); + $this->assertEquals($correct, $year->toString()); + } + + /** + * test past + * @group Core + * @group Period + * @group Period_Year + */ + function testYearPastAndWrongdate() + { + $correct=array( + '2000-01-01', + '2000-02-01', + '2000-03-01', + '2000-04-01', + '2000-05-01', + '2000-06-01', + '2000-07-01', + '2000-08-01', + '2000-09-01', + '2000-10-01', + '2000-11-01', + '2000-12-01', + ); + + $week = new Piwik_Period_Year( Piwik_Date::factory('2000-02-15')); + $this->assertEquals(12, $week->getNumberOfSubperiods()); + $this->assertEquals($correct, $week->toString()); + } +} \ No newline at end of file