/** * Data Provider for testGenerateArrayMap * @dataProvider */ public function providerTestGenerateArrayMap() { /* Category Element/Attribute maps */ // Select a random category $category = ORM::factory('category', testutils::get_random_id('category')); // Category map $category_map = array('attributes' => array('color' => 'category_color', 'visible' => 'category_visible', 'trusted' => 'category_trusted'), 'elements' => array('title' => 'category_title', 'description' => 'category_description')); // Expected category array map $category_element_map = array('attributes' => array('color' => $category->category_color, 'visible' => $category->category_visible, 'trusted' => $category->category_trusted), 'elements' => array('title' => $category->category_title, 'description' => $category->category_description)); /* Category translation Element/Attribute maps */ // Translation ORM Object $translation = ORM::factory('category_lang', testutils::get_random_id('category_lang', 'WHERE category_id =' . $category->id . '')); // Translation map $translation_map = array('attributes' => array('locale' => 'locale'), 'elements' => array('translation_title' => 'category_title', 'translation_description' => 'category_description')); // Expected translation array map $translation_element_map = array('attributes' => array('locale' => $translation->locale), 'elements' => array('translation_title' => $translation->category_title, 'translation_description' => $translation->category_description)); /* Form element/attribute maps */ // Select a random form $form = ORM::factory('form', testutils::get_random_id('form')); // Forms map $form_map = array('attributes' => array('active' => 'form_active'), 'elements' => array('title' => 'form_title', 'description' => 'form_description')); // Expected form array map $form_element_map = array('attributes' => array('active' => $form->form_active), 'elements' => array('title' => $form->form_title, 'description' => $form->form_description)); /* Reports element/attribute maps */ // Select a random incident $incident = ORM::factory('incident', testutils::get_random_id('incident')); // Report map $report_map = array('attributes' => array('id' => 'id', 'approved' => 'incident_active', 'verified' => 'incident_verified', 'mode' => 'incident_mode'), 'elements' => array('title' => 'incident_title', 'date' => 'incident_date', 'dateadd' => 'incident_dateadd', 'description' => 'incident_description')); // Expected report array map $report_element_map = array('attributes' => array('id' => $incident->id, 'approved' => $incident->incident_active, 'verified' => $incident->incident_verified, 'mode' => $incident->incident_mode), 'elements' => array('title' => $incident->incident_title, 'date' => $incident->incident_date, 'dateadd' => $incident->incident_dateadd, 'description' => $incident->incident_description)); /* Report Location */ // Report location ORM object $location = $incident->location; // Location Map $location_map = array('attributes' => array(), 'elements' => array('name' => 'location_name', 'longitude' => 'longitude', 'latitude' => 'latitude')); // Expected location array map $location_element_map = array('attributes' => array(), 'elements' => array('name' => $location->location_name, 'longitude' => $location->longitude, 'latitude' => $location->latitude)); /* Report Media */ // Report Media ORM Object $media = ORM::factory('media', testutils::get_random_id('media', 'WHERE incident_id =' . $incident->id . '')); // Media Map $media_map = array('attributes' => array('type' => 'media_type', 'active' => 'media_active', 'date' => 'media_date'), 'elements' => array()); // Expected media array map $media_element_map = array('attributes' => array('type' => $media->media_type, 'active' => $media->media_active, 'date' => $media->media_date), 'elements' => array()); /* Report personal info */ // Personal info ORM Object $person = $incident->incident_person; // Personal info map $person_map = array('attributes' => array(), 'elements' => array('firstname' => 'person_first', 'lastname' => 'person_last', 'email' => 'person_email')); // Expected personal info array map $person_element_map = array('attributes' => array(), 'elements' => array('firstname' => $person->person_first, 'lastname' => $person->person_last, 'email' => $person->person_email)); /* Incident Categories */ // Incident Category ORM Object $incident_cat = ORM::Factory('category')->join('incident_category', 'incident_category.category_id', 'category.id', 'inner')->where('incident_category.incident_id', $incident->id)->limit(1)->find(); // Incident Category map $incident_cat_map = array('attributes' => array(), 'elements' => array('category' => 'category_title')); // Expected incident category array Map $incident_cat_element_map = array('attributes' => array(), 'elements' => array('category' => $incident_cat->category_title)); return array(array($category_map, $category_element_map, $category, 'Category'), array($translation_map, $translation_element_map, $translation, 'Category Translation'), array($form_map, $form_element_map, $form, 'Form'), array($report_map, $report_element_map, $incident, 'Report'), array($location_map, $location_element_map, $location, 'Report Location'), array($media_map, $media_element_map, $media, 'Report Media'), array($person_map, $person_element_map, $person, 'Reporter'), array($incident_cat_map, $incident_cat_element_map, $incident_cat, 'Incident category')); }
/** * Tests Incident_Model::is_valid_incident * @test */ public function testIsValidIncident() { // Get any incident $random_incident = testutils::get_random_id('incident'); $inactive_incident = testutils::get_random_id('incident', 'WHERE incident_active = 0'); $active_incident = testutils::get_random_id('incident', 'WHERE incident_active = 1'); //Test to see if there are data in the incident table to test with. if (empty($random_incident)) { $this->markTestSkipped('The incident table is empty.'); } elseif (empty($inactive_incident)) { $this->markTestSkipped('No inactive incidents in incident table.'); } elseif (empty($active_incident)) { $this->markTestSkipped('No active incidents in incident table.'); } else { $this->assertEquals(TRUE, Incident_Model::is_valid_incident($random_incident, FALSE)); // Get inactive incident $inactive_incident = testutils::get_random_id('incident', 'WHERE incident_active = 0'); // Check fails with default args and explicitly limit to active only $this->assertEquals(FALSE, Incident_Model::is_valid_incident($inactive_incident)); $this->assertEquals(FALSE, Incident_Model::is_valid_incident($inactive_incident, TRUE)); // Check success when including inactive incidents $this->assertEquals(TRUE, Incident_Model::is_valid_incident($inactive_incident, FALSE)); // Get active incident $active_incident = testutils::get_random_id('incident', 'WHERE incident_active = 1'); // Check success with default args and explicitly limit to active only $this->assertEquals(TRUE, Incident_Model::is_valid_incident($active_incident)); $this->assertEquals(TRUE, Incident_Model::is_valid_incident($active_incident, TRUE)); // Null incident value $this->assertEquals(FALSE, Incident_Model::is_valid_incident(NULL)); // Non numeric incident value $this->assertEquals(FALSE, Incident_Model::is_valid_incident('0.999')); } }
/** * Tests Feed_Model::is_valid_feed * @test */ public function testIsValidFeed() { if (ORM::factory('feed')->count_all() == 0) { $this->markTestSkipped('There are no records in the feeds table'); } // Test with a valid feed id $random_feed_id = testutils::get_random_id('feed'); $this->assertEquals(TRUE, Feed_Model::is_valid_feed($random_feed_id)); // Test with an invalid feed id $this->assertEquals(FALSE, Feed_Model::is_valid_feed('90.9999')); }
/** * Tests fetching of locations by location id */ public function testGetLocationsByLocationId() { // Get a random location $location_id = testutils::get_random_id('location'); // Parameters to submit $_GET = array('task' => 'locations', 'by' => 'locid', 'id' => $location_id); ob_start(); $this->api_controller->index(); $contents = json_decode(ob_get_clean()); $this->assertEquals("0", $contents->error->code); $this->assertEquals($location_id, (int) $contents->payload->locations[0]->location->id); }
/** * Tests reports::fetch_incidents() * @test * * This tests compares the output SQL of fetch_incidents against a pre-defined SQL * statement based on dummy values. The objective of this test is to check whether * reports::fetch_incidents processes the parameters property */ public function testFetchIncidents() { // Get random location and fetch the latitude and longitude $location = ORM::factory('location', testutils::get_random_id('location')); $longitude = $location->longitude; $latitude = $location->latitude; // Build the list of HTTP_GET parameters $filter_params = array('c' => array(3, 4, 5), 'start_loc' => $latitude . "," . $longitude, 'radius' => '20', 'mode' => array(1, 2), 'm' => array(1), 'from' => '07/07/2011', 'to' => '07/21/2011', 'v' => 1); // Add the report filter params to the list of HTTP_GET parameters $_GET = array_merge($_GET, $filter_params); // Get the incidents $incidents = reports::fetch_incidents(); // Get the table prefix $table_prefix = Kohana::config('database.default.table_prefix'); // Expected SQL statement; based on the $filter_params above $expected_sql = "SELECT DISTINCT i.id incident_id, i.incident_title, i.incident_description, i.incident_date, " . "i.incident_mode, i.incident_active, i.incident_verified, i.location_id, l.country_id, l.location_name, l.latitude, l.longitude " . ", ((ACOS(SIN(" . $latitude . " * PI() / 180) * SIN(l.`latitude` * PI() / 180) + COS(" . $latitude . " * PI() / 180) * " . "\tCOS(l.`latitude` * PI() / 180) * COS((" . $longitude . " - l.`longitude`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance " . "FROM " . $table_prefix . "incident i " . "INNER JOIN " . $table_prefix . "location l ON (i.location_id = l.id) " . "INNER JOIN " . $table_prefix . "incident_category ic ON (ic.incident_id = i.id) " . "INNER JOIN " . $table_prefix . "category c ON (ic.category_id = c.id) " . "WHERE i.incident_active = 1 " . "AND (c.id IN (" . implode(",", $filter_params['c']) . ") OR c.parent_id IN (" . implode(",", $filter_params['c']) . ")) " . "AND c.category_visible = 1 " . "AND i.incident_mode IN (" . implode(",", $filter_params['mode']) . ") " . "AND i.incident_date >= \"2011-07-07\" " . "AND i.incident_date <= \"2011-07-21\" " . "AND i.id IN (SELECT DISTINCT incident_id FROM " . $table_prefix . "media WHERE media_type IN (" . implode(",", $filter_params['m']) . ")) " . "AND i.incident_verified IN (" . $filter_params['v'] . ") " . "HAVING distance <= " . $filter_params['radius'] . " " . "ORDER BY i.incident_date DESC "; // Test the expected SQL against the returned $this->assertEquals($expected_sql, $incidents->sql()); // Garbage collection unset($location, $latitude, $longitude, $incidents, $filter_params); }
/** * Tests Incident_Model::is_valid_incident * @test */ public function testIsValidIncident() { // Get any incident $random_incident = testutils::get_random_id('incident'); //Test to see if there are data in the incident table to test with. if (empty($random_incident)) { $this->markTestSkipped('The incident table is empty.'); } else { $this->assertEquals(TRUE, Incident_Model::is_valid_incident($random_incident)); // Get inactive incident $inactive_incident = testutils::get_random_id('incident', 'WHERE incident_active = 0'); $this->assertEquals(FALSE, Incident_Model::is_valid_incident($inactive_incident, TRUE)); // Get active incident $active_incident = testutils::get_random_id('incident', 'WHERE incident_active = 1'); $this->assertEquals(TRUE, Incident_Model::is_valid_incident($active_incident, TRUE)); // Null incident value $this->assertEquals(FALSE, Incident_Model::is_valid_incident(NULL)); // Non numeric incident value $this->assertEquals(FALSE, Incident_Model::is_valid_incident('0.999')); } }
/** * Tests reports::fetch_incidents() * @test * * This tests compares the output SQL of fetch_incidents against a pre-defined SQL * statement based on dummy values. The objective of this test is to check whether * reports::fetch_incidents processes the parameters property */ public function testFetchIncidents() { // Get random location and fetch the latitude and longitude $location = ORM::factory('location', testutils::get_random_id('location')); $longitude = $location->longitude; $latitude = $location->latitude; // Build the list of HTTP_GET parameters $filter_params = array('c' => array(3, 4, 5), 'start_loc' => $latitude . "," . $longitude, 'radius' => '20', 'mode' => array(1, 2), 'm' => array(1), 'from' => '07/07/2011', 'to' => '07/21/2011', 'v' => 1); // Add the report filter params to the list of HTTP_GET parameters $_GET = array_merge($_GET, $filter_params); // Get the incidents $incidents = reports::fetch_incidents(); // Get the table prefix $table_prefix = Kohana::config('database.default.table_prefix'); // Expected SQL statement; based on the $filter_params above // Distance calculation deets: // 60 = nautical miles per degree of latitude, 1.1515 miles in every nautical mile, 1.609344 km = 1 km // more details about the math here: http://sgowtham.net/ramblings/2009/08/04/php-calculating-distance-between-two-locations-given-their-gps-coordinates/ $expected_sql = "SELECT DISTINCT i.id incident_id, i.incident_title, i.incident_description, i.incident_date, " . "i.incident_mode, i.incident_active, i.incident_verified, i.location_id, l.country_id, l.location_name, l.latitude, l.longitude " . ", ((ACOS(SIN(" . $latitude . " * PI() / 180) * SIN(l.`latitude` * PI() / 180) + COS(" . $latitude . " * PI() / 180) * " . "\tCOS(l.`latitude` * PI() / 180) * COS((" . $longitude . " - l.`longitude`) * PI() / 180)) * 180 / PI()) * 60 * 1.1515 * 1.609344) AS distance " . "FROM " . $table_prefix . "incident i " . "LEFT JOIN " . $table_prefix . "location l ON (i.location_id = l.id) " . "LEFT JOIN " . $table_prefix . "incident_category ic ON (ic.incident_id = i.id) " . "LEFT JOIN " . $table_prefix . "category c ON (ic.category_id = c.id) " . "WHERE i.incident_active = 1 " . "AND (c.id IN (" . implode(",", $filter_params['c']) . ") OR c.parent_id IN (" . implode(",", $filter_params['c']) . ")) " . "AND c.category_visible = 1 " . "AND i.incident_mode IN (" . implode(",", $filter_params['mode']) . ") " . "AND i.incident_date >= \"2011-07-07 00:00:00\" " . "AND i.incident_date <= \"2011-07-21 23:59:59\" " . "AND i.id IN (SELECT DISTINCT incident_id FROM " . $table_prefix . "media WHERE media_type IN (" . implode(",", $filter_params['m']) . ")) " . "AND i.incident_verified IN (" . $filter_params['v'] . ") " . "HAVING distance <= " . $filter_params['radius'] . " " . "ORDER BY i.incident_date DESC "; // Test the expected SQL against the returned $this->assertEquals($expected_sql, $incidents->sql()); // Garbage collection unset($location, $latitude, $longitude, $incidents, $filter_params); }
/** * Tests fetching countries by country ID * @test */ public function testGetCountryById() { // Test fetching by ISO code $country = ORM::factory('country', testutils::get_random_id('country')); // Test fetching countries by database id $_GET = array('task' => 'countries', 'by' => 'countryid', 'id' => $country->id); // Fetch the content ob_start(); $this->api_controller->index(); $contents = json_decode(ob_get_clean()); $this->assertEquals($country->id, $contents->payload->countries[0]->country->id); $this->assertEquals(0, (int) $contents->error->code); // Test "No Data Found" // Test using invalid country id $_GET['id'] = 'PL'; ob_start(); $this->api_controller->index(); $contents = json_decode(ob_get_clean()); $this->assertEquals("007", $contents->error->code); }
public function testGetIncidentsByMaxId() { // Get random incident id - it must be active and should not be first record in the table $incident_id = testutils::get_random_id('incident', 'WHERE incident_active = 1 AND id NOT IN(SELECT * FROM (SELECT id from incident where incident_active = 1 ORDER BY id ASC LIMIT 1) as first_id)'); // HTTP GET data $_GET = array('task' => 'incidents', 'by' => 'maxid', 'id' => $incident_id); ob_start(); $this->api_controller->index(); $contents = json_decode(ob_get_clean()); // Test for successful execution $this->assertEquals("0", $contents->error->code); // Get random index for the payload data $index = rand(0, count($contents->payload->incidents) - 1); // Fetched incidents should have an id less than or equal to the search parameter $this->assertLessThanOrEqual($incident_id, (int) $contents->payload->incidents[$index]->incident->incidentid); }
/** * Data provider for test_alert_code_exists() * * @return array */ public function providerValidate() { return array(array(testutils::get_random_id('alert', 'WHERE alert_confirmed = 1'))); }
/** * Data provider for testIsValidCategory * * @dataProvider */ public function providerIsValidCategory() { return array(array(testutils::get_random_id('category'), '-1')); }