PageRenderTime 87ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/phpunit/classes/libraries/MY_Incidents_Api_Object_Test.php

https://github.com/Magaz/Ushahidi_Web
PHP | 397 lines | 181 code | 83 blank | 133 comment | 8 complexity | fee1265ad323794b6c0331e1e4eddce1 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-2.1
  1. <?php defined('SYSPATH') or die('No direct script access');
  2. /**
  3. * Unit tests for the incidents API
  4. *
  5. * PHP version 5
  6. * LICENSE: This source file is subject to LGPL license
  7. * that is available through the world-wide-web at the following URI:
  8. * http://www.gnu.org/copyleft/lesser.html
  9. * @author Ushahidi Team <team@ushahidi.com>
  10. * @package Ushahidi - http://source.ushahididev.com
  11. * @module Unit Tests
  12. * @copyright Ushahidi - http://www.ushahidi.com
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License (LGPL)
  14. */
  15. class Incidents_Api_Object_Test extends PHPUnit_Framework_TestCase {
  16. /**
  17. * API controller object to run the tests
  18. * @var Api_Controller
  19. */
  20. private $api_controller;
  21. /**
  22. * Database object for straight SQL queries
  23. * @var Database
  24. */
  25. private $db;
  26. /**
  27. * Database table prefix
  28. * @var string
  29. */
  30. private $table_prefix;
  31. protected function setUp()
  32. {
  33. if (ORM::factory('incident')->count_all() == 0)
  34. {
  35. $this->markTestSkipped('The incident table is empty.');
  36. }
  37. else
  38. {
  39. // $_SERVER values
  40. $_SERVER = array_merge($_SERVER, array(
  41. 'REQUEST_METHOD' => 'GET',
  42. 'REQUEST_URI' => url::base().'api/?task=incidents',
  43. ));
  44. // Instantiate the API controller
  45. $this->api_controller = new Api_Controller();
  46. // Instantiate the DB object
  47. $this->db = new Database();
  48. // Table prefix
  49. $this->table_prefix = Kohana::config('database.default.table_prefix');
  50. }
  51. }
  52. protected function tearDown()
  53. {
  54. // Garbage collection
  55. unset ($this->api_controller, $this->db, $this->table_prefix);
  56. }
  57. /**
  58. * Tests fetching incidents when only the task is specified - without any
  59. * extra parameters
  60. * @test
  61. */
  62. public function testGetIncidentsByAll()
  63. {
  64. // HTTP GET data
  65. $_GET = array('task' => 'incidents');
  66. ob_start();
  67. $this->api_controller->index();
  68. $contents = json_decode(ob_get_clean());
  69. // Assert the results
  70. $this->assertEquals("0", $contents->error->code);
  71. $this->assertEquals(count(Incident_Model::get_incidents(array(), 20)), count($contents->payload->incidents));
  72. }
  73. /**
  74. * Tests fetching of incidents when the limit parameter is specified
  75. * @test
  76. */
  77. public function testGetIncidentsByLimit()
  78. {
  79. // Randomly generate the record limit
  80. $limit = rand(1, Incident_Model::get_incidents()->count());
  81. // HTTP GET data
  82. $_GET = array('task' => 'incidents', 'limit' => $limit);
  83. ob_start();
  84. $this->api_controller->index();
  85. $contents = json_decode(ob_get_clean());
  86. $this->assertEquals("0", $contents->error->code);
  87. $this->assertEquals($limit, count($contents->payload->incidents));
  88. }
  89. /**
  90. * Tests fetching an incident by its database ID.
  91. * The operation should return a single record on succeed
  92. * @test
  93. */
  94. public function testGetIncidentsById()
  95. {
  96. // Get a random incident id
  97. $incident_id = testutils::get_random_id('incident', 'WHERE incident_active = 1');
  98. // HTTP GET data
  99. $_GET = array('task' => 'incidents', 'by' => 'incidentid', 'id'=> $incident_id);
  100. ob_start();
  101. $this->api_controller->index();
  102. $contents = json_decode(ob_get_clean());
  103. $this->assertEquals("0", $contents->error->code);
  104. $this->assertEquals($incident_id, (int)$contents->payload->incidents[0]->incident->incidentid);
  105. }
  106. /**
  107. * Tests fetching incidents by the ID of an incident that has not been
  108. * approved
  109. */
  110. public function testGetIncidentsByUnapprovedIncidentId()
  111. {
  112. // Get a random incident
  113. $incident = ORM::factory('incident', testutils::get_random_id('incident'));
  114. // Get the current incident status
  115. $incident_active = $incident->incident_active;
  116. $incident->incident_active = 0;
  117. $incident->save();
  118. // Verify that incident_active = 0
  119. $this->assertEquals(0, $incident->incident_active);
  120. // HTTP GET data
  121. $_GET = array('task' => 'incidents', 'by' => 'incidentid', 'id'=> $incident->id);
  122. ob_start();
  123. $this->api_controller->index();
  124. $contents = json_decode(ob_get_clean());
  125. // Verify that no records have been returned
  126. $this->assertEquals("007", $contents->error->code, sprintf("No records should be returned for incident %d", $incident->id));
  127. // Change the incident_active back to its original value
  128. $incident->incident_active = $incident_active;
  129. $incident->save();
  130. // Garbage collection
  131. unset ($incident, $incident_active);
  132. }
  133. /**
  134. * Gets incidents by lat/lon
  135. * @test
  136. */
  137. public function testGetIncidentsByLatLon()
  138. {
  139. // @todo Get random lat/lon
  140. }
  141. /**
  142. * Gets incidents by location id
  143. * @test
  144. */
  145. public function testGetIncidentsByLocationId()
  146. {
  147. // Get location_id from a randomly selected incident
  148. $location_id = testutils::get_random_id('location',
  149. 'WHERE id IN (SELECT location_id FROM '.$this->table_prefix.'incident WHERE incident_active = 1)');
  150. // HTTP GET data
  151. $_GET = array('task' => 'incidents', 'by' => 'locid', 'id'=> $location_id);
  152. ob_start();
  153. $this->api_controller->index();
  154. $contents = json_decode(ob_get_clean());
  155. // Vary test depending on the incident status
  156. $this->assertEquals("0", $contents->error->code, sprintf("No records found for location id: %d", $location_id));
  157. // Garbage collection
  158. unset($location_id);
  159. }
  160. /**
  161. * Gets incidents by location name
  162. * @test
  163. */
  164. public function testGetIncidentsByLocationName()
  165. {
  166. // Get random location id
  167. $location_id = testutils::get_random_id('location',
  168. 'WHERE id IN (SELECT location_id FROM '.$this->table_prefix.'incident WHERE incident_active = 1)');
  169. // Get the location name
  170. $location_name = ORM::factory('location', $location_id)->location_name;
  171. // HTTP GET data
  172. $_GET = array('task' => 'incidents', 'by' => 'locname', 'name'=> $location_name);
  173. ob_start();
  174. $this->api_controller->index();
  175. $contents = json_decode(ob_get_clean());
  176. // Vary test depending on the incident status
  177. $this->assertEquals("0", $contents->error->code, sprintf("No data found for location :%s", $location_name));
  178. // Garbage collection
  179. unset ($location_id, $location_name);
  180. }
  181. /**
  182. * Gets incidents by catgory id that is visible
  183. * @test
  184. */
  185. public function testGetIncidentsByCategoryId()
  186. {
  187. // Get a random category id
  188. $category = ORM::factory('incident_category', testutils::get_random_id('incident_category'))->category;
  189. $category_visible = $category->category_visible;
  190. $category->category_visible = 1;
  191. $category->save();
  192. // HTTP GET data
  193. $_GET = array('task' => 'incidents', 'by' => 'catid', 'id'=> $category->id);
  194. ob_start();
  195. $this->api_controller->index();
  196. $contents = json_decode(ob_get_clean());
  197. $this->assertEquals(0, (int)$contents->error->code, sprintf("No incidents for category id %d", $category->id));
  198. // Get random index for the payload data items
  199. $index = rand(0, count($contents->payload->incidents) - 1);
  200. $this->assertGreaterThanOrEqual(1, count($contents->payload->incidents[$index]->categories));
  201. // Set the category back to its original visibile status
  202. $category->category_visible = $category_visible;
  203. $category->save();
  204. // Garbage collection
  205. unset ($category, $category_visible);
  206. }
  207. /**
  208. * Tests fetching of incidents via a category id that is invisible
  209. */
  210. public function testIncidentsByInvisibleCategoryId()
  211. {
  212. // Get random category
  213. $incident_category = ORM::factory('incident_category', testutils::get_random_id('incident_category'));
  214. if ( ! $incident_category->loaded)
  215. {
  216. // Skip test if not loaded
  217. $this->markTestSkipped('There are no entries in the incident_category table');
  218. }
  219. else
  220. {
  221. // Get the category
  222. $category = $incident_category->category;
  223. // Verify that the category is loaded
  224. $this->assertTrue($category->loaded, 'The category could not be loaded');
  225. // Verify the category id
  226. $category_visible = $category->category_visible;
  227. // Set the category visibility to 0
  228. $category->category_visible = 0;
  229. // Save
  230. $category->save();
  231. // Verify that the category visibility is 0
  232. $this->assertEquals(0, $category->category_visible, sprintf("Category %d is still visible", $category->id));
  233. // HTTP GET data
  234. $_GET = array('task' => 'incidents', 'by' => 'catid', 'id'=> $category->id);
  235. ob_start();
  236. $this->api_controller->index();
  237. $contents = json_decode(ob_get_clean());
  238. // Category is invisible, therefore no records should be returned
  239. $this->assertEquals("007", $contents->error->code,
  240. sprintf("Catgeory %d is invisible therefore no records should be returned", $category->id));
  241. // Set the category visibility back to its original value
  242. $category->category_visible = $category_visible;
  243. $category->save();
  244. $this->assertEquals($category_visible, $category->category_visible, "Category visibility not changed back to original value");
  245. // Garbage collection
  246. unset ($category);
  247. }
  248. // Garbage collection
  249. unset ($incident_category);
  250. }
  251. /**
  252. * Tests fetching incidents by category name
  253. */
  254. public function testGetIncidentsByCategoryName()
  255. {
  256. // Get random incident category record
  257. $category_id = testutils::get_random_id('category',
  258. 'WHERE category_visible = 1 AND id IN (SELECT category_id FROM '.$this->table_prefix.'incident_category)');
  259. // Get the category name
  260. $category_name = ORM::factory('category', $category_id)->category_title;
  261. // HTTP GET data
  262. $_GET = array('task' => 'incidents', 'by' => 'catname', 'name'=> $category_name);
  263. ob_start();
  264. $this->api_controller->index();
  265. $contents = json_decode(ob_get_clean());
  266. // Test for successful execution
  267. $this->assertEquals("0", $contents->error->code, sprintf("No data found for the '%s' category", $category_name));
  268. // Garbage collection
  269. unset ($category_id, $category_name);
  270. }
  271. /**
  272. * Tests fetching incidents using the sinceid parameter
  273. */
  274. public function testGetIncidentsBySinceId()
  275. {
  276. // Get random incident id - it must be active
  277. $incident_id = testutils::get_random_id('incident', 'WHERE incident_active = 1');
  278. // HTTP GET data
  279. $_GET = array('task' => 'incidents', 'by' => 'sinceid', 'id'=> $incident_id);
  280. ob_start();
  281. $this->api_controller->index();
  282. $contents = json_decode(ob_get_clean());
  283. $this->assertEquals("0", $contents->error->code);
  284. // Get random index for the payload data
  285. $index = rand(0, count($contents->payload->incidents) - 1);
  286. // Fetched incidents should have an id greather than the search parameter
  287. $this->assertGreaterThan($incident_id, (int)$contents->payload->incidents[$index]->incident->incidentid);
  288. }
  289. public function testGetIncidentsByMaxId()
  290. {
  291. // Get random incident id - it must be active
  292. $incident_id = testutils::get_random_id('incident', 'WHERE incident_active = 1');
  293. // HTTP GET data
  294. $_GET = array('task' => 'incidents', 'by' => 'maxid', 'id'=> $incident_id);
  295. ob_start();
  296. $this->api_controller->index();
  297. $contents = json_decode(ob_get_clean());
  298. // Test for successful execution
  299. $this->assertEquals("0", $contents->error->code);
  300. // Get random index for the payload data
  301. $index = rand(0, count($contents->payload->incidents) - 1);
  302. // Fetched incidents should have an id less than or equal to the search parameter
  303. $this->assertLessThanOrEqual($incident_id, (int)$contents->payload->incidents[$index]->incident->incidentid);
  304. }
  305. /**
  306. * Tests fetching incidents by a bounded area
  307. * @test
  308. */
  309. public function testGetIncidentsByBounds()
  310. {
  311. // @todo Helper method to generate a bounding box
  312. }
  313. }
  314. ?>