Making WordPress.org


Ignore:
Timestamp:
01/14/2021 04:31:51 PM (6 years ago)
Author:
iandunn
Message:

Events: Port remaining tests to PHPUnit.

File:
1 edited

Legend:

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

    r10557 r10581  
    33namespace Dotorg\API\Events\Tests;
    44use PHPUnit\Framework\TestCase;
    5 use function Dotorg\API\Events\{ get_events, get_location, build_response, is_client_core, pin_one_off_events };
     5use function Dotorg\API\Events\{
     6        get_events, get_location, build_response, is_client_core, pin_one_off_events,
     7        maybe_add_regional_wordcamps, get_iso_3166_2_country_codes, maybe_add_wp15_promo, remove_duplicate_events
     8};
    69
    710/**
     
    117120
    118121        /**
     122         * @covers ::maybe_add_regional_wordcamps
     123         * @covers ::get_iso_3166_2_country_codes
     124         *
     125         * @group unit
     126         */
     127        public function test_maybe_add_regional_wordcamps() : void {
     128                $local_events = get_events( array(
     129                        'number' => '5',
     130                        'nearby' => array(
     131                                'latitude'  => '-33.849951',
     132                                'longitude' => '18.426246',
     133                        ),
     134                ) );
     135
     136                $region_data = array(
     137                        'us' => array(
     138                                'promo_start' => strtotime( '2019-08-16 00:00:00' ),
     139
     140                                'regional_countries' => array_merge(
     141                                        get_iso_3166_2_country_codes( 'south america' ),
     142                                        get_iso_3166_2_country_codes( 'north america' )
     143                                ),
     144
     145                                'event' => array(
     146                                        'type'       => 'wordcamp',
     147                                        'title'      => 'WordCamp US',
     148                                        'url'        => 'https://2019.us.wordcamp.org/',
     149                                        'meetup'     => '',
     150                                        'meetup_url' => '',
     151                                        'date'       => '2019-11-01 00:00:00',
     152                                        'location'   => array(
     153                                                'location'  => 'St. Louis, MO, USA',
     154                                                'country'   => 'US',
     155                                                'latitude'  => 38.6532135,
     156                                                'longitude' => -90.3136733,
     157                                        ),
     158                                ),
     159                        ),
     160                );
     161
     162                $core_user_agent  = 'WordPress/5.2; https://example.org';
     163                $other_user_agent = 'Smith';
     164
     165                $time_before_promo         = strtotime( '2019-08-15 00:00:00' );
     166                $time_during_promo_phase_1 = strtotime( '+ 1 day', $region_data['us']['promo_start'] );
     167                $time_during_promo_phase_2 = strtotime( '+ 2 weeks + 1 day', $region_data['us']['promo_start'] );
     168                $time_during_promo_phase_3 = strtotime( '+ 4 weeks + 1 day', $region_data['us']['promo_start'] );
     169                $time_after_promo          = strtotime( '+ 6 weeks + 1 day', $region_data['us']['promo_start'] );
     170
     171                $location_country_within_region = array(
     172                        'country' => 'us',
     173                );
     174
     175                $location_country_outside_region = array(
     176                        'country' => 'es',
     177                );
     178
     179                $location_ip_only = array(
     180                        'ip' => '8.8.8.8',
     181                );
     182
     183                // Make sure there's at least one event, otherwise there could be false positives.
     184                if ( ! $local_events ) {
     185                        $local_events[] = array( 'title' => 'Mock Event' );
     186                }
     187
     188                $tests_expect_no_changes = array();
     189                $tests_expect_changes    = array();
     190
     191                // No regional camps should be added if before the promo start date or after the promo window is past (6 weeks).
     192                $tests_expect_no_changes['before-promo'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_before_promo, $location_country_within_region );
     193                $tests_expect_no_changes['before-promo'] = maybe_add_regional_wordcamps( $local_events, $region_data, $core_user_agent, $time_after_promo, $location_country_within_region );
     194
     195                // Regional camp should be added if it's within phase 1 of the promo, regardless of location.
     196                $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 );
     197                $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 );
     198
     199                // Regional camp should only be added during phase 2 of promo if location is within region.
     200                $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 );
     201                $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 );
     202                $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 );
     203
     204                // Regional camp should only be added during phase 3 of promo if location is within event country.
     205                $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 );
     206                $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 );
     207                $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 );
     208
     209                // Regional camp should only be added if the user agent is Core.
     210                $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 );
     211                $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 );
     212
     213                foreach ( $tests_expect_no_changes as $name => $result ) {
     214                        $this->assertSame( $local_events, $result );
     215                }
     216
     217                $unchanged_count = count( $local_events );
     218                $expected_count  = $unchanged_count + 1;
     219
     220                foreach ( $tests_expect_changes as $name => $result ) {
     221                        $actual_count = count( $result );
     222
     223                        $this->assertSame( $expected_count, $actual_count );
     224                }
     225        }
     226
     227        /**
     228         * @covers ::get_iso_3166_2_country_codes
     229         *
     230         * @dataProvider data_get_iso_3166_2_country_codes
     231         *
     232         * @group unit
     233         */
     234        public function test_get_iso_3166_2_country_codes( $continent, $sample_country ) : void {
     235                $countries = get_iso_3166_2_country_codes( $continent );
     236
     237                $this->assertContains( $sample_country, $countries );
     238        }
     239
     240        public function data_get_iso_3166_2_country_codes() : array {
     241                return array(
     242                        array( 'antarctica',    'HM' ),
     243                        array( 'africa',        'KM' ),
     244                        array( 'asia',          'SA' ),
     245                        array( 'europe',        'IM' ),
     246                        array( 'north america', 'MQ' ),
     247                        array( 'oceania',       'MP' ),
     248                        array( 'south america', 'GY' ),
     249                );
     250        }
     251
     252        /**
     253         * @covers ::maybe_add_wp15_promo
     254         *
     255         * @group unit
     256         */
     257        public function test_maybe_add_wp15_promo() : void {
     258                $local_events_yes_wp15 = array(
     259                        array(
     260                                'type'       => 'meetup',
     261                                'title'      => 'WordPress 15th Anniversary Celebration',
     262                                'url'        => 'https://www.meetup.com/pdx-wp/events/250109566/',
     263                                'meetup'     => 'Portland WordPress Meetup',
     264                                'meetup_url' => 'https://www.meetup.com/pdx-wp/',
     265                                'date'       => '2018-05-27 12:00:00',
     266                                'location'   => array(
     267                                        'location'  => 'Portland, OR, USA',
     268                                        'country'   => 'us',
     269                                        'latitude'  => 45.540115,
     270                                        'longitude' => - 122.630699,
     271                                ),
     272                        ),
     273
     274                        array(
     275                                'type'       => 'wordcamp',
     276                                'title'      => 'WordCamp Portland, Oregon, USA',
     277                                'url'        => 'https://2018.portland.wordcamp.org',
     278                                'meetup'     => '',
     279                                'meetup_url' => '',
     280                                'date'       => '2018-11-03 00:00:00',
     281                                'location'   => array(
     282                                        'location'  => 'Portland, OR, USA',
     283                                        'country'   => 'us',
     284                                        'latitude'  => 45.540115,
     285                                        'longitude' => - 122.630699,
     286                                ),
     287                        ),
     288                );
     289
     290                $local_events_no_wp15 = array(
     291                        array(
     292                                'type'       => 'meetup',
     293                                'title'      => 'Kickoff: Meet and greet, roundtable discussion',
     294                                'url'        => 'https://www.meetup.com/Corvallis-WordPress-Meetup/events/250327006/',
     295                                'meetup'     => 'Corvallis WordPress Meetup',
     296                                'meetup_url' => 'https://www.meetup.com/Corvallis-WordPress-Meetup/',
     297                                'date'       => '2018-05-22 18:30:00',
     298                                'location'   => array(
     299                                        'location'  => 'Corvallis, OR, USA',
     300                                        'country'   => 'us',
     301                                        'latitude'  => 44.563564,
     302                                        'longitude' => - 123.26095,
     303                                ),
     304                        ),
     305
     306                        array(
     307                                'type'       => 'wordcamp',
     308                                'title'      => 'WordCamp Portland, Oregon, USA',
     309                                'url'        => 'https://2018.portland.wordcamp.org',
     310                                'meetup'     => '',
     311                                'meetup_url' => '',
     312                                'date'       => '2018-11-03 00:00:00',
     313                                'location'   => array(
     314                                        'location'  => 'Portland, OR, USA',
     315                                        'country'   => 'us',
     316                                        'latitude'  => 45.540115,
     317                                        'longitude' => - 122.630699,
     318                                ),
     319                        ),
     320                );
     321
     322                $local_events_added_wp15 = array(
     323                        array(
     324                                'type'       => 'meetup',
     325                                'title'      => 'WP15',
     326                                'url'        => 'https://wordpress.org/news/2018/04/celebrate-the-wordpress-15th-anniversary-on-may-27/',
     327                                'meetup'     => '',
     328                                'meetup_url' => '',
     329                                'date'       => '2018-05-27 12:00:00',
     330                                'location'   => array(
     331                                        'location' => 'Everywhere',
     332                                ),
     333                        ),
     334
     335                        array(
     336                                'type'       => 'meetup',
     337                                'title'      => 'Kickoff: Meet and greet, roundtable discussion',
     338                                'url'        => 'https://www.meetup.com/Corvallis-WordPress-Meetup/events/250327006/',
     339                                'meetup'     => 'Corvallis WordPress Meetup',
     340                                'meetup_url' => 'https://www.meetup.com/Corvallis-WordPress-Meetup/',
     341                                'date'       => '2018-05-22 18:30:00',
     342                                'location'   => array(
     343                                        'location'  => 'Corvallis, OR, USA',
     344                                        'country'   => 'us',
     345                                        'latitude'  => 44.563564,
     346                                        'longitude' => - 123.26095,
     347                                ),
     348                        ),
     349
     350                        array(
     351                                'type'       => 'wordcamp',
     352                                'title'      => 'WordCamp Portland, Oregon, USA',
     353                                'url'        => 'https://2018.portland.wordcamp.org',
     354                                'meetup'     => '',
     355                                'meetup_url' => '',
     356                                'date'       => '2018-11-03 00:00:00',
     357                                'location'   => array(
     358                                        'location'  => 'Portland, OR, USA',
     359                                        'country'   => 'us',
     360                                        'latitude'  => 45.540115,
     361                                        'longitude' => - 122.630699,
     362                                ),
     363                        ),
     364                );
     365
     366                $user_agent = 'WordPress/4.9; https://example.org';
     367
     368                $time_before_date_range = 1523295832;
     369                $time_during_date_range = 1525887832;
     370
     371                // Test that the promo is added if there is not already a WP15 event.
     372                $events_promo_added = maybe_add_wp15_promo( $local_events_no_wp15, $user_agent, $time_during_date_range );
     373                $this->assertSame( $local_events_added_wp15, $events_promo_added, 'needs-promo' );
     374
     375                // Test that no promo is added if there is already a WP15 event.
     376                $events_already_has_one = maybe_add_wp15_promo( $local_events_yes_wp15, $user_agent, $time_during_date_range );
     377
     378                $this->assertSame( $local_events_yes_wp15, $events_already_has_one, 'already-has-event' );
     379
     380                // Test that no changes are made if the user agent isn't Core.
     381                $events_no_user_agent = maybe_add_wp15_promo( $local_events_no_wp15, '', $time_during_date_range );
     382                $this->assertSame( $local_events_no_wp15, $events_no_user_agent, 'no-user-agent' );
     383
     384                // Test that no promo is added if the time is outside the date range.
     385                $events_outside_date_range = maybe_add_wp15_promo( $local_events_no_wp15, $user_agent, $time_before_date_range );
     386                $this->assertSame( $local_events_no_wp15, $events_outside_date_range, 'outside-date-range' );
     387        }
     388
     389        /**
     390         * @covers ::remove_duplicate_events
     391         *
     392         * @group unit
     393         */
     394        public function test_remove_duplicate_events() : void {
     395                $duplicate_events = array(
     396                        // Each of these represents an event; extraneous fields have been removed for readability.
     397                        array(
     398                                'url' => 'https://2020.us.wordcamp.org/',
     399                        ),
     400
     401                        array(
     402                                'url' => 'https://2020.detroit.wordcamp.org/',
     403                        ),
     404
     405                        array(
     406                                // Intentionally missing the trailing slash, to account for inconsistencies in data.
     407                                'url' => 'https://2020.us.wordcamp.org',
     408                        ),
     409                );
     410
     411                $unique_events = array(
     412                        array(
     413                                'url' => 'https://2020.us.wordcamp.org',
     414                        ),
     415
     416                        array(
     417                                'url' => 'https://2020.detroit.wordcamp.org/',
     418                        ),
     419                );
     420
     421                $this->assertSame( $unique_events, remove_duplicate_events( $duplicate_events ) );
     422        }
     423
     424        /**
    119425         * @covers ::get_location
    120426         *
     
    11431449                );
    11441450        }
    1145 
    1146         function test_port_remaining_tests() {
    1147                 $this->markTestIncomplete( 'Not all of the tests from ./test-index.php have been ported to PHPUnit yet. See the notes in that file.' );
    1148         }
    11491451}
Note: See TracChangeset for help on using the changeset viewer.