PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/pacore/tools/tests/php/EventArchitectureTest.php

https://github.com/DigitalCityMechanics/PeopleAggregator
PHP | 231 lines | 152 code | 32 blank | 47 comment | 6 complexity | 28ae05a415d38fd83a4c5ac782418adb MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, LGPL-2.1
  1. <?php
  2. /** !
  3. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  4. * [filename] is a part of PeopleAggregator.
  5. * [description including history]
  6. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  7. * @author [creator, or "Original Author"]
  8. * @license http://bit.ly/aVWqRV PayAsYouGo License
  9. * @copyright Copyright (c) 2010 Broadband Mechanics
  10. * @package PeopleAggregator
  11. */
  12. ?>
  13. <?php
  14. require_once dirname(__FILE__)."/lib/common.php";
  15. require_once("api/Event/Event.php");
  16. require_once("api/Event/EventAssociation.php");
  17. require_once("api/Event/Calendar.php");
  18. require_once("api/Network/Network.php");
  19. class EventArchitectureTest extends PHPUnit_Framework_TestCase {
  20. public function testAddUpdateDeleteEvent() {
  21. // Dal::register_query_callback("explain_query");
  22. echo "getting a user\n";
  23. $user = Test::get_test_user();
  24. $testusername = $user->first_name . " " . $user->last_name;
  25. echo "test user = $testusername\n";
  26. /* setup some times and time strings */
  27. $today = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
  28. $tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
  29. $yesterday = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
  30. $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
  31. $nextmonth = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));
  32. $nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
  33. $oneday = 60*60*24;
  34. $simple_dateformat = "Y-m-d";
  35. /* use the constants in the format parameter */
  36. // something like: Mon, 15 Aug 2005 15:12:46 UTC
  37. $now_rfc822 = date(DATE_RFC822);
  38. // something like: 2000-07-01T00:00:00+00:00
  39. // $now_atom = date(DATE_ATOM);
  40. // create an Event
  41. echo "create and save Event\n";
  42. $e = new Event();
  43. $e->content_id = "http://myevent.info/1"; // anything basically
  44. $e->user_id = $user->user_id;
  45. $e->event_title = "Test Event for $testusername";
  46. $now = time();
  47. $nowplusoneday = $now + $oneday;
  48. $e->start_time = date(DATE_ATOM, $now);
  49. $e->end_time = date(DATE_ATOM, $now + 60*60); // duration 1h
  50. $e->event_data = array(
  51. 'description' => "This Event takes place to test the class Event",
  52. 'start' => $now,
  53. 'end' => $now + 60*60
  54. );
  55. $e->save();
  56. // print_r($e);
  57. // see if we got it
  58. echo "Retrieving Event $e->event_id\n";
  59. $e2 = new Event();
  60. $e2->load($e->event_id);
  61. echo "Testing integrity of dates\n";
  62. // print_r($e2);
  63. // see if the stored timestamps match
  64. $this->assertEquals($now, $e2->event_data['start']);
  65. // see if our dates survived the DB conversion roundtrip
  66. $this->assertEquals($now, strtotime($e2->start_time));
  67. $this->assertEquals($now + 60*60, strtotime($e2->end_time));
  68. // create two EventAssociations
  69. $ea1 = new EventAssociation();
  70. $ea2 = new EventAssociation();
  71. $ea1->user_id = $user->user_id;
  72. $ea2->user_id = $user->user_id;
  73. // user EventAssocoiation
  74. $ea1->assoc_target_type = 'user';
  75. $ea1->assoc_target_id = $user->user_id; // could very well be other user
  76. $ea1->assoc_target_name = $testusername;
  77. $ea1->event_id = $e->event_id;
  78. $ea1->save();
  79. // network EventAssocoiation
  80. // find a network the user is member of
  81. // $networks = Network::get_user_networks($user->user_id);
  82. $network = Network::get_mothership_info(); // use the mothership
  83. // print_r($network);
  84. $ea2->assoc_target_type = 'network';
  85. $ea2->assoc_target_id = $network->network_id; // could very well be other user
  86. $ea2->assoc_target_name = $network->name;
  87. $ea2->event_id = $e->event_id;
  88. $ea2->save();
  89. echo "Testing EventAssociations for Event $e->event_id\n";
  90. $assoc_ids = EventAssociation::find_for_event($e->event_id);
  91. // print_r($assoc_ids);
  92. $a_cnt = count($assoc_ids);
  93. $this->assertEquals($a_cnt, 2, "expected 2 assocs, got $a_cnt\n");
  94. echo "Testing EventAssociations::find_for_target_and_delta for Network\n";
  95. $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id);
  96. // find_for_target_and_delta($target_type, $target_id, $range_start = NULL, $range_end = NULL)
  97. // print_r($assoc_ids);
  98. $a_cnt = count($assoc_ids);
  99. // we expect at least one (or more, the user might have others too)
  100. $this->assertTrue(($a_cnt >= 1), "expected 1 or more assocs, got $a_cnt\n");
  101. echo "Testing EventAssociations::find_for_target_and_delta for Today\n";
  102. /*
  103. echo "yesterday = " . date(DATE_ATOM, $yesterday) . "\n";
  104. echo "today = " . date(DATE_ATOM, $today) . "\n";
  105. echo "event start_time = " . date(DATE_ATOM, strtotime($e2->start_time)) . "\n";
  106. echo "event end_time = " . date(DATE_ATOM, strtotime($e2->end_time)) . "\n";
  107. echo "tomorrow = " . date(DATE_ATOM, $tomorrow) . "\n";
  108. */
  109. $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id, date(DATE_ATOM, $today), date(DATE_ATOM, $tomorrow));
  110. print_r($assoc_ids);
  111. /*
  112. $assocs = EventAssociation::load_in_list($assoc_ids);
  113. print_r($assocs);
  114. */
  115. $a_cnt = count($assoc_ids);
  116. // we expect at least one (or more, the user might have others too)
  117. $this->assertTrue(($a_cnt >= 1), "expected 1 or more assocs, got $a_cnt\n");
  118. echo "Testing if the EventAssociations now show up in Tomorrow's Calendar\n";
  119. $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id, date(DATE_ATOM, $tomorrow), date(DATE_ATOM, $tomorrow + $oneday));
  120. print_r($assoc_ids);
  121. $a_cnt2 = count($assoc_ids);
  122. // we expect one less than before
  123. $this->assertTrue(($a_cnt2 < $a_cnt), "expected "
  124. . $a_cnt - 1
  125. . " assocs, got $a_cnt2\n");
  126. echo "Modifying original Event\n";
  127. $e2->title = "changed title";
  128. $e2->end_time = date(DATE_ATOM, $nextmonth);
  129. $e2->save();
  130. // see if we got it
  131. $e3 = new Event();
  132. $e3->load($e->event_id);
  133. echo "Testing integrity of dates again in the Event\n";
  134. // see if our dates survived the DB conversion roundtrip
  135. $this->assertEquals($now, strtotime($e3->start_time));
  136. $this->assertEquals(
  137. date(DATE_ATOM, strtotime($e3->end_time)),
  138. date(DATE_ATOM, $nextmonth));
  139. echo "Testing if modified dates made it to the EventAssociations\n";
  140. $assoc_ids = EventAssociation::find_for_event($e3->event_id);
  141. $assocs = EventAssociation::load_in_list($assoc_ids);
  142. echo "e3 end_time: " . date(DATE_ATOM, strtotime($assocs[0]->end_time))
  143. . "\nnextmonth: "
  144. . date(DATE_ATOM, $nextmonth)
  145. . "\n";
  146. $this->assertEquals(
  147. date(DATE_ATOM, strtotime($assocs[0]->end_time)),
  148. date(DATE_ATOM, $nextmonth));
  149. echo "Testing if the EventAssociations now show up in Tomorrow's Calendar\n";
  150. echo "test range: "
  151. . date(DATE_ATOM, $tomorrow) . " - " . date(DATE_ATOM, $tomorrow + $oneday)
  152. . "\n";
  153. echo "event duration: "
  154. . date(DATE_ATOM, strtotime($e3->start_time))
  155. . " - "
  156. . date(DATE_ATOM, strtotime($e3->end_time))
  157. . "\n";
  158. // print_r($e3);
  159. $assoc_ids = EventAssociation::find_for_target_and_delta('network', $network->network_id, date(DATE_ATOM, $tomorrow), date(DATE_ATOM, $tomorrow + $oneday));
  160. print_r($assoc_ids);
  161. $a_cnt = count($assoc_ids);
  162. // we expect at least one (or more, the user might have others too)
  163. $this->assertTrue(($a_cnt >= 1), "expected 1 or more assocs, got $a_cnt\n");
  164. echo "Deleting Event $e->event_id\n";
  165. Event::delete($e->event_id);
  166. // try loading
  167. $this->assertNull($e3->load($e->event_id));
  168. echo "Testing if all EventAssociations have been removed\n";
  169. $assoc_ids = EventAssociation::find_for_event($e->event_id);
  170. $a_cnt = count($assoc_ids);
  171. $this->assertEquals($a_cnt, 0, "expected 0 assocs, got $a_cnt\n");
  172. }
  173. public function testStaticCalendarFunctions() {
  174. $today = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
  175. $tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
  176. $yesterday = mktime(0, 0, 0, date("m"), date("d")-1, date("Y"));
  177. $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
  178. $nextmonth = mktime(0, 0, 0, date("m")+1, date("d"), date("Y"));
  179. echo "Testing static Calendar Functions\n";
  180. echo "Calendar::range()\n";
  181. print_r(Calendar::range());
  182. echo "Calendar::range(\$today, 'month')\n";
  183. print_r(Calendar::range($today, 'month'));
  184. echo "Calendar::range(date(DATE_ATOM, \$today), 'month')\n";
  185. print_r(Calendar::range(date(DATE_ATOM, $today), 'month'));
  186. echo "Calendar::range(\$today, 'day')\n";
  187. print_r(Calendar::range($today, 'day'));
  188. echo "Calendar::range(\$today, 'year')\n";
  189. print_r(Calendar::range($today, 'year'));
  190. }
  191. }
  192. ?>