- Timestamp:
- 08/17/2019 12:50:17 AM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php
r7188 r9108 22 22 $tests_failed += test_get_events(); 23 23 $tests_failed += test_get_events_country_restriction(); 24 $tests_failed += test_ add_regional_wordcamps();24 $tests_failed += test_maybe_add_regional_wordcamps(); 25 25 $tests_failed += test_maybe_add_wp15_promo(); 26 26 $tests_failed += test_build_response(); … … 732 732 733 733 /* 734 * Only the IPv4 address is given 734 * Only the IPv4 address is given. 735 * 736 * Note that IP locations change frequently, so some of these expected results will inevitably become outdated 737 * and cause tests to fail. 735 738 * 736 739 * See https://awebanalysis.com/en/ipv4-directory/ … … 750 753 'input' => array( 'ip' => '86.108.55.28' ), 751 754 'expected' => array( 752 'description' => ' amman',753 'latitude' => '3 1.955',754 'longitude' => '35. 945',755 'description' => 'hakama', 756 'latitude' => '32.594', 757 'longitude' => '35.884', 755 758 'country' => 'JO', 756 759 'internal' => true, … … 761 764 'input' => array( 'ip' => '80.95.186.144' ), 762 765 'expected' => array( 763 'description' => ' belfast',764 'latitude' => '54. 583',765 'longitude' => '- 5.933',766 'description' => 'antrim', 767 'latitude' => '54.700', 768 'longitude' => '-6.200', 766 769 'country' => 'GB', 767 770 'internal' => true, … … 772 775 'input' => array( 'ip' => '189.147.186.0' ), 773 776 'expected' => array( 774 'description' => ' cuauhtemoc',775 'latitude' => '19.4 17',776 'longitude' => '-99.1 57',777 'description' => 'mexico city', 778 'latitude' => '19.428', 779 'longitude' => '-99.128', 777 780 'country' => 'MX', 778 781 'internal' => true, … … 803 806 804 807 /* 805 * Only an IPv6 address is given 808 * Only an IPv6 address is given. 809 * 810 * Note that IP locations change frequently, so some of these expected results will inevitably become outdated 811 * and cause tests to fail. 806 812 * 807 813 * See https://www.google.com/intl/en/ipv6/statistics.html#tab=per-country-ipv6-adoption&tab=per-country-ipv6-adoption … … 867 873 'input' => array( 'ip' => '2001:1388:6643:2736:10f1:897c:428c:1b3b' ), 868 874 'expected' => array( 869 'description' => ' ayacucho',870 'latitude' => '-1 3.158',871 'longitude' => '-7 4.224',875 'description' => 'lima', 876 'latitude' => '-12.043', 877 'longitude' => '-77.028', 872 878 'country' => 'PE', 873 879 'internal' => true, … … 1162 1168 * @return int 1163 1169 */ 1164 function test_ add_regional_wordcamps() {1170 function test_maybe_add_regional_wordcamps() { 1165 1171 $failed = 0; 1166 1172 … … 1173 1179 ) ); 1174 1180 1181 $region_data = array( 1182 'us' => array( 1183 'promo_start' => strtotime( '2019-08-16 00:00:00' ), 1184 'regional_countries' => array( 1185 'us', 'ca', 'bz', 'cr', 'sv', 'gt', 'hn', 'mx', 'ni', 'pa', 1186 'ar', 'bo', 'br', 'cl', 'co', 'ec', 'gf', 'gy', 'py', 'pe', 1187 'sr', 'uy', 've', 'ag', 'aw', 'bs', 'bb', 'ky', 'cu', 'dm', 1188 'do', 'gd', 'ht', 'jm', 'kn', 'lc', 'vc', 'tt', 1189 ), 1190 'event' => array( 1191 'type' => 'wordcamp', 1192 'title' => 'WordCamp US', 1193 'url' => 'https://2019.us.wordcamp.org/', 1194 'meetup' => '', 1195 'meetup_url' => '', 1196 'date' => '2019-11-01 00:00:00', 1197 'location' => array( 1198 'location' => 'St. Louis, MO, USA', 1199 'country' => 'US', 1200 'latitude' => 38.6532135, 1201 'longitude' => -90.3136733, 1202 ), 1203 ), 1204 ), 1205 ); 1206 1207 $core_user_agent = 'WordPress/5.2; https://example.org'; 1208 $other_user_agent = 'Smith'; 1209 1210 $time_before_promo = strtotime( '2019-08-15 00:00:00' ); 1211 $time_during_promo_phase_1 = strtotime( '+ 1 day', $region_data['us']['promo_start'] ); 1212 $time_during_promo_phase_2 = strtotime( '+ 2 weeks + 1 day', $region_data['us']['promo_start'] ); 1213 $time_during_promo_phase_3 = strtotime( '+ 4 weeks + 1 day', $region_data['us']['promo_start'] ); 1214 $time_after_promo = strtotime( '+ 6 weeks + 1 day', $region_data['us']['promo_start'] ); 1215 1216 $location_country_within_region = array( 1217 'country' => 'us', 1218 ); 1219 1220 $location_country_outside_region = array( 1221 'country' => 'es', 1222 ); 1223 1224 $location_ip_only = array( 1225 'ip' => '8.8.8.8', 1226 ); 1227 1175 1228 // Make sure there's at least one event, otherwise there could be false positives. 1176 1229 if ( ! $local_events ) { … … 1178 1231 } 1179 1232 1180 printf( "\n\nRunning %d add_regional_wordcamps() tests\n", 2 ); 1181 1182 // Test that no changes were made if the user agent isn't Core. 1183 $events_no_user_agent = add_regional_wordcamps( $local_events, '' ); 1184 1185 if ( $events_no_user_agent !== $local_events ) { 1186 $failed++; 1187 output_results( 'no-user-agent', false, $local_events, $events_no_user_agent ); 1233 printf( "\n\nRunning %d add_regional_wordcamps() tests\n", 13 ); 1234 1235 $tests_expect_no_changes = array(); 1236 $tests_expect_changes = array(); 1237 1238 // No regional camps should be added if before the promo start date or after the promo window is past (6 weeks). 1239 $tests_expect_no_changes['before-promo'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_before_promo, $location_country_within_region ); 1240 $tests_expect_no_changes['before-promo'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_after_promo, $location_country_within_region ); 1241 1242 // Regional camp should be added if it's within phase 1 of the promo, regardless of location. 1243 $tests_expect_changes['promo-phase-1-within-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_within_region ); 1244 $tests_expect_changes['promo-phase-1-outside-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_1, $location_country_outside_region ); 1245 1246 // Regional camp should only be added during phase 2 of promo if location is within region. 1247 $tests_expect_changes['promo-phase-2-within-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_2, $location_country_within_region ); 1248 $tests_expect_no_changes['promo-phase-2-outside-region'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_2, $location_country_outside_region ); 1249 $tests_expect_no_changes['promo-phase-2-ip-only'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_2, $location_ip_only ); 1250 1251 // Regional camp should only be added during phase 3 of promo if location is within event country. 1252 $tests_expect_changes['promo-phase-3-within-event-country'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_3, $location_country_within_region ); 1253 $tests_expect_no_changes['promo-phase-3-outside-event-country'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_3, $location_country_outside_region ); 1254 $tests_expect_no_changes['promo-phase-3-ip-only'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_during_promo_phase_3, $location_ip_only ); 1255 1256 // Regional camp should only be added if the user agent is Core. 1257 $tests_expect_no_changes['other-user-agent'] = maybe_add_regional_wordcamps( $local_events, $region_data, $other_user_agent, $time_during_promo_phase_1, $location_country_within_region ); 1258 $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 // 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 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; 1277 } 1188 1278 } 1189 1279 1190 /* 1191 * Test that local events were unharmed if the user agent is Core. 1192 * 1193 * There isn't an easy way to mock time(), so this doesn't test that the events were added 1194 * correctly. It just makes sure that local events weren't removed. 1195 */ 1196 $events_core_user_agent = add_regional_wordcamps( $local_events, 'WordPress/4.9; https://example.org' ); 1197 1198 if ( 1199 count( $events_core_user_agent ) < count( $local_events ) || 1200 ! in_array( $local_events[0], $events_core_user_agent, true ) 1201 ) { 1202 $failed++; 1203 output_results( 'core-user-agent', false, 'local events were not affected', $events_core_user_agent ); 1280 $unchanged_count = count( $local_events ); 1281 $expected_count = $unchanged_count + 1; 1282 1283 foreach ( $tests_expect_changes as $name => $result ) { 1284 $actual_count = count( $result ); 1285 1286 if ( $actual_count !== $expected_count ) { 1287 $failed++; 1288 output_results( $name, false, $expected_count, $actual_count ); 1289 } 1204 1290 } 1205 1291
Note: See TracChangeset
for help on using the changeset viewer.