Changeset 9750
- Timestamp:
- 04/20/2020 10:28:21 PM (5 years ago)
- Location:
- sites/trunk/api.wordpress.org/public_html/events/1.0
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/api.wordpress.org/public_html/events/1.0/index.php
r9749 r9750 182 182 $location 183 183 ); 184 185 $events = remove_duplicate_events( $events ); 184 186 185 187 // Internal location data cannot be exposed in the response, see get_location(). … … 1136 1138 } 1137 1139 } 1138 } 1139 1140 /** 1141 * Remove duplicates events. 1142 * Favor the regional event since it'll be pinned to the top. 1143 */ 1144 foreach ( $regional_wordcamps as $regional_event ) { 1145 $local_events = array_filter( $local_events, function( $local_event ) use ( $regional_event ) { 1146 if ( parse_url( $regional_event['url'], PHP_URL_HOST ) === parse_url( $local_event['url'], PHP_URL_HOST ) ) { 1147 return false; 1148 } 1149 1150 return true; 1151 } ); 1140 1141 1152 1142 } 1153 1143 … … 1274 1264 1275 1265 /** 1266 * Remove duplicate events, based on the URL. 1267 * 1268 * This is needed because sometimes an event gets added from multiple functions; e.g., `get_events()` and also `maybe_add_regional_wordcamps()` and/or `pin_next_online_wordcamp()`. 1269 * 1270 * @param array $events 1271 * 1272 * @return array 1273 */ 1274 function remove_duplicate_events( $events ) { 1275 // Re-index them by a URL to overwrite duplicates 1276 $unique_events = array(); 1277 1278 foreach ( $events as $event ) { 1279 $parsed_url = parse_url( $event['url'] ); 1280 1281 // Take all essential parts to cover current URL structures, and potential future ones. 1282 $normalized_url = sprintf( 1283 '%s%s%s', 1284 $parsed_url['host'], 1285 $parsed_url['path'], 1286 $parsed_url['query'] 1287 ); 1288 $normalized_url = str_replace( '/', '', $normalized_url ); 1289 1290 $unique_events[ $normalized_url ] = $event; 1291 } 1292 1293 // Restore integer keys. 1294 return array_values( $unique_events ); 1295 } 1296 1297 /** 1276 1298 * Create a bounded latitude/longitude box of x KM around specific coordinates. 1277 1299 * -
sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php
r9749 r9750 27 27 $tests_failed += test_is_client_core(); 28 28 $tests_failed += test_get_iso_3166_2_country_codes(); 29 $tests_failed += test_remove_duplicate_events(); 29 30 30 31 $query_count = count( $wpdb->queries ); … … 1258 1259 $tests_expect_changes['core-user-agent'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region ); 1259 1260 1260 // There should only be one entry for an event, even if the local event array already contains the regional event.1261 $tests_expect_no_changes['duplicate-event'] = maybe_add_regional_wordcamps( array( $region_data['us']['event'] ), $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region );1262 1263 1261 foreach ( $tests_expect_no_changes as $name => $result ) { 1264 switch ( $name ) { 1265 case 'duplicate-event': 1266 if ( $result !== array( $region_data['us']['event'] ) ) { 1267 $failed++; 1268 output_results( $name, false, array( $region_data['us']['event'] ), $result ); 1269 } 1270 break; 1271 default: 1272 if ( $result !== $local_events ) { 1273 $failed++; 1274 output_results( $name, false, $local_events, $result ); 1275 } 1276 break; 1262 if ( $result !== $local_events ) { 1263 $failed++; 1264 output_results( $name, false, $local_events, $result ); 1277 1265 } 1278 1266 } … … 1482 1470 1483 1471 /** 1472 * Test `remove_duplicate_events()`. 1473 */ 1474 function test_remove_duplicate_events() { 1475 $duplicate_events = array( 1476 // Each of these represents an event; extraneous fields have been removed for readability. 1477 array ( 1478 'url' => 'https://2020.us.wordcamp.org/', 1479 ), 1480 1481 array ( 1482 'url' => 'https://2020.detroit.wordcamp.org/', 1483 ), 1484 1485 array( 1486 // Intentionally missing the trailing slash, to account for inconsistencies in data. 1487 'url' => 'https://2020.us.wordcamp.org', 1488 ) 1489 ); 1490 1491 printf( "\n\nRunning 1 remove_duplicate_events() test\n" ); 1492 1493 $expected_result = array( 1494 array ( 1495 'url' => 'https://2020.us.wordcamp.org', 1496 ), 1497 1498 array ( 1499 'url' => 'https://2020.detroit.wordcamp.org/', 1500 ), 1501 ); 1502 1503 $actual_result = remove_duplicate_events( $duplicate_events ); 1504 $passed = $expected_result === $actual_result; 1505 1506 output_results( 'remove duplicate events', $passed, $expected_result, $actual_result ); 1507 1508 return $passed ? 0 : 1; 1509 } 1510 1511 /** 1484 1512 * Stub to simulate cache misses, so that the tests always get fresh results 1485 1513 *
Note: See TracChangeset
for help on using the changeset viewer.