PageRenderTime 113ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/tests/core/Period.test.php

https://github.com/quarkness/piwik
PHP | 955 lines | 768 code | 105 blank | 82 comment | 11 complexity | d59b2d98a82cf0f34234851e15ea71e5 MD5 | raw file
  1. <?php
  2. if(!defined('PIWIK_CONFIG_TEST_INCLUDED'))
  3. {
  4. require_once dirname(__FILE__)."/../../tests/config_test.php";
  5. }
  6. require_once 'Period.php';
  7. require_once 'Period/Week.php';
  8. require_once 'Period/Month.php';
  9. require_once 'Period/Year.php';
  10. require_once 'Date.php';
  11. class Test_Piwik_Period extends UnitTestCase
  12. {
  13. function __construct( $title = '')
  14. {
  15. parent::__construct( $title );
  16. }
  17. public function test_getId()
  18. {
  19. $period = new Piwik_Period_Day( Piwik_Date::today() );
  20. $this->assertTrue( $period->getId() !== 0 );
  21. $period = new Piwik_Period_Week( Piwik_Date::today() );
  22. $this->assertTrue( $period->getId() !== 0 );
  23. $period = new Piwik_Period_Month( Piwik_Date::today() );
  24. $this->assertTrue( $period->getId() !== 0 );
  25. $period = new Piwik_Period_Year( Piwik_Date::today() );
  26. $this->assertTrue( $period->getId() !== 0 );
  27. }
  28. public function test_getLabel()
  29. {
  30. $period = new Piwik_Period_Day( Piwik_Date::today() );
  31. $label = $period->getLabel();
  32. $this->assertTrue( is_string($label) && !empty($label));
  33. $period = new Piwik_Period_Week( Piwik_Date::today() );
  34. $label = $period->getLabel();
  35. $this->assertTrue( is_string($label) && !empty($label));
  36. $period = new Piwik_Period_Month( Piwik_Date::today() );
  37. $label = $period->getLabel();
  38. $this->assertTrue( is_string($label) && !empty($label));
  39. $period = new Piwik_Period_Year( Piwik_Date::today() );
  40. $label = $period->getLabel();
  41. $this->assertTrue( is_string($label) && !empty($label));
  42. }
  43. /**
  44. * Testing Period_Day
  45. */
  46. // today is NOT finished
  47. function test_day_isFinished_today()
  48. {
  49. $period = new Piwik_Period_Day( Piwik_Date::today());
  50. $this->assertEqual( $period->toString(), date("Y-m-d"));
  51. $this->assertEqual( $period->getSubperiods(), array());
  52. $this->assertEqual( $period->getNumberOfSubperiods(), 0);
  53. }
  54. // yesterday 23:59:59 is finished
  55. function test_day_isFinished_yesterday()
  56. {
  57. $period = new Piwik_Period_Day( Piwik_Date::yesterday());
  58. $this->assertEqual( $period->toString(), date("Y-m-d", time()-86400));
  59. $this->assertEqual( $period->getSubperiods(), array());
  60. $this->assertEqual( $period->getNumberOfSubperiods(), 0);
  61. }
  62. // tomorrow is not finished
  63. function test_day_isFinished_tomorrow()
  64. {
  65. $period = new Piwik_Period_Day( Piwik_Date::factory(date("Y-m-d",time()+86400)));
  66. $this->assertEqual( $period->toString(), date("Y-m-d", time()+86400));
  67. $this->assertEqual( $period->getSubperiods(), array());
  68. $this->assertEqual( $period->getNumberOfSubperiods(), 0);
  69. }
  70. // test day doesnt exist 31st feb
  71. function test_day_isFinished_31stfeb()
  72. {
  73. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-02-31"));
  74. $this->assertEqual( $period->toString(), "2007-03-03");
  75. $this->assertEqual( $period->getSubperiods(), array());
  76. $this->assertEqual( $period->getNumberOfSubperiods(), 0);
  77. }
  78. // test date that doesn't exist, should return the corresponding correct date
  79. function test_day_getDateStart1()
  80. {
  81. // create the period
  82. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-02-31"));
  83. // start date
  84. $startDate = $period->getDateStart();
  85. // expected string
  86. $this->assertEqual( $startDate->toString(), "2007-03-03");
  87. // check that for a day, getDateStart = getStartEnd
  88. $this->assertEqual($startDate, $period->getDateEnd() );
  89. }
  90. // test normal date
  91. function test_day_getDateStart2()
  92. {
  93. // create the period
  94. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-01-03"));
  95. // start date
  96. $startDate = $period->getDateStart();
  97. // expected string
  98. $this->assertEqual( $startDate->toString(), "2007-01-03");
  99. // check that for a day, getDateStart = getStartEnd
  100. $this->assertEqual($startDate, $period->getDateEnd() );
  101. }
  102. // test last day of year
  103. function test_day_getDateStart3()
  104. {
  105. // create the period
  106. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-12-31"));
  107. // start date
  108. $startDate = $period->getDateStart();
  109. // expected string
  110. $this->assertEqual( $startDate->toString(), "2007-12-31");
  111. // check that for a day, getDateStart = getStartEnd
  112. $this->assertEqual($startDate, $period->getDateEnd() );
  113. }
  114. // test date that doesn't exist, should return the corresponding correct date
  115. function test_day_getDateEnd1()
  116. {
  117. // create the period
  118. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-02-31"));
  119. // end date
  120. $endDate = $period->getDateEnd();
  121. // expected string
  122. $this->assertEqual( $endDate->toString(), "2007-03-03");
  123. }
  124. // test normal date
  125. function test_day_getDateEnd2()
  126. {
  127. // create the period
  128. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-04-15"));
  129. // end date
  130. $endDate = $period->getDateEnd();
  131. // expected string
  132. $this->assertEqual( $endDate->toString(), "2007-04-15");
  133. }
  134. // test last day of year
  135. function test_day_getDateEnd3()
  136. {
  137. // create the period
  138. $period = new Piwik_Period_Day( Piwik_Date::factory("2007-12-31"));
  139. // end date
  140. $endDate = $period->getDateEnd();
  141. // expected string
  142. $this->assertEqual( $endDate->toString(), "2007-12-31");
  143. }
  144. // test date that doesn't exist, should return the corresponding correct date
  145. /**
  146. * Testing Period_Month
  147. *
  148. */
  149. // testing december
  150. function test_month_Dec()
  151. {
  152. $month = new Piwik_Period_Month( Piwik_Date::factory("2006-12-31"));
  153. $correct=array(
  154. "2006-12-01",
  155. "2006-12-02",
  156. "2006-12-03",
  157. "2006-12-04",
  158. "2006-12-05",
  159. "2006-12-06",
  160. "2006-12-07",
  161. "2006-12-08",
  162. "2006-12-09",
  163. "2006-12-10",
  164. "2006-12-11",
  165. "2006-12-12",
  166. "2006-12-13",
  167. "2006-12-14",
  168. "2006-12-15",
  169. "2006-12-16",
  170. "2006-12-17",
  171. "2006-12-18",
  172. "2006-12-19",
  173. "2006-12-20",
  174. "2006-12-21",
  175. "2006-12-22",
  176. "2006-12-23",
  177. "2006-12-24",
  178. "2006-12-25",
  179. "2006-12-26",
  180. "2006-12-27",
  181. "2006-12-28",
  182. "2006-12-29",
  183. "2006-12-30",
  184. "2006-12-31",);
  185. $this->assertEqual( $month->toString(), $correct);
  186. $this->assertEqual( $month->getNumberOfSubperiods(), 31);
  187. }
  188. // testing month feb leap year
  189. function test_month_FebLeap()
  190. {
  191. $month = new Piwik_Period_Month( Piwik_Date::factory("2024-02-11"));
  192. $correct=array(
  193. "2024-02-01",
  194. "2024-02-02",
  195. "2024-02-03",
  196. "2024-02-04",
  197. "2024-02-05",
  198. "2024-02-06",
  199. "2024-02-07",
  200. "2024-02-08",
  201. "2024-02-09",
  202. "2024-02-10",
  203. "2024-02-11",
  204. "2024-02-12",
  205. "2024-02-13",
  206. "2024-02-14",
  207. "2024-02-15",
  208. "2024-02-16",
  209. "2024-02-17",
  210. "2024-02-18",
  211. "2024-02-19",
  212. "2024-02-20",
  213. "2024-02-21",
  214. "2024-02-22",
  215. "2024-02-23",
  216. "2024-02-24",
  217. "2024-02-25",
  218. "2024-02-26",
  219. "2024-02-27",
  220. "2024-02-28",
  221. "2024-02-29",);
  222. $this->assertEqual( $month->toString(), $correct);
  223. $this->assertEqual( $month->getNumberOfSubperiods(), 29);
  224. }
  225. // testing month feb non-leap year
  226. function test_month_FebNonLeap()
  227. {
  228. $month = new Piwik_Period_Month( Piwik_Date::factory("2023-02-11"));
  229. $correct=array(
  230. "2023-02-01",
  231. "2023-02-02",
  232. "2023-02-03",
  233. "2023-02-04",
  234. "2023-02-05",
  235. "2023-02-06",
  236. "2023-02-07",
  237. "2023-02-08",
  238. "2023-02-09",
  239. "2023-02-10",
  240. "2023-02-11",
  241. "2023-02-12",
  242. "2023-02-13",
  243. "2023-02-14",
  244. "2023-02-15",
  245. "2023-02-16",
  246. "2023-02-17",
  247. "2023-02-18",
  248. "2023-02-19",
  249. "2023-02-20",
  250. "2023-02-21",
  251. "2023-02-22",
  252. "2023-02-23",
  253. "2023-02-24",
  254. "2023-02-25",
  255. "2023-02-26",
  256. "2023-02-27",
  257. "2023-02-28",);
  258. $this->assertEqual( $month->toString(), $correct);
  259. $this->assertEqual( $month->getNumberOfSubperiods(), 28);
  260. }
  261. // testing jan
  262. function test_month_Jan()
  263. {
  264. $month = new Piwik_Period_Month( Piwik_Date::factory("2007-01-01"));
  265. $correct=array(
  266. "2007-01-01",
  267. "2007-01-02",
  268. "2007-01-03",
  269. "2007-01-04",
  270. "2007-01-05",
  271. "2007-01-06",
  272. "2007-01-07",
  273. "2007-01-08",
  274. "2007-01-09",
  275. "2007-01-10",
  276. "2007-01-11",
  277. "2007-01-12",
  278. "2007-01-13",
  279. "2007-01-14",
  280. "2007-01-15",
  281. "2007-01-16",
  282. "2007-01-17",
  283. "2007-01-18",
  284. "2007-01-19",
  285. "2007-01-20",
  286. "2007-01-21",
  287. "2007-01-22",
  288. "2007-01-23",
  289. "2007-01-24",
  290. "2007-01-25",
  291. "2007-01-26",
  292. "2007-01-27",
  293. "2007-01-28",
  294. "2007-01-29",
  295. "2007-01-30",
  296. "2007-01-31",);
  297. $this->assertEqual( $month->toString(), $correct);
  298. $this->assertEqual( $month->getNumberOfSubperiods(), 31);
  299. }
  300. // testing month containing a time change (DST)
  301. function test_month_DSTChangeMarch()
  302. {
  303. $month = new Piwik_Period_Month( Piwik_Date::factory("2007-02-31"));
  304. $correct=array(
  305. "2007-03-01",
  306. "2007-03-02",
  307. "2007-03-03",
  308. "2007-03-04",
  309. "2007-03-05",
  310. "2007-03-06",
  311. "2007-03-07",
  312. "2007-03-08",
  313. "2007-03-09",
  314. "2007-03-10",
  315. "2007-03-11",
  316. "2007-03-12",
  317. "2007-03-13",
  318. "2007-03-14",
  319. "2007-03-15",
  320. "2007-03-16",
  321. "2007-03-17",
  322. "2007-03-18",
  323. "2007-03-19",
  324. "2007-03-20",
  325. "2007-03-21",
  326. "2007-03-22",
  327. "2007-03-23",
  328. "2007-03-24",
  329. "2007-03-25",
  330. "2007-03-26",
  331. "2007-03-27",
  332. "2007-03-28",
  333. "2007-03-29",
  334. "2007-03-30",
  335. "2007-03-31",);
  336. $this->assertEqual( $month->toString(), $correct);
  337. $this->assertEqual( $month->getNumberOfSubperiods(), 31);
  338. }
  339. function test_month_DSTChangeOct()
  340. {
  341. $month = new Piwik_Period_Month( Piwik_Date::factory("2017-10-31"));
  342. $correct=array(
  343. "2017-10-01",
  344. "2017-10-02",
  345. "2017-10-03",
  346. "2017-10-04",
  347. "2017-10-05",
  348. "2017-10-06",
  349. "2017-10-07",
  350. "2017-10-08",
  351. "2017-10-09",
  352. "2017-10-10",
  353. "2017-10-11",
  354. "2017-10-12",
  355. "2017-10-13",
  356. "2017-10-14",
  357. "2017-10-15",
  358. "2017-10-16",
  359. "2017-10-17",
  360. "2017-10-18",
  361. "2017-10-19",
  362. "2017-10-20",
  363. "2017-10-21",
  364. "2017-10-22",
  365. "2017-10-23",
  366. "2017-10-24",
  367. "2017-10-25",
  368. "2017-10-26",
  369. "2017-10-27",
  370. "2017-10-28",
  371. "2017-10-29",
  372. "2017-10-30",
  373. "2017-10-31",);
  374. $this->assertEqual( $month->toString(), $correct);
  375. $this->assertEqual( $month->getNumberOfSubperiods(), 31);
  376. }
  377. /**
  378. * Testing Period_Week
  379. *
  380. */
  381. /* //http://framework.zend.com/issues/browse/ZF-1832
  382. function test_week_zendsetweekday()
  383. {
  384. $date = Piwik_Date::factory('2006-01-01','YYYY-MM-dd', 'en');
  385. $date->setWeekday(1);
  386. $this->assertEqual('2005-12-26', $date->toString("Y-m-d"));
  387. }*/
  388. // test week between 2 years
  389. function test_week_Between2years()
  390. {
  391. $week = new Piwik_Period_Week( Piwik_Date::factory("2006-01-01"));
  392. $correct=array(
  393. "2005-12-26",
  394. "2005-12-27",
  395. "2005-12-28",
  396. "2005-12-29",
  397. "2005-12-30",
  398. "2005-12-31",
  399. "2006-01-01",);
  400. $this->assertEqual( $week->toString(), $correct);
  401. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  402. }
  403. // test week between 2 months Week Mai 29 To Mai 31 2006
  404. function test_week_Between2month()
  405. {
  406. $week = new Piwik_Period_Week( Piwik_Date::factory("2006-05-29"));
  407. $correct=array(
  408. "2006-05-29",
  409. "2006-05-30",
  410. "2006-05-31",
  411. "2006-06-01",
  412. "2006-06-02",
  413. "2006-06-03",
  414. "2006-06-04",);
  415. $this->assertEqual( $week->toString(), $correct);
  416. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  417. }
  418. // test week between feb and march for leap year
  419. function test_week_febLeapyear()
  420. {
  421. $correct=array(
  422. '2023-02-27',
  423. '2023-02-28',
  424. '2023-03-01',
  425. '2023-03-02',
  426. '2023-03-03',
  427. '2023-03-04',
  428. '2023-03-05',);
  429. $week = new Piwik_Period_Week( Piwik_Date::factory('2023-02-27'));
  430. $this->assertEqual( $week->toString(), $correct);
  431. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  432. $week = new Piwik_Period_Week( Piwik_Date::factory('2023-03-01'));
  433. $this->assertEqual( $week->toString(), $correct);
  434. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  435. }
  436. // test week between feb and march for no leap year
  437. function test_week_febnonLeapyear()
  438. {
  439. $correct=array(
  440. '2024-02-26',
  441. '2024-02-27',
  442. '2024-02-28',
  443. '2024-02-29',
  444. '2024-03-01',
  445. '2024-03-02',
  446. '2024-03-03',);
  447. $week = new Piwik_Period_Week( Piwik_Date::factory('2024-02-27'));
  448. $this->assertEqual( $week->toString(), $correct);
  449. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  450. $week = new Piwik_Period_Week( Piwik_Date::factory('2024-03-01'));
  451. $this->assertEqual( $week->toString(), $correct);
  452. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  453. }
  454. // test week normal middle of the month
  455. function test_week_middleofmonth()
  456. {
  457. $correct=array(
  458. '2024-10-07',
  459. '2024-10-08',
  460. '2024-10-09',
  461. '2024-10-10',
  462. '2024-10-11',
  463. '2024-10-12',
  464. '2024-10-13',);
  465. $week = new Piwik_Period_Week( Piwik_Date::factory('2024-10-09'));
  466. $this->assertEqual( $week->toString(), $correct);
  467. $this->assertEqual( $week->getNumberOfSubperiods(), 7);
  468. }
  469. /**
  470. * Testing Period_Year
  471. */
  472. // test normal case
  473. function test_year_normalcase()
  474. {
  475. $correct=array(
  476. '2024-01-01',
  477. '2024-02-01',
  478. '2024-03-01',
  479. '2024-04-01',
  480. '2024-05-01',
  481. '2024-06-01',
  482. '2024-07-01',
  483. '2024-08-01',
  484. '2024-09-01',
  485. '2024-10-01',
  486. '2024-11-01',
  487. '2024-12-01',);
  488. $year = new Piwik_Period_Year( Piwik_Date::factory('2024-10-09'));
  489. $this->assertEqual( $year->getNumberOfSubperiods(), 12);
  490. $this->assertEqual( $year->toString(), $correct);
  491. }
  492. // test past
  493. function test_year_pastAndWrongdate()
  494. {
  495. $correct=array(
  496. '2000-01-01',
  497. '2000-02-01',
  498. '2000-03-01',
  499. '2000-04-01',
  500. '2000-05-01',
  501. '2000-06-01',
  502. '2000-07-01',
  503. '2000-08-01',
  504. '2000-09-01',
  505. '2000-10-01',
  506. '2000-11-01',
  507. '2000-12-01',
  508. );
  509. $week = new Piwik_Period_Year( Piwik_Date::factory('2000-02-15'));
  510. $this->assertEqual( $week->getNumberOfSubperiods(), 12);
  511. $this->assertEqual( $week->toString(), $correct);
  512. }
  513. // test range 1
  514. function test_range_today()
  515. {
  516. $range = new Piwik_Period_Range( 'day', 'last1' );
  517. $today = Piwik_Date::today();
  518. $correct=array(
  519. $today->toString(),
  520. );
  521. $correct = array_reverse($correct);
  522. $this->assertEqual( $range->getNumberOfSubperiods(), 1);
  523. $this->assertEqual( $range->toString(), $correct);
  524. }
  525. function test_range_today_UtcPlus12()
  526. {
  527. // rather ugly test, UTC+23 doesn't exist, but it's a way to test that last1 in UTC+23 will be "our" UTC tomorrow
  528. $range = new Piwik_Period_Range( 'day', 'last1', 'UTC+23' );
  529. $today = Piwik_Date::now()->addHour(23);
  530. $correct=array(
  531. $today->toString(),
  532. );
  533. $correct = array_reverse($correct);
  534. $this->assertEqual( $range->getNumberOfSubperiods(), 1);
  535. $this->assertEqual( $range->toString(), $correct);
  536. }
  537. // test range 2
  538. function test_range_2days()
  539. {
  540. $range = new Piwik_Period_Range( 'day', 'last2' );
  541. $today = Piwik_Date::today();
  542. $correct=array(
  543. $today->toString(),
  544. $today->subDay(1)->toString()
  545. );
  546. $correct = array_reverse($correct);
  547. $this->assertEqual( $range->getNumberOfSubperiods(), 2);
  548. $this->assertEqual( $range->toString(), $correct);
  549. }
  550. // test range 3
  551. function test_range_5days()
  552. {
  553. $range = new Piwik_Period_Range( 'day', 'last50' );
  554. $today = Piwik_Date::today();
  555. $correct = array();
  556. for($i=0;$i<50;$i++)
  557. {
  558. $correct[]=$today->subDay($i)->toString();
  559. }
  560. $correct = array_reverse($correct);
  561. $this->assertEqual( $range->getNumberOfSubperiods(), 50);
  562. $this->assertEqual( $range->toString(), $correct);
  563. }
  564. // test range 4
  565. function test_range_previous3days()
  566. {
  567. $range = new Piwik_Period_Range( 'day', 'previous3' );
  568. $yesterday = Piwik_Date::yesterday();
  569. $correct = array();
  570. for($i=0;$i<3;$i++)
  571. {
  572. $correct[]=$yesterday->subDay($i)->toString();
  573. }
  574. $correct = array_reverse($correct);
  575. $this->assertEqual( $range->getNumberOfSubperiods(), 3);
  576. $this->assertEqual( $range->toString(), $correct);
  577. }
  578. // test range date1,date2
  579. function test_range_comma1()
  580. {
  581. $range = new Piwik_Period_Range( 'day', '2008-01-01,2008-01-03' );
  582. $correct = array(
  583. '2008-01-01',
  584. '2008-01-02',
  585. '2008-01-03',
  586. );
  587. $this->assertEqual( $range->getNumberOfSubperiods(), count($correct));
  588. $this->assertEqual( $range->toString(), $correct);
  589. }
  590. // test range date1,date2
  591. function test_range_comma2()
  592. {
  593. $range = new Piwik_Period_Range( 'day', '2007-12-22,2008-01-03' );
  594. $correct = array(
  595. '2007-12-22',
  596. '2007-12-23',
  597. '2007-12-24',
  598. '2007-12-25',
  599. '2007-12-26',
  600. '2007-12-27',
  601. '2007-12-28',
  602. '2007-12-29',
  603. '2007-12-30',
  604. '2007-12-31',
  605. '2008-01-01',
  606. '2008-01-02',
  607. '2008-01-03',
  608. );
  609. $this->assertEqual( $range->getNumberOfSubperiods(), count($correct));
  610. $this->assertEqual( $range->toString(), $correct);
  611. }
  612. // test range date1,date2
  613. function test_range_weekcomma1()
  614. {
  615. $range = new Piwik_Period_Range( 'week', '2007-12-22,2008-01-03' );
  616. $correct = array(
  617. array(
  618. '2007-12-17',
  619. '2007-12-18',
  620. '2007-12-19',
  621. '2007-12-20',
  622. '2007-12-21',
  623. '2007-12-22',
  624. '2007-12-23',
  625. ),
  626. array(
  627. '2007-12-24',
  628. '2007-12-25',
  629. '2007-12-26',
  630. '2007-12-27',
  631. '2007-12-28',
  632. '2007-12-29',
  633. '2007-12-30',
  634. ),
  635. array(
  636. '2007-12-31',
  637. '2008-01-01',
  638. '2008-01-02',
  639. '2008-01-03',
  640. '2008-01-04',
  641. '2008-01-05',
  642. '2008-01-06',
  643. )
  644. );
  645. $this->assertEqual( $range->getNumberOfSubperiods(), count($correct));
  646. $this->assertEqual( $range->toString(), $correct);
  647. }
  648. // test range date1,date2
  649. function test_range_yearcomma1()
  650. {
  651. $range = new Piwik_Period_Range( 'year', '2006-12-22,2007-01-03' );
  652. $correct = array(
  653. array (
  654. 0 => '2006-01-01',
  655. 1 => '2006-02-01',
  656. 2 => '2006-03-01',
  657. 3 => '2006-04-01',
  658. 4 => '2006-05-01',
  659. 5 => '2006-06-01',
  660. 6 => '2006-07-01',
  661. 7 => '2006-08-01',
  662. 8 => '2006-09-01',
  663. 9 => '2006-10-01',
  664. 10 => '2006-11-01',
  665. 11 => '2006-12-01',
  666. ),
  667. 1 =>
  668. array (
  669. 0 => '2007-01-01',
  670. 1 => '2007-02-01',
  671. 2 => '2007-03-01',
  672. 3 => '2007-04-01',
  673. 4 => '2007-05-01',
  674. 5 => '2007-06-01',
  675. 6 => '2007-07-01',
  676. 7 => '2007-08-01',
  677. 8 => '2007-09-01',
  678. 9 => '2007-10-01',
  679. 10 => '2007-11-01',
  680. 11 => '2007-12-01',
  681. ),
  682. );
  683. $this->assertEqual( $range->getNumberOfSubperiods(), count($correct));
  684. $this->assertEqual( $range->toString(), $correct);
  685. }
  686. // test range date1,date2
  687. function test_range_monthcomma1()
  688. {
  689. $range = new Piwik_Period_Range( 'month', '2006-12-22,2007-01-03' );
  690. $correct = array(
  691. array(
  692. '2006-12-01',
  693. '2006-12-02',
  694. '2006-12-03',
  695. '2006-12-04',
  696. '2006-12-05',
  697. '2006-12-06',
  698. '2006-12-07',
  699. '2006-12-08',
  700. '2006-12-09',
  701. '2006-12-10',
  702. '2006-12-11',
  703. '2006-12-12',
  704. '2006-12-13',
  705. '2006-12-14',
  706. '2006-12-15',
  707. '2006-12-16',
  708. '2006-12-17',
  709. '2006-12-18',
  710. '2006-12-19',
  711. '2006-12-20',
  712. '2006-12-21',
  713. '2006-12-22',
  714. '2006-12-23',
  715. '2006-12-24',
  716. '2006-12-25',
  717. '2006-12-26',
  718. '2006-12-27',
  719. '2006-12-28',
  720. '2006-12-29',
  721. '2006-12-30',
  722. '2006-12-31',
  723. ),
  724. array(
  725. '2007-01-01',
  726. '2007-01-02',
  727. '2007-01-03',
  728. '2007-01-04',
  729. '2007-01-05',
  730. '2007-01-06',
  731. '2007-01-07',
  732. '2007-01-08',
  733. '2007-01-09',
  734. '2007-01-10',
  735. '2007-01-11',
  736. '2007-01-12',
  737. '2007-01-13',
  738. '2007-01-14',
  739. '2007-01-15',
  740. '2007-01-16',
  741. '2007-01-17',
  742. '2007-01-18',
  743. '2007-01-19',
  744. '2007-01-20',
  745. '2007-01-21',
  746. '2007-01-22',
  747. '2007-01-23',
  748. '2007-01-24',
  749. '2007-01-25',
  750. '2007-01-26',
  751. '2007-01-27',
  752. '2007-01-28',
  753. '2007-01-29',
  754. '2007-01-30',
  755. '2007-01-31',
  756. ),
  757. );
  758. $this->assertEqual( $range->getNumberOfSubperiods(), count($correct));
  759. $this->assertEqual( $range->toString(), $correct);
  760. }
  761. // test range WEEK
  762. function test_range_week()
  763. {
  764. $range = new Piwik_Period_Range( 'week', 'last50' );
  765. $today = Piwik_Date::today();
  766. $correct = array();
  767. for($i=0;$i<50;$i++)
  768. {
  769. $date = $today->subDay($i*7);
  770. $week = new Piwik_Period_Week($date);
  771. $correct[]= $week->toString();
  772. }
  773. $correct = array_reverse($correct);
  774. $this->assertEqual( $range->getNumberOfSubperiods(), 50);
  775. $this->assertEqual( $range->toString(), $correct);
  776. }
  777. // test range WEEK last1
  778. function test_range_week_last1()
  779. {
  780. $range = new Piwik_Period_Range( 'week', 'last1' );
  781. $currentWeek = new Piwik_Period_Week(Piwik_Date::today());
  782. $this->assertEqual( $range->getNumberOfSubperiods(), 1);
  783. $this->assertEqual( $range->toString(), array($currentWeek->toString()));
  784. }
  785. // test range MONTH
  786. function test_range_month()
  787. {
  788. $range = new Piwik_Period_Range( 'month', 'last20' );
  789. $today = Piwik_Date::today();
  790. $correct = array();
  791. for($i=0;$i<20;$i++)
  792. {
  793. $date = $today->subMonth($i);
  794. $week = new Piwik_Period_Month($date);
  795. $correct[]= $week->toString();
  796. }
  797. $correct = array_reverse($correct);
  798. $this->assertEqual( $range->getNumberOfSubperiods(), 20);
  799. $this->assertEqual( $range->toString(), $correct);
  800. }
  801. // test range MONTH last1
  802. function test_range_month_last1()
  803. {
  804. $range = new Piwik_Period_Range( 'month', 'last1' );
  805. $month = new Piwik_Period_Month(Piwik_Date::today());
  806. $this->assertEqual( $range->getNumberOfSubperiods(), 1);
  807. $this->assertEqual( $range->toString(), array($month->toString()));
  808. }
  809. // test range PREVIOUS MONTH
  810. function test_range_previousmonth()
  811. {
  812. $range = new Piwik_Period_Range( 'month', 'previous10' );
  813. $end = Piwik_Date::today();
  814. $end = $end->subMonth(1);
  815. $correct = array();
  816. for($i=0;$i<10;$i++)
  817. {
  818. $date = $end->subMonth($i);
  819. $week = new Piwik_Period_Month($date);
  820. $correct[]= $week->toString();
  821. }
  822. $correct = array_reverse($correct);
  823. $this->assertEqual( $range->getNumberOfSubperiods(), 10);
  824. $this->assertEqual( $range->toString(), $correct);
  825. }
  826. // test range YEAR
  827. function test_range_year()
  828. {
  829. $range = new Piwik_Period_Range( 'year', 'last10' );
  830. $today = Piwik_Date::today();
  831. $correct = array();
  832. for($i=0;$i<10;$i++)
  833. {
  834. $date = $today->subMonth(12*$i);
  835. $week = new Piwik_Period_Year($date);
  836. $correct[]= $week->toString();
  837. }
  838. $correct = array_reverse($correct);
  839. $this->assertEqual( $range->getNumberOfSubperiods(), 10);
  840. $this->assertEqual( $range->toString(), $correct);
  841. }
  842. // test range YEAR last1
  843. function test_range_year_last1()
  844. {
  845. $range = new Piwik_Period_Range( 'year', 'last1' );
  846. $currentYear = new Piwik_Period_Year(Piwik_Date::today());
  847. $this->assertEqual( $range->getNumberOfSubperiods(), 1);
  848. $this->assertEqual( $range->toString(), array($currentYear->toString()));
  849. }
  850. }