Skip to content

Commit 2a32f0e

Browse files
committed
API: Events: Update the unit tests to pass with the latest geoname updates.
git-svn-id: https://meta.svn.wordpress.org/sites/trunk@14889 74240141-8908-4e6f-9713-ba540dce6ec7
1 parent 2aa14d5 commit 2a32f0e

3 files changed

Lines changed: 59 additions & 26 deletions

File tree

‎api.wordpress.org/public_html/events/1.0/index.php‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,14 +223,17 @@ function build_response( $location, $location_args ) {
223223
$events = array();
224224
$error = null;
225225

226+
// Define defaults if not set in the request.
227+
$location_args += array( 'restrict_by_country' => false );
228+
226229
if ( 'temp-request-throttled' === $location ) {
227230
$location = array();
228231
$error = 'temp-request-throttled';
229232
}
230233

231234
if ( $location ) {
232235
$event_args = array(
233-
'is_client_core' => is_client_core( $_SERVER['HTTP_USER_AGENT'] ),
236+
'is_client_core' => is_client_core( $_SERVER['HTTP_USER_AGENT'] ),
234237
'restrict_by_country' => $location_args['restrict_by_country'],
235238
);
236239

@@ -810,13 +813,19 @@ function get_country_from_name( $country_name ) {
810813
function get_events( $args = array() ) {
811814
global $wpdb, $cache_life, $cache_group;
812815

813-
// Sort to ensure consistent cache keys.
814-
ksort( $args );
816+
// Define defaults if not set in the request.
817+
$args += array(
818+
'is_client_core' => false,
819+
'restrict_by_country' => false,
820+
'number' => 10,
821+
);
815822

816823
// number should be between 0 and 100, with a default of 10.
817-
$args['number'] = $args['number'] ?? 10;
818824
$args['number'] = max( 0, min( $args['number'], 100 ) );
819825

826+
// Sort to ensure consistent cache keys.
827+
ksort( $args );
828+
820829
// Distances in kilometers
821830
$event_distances = array(
822831
'meetup' => 100,

‎api.wordpress.org/public_html/events/1.0/tests/Test_Events.php‎

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class Test_Events extends TestCase {
1717
* Asserts that an HTTP response is valid and contains an event.
1818
*/
1919
public function assertResponseHasEvent( $response ) {
20-
$body = json_decode( $response->body );
21-
$event = $body->events[0];
20+
$this->assertSame( 200, $response->status_code, 'Non-200 response body: ' . substr( $response->body, 0, 200 ) );
2221

23-
$this->assertSame( 200, $response->status_code );
22+
$body = json_decode( $response->body );
23+
$this->assertIsObject( $body, 'Response body was not valid JSON: ' . substr( $response->body, 0, 200 ) );
2424
$this->assertNull( $body->error );
2525

2626
$this->assertIsObject( $body->location );
@@ -29,6 +29,8 @@ public function assertResponseHasEvent( $response ) {
2929
( isset( $body->location->latitude ) && is_numeric( $body->location->latitude ) )
3030
);
3131

32+
$this->assertNotEmpty( $body->events, 'Response contained no events.' );
33+
$event = $body->events[0];
3234
$this->assertContains( $event->type, array( 'wordcamp', 'meetup' ) );
3335
$this->assertIsString( $event->title );
3436
$this->assertIsNumeric( $event->start_unix_timestamp );
@@ -471,20 +473,31 @@ public function test_get_location( array $input, $expected ) : void {
471473
$actual_result['description'] = strtolower( $actual_result['description'] );
472474
}
473475

474-
/*
475-
* Normalize coordinates to account for minor differences in the databases.
476-
*
477-
* Rounding to three decimal places means that we're still accurate within about 110 meters, which is
478-
* good enough for our purposes.
479-
*
480-
* @link https://gis.stackexchange.com/a/8674/49125.
481-
*/
482-
if ( isset( $actual_result['latitude'], $actual_result['longitude'] ) ) {
483-
$actual_result['latitude'] = number_format( round( $actual_result['latitude'], 3 ), 3 );
484-
$actual_result['longitude'] = number_format( round( $actual_result['longitude'], 3 ), 3 );
476+
$this->assertSameWithLatLonDelta( $expected, $actual_result );
477+
}
478+
479+
/**
480+
* Assert that two location arrays match, allowing small drift in latitude/longitude.
481+
*
482+
* Compares lat/lon with a tolerance to absorb routine drift in the geo databases, and
483+
* compares the remaining fields exactly. A delta of 0.01 degrees is roughly 1km at the
484+
* equator — tight enough to catch a wrong-city match, loose enough to survive geonames
485+
* updates.
486+
*
487+
* @link https://gis.stackexchange.com/a/8674/49125.
488+
*/
489+
public function assertSameWithLatLonDelta( $expected, $actual, float $delta = 0.01 ) : void {
490+
if ( is_array( $expected ) && is_array( $actual )
491+
&& isset( $expected['latitude'], $expected['longitude'], $actual['latitude'], $actual['longitude'] )
492+
) {
493+
$this->assertEqualsWithDelta( (float) $expected['latitude'], (float) $actual['latitude'], $delta, 'latitude drifted beyond tolerance' );
494+
$this->assertEqualsWithDelta( (float) $expected['longitude'], (float) $actual['longitude'], $delta, 'longitude drifted beyond tolerance' );
495+
496+
// Strip the coordinates so the remaining fields can be compared exactly.
497+
unset( $expected['latitude'], $expected['longitude'], $actual['latitude'], $actual['longitude'] );
485498
}
486499

487-
$this->assertSame( $expected, $actual_result );
500+
$this->assertSame( $expected, $actual );
488501
}
489502

490503
public static function data_get_location() : array {
@@ -813,12 +826,12 @@ public static function data_get_location() : array {
813826

814827
'city-endonym-ideographic-asia3' => array(
815828
'input' => array(
816-
'location_name' => 'كراچى',
829+
'location_name' => 'کراچی',
817830
'locale' => 'ur',
818831
'timezone' => 'Asia/Karachi',
819832
),
820833
'expected' => array(
821-
'description' => 'كراچى',
834+
'description' => 'کراچی',
822835
'latitude' => '24.861',
823836
'longitude' => '67.010',
824837
'country' => 'PK',
@@ -1464,7 +1477,8 @@ public static function data_build_response() : array {
14641477

14651478
'throttled' => array(
14661479
'input' => array(
1467-
'location' => 'temp-request-throttled',
1480+
'location' => 'temp-request-throttled',
1481+
'location_args' => array(),
14681482
),
14691483
'expected' => array(
14701484
'location' => array(),
@@ -1475,7 +1489,8 @@ public static function data_build_response() : array {
14751489

14761490
'no-location' => array(
14771491
'input' => array(
1478-
'location' => array(),
1492+
'location' => array(),
1493+
'location_args' => array(),
14791494
),
14801495
'expected' => array(
14811496
'location' => array(),

‎api.wordpress.org/public_html/events/1.0/tests/bootstrap.php‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
}
1212

1313
// Signal to index.php that we're running tests so it skips main() / bootstrap() / init.php.
14-
define( 'WPORG_RUNNING_TESTS', true );
14+
defined( 'WPORG_RUNNING_TESTS' ) || define( 'WPORG_RUNNING_TESTS', true );
1515

1616
// Time constants normally defined inside main().
1717
defined( 'HOUR_IN_SECONDS' ) || define( 'HOUR_IN_SECONDS', 60 * 60 );
@@ -41,6 +41,13 @@
4141
// Load the API entry-point so its functions are defined. main() is gated by WPORG_RUNNING_TESTS.
4242
require_once dirname( __DIR__ ) . '/index.php';
4343

44+
// Load deps (hyperdb, etc.) so $wpdb is available for needs-db tests. Skipped on
45+
// open-source checkouts that don't have the API config — the bootstrap() call
46+
// itself requires init.php internally, which is gated above.
47+
if ( file_exists( $api_init_file ) ) {
48+
\Dotorg\API\Events\bootstrap();
49+
}
50+
4451
// Provide cache-function stubs (normally registered by disable_caching() inside main()).
4552
\Dotorg\API\Events\disable_caching();
4653

@@ -54,8 +61,9 @@
5461
* @throws \RuntimeException When the request fails at the transport layer.
5562
*/
5663
function send_request( $path ) {
57-
$sandboxed = defined( 'WPORG_SANDBOXED' ) ? WPORG_SANDBOXED : false;
58-
$host = $sandboxed ? $sandboxed . '.wordpress.org' : 'api.wordpress.org';
64+
// On a sandbox, the request has to be made to localhost with the Host header overridden.
65+
$sandboxed = defined( 'WPORG_SANDBOXED' ) && WPORG_SANDBOXED;
66+
$host = $sandboxed ? '127.0.0.1' : 'api.wordpress.org';
5967
$url = 'https://' . $host . $path;
6068

6169
$body = file_get_contents(
@@ -69,6 +77,7 @@ function send_request( $path ) {
6977
'timeout' => 15,
7078
),
7179
'ssl' => array(
80+
'verify_peer' => ! $sandboxed,
7281
'verify_peer_name' => ! $sandboxed,
7382
),
7483
) )

0 commit comments

Comments
 (0)