Making WordPress.org


Ignore:
Timestamp:
12/23/2020 09:33:48 PM (5 years ago)
Author:
iandunn
Message:

Events: Port more tests to PHPUnit.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r10544 r10546  
    3333
    3434    $tests_failed  = 0;
    35     $tests_failed += test_get_location();
    3635    $tests_failed += test_maybe_add_regional_wordcamps();
    3736    $tests_failed += test_maybe_add_wp15_promo();
    38     $tests_failed += test_build_response();
    39     $tests_failed += test_is_client_core();
    4037    $tests_failed += test_get_iso_3166_2_country_codes();
    4138    $tests_failed += test_remove_duplicate_events();
     
    10299        }
    103100    }
    104 }
    105 
    106 /**
    107  * Test `get_location()`
    108  *
    109  * @return bool The number of failures
    110  */
    111 function test_get_location() {
    112     $failed = 0;
    113     $cases  = get_location_test_cases();
    114 
    115     printf( "\nRunning %d location tests", count( $cases ) );
    116 
    117     foreach ( $cases as $case_id => $case ) {
    118         $actual_result = get_location( $case['input'] );
    119 
    120         // Normalize to lowercase to account for inconsistency in the IP database
    121         if ( isset( $actual_result['description'] ) && is_string( $actual_result['description'] ) ) {
    122             $actual_result['description'] = strtolower( $actual_result['description'] );
    123         }
    124 
    125         /*
    126          * Normalize coordinates to account for minor differences in the databases
    127          *
    128          * Rounding to three decimal places means that we're still accurate within about 110 meters, which is
    129          * good enough for our purposes.
    130          *
    131          * See https://gis.stackexchange.com/a/8674/49125
    132          */
    133         if ( isset( $actual_result['latitude'], $actual_result['longitude'] ) ) {
    134             $actual_result['latitude']  = number_format( round( $actual_result['latitude'],  3 ), 3 );
    135             $actual_result['longitude'] = number_format( round( $actual_result['longitude'], 3 ), 3 );
    136         }
    137 
    138         $passed = $case['expected'] === $actual_result;
    139 
    140         output_results( $case_id, $passed, $case['expected'], $actual_result );
    141 
    142         if ( ! $passed ) {
    143             $failed++;
    144         }
    145     }
    146 
    147     return $failed;
    148 }
    149 
    150 /**
    151  * Get the cases for testing `get_location()`
    152  *
    153  * @return array
    154  */
    155 function get_location_test_cases() {
    156     $cases = array(
    157         /*
    158          * Only the country code is given
    159          */
    160         'country-code-australia' => array(
    161             'input' => array(
    162                 'country' => 'AU',
    163                 'restrict_by_country' => true,
    164             ),
    165             'expected' => array(
    166                 'country' => 'AU',
    167             ),
    168         ),
    169 
    170 
    171         /*
    172          * The country name, locale, and timezone are given
    173          */
    174         'country-exonym-1-word' => array(
    175             'input' => array(
    176                 'location_name' => 'Indonesia',
    177                 'locale'        => 'id_ID',
    178                 'timezone'      => 'Asia/Jakarta',
    179             ),
    180             'expected' => array(
    181                 'country'     => 'ID',
    182                 'description' => 'indonesia',
    183             ),
    184         ),
    185 
    186         'country-exonym-2-words' => array(
    187             'input' => array(
    188                 'location_name' => 'Bosnia and Herzegovina',
    189                 'locale'        => 'bs_BA',
    190                 'timezone'      => 'Europe/Sarajevo',
    191             ),
    192             'expected' => array(
    193                 'country'     => 'BA',
    194                 'description' => 'bosnia and herzegovina',
    195             ),
    196         ),
    197 
    198         /*
    199          * A location couldn't be found
    200          */
    201         'city-invalid-private-ip' => array(
    202             'input' => array(
    203                 'location_name' => 'InvalidCity',
    204                 'ip'            => '127.0.0.1',
    205             ),
    206             'expected' => false,
    207         ),
    208 
    209         /*
    210          * No input was provided
    211          */
    212         'input-empty' => array(
    213             'input'    => array(),
    214             'expected' => array(),
    215         ),
    216 
    217 
    218         /*
    219          * The English city exonym, locale, and timezone are given
    220          */
    221         'city-africa' => array(
    222             'input' => array(
    223                 'location_name' => 'Nairobi',
    224                 'locale'        => 'en_GB',
    225                 'timezone'      => 'Africa/Nairobi',
    226             ),
    227             'expected' => array(
    228                 'description' => 'nairobi',
    229                 'latitude'    => '-1.283',
    230                 'longitude'   => '36.817',
    231                 'country'     => 'KE',
    232             ),
    233         ),
    234 
    235         'city-asia' => array(
    236             'input' => array(
    237                 'location_name' => 'Tokyo',
    238                 'locale'        => 'ja',
    239                 'timezone'      => 'Asia/Tokyo',
    240             ),
    241             'expected' => array(
    242                 'description' => 'tokyo',
    243                 'latitude'    => '35.690',
    244                 'longitude'   => '139.692',
    245                 'country'     => 'JP',
    246             ),
    247         ),
    248 
    249         'city-europe' => array(
    250             'input' => array(
    251                 'location_name' => 'Berlin',
    252                 'locale'        => 'de_DE',
    253                 'timezone'      => 'Europe/Berlin',
    254             ),
    255             'expected' => array(
    256                 'description' => 'berlin',
    257                 'latitude'    => '52.524',
    258                 'longitude'   => '13.411',
    259                 'country'     => 'DE',
    260             ),
    261         ),
    262 
    263         'city-north-america' => array(
    264             'input' => array(
    265                 'location_name' => 'Vancouver',
    266                 'locale'        => 'en_CA',
    267                 'timezone'      => 'America/Vancouver',
    268             ),
    269             'expected' => array(
    270                 'description' => 'vancouver',
    271                 'latitude'    => '49.250',
    272                 'longitude'   => '-123.119',
    273                 'country'     => 'CA',
    274             ),
    275         ),
    276 
    277         'city-oceania' => array(
    278             'input' => array(
    279                 'location_name' => 'Brisbane',
    280                 'locale'        => 'en_AU',
    281                 'timezone'      => 'Australia/Brisbane',
    282             ),
    283             'expected' => array(
    284                 'description' => 'brisbane',
    285                 'latitude'    => '-27.468',
    286                 'longitude'   => '153.028',
    287                 'country'     => 'AU',
    288             ),
    289         ),
    290 
    291         // Many users never change the default `en_US` locale in Core
    292         'city-oceania-with-en_US' => array(
    293             'input' => array(
    294                 'location_name' => 'Sydney',
    295                 'locale'        => 'en_US',
    296                 'timezone'      => 'Australia/Sydney',
    297             ),
    298             'expected' => array(
    299                 'description' => 'sydney',
    300                 'latitude'    => '-33.868',
    301                 'longitude'   => '151.207',
    302                 'country'     => 'AU',
    303             ),
    304         ),
    305 
    306         'city-south-america' => array(
    307             'input' => array(
    308                 'location_name' => 'Sao Paulo',
    309                 'locale'        => 'pt_BR',
    310                 'timezone'      => 'America/Sao_Paulo',
    311             ),
    312             'expected' => array(
    313                 'description' => 'sao paulo',
    314                 'latitude'    => '-23.548',
    315                 'longitude'   => '-46.636',
    316                 'country'     => 'BR',
    317             ),
    318         ),
    319 
    320         // Users will often type them without the dash, bypassing an exact match
    321         'city-with-dashes-in-formal-name' => array(
    322             'input' => array(
    323                 'location_name' => 'Osakashi',
    324                 'locale'        => 'ja',
    325                 'timezone'      => 'Asia/Tokyo',
    326             ),
    327             'expected' => array(
    328                 'description' => 'osakashi',
    329                 'latitude'    => '34.694',
    330                 'longitude'   => '135.502',
    331                 'country'     => 'JP',
    332             ),
    333         ),
    334 
    335         // If a location is provided, the fallback search should be attempted before an IP search
    336         'fallback-with-public-ip' => array(
    337             'input' => array(
    338                 'location_name' => 'Osakashi',
    339                 'locale'        => 'ja',
    340                 'timezone'      => 'Asia/Tokyo',
    341                 'ip'            => '153.163.68.148', // Tokyo
    342             ),
    343             'expected' => array(
    344                 'description' => 'osakashi',
    345                 'latitude'    => '34.694',
    346                 'longitude'   => '135.502',
    347                 'country'     => 'JP',
    348             ),
    349         ),
    350 
    351         'city-with-apostrophe-in-formal-name' => array(
    352             'input' => array(
    353                 'location_name' => "Coeur d'Alene",
    354                 'locale'        => 'en_US',
    355                 'timezone'      => 'America/Los_Angeles',
    356             ),
    357             'expected' => array(
    358                 'description' => "coeur d'alene",
    359                 'latitude'    => '47.678',
    360                 'longitude'   => '-116.780',
    361                 'country'     => 'US',
    362             ),
    363         ),
    364 
    365         'city-with-diacritics-in-query' => array(
    366             'input' => array(
    367                 'location_name' => 'Doña Ana',
    368                 'locale'        => 'en_US',
    369                 'timezone'      => 'America/Denver',
    370             ),
    371             'expected' => array(
    372                 'description' => 'doña ana',
    373                 'latitude'    => '32.390',
    374                 'longitude'   => '-106.814',
    375                 'country'     => 'US',
    376             ),
    377         ),
    378 
    379         'city-with-diacritics-in-formal-name-but-not-in-query' => array(
    380             'input' => array(
    381                 'location_name' => 'Dona Ana',
    382                 'locale'        => 'en_US',
    383                 'timezone'      => 'America/Denver',
    384             ),
    385             'expected' => array(
    386                 'description' => 'dona ana',
    387                 'latitude'    => '32.390',
    388                 'longitude'   => '-106.814',
    389                 'country'     => 'US',
    390             ),
    391         ),
    392 
    393         'city-with-period-in-query' => array(
    394             'input' => array(
    395                 'location_name' => 'St. Louis',
    396                 'locale'        => 'en_US',
    397                 'timezone'      => 'America/Chicago',
    398             ),
    399             'expected' => array(
    400                 'description' => 'st. louis',
    401                 'latitude'    => '38.627',
    402                 'longitude'   => '-90.198',
    403                 'country'     => 'US',
    404             ),
    405         ),
    406 
    407         'city-with-period-in-formal-name-but-not-in-query' => array(
    408             'input' => array(
    409                 'location_name' => 'St Louis',
    410                 'locale'        => 'en_US',
    411                 'timezone'      => 'America/Chicago',
    412             ),
    413             'expected' => array(
    414                 'description' => 'st louis',
    415                 'latitude'    => '38.627',
    416                 'longitude'   => '-90.198',
    417                 'country'     => 'US',
    418             ),
    419         ),
    420 
    421         /*
    422          * The city endonym, locale, and timezone are given
    423          */
    424         'city-endonym-accents-africa' => array(
    425             'input' => array(
    426                 'location_name' => 'Yaoundé',
    427                 'locale'        => 'fr_FR',
    428                 'timezone'      => 'Africa/Douala',
    429             ),
    430             'expected' => array(
    431                 'description' => 'yaoundé',
    432                 'latitude'    => '3.867',
    433                 'longitude'   => '11.517',
    434                 'country'     => 'CM',
    435             ),
    436         ),
    437 
    438         'city-endonym-non-latin-africa' => array(
    439             'input' => array(
    440                 'location_name' => 'አዲስ አበ',
    441                 'locale'        => 'am',
    442                 'timezone'      => 'Africa/Addis_Ababa',
    443             ),
    444             'expected' => array(
    445                 'description' => 'አዲስ አበባ',
    446                 'latitude'    => '9.025',
    447                 'longitude'   => '38.747',
    448                 'country'     => 'ET',
    449             ),
    450         ),
    451 
    452         'city-endonym-ideographic-asia1' => array(
    453             'input' => array(
    454                 'location_name' => '白浜町宇佐崎南',
    455                 'locale'        => 'ja',
    456                 'timezone'      => 'Asia/Tokyo',
    457             ),
    458             'expected' => array(
    459                 'description' => '白浜町宇佐崎南',
    460                 'latitude'    => '34.783',
    461                 'longitude'   => '134.717',
    462                 'country'     => 'JP',
    463             ),
    464         ),
    465 
    466         'city-endonym-ideographic-asia2' => array(
    467             'input' => array(
    468                 'location_name' => 'تهران',
    469                 'locale'        => 'fa_IR',
    470                 'timezone'      => 'Asia/Tehran',
    471             ),
    472             'expected' => array(
    473                 'description' => 'تهران',
    474                 'latitude'    => '35.694',
    475                 'longitude'   => '51.422',
    476                 'country'     => 'IR',
    477             ),
    478         ),
    479 
    480         'city-endonym-ideographic-asia3' => array(
    481             'input' => array(
    482                 'location_name' => 'كراچى',
    483                 'locale'        => 'ur',
    484                 'timezone'      => 'Asia/Karachi',
    485             ),
    486             'expected' => array(
    487                 'description' => 'كراچى',
    488                 'latitude'    => '24.861',
    489                 'longitude'   => '67.010',
    490                 'country'     => 'PK',
    491             ),
    492         ),
    493 
    494         'city-endonym-ideographic-asia4' => array(
    495             'input' => array(
    496                 'location_name' => '京都',
    497                 'locale'        => 'ja',
    498                 'timezone'      => 'Asia/Tokyo',
    499             ),
    500             'expected' => array(
    501                 'description' => '京都',
    502                 'latitude'    => '35.021',
    503                 'longitude'   => '135.754',
    504                 'country'     => 'JP',
    505             ),
    506         ),
    507 
    508         'city-endonym-ideographic-asia5' => array(
    509             'input' => array(
    510                 'location_name' => '東京',
    511                 'locale'        => 'ja',
    512                 'timezone'      => 'Asia/Tokyo',
    513             ),
    514             'expected' => array(
    515                 'description' => '東京',
    516                 'latitude'    => '35.690',
    517                 'longitude'   => '139.692',
    518                 'country'     => 'JP',
    519             ),
    520         ),
    521 
    522         'city-endonym-ideographic-municipal-unit-asia' => array(
    523             'input' => array(
    524                 'location_name' => '大阪',
    525                 'locale'        => 'ja',
    526                 'timezone'      => 'Asia/Tokyo',
    527             ),
    528             'expected' => array(
    529                 'description' => '大阪',
    530                 'latitude'    => '34.694',
    531                 'longitude'   => '135.502',
    532                 'country'     => 'JP',
    533             ),
    534         ),
    535 
    536         'city-endonym-europe' => array(
    537             'input' => array(
    538                 'location_name' => 'Wien',
    539                 'locale'        => 'de_DE',
    540                 'timezone'      => 'Europe/Berlin',
    541             ),
    542             'expected' => array(
    543                 'description' => 'wien',
    544                 'latitude'    => '48.208',
    545                 'longitude'   => '16.372',
    546                 'country'     => 'AT',
    547             ),
    548         ),
    549 
    550         'city-endonym-europe2' => array(
    551             'input' => array(
    552                 'location_name' => 'Москва',
    553                 'locale'        => 'ru_RU',
    554                 'timezone'      => 'Europe/Moscow',
    555             ),
    556             'expected' => array(
    557                 'description' => 'Москва',
    558                 'latitude'    => '55.752',
    559                 'longitude'   => '37.616',
    560                 'country'     => 'RU',
    561             ),
    562         ),
    563 
    564         // https://meta.trac.wordpress.org/ticket/3295
    565         'city-endonym-europe3-uppercase' => array(
    566             'input' => array(
    567                 'location_name' => 'Санкт-Петербург',
    568                 'locale'        => 'ru_RU',
    569                 'timezone'      => 'Europe/Moscow',
    570             ),
    571             'expected' => array(
    572                 'description' => 'Санкт-Петербург',
    573                 'latitude'    => '59.939',
    574                 'longitude'   => '30.314',
    575                 'country'     => 'RU',
    576             ),
    577         ),
    578 
    579         // https://meta.trac.wordpress.org/ticket/3295
    580         'city-endonym-europe3-lowercase' => array(
    581             'input' => array(
    582                 'location_name' => 'санкт-петербург',
    583                 'locale'        => 'ru_RU',
    584                 'timezone'      => 'Europe/Moscow',
    585             ),
    586             'expected' => array(
    587                 'description' => 'Санкт-Петербург',
    588                 'latitude'    => '59.939',
    589                 'longitude'   => '30.314',
    590                 'country'     => 'RU',
    591             ),
    592         ),
    593 
    594         'city-endonym-accents-north-america' => array(
    595             'input' => array(
    596                 'location_name' => 'Ciudad de México',
    597                 'locale'        => 'en_MX',
    598                 'timezone'      => 'America/Mexico_City',
    599             ),
    600             'expected' => array(
    601                 'description' => 'ciudad de méxico',
    602                 'latitude'    => '19.428',
    603                 'longitude'   => '-99.128',
    604                 'country'     => 'MX',
    605             ),
    606         ),
    607 
    608         'city-endonym-accents-oceania' => array(
    609             'input' => array(
    610                 'location_name' => 'Hagåtña',
    611                 'locale'        => 'en_US',
    612                 'timezone'      => 'Pacific/Guam',
    613             ),
    614             'expected' => array(
    615                 'description' => 'hagåtña',
    616                 'latitude'    => '13.476',
    617                 'longitude'   => '144.749',
    618                 'country'     => 'GU',
    619             ),
    620         ),
    621 
    622         'city-endonym-south-america' => array(
    623             'input' => array(
    624                 'location_name' => 'Bogotá',
    625                 'locale'        => 'es_CO',
    626                 'timezone'      => 'America/Bogota',
    627             ),
    628             'expected' => array(
    629                 'description' => 'bogotá',
    630                 'latitude'    => '4.610',
    631                 'longitude'   => '-74.082',
    632                 'country'     => 'CO',
    633             ),
    634         ),
    635 
    636         // https://meta.trac.wordpress.org/ticket/3367
    637         // The following tests ensure that West/East portlands return correctly with inversed timezones.
    638         'usa-city-disambiguation' => array(
    639             'input' => array(
    640                 'location_name' => 'Portland',
    641                 'locale'        => 'en_US',
    642                 'timezone'      => 'America/Los_Angeles',
    643             ),
    644             'expected' => array(
    645                 'description' => 'portland',
    646                 'latitude'    => '45.523',
    647                 'longitude'   => '-122.676',
    648                 'country'     => 'US',
    649             ),
    650         ),
    651         'usa-city-disambiguation-2' => array(
    652             'input' => array(
    653                 'location_name' => 'Portland, OR',
    654                 'locale'        => 'en_US',
    655                 'timezone'      => 'America/New_York',
    656             ),
    657             'expected' => array(
    658                 'description' => 'portland, or',
    659                 'latitude'    => '45.523',
    660                 'longitude'   => '-122.676',
    661                 'country'     => 'US',
    662             ),
    663         ),
    664         'usa-city-disambiguation-3' => array(
    665             'input' => array(
    666                 'location_name' => 'Portland',
    667                 'locale'        => 'en_US',
    668                 'timezone'      => 'America/New_York',
    669             ),
    670             'expected' => array(
    671                 'description' => 'portland',
    672                 'latitude'    => '43.657',
    673                 'longitude'   => '-70.259',
    674                 'country'     => 'US',
    675             ),
    676         ),
    677         'usa-city-disambiguation-4' => array(
    678             'input' => array(
    679                 'location_name' => 'Portland, ME',
    680                 'locale'        => 'en_US',
    681                 'timezone'      => 'America/Los_Angeles',
    682             ),
    683             'expected' => array(
    684                 'description' => 'portland, me',
    685                 'latitude'    => '43.657',
    686                 'longitude'   => '-70.259',
    687                 'country'     => 'US',
    688             ),
    689         ),
    690 
    691         /*
    692          * A combination of city, region, and country are given, along with the locale and timezone
    693          *
    694          * InvalidCity is used in tests that want to bypass the guess_location_from_city() tests and only test the country
    695          */
    696         '1-word-city-region' => array(
    697             'input' => array(
    698                 'location_name' => 'Portland Maine',
    699                 'locale'        => 'en_US',
    700                 'timezone'      => 'America/New_York',
    701             ),
    702             'expected' => array(
    703                 'description' => 'portland',
    704                 'latitude'    => '43.657',
    705                 'longitude'   => '-70.259',
    706                 'country'     => 'US',
    707             ),
    708         ),
    709 
    710         '2-word-city-region' => array(
    711             'input' => array(
    712                 'location_name' => 'São Paulo Brazil',
    713                 'locale'        => 'pt_BR',
    714                 'timezone'      => 'America/Sao_Paulo',
    715             ),
    716             'expected' => array(
    717                 'description' => 'são paulo',
    718                 'latitude'    => '-23.548',
    719                 'longitude'   => '-46.636',
    720                 'country'     => 'BR',
    721             ),
    722         ),
    723 
    724         'city-1-word-country' => array(
    725             'input' => array(
    726                 'location_name' => 'InvalidCity Canada',
    727                 'locale'        => 'en_CA',
    728                 'timezone'      => 'America/Vancouver',
    729             ),
    730             'expected' => array(
    731                 'country'     => 'CA',
    732                 'description' => 'canada',
    733             ),
    734         ),
    735 
    736         'city-2-word-country' => array(
    737             'input' => array(
    738                 'location_name' => 'InvalidCity Dominican Republic',
    739                 'locale'        => 'es_ES',
    740                 'timezone'      => 'America/Santo_Domingo',
    741             ),
    742             'expected' => array(
    743                 'country'     => 'DO',
    744                 'description' => 'dominican republic',
    745             ),
    746         ),
    747 
    748         'city-3-word-country' => array(
    749             'input' => array(
    750                 'location_name' => 'InvalidCity Central African Republic',
    751                 'locale'        => 'fr_FR',
    752                 'timezone'      => 'Africa/Bangui',
    753             ),
    754             'expected' => array(
    755                 'country'     => 'CF',
    756                 'description' => 'central african republic',
    757             ),
    758         ),
    759 
    760         'country-code' => array(
    761             'input' => array(
    762                 'location_name' => 'GB',
    763                 'locale'        => 'en_GB',
    764                 'timezone'      => 'Europe/London',
    765             ),
    766             'expected' => array(
    767                 'country'     => 'GB',
    768                 'description' => 'united kingdom',
    769             ),
    770         ),
    771 
    772         'city-country-code' => array(
    773             'input' => array(
    774                 'location_name' => 'InvalidCity BI',
    775                 'locale'        => 'fr_FR',
    776                 'timezone'      => 'Africa/Bujumbura',
    777             ),
    778             'expected' => array(
    779                 'country'     => 'BI',
    780                 'description' => 'burundi',
    781             ),
    782         ),
    783 
    784         /*
    785          * Coordinates should take precedence over IP addresses
    786          */
    787         'coordinates-over-ip-us' => array(
    788             'input' => array(
    789                 'latitude'  => '47.6062100',
    790                 'longitude' => '-122.3320700',
    791                 'ip'        => '192.0.70.251',  // San Francisco, USA
    792                 'timezone'  => 'America/Los_Angeles',
    793                 'locale'    => 'en_US',
    794             ),
    795             'expected' => array(
    796                 'description' => false,
    797                 'latitude'    => '47.606',
    798                 'longitude'   => '-122.332',
    799             ),
    800         ),
    801 
    802         'coordinates-over-ip-africa' => array(
    803             'input' => array(
    804                 'latitude'  => '-19.634233',
    805                 'longitude' => '17.331767',
    806                 'ip'        => '41.190.96.5',   // Tsumeb, Namibia
    807                 'timezone'  => 'Africa/Windhoek',
    808                 'locale'    => 'af',
    809             ),
    810             'expected' => array(
    811                 'description' => false,
    812                 'latitude'    => '-19.634',
    813                 'longitude'   => '17.332',
    814             ),
    815         ),
    816 
    817         /*
    818          * Only the IPv4 address is given.
    819          *
    820          * Note that IP locations change frequently, so some of these expected results will inevitably become outdated
    821          * and cause tests to fail.
    822          *
    823          * See https://awebanalysis.com/en/ipv4-directory/
    824          */
    825         'ip-africa' => array(
    826             'input' => array( 'ip' => '41.191.232.22' ),
    827             'expected' => array(
    828                 'description' => 'harare',
    829                 'latitude'    => '-17.829',
    830                 'longitude'   => '31.054',
    831                 'country'     => 'ZW',
    832                 'internal'    => true,
    833             ),
    834         ),
    835 
    836         'ip-asia' => array(
    837             'input' => array( 'ip' => '86.108.55.28' ),
    838             'expected' => array(
    839                 'description' => 'amman',
    840                 'latitude'    => '31.955',
    841                 'longitude'   => '35.945',
    842                 'country'     => 'JO',
    843                 'internal'    => true,
    844             ),
    845         ),
    846 
    847         'ip-europe' => array(
    848             'input' => array( 'ip' => '80.95.186.144' ),
    849             'expected' => array(
    850                 'description' => 'belfast',
    851                 'latitude'    => '54.583',
    852                 'longitude'   => '-5.933',
    853                 'country'     => 'GB',
    854                 'internal'    => true,
    855             ),
    856         ),
    857 
    858         'ip-north-america' => array(
    859             'input' => array( 'ip' => '189.147.186.0' ),
    860             'expected' => array(
    861                 'description' => 'mexico city',
    862                 'latitude'    => '19.428',
    863                 'longitude'   => '-99.128',
    864                 'country'     => 'MX',
    865                 'internal'    => true,
    866             ),
    867         ),
    868 
    869         'ip-oceania' => array(
    870             'input' => array( 'ip' => '116.12.57.122' ),
    871             'expected' => array(
    872                 'description' => 'auckland',
    873                 'latitude'    => '-36.867',
    874                 'longitude'   => '174.767',
    875                 'country'     => 'NZ',
    876                 'internal'    => true,
    877             ),
    878         ),
    879 
    880         'ip-south-america' => array(
    881             'input' => array( 'ip' => '181.66.32.136' ),
    882             'expected' => array(
    883                 'description' => 'lima',
    884                 'latitude'    => '-12.043',
    885                 'longitude'   => '-77.028',
    886                 'country'     => 'PE',
    887                 'internal'    => true,
    888             ),
    889         ),
    890 
    891         /*
    892          * Only an IPv6 address is given.
    893          *
    894          * Note that IP locations change frequently, so some of these expected results will inevitably become outdated
    895          * and cause tests to fail.
    896          *
    897          * See https://www.google.com/intl/en/ipv6/statistics.html#tab=per-country-ipv6-adoption&tab=per-country-ipv6-adoption
    898          * See https://awebanalysis.com/en/ipv6-directory/
    899          * See https://www.google.com/search?q=australia+site%3Ahttps%3A%2F%2Fawebanalysis.com%2Fen%2Fipv6-directory%2F
    900          */
    901         'ipv6-africa' => array(
    902             'input'    => array( 'ip' => '2c0f:f8f0:ffff:ffff:ffff:ffff:ffff:ffff' ),
    903             'expected' => array(
    904                 'description' => 'harare',
    905                 'latitude'    => '-17.829',
    906                 'longitude'   => '31.054',
    907                 'country'     => 'ZW',
    908                 'internal'    => true,
    909             ),
    910         ),
    911 
    912         'ipv6-asia-anonymized' => array(
    913             'input'    => array( 'ip' => '2405:200:1000::' ),
    914             'expected' => array(
    915                 'description' => 'mumbai',
    916                 'latitude'    => '19.014',
    917                 'longitude'   => '72.848',
    918                 'country'     => 'IN',
    919                 'internal'    => true,
    920             ),
    921         ),
    922 
    923         'ipv6-europe-anonymized' => array(
    924             'input'    => array( 'ip' => '2a02:578:1000::' ),
    925             'expected' => array(
    926                 'description' => 'sint-niklaas',
    927                 'latitude'    => '51.165',
    928                 'longitude'   => '4.144',
    929                 'country'     => 'BE',
    930                 'internal'    => true,
    931             ),
    932         ),
    933 
    934         'ipv6-north-america-anonymized' => array(
    935             'input'    => array( 'ip' => '2605:a600::' ),
    936             'expected' => array(
    937                 'description' => 'mountain view',
    938                 'latitude'    => '37.386',
    939                 'longitude'   => '-122.084',
    940                 'country'     => 'US',
    941                 'internal'    => true,
    942             ),
    943         ),
    944 
    945         'ipv6-oceania-collapsed-prefix' => array(
    946             'input'    => array( 'ip' => '::ffff:0190:c500' ),
    947             'expected' => array(
    948                 'description' => 'perth',
    949                 'latitude'    => '-31.952',
    950                 'longitude'   => '115.861',
    951                 'country'     => 'AU',
    952                 'internal'    => true,
    953             ),
    954         ),
    955 
    956         'ipv6-south-america' => array(
    957             'input'    => array( 'ip' => '2001:1388:6643:2736:10f1:897c:428c:1b3b' ),
    958             'expected' => array(
    959                 'description' => 'lima',
    960                 'latitude'    => '-12.043',
    961                 'longitude'   => '-77.028',
    962                 'country'     => 'PE',
    963                 'internal'    => true,
    964             ),
    965         ),
    966     );
    967 
    968     return $cases;
    969 }
    970 
    971 /**
    972  * Test `build_response()`
    973  *
    974  * @todo It might be better to do more abstracted tests of `main()`, rather than coupling to the
    975  *       internals of `build_request()`.
    976  *
    977  * @return bool The number of failures
    978  */
    979 function test_build_response() {
    980     $failed = 0;
    981     $cases  = build_response_test_cases();
    982 
    983     printf( "\nRunning %d build_response() tests", count( $cases ) );
    984 
    985     foreach ( $cases as $case_id => $case ) {
    986         $actual_result = build_response( $case['input']['location'], $case['input']['location_args'] );
    987 
    988         $passed = $case['expected']['location']       === $actual_result['location'] &&
    989                   isset( $case['expected']['error'] ) === isset( $actual_result['error'] );
    990 
    991         if ( $passed && $case['expected']['events'] ) {
    992             $passed = ! empty( $actual_result['events'] ) &&
    993                       ! empty( $actual_result['events'][0]['url'] ) &&
    994                       strtotime( $actual_result['events'][0]['date'] ) > time() - ( 2 * 24 * 60 * 60 );
    995         }
    996 
    997         if ( $passed && isset( $case['expected']['error'] ) ) {
    998             $passed = $case['expected']['error'] === $actual_result['error'];
    999         }
    1000 
    1001         output_results( $case_id, $passed, $case['expected'], $actual_result );
    1002 
    1003         if ( ! $passed ) {
    1004             $failed++;
    1005         }
    1006     }
    1007 
    1008     return $failed;
    1009 }
    1010 
    1011 /**
    1012  * Get the cases for testing `build_response()`
    1013  *
    1014  * @return array
    1015  */
    1016 function build_response_test_cases() {
    1017     $cases = array(
    1018         'utrecht-ip' => array(
    1019             'input' => array(
    1020                 'location' => array(
    1021                     'latitude'  => '52.090284',
    1022                     'longitude' => '5.124719',
    1023                     'internal'  => true,
    1024                 ),
    1025                 'location_args' => array( 'ip' => '84.31.177.21' ),
    1026             ),
    1027             'expected' => array(
    1028                 'location' => array(
    1029                     'ip' => '84.31.177.21',
    1030                 ),
    1031                 'events' => true,
    1032             ),
    1033         ),
    1034 
    1035         'canada-country' => array(
    1036             'input' => array(
    1037                 'location' => array(
    1038                     'country' => 'CA',
    1039                 ),
    1040                 'location_args' => array(
    1041                     'restrict_by_country' => true,
    1042                 ),
    1043             ),
    1044             'expected' => array(
    1045                 'location' => array(
    1046                     'country' => 'CA',
    1047                 ),
    1048                 'events' => true,
    1049             ),
    1050         ),
    1051 
    1052         'throttled' => array(
    1053             'input' => array(
    1054                 'location' => 'temp-request-throttled',
    1055             ),
    1056             'expected' => array(
    1057                 'location' => array(),
    1058                 'error'    => 'temp-request-throttled',
    1059                 'events'   => false,
    1060             ),
    1061         ),
    1062 
    1063         'no-location' => array(
    1064             'input' => array(
    1065                 'location' => array(),
    1066             ),
    1067             'expected' => array(
    1068                 'location' => array(),
    1069                 'error'    => 'no_location_available',
    1070                 'events'   => false,
    1071             ),
    1072         ),
    1073     );
    1074 
    1075     return $cases;
    1076 }
    1077 
    1078 /**
    1079  * Test `is_client_core()`.
    1080  *
    1081  * @return int
    1082  */
    1083 function test_is_client_core() {
    1084     $failed = 0;
    1085     $cases  = array(
    1086         ''                                   => false,
    1087         'Contains WordPress but no slash'    => false,
    1088         'WordPress/4.9; https://example.org' => true,
    1089         'WordPress/10.0'                     => true,
    1090     );
    1091 
    1092     printf( "\nRunning %d is_client_core() tests", count( $cases ) );
    1093 
    1094     foreach ( $cases as $user_agent => $expected_result ) {
    1095         $actual_result = is_client_core( $user_agent );
    1096         $passed        = $expected_result === $actual_result;
    1097 
    1098         output_results( $user_agent, $passed, $expected_result, $actual_result );
    1099 
    1100         if ( ! $passed ) {
    1101             $failed++;
    1102         }
    1103     }
    1104 
    1105     return $failed;
    1106101}
    1107102
Note: See TracChangeset for help on using the changeset viewer.