Making WordPress.org


Ignore:
Timestamp:
05/09/2018 06:06:54 PM (8 years ago)
Author:
coreymckrill
Message:

Events: Add WP15 promo when no local WP15 events

  • Add function to detect WP15 events in a list
  • Add function to check conditions and add promo item if conditions are met
  • Add unit tests
File:
1 edited

Legend:

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

    r7145 r7188  
    2323    $tests_failed += test_get_events_country_restriction();
    2424    $tests_failed += test_add_regional_wordcamps();
     25    $tests_failed += test_maybe_add_wp15_promo();
    2526    $tests_failed += test_build_response();
    2627    $tests_failed += test_is_client_core();
     
    12071208
    12081209/**
     1210 * Test `maybe_add_wp15_promo()`
     1211 *
     1212 * @return int
     1213 */
     1214function test_maybe_add_wp15_promo() {
     1215    $failed = 0;
     1216
     1217    $local_events_yes_wp15 = array(
     1218        array(
     1219            'type'       => 'meetup',
     1220            'title'      => 'WordPress 15th Anniversary Celebration',
     1221            'url'        => 'https://www.meetup.com/pdx-wp/events/250109566/',
     1222            'meetup'     => 'Portland WordPress Meetup',
     1223            'meetup_url' => 'https://www.meetup.com/pdx-wp/',
     1224            'date'       => '2018-05-27 12:00:00',
     1225            'location'   => array(
     1226                'location'  => 'Portland, OR, USA',
     1227                'country'   => 'us',
     1228                'latitude'  => 45.540115,
     1229                'longitude' => - 122.630699,
     1230            ),
     1231        ),
     1232        array(
     1233            'type'       => 'wordcamp',
     1234            'title'      => 'WordCamp Portland, Oregon, USA',
     1235            'url'        => 'https://2018.portland.wordcamp.org',
     1236            'meetup'     => null,
     1237            'meetup_url' => null,
     1238            'date'       => '2018-11-03 00:00:00',
     1239            'location'   => array(
     1240                'location'  => 'Portland, OR, USA',
     1241                'country'   => 'us',
     1242                'latitude'  => 45.540115,
     1243                'longitude' => - 122.630699,
     1244            ),
     1245        ),
     1246    );
     1247
     1248    $local_events_no_wp15 = array(
     1249        array(
     1250            'type'       => 'meetup',
     1251            'title'      => 'Kickoff: Meet and greet, roundtable discussion',
     1252            'url'        => 'https://www.meetup.com/Corvallis-WordPress-Meetup/events/250327006/',
     1253            'meetup'     => 'Corvallis WordPress Meetup',
     1254            'meetup_url' => 'https://www.meetup.com/Corvallis-WordPress-Meetup/',
     1255            'date'       => '2018-05-22 18:30:00',
     1256            'location'   => array(
     1257                'location'  => 'Corvallis, OR, USA',
     1258                'country'   => 'us',
     1259                'latitude'  => 44.563564,
     1260                'longitude' => - 123.26095,
     1261            ),
     1262        ),
     1263        array(
     1264            'type'       => 'wordcamp',
     1265            'title'      => 'WordCamp Portland, Oregon, USA',
     1266            'url'        => 'https://2018.portland.wordcamp.org',
     1267            'meetup'     => null,
     1268            'meetup_url' => null,
     1269            'date'       => '2018-11-03 00:00:00',
     1270            'location'   => array(
     1271                'location'  => 'Portland, OR, USA',
     1272                'country'   => 'us',
     1273                'latitude'  => 45.540115,
     1274                'longitude' => - 122.630699,
     1275            ),
     1276        ),
     1277    );
     1278
     1279    $local_events_added_wp15 = array(
     1280        array(
     1281            'type'       => 'meetup',
     1282            'title'      => 'WP15',
     1283            'url'        => 'https://wordpress.org/news/2018/04/celebrate-the-wordpress-15th-anniversary-on-may-27/',
     1284            'meetup'     => null,
     1285            'meetup_url' => null,
     1286            'date'       => '2018-05-27 12:00:00',
     1287            'location'   => array(
     1288                'location' => 'Everywhere',
     1289            ),
     1290        ),
     1291        array(
     1292            'type'       => 'meetup',
     1293            'title'      => 'Kickoff: Meet and greet, roundtable discussion',
     1294            'url'        => 'https://www.meetup.com/Corvallis-WordPress-Meetup/events/250327006/',
     1295            'meetup'     => 'Corvallis WordPress Meetup',
     1296            'meetup_url' => 'https://www.meetup.com/Corvallis-WordPress-Meetup/',
     1297            'date'       => '2018-05-22 18:30:00',
     1298            'location'   => array(
     1299                'location'  => 'Corvallis, OR, USA',
     1300                'country'   => 'us',
     1301                'latitude'  => 44.563564,
     1302                'longitude' => - 123.26095,
     1303            ),
     1304        ),
     1305        array(
     1306            'type'       => 'wordcamp',
     1307            'title'      => 'WordCamp Portland, Oregon, USA',
     1308            'url'        => 'https://2018.portland.wordcamp.org',
     1309            'meetup'     => null,
     1310            'meetup_url' => null,
     1311            'date'       => '2018-11-03 00:00:00',
     1312            'location'   => array(
     1313                'location'  => 'Portland, OR, USA',
     1314                'country'   => 'us',
     1315                'latitude'  => 45.540115,
     1316                'longitude' => - 122.630699,
     1317            ),
     1318        ),
     1319    );
     1320
     1321    $user_agent = 'WordPress/4.9; https://example.org';
     1322
     1323    $time_before_date_range = 1523295832;
     1324    $time_during_date_range = 1525887832;
     1325
     1326    printf( "\n\nRunning %d maybe_add_wp15_promo() tests\n", 4 );
     1327
     1328    // Test that the promo is added if there is not already a WP15 event.
     1329    $events_promo_added = maybe_add_wp15_promo( $local_events_no_wp15, $user_agent, $time_during_date_range );
     1330
     1331    if ( $events_promo_added !== $local_events_added_wp15 ) {
     1332        $failed++;
     1333        output_results( 'needs-promo', false, $local_events_added_wp15, $events_promo_added );
     1334    }
     1335
     1336    // Test that no promo is added if there is already a WP15 event.
     1337    $events_already_has_one = maybe_add_wp15_promo( $local_events_yes_wp15, $user_agent, $time_during_date_range );
     1338
     1339    if ( $events_already_has_one !== $local_events_yes_wp15 ) {
     1340        $failed++;
     1341        output_results( 'already-has-event', false, $local_events_yes_wp15, $events_already_has_one );
     1342    }
     1343
     1344    // Test that no changes are made if the user agent isn't Core.
     1345    $events_no_user_agent = maybe_add_wp15_promo( $local_events_no_wp15, '', $time_during_date_range );
     1346
     1347    if ( $events_no_user_agent !== $local_events_no_wp15 ) {
     1348        $failed++;
     1349        output_results( 'no-user-agent', false, $local_events_no_wp15, $events_no_user_agent );
     1350    }
     1351
     1352    // Test that no promo is added if the time is outside the date range.
     1353    $events_outside_date_range = maybe_add_wp15_promo( $local_events_no_wp15, $user_agent, $time_before_date_range );
     1354
     1355    if ( $events_outside_date_range !== $local_events_no_wp15 ) {
     1356        $failed++;
     1357        output_results( 'outside-date-range', false, $local_events_no_wp15, $events_outside_date_range );
     1358    }
     1359
     1360    return $failed;
     1361}
     1362
     1363/**
    12091364 * Stub to simulate cache misses, so that the tests always get fresh results
    12101365 *
Note: See TracChangeset for help on using the changeset viewer.