123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580 |
- <?php
- /**
- * Unit tests for methods in WP_Community_Events.
- *
- * @package WordPress
- * @subpackage UnitTests
- * @since 4.8.0
- */
- /**
- * Class Test_WP_Community_Events.
- *
- * @group admin
- * @group community-events
- *
- * @since 4.8.0
- */
- class Test_WP_Community_Events extends WP_UnitTestCase {
- /**
- * An instance of the class to test.
- *
- * @access private
- * @since 4.8.0
- *
- * @var WP_Community_Events
- */
- private $instance;
- /**
- * Performs setup tasks for every test.
- *
- * @since 4.8.0
- */
- public function setUp() {
- parent::setUp();
- require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
- $this->instance = new WP_Community_Events( 1, $this->get_user_location() );
- }
- /**
- * Simulates a stored user location.
- *
- * @access private
- * @since 4.8.0
- *
- * @return array The mock location.
- */
- private function get_user_location() {
- return array(
- 'description' => 'San Francisco',
- 'latitude' => '37.7749300',
- 'longitude' => '-122.4194200',
- 'country' => 'US',
- );
- }
- /**
- * Test: get_events() should return an instance of WP_Error if the response code is not 200.
- *
- * @since 4.8.0
- */
- public function test_get_events_bad_response_code() {
- add_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
- $this->assertWPError( $this->instance->get_events() );
- remove_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
- }
- /**
- * Test: The response body should not be cached if the response code is not 200.
- *
- * @since 4.8.0
- */
- public function test_get_cached_events_bad_response_code() {
- add_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
- $this->instance->get_events();
- $this->assertFalse( $this->instance->get_cached_events() );
- remove_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
- }
- /**
- * Simulates an HTTP response with a non-200 response code.
- *
- * @since 4.8.0
- *
- * @return array A mock response with a 404 HTTP status code
- */
- public function _http_request_bad_response_code() {
- return array(
- 'headers' => '',
- 'body' => '',
- 'response' => array(
- 'code' => 404,
- ),
- 'cookies' => '',
- 'filename' => '',
- );
- }
- /**
- * Test: get_events() should return an instance of WP_Error if the response body does not have
- * the required properties.
- *
- * @since 4.8.0
- */
- public function test_get_events_invalid_response() {
- add_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
- $this->assertWPError( $this->instance->get_events() );
- remove_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
- }
- /**
- * Test: The response body should not be cached if it does not have the required properties.
- *
- * @since 4.8.0
- */
- public function test_get_cached_events_invalid_response() {
- add_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
- $this->instance->get_events();
- $this->assertFalse( $this->instance->get_cached_events() );
- remove_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
- }
- /**
- * Simulates an HTTP response with a body that does not have the required properties.
- *
- * @since 4.8.0
- *
- * @return array A mock response that's missing required properties.
- */
- public function _http_request_invalid_response() {
- return array(
- 'headers' => '',
- 'body' => wp_json_encode( array() ),
- 'response' => array(
- 'code' => 200,
- ),
- 'cookies' => '',
- 'filename' => '',
- );
- }
- /**
- * Test: With a valid response, get_events() should return an associative array containing a location array and
- * an events array with individual events that have Unix start/end timestamps.
- *
- * @since 4.8.0
- */
- public function test_get_events_valid_response() {
- add_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
- $response = $this->instance->get_events();
- $this->assertNotWPError( $response );
- $this->assertSameSetsWithIndex( $this->get_user_location(), $response['location'] );
- $this->assertSame( strtotime( 'next Sunday 1pm' ), $response['events'][0]['start_unix_timestamp'] );
- $this->assertSame( strtotime( 'next Sunday 2pm' ), $response['events'][0]['end_unix_timestamp'] );
- remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
- }
- /**
- * Test: `get_cached_events()` should return the same data as get_events(), including Unix start/end
- * timestamps for each event.
- *
- * @since 4.8.0
- */
- public function test_get_cached_events_valid_response() {
- add_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
- $this->instance->get_events();
- $cached_events = $this->instance->get_cached_events();
- $this->assertNotWPError( $cached_events );
- $this->assertSameSetsWithIndex( $this->get_user_location(), $cached_events['location'] );
- $this->assertSame( strtotime( 'next Sunday 1pm' ), $cached_events['events'][0]['start_unix_timestamp'] );
- $this->assertSame( strtotime( 'next Sunday 2pm' ), $cached_events['events'][0]['end_unix_timestamp'] );
- remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
- }
- /**
- * Simulates an HTTP response with valid location and event data.
- *
- * @since 4.8.0
- *
- * @return array A mock HTTP response with valid data.
- */
- public function _http_request_valid_response() {
- return array(
- 'headers' => '',
- 'body' => wp_json_encode(
- array(
- 'location' => $this->get_user_location(),
- 'events' => $this->get_valid_events(),
- )
- ),
- 'response' => array(
- 'code' => 200,
- ),
- 'cookies' => '',
- 'filename' => '',
- );
- }
- /**
- * Get a sample of valid events.
- *
- * @return array[]
- */
- protected function get_valid_events() {
- return array(
- array(
- 'type' => 'meetup',
- 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
- 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
- 'meetup' => 'The East Bay WordPress Meetup Group',
- 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
- 'start_unix_timestamp' => strtotime( 'next Sunday 1pm' ),
- 'end_unix_timestamp' => strtotime( 'next Sunday 2pm' ),
- 'location' => array(
- 'location' => 'Oakland, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.808453,
- 'longitude' => -122.26593,
- ),
- ),
- array(
- 'type' => 'meetup',
- 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
- 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
- 'meetup' => 'WordPress Bay Area Foothills Group',
- 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
- 'start_unix_timestamp' => strtotime( 'next Wednesday 1:30pm' ),
- 'end_unix_timestamp' => strtotime( 'next Wednesday 2:30pm' ),
- 'location' => array(
- 'location' => 'Milpitas, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.432813,
- 'longitude' => -121.907095,
- ),
- ),
- array(
- 'type' => 'wordcamp',
- 'title' => 'WordCamp San Francisco',
- 'url' => 'https://sf.wordcamp.org/2020/',
- 'meetup' => null,
- 'meetup_url' => null,
- 'start_unix_timestamp' => strtotime( 'next Saturday' ),
- 'end_unix_timestamp' => strtotime( 'next Saturday 8pm' ),
- 'location' => array(
- 'location' => 'San Francisco, CA',
- 'country' => 'US',
- 'latitude' => 37.432813,
- 'longitude' => -121.907095,
- ),
- ),
- );
- }
- /**
- * Test: `trim_events()` should immediately remove expired events.
- *
- * @covers WP_Community_Events::trim_events
- *
- * @since 5.5.2
- */
- public function test_trim_expired_events() {
- $trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
- $trim_events->setAccessible( true );
- $events = $this->get_valid_events();
- // This should be removed because it's already ended.
- $events[0]['start_unix_timestamp'] = strtotime( '1 hour ago' );
- $events[0]['end_unix_timestamp'] = strtotime( '2 seconds ago' );
- // This should remain because it hasn't ended yet.
- $events[1]['start_unix_timestamp'] = strtotime( '2 seconds ago' );
- $events[1]['end_unix_timestamp'] = strtotime( '+1 hour' );
- $actual = $trim_events->invoke( $this->instance, $events );
- $this->assertCount( 2, $actual );
- $this->assertSame( $actual[0]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
- $this->assertSame( $actual[1]['title'], 'WordCamp San Francisco' );
- }
- /**
- * Test: get_events() should return the events with the WordCamp pinned in the prepared list.
- *
- * @covers WP_Community_Events::trim_events
- *
- * @since 4.9.7
- * @since 5.5.2 Tests `trim_events()` directly instead of indirectly via `get_events()`.
- */
- public function test_trim_events_pin_wordcamp() {
- $trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
- $trim_events->setAccessible( true );
- $actual = $trim_events->invoke( $this->instance, $this->_events_with_unpinned_wordcamp() );
- /*
- * San Diego was at index 3 in the mock API response, but pinning puts it at index 2,
- * so that it remains in the list. The other events should remain unchanged.
- */
- $this->assertCount( 3, $actual );
- $this->assertSame( $actual[0]['title'], 'Flexbox + CSS Grid: Magic for Responsive Layouts' );
- $this->assertSame( $actual[1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
- $this->assertSame( $actual[2]['title'], 'WordCamp San Diego' );
- }
- /**
- * Simulates a scenario where a WordCamp needs to be pinned higher than it's default position.
- *
- * @since 4.9.7
- * @since 5.5.2 Accepts and returns only the events, rather than an entire HTTP response.
- *
- * @return array A list of mock events.
- */
- public function _events_with_unpinned_wordcamp() {
- return array(
- array(
- 'type' => 'meetup',
- 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
- 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
- 'meetup' => 'The East Bay WordPress Meetup Group',
- 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
- 'start_unix_timestamp' => strtotime( 'next Monday 1pm' ),
- 'end_unix_timestamp' => strtotime( 'next Monday 2pm' ),
- 'location' => array(
- 'location' => 'Oakland, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.808453,
- 'longitude' => -122.26593,
- ),
- ),
- array(
- 'type' => 'meetup',
- 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
- 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
- 'meetup' => 'WordPress Bay Area Foothills Group',
- 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
- 'start_unix_timestamp' => strtotime( 'next Tuesday 1:30pm' ),
- 'end_unix_timestamp' => strtotime( 'next Tuesday 2:30pm' ),
- 'location' => array(
- 'location' => 'Milpitas, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.432813,
- 'longitude' => -121.907095,
- ),
- ),
- array(
- 'type' => 'meetup',
- 'title' => 'WordPress Q&A',
- 'url' => 'https://www.meetup.com/sanjosewp/events/245419844/',
- 'meetup' => 'The San Jose WordPress Meetup',
- 'meetup_url' => 'https://www.meetup.com/sanjosewp/',
- 'start_unix_timestamp' => strtotime( 'next Wednesday 5:30pm' ),
- 'end_unix_timestamp' => strtotime( 'next Wednesday 6:30pm' ),
- 'location' => array(
- 'location' => 'Milpitas, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.244194,
- 'longitude' => -121.889313,
- ),
- ),
- array(
- 'type' => 'wordcamp',
- 'title' => 'WordCamp San Diego',
- 'url' => 'https://2018.sandiego.wordcamp.org',
- 'meetup' => null,
- 'meetup_url' => null,
- 'start_unix_timestamp' => strtotime( 'next Thursday 9am' ),
- 'end_unix_timestamp' => strtotime( 'next Thursday 10am' ),
- 'location' => array(
- 'location' => 'San Diego, CA',
- 'country' => 'US',
- 'latitude' => 32.7220419,
- 'longitude' => -117.1534513,
- ),
- ),
- );
- }
- /**
- * Test: get_events() shouldn't stick an extra WordCamp when there's already one that naturally
- * falls into the list.
- *
- * @covers WP_Community_Events::trim_events
- *
- * @since 4.9.7
- * @since 5.5.2 Tests `trim_events()` directly instead of indirectly via `get_events()`.
- */
- public function test_trim_events_dont_pin_multiple_wordcamps() {
- $trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
- $trim_events->setAccessible( true );
- $actual = $trim_events->invoke( $this->instance, $this->_events_with_multiple_wordcamps() );
- /*
- * The first meetup should be removed because it's expired, while the next 3 events are selected.
- * WordCamp LA should not be stuck to the list, because San Diego already appears naturally.
- */
- $this->assertCount( 3, $actual );
- $this->assertSame( $actual[0]['title'], 'WordCamp San Diego' );
- $this->assertSame( $actual[1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
- $this->assertSame( $actual[2]['title'], 'WordPress Q&A' );
- }
- /**
- * Simulates a valid HTTP response where a WordCamp needs to be pinned higher than it's default position.
- * no need to pin extra camp b/c one already exists in response
- *
- * @since 4.9.7
- * @since 5.5.2 Tests `trim_events()` directly instead of indirectly via `get_events()`.
- *
- * @return array A mock HTTP response.
- */
- public function _events_with_multiple_wordcamps() {
- return array(
- array(
- 'type' => 'meetup',
- 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
- 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
- 'meetup' => 'The East Bay WordPress Meetup Group',
- 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
- 'start_unix_timestamp' => strtotime( '2 days ago' ) - HOUR_IN_SECONDS,
- 'end_unix_timestamp' => strtotime( '2 days ago' ),
- 'location' => array(
- 'location' => 'Oakland, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.808453,
- 'longitude' => -122.26593,
- ),
- ),
- array(
- 'type' => 'wordcamp',
- 'title' => 'WordCamp San Diego',
- 'url' => 'https://2018.sandiego.wordcamp.org',
- 'meetup' => null,
- 'meetup_url' => null,
- 'start_unix_timestamp' => strtotime( 'next Tuesday 9am' ),
- 'end_unix_timestamp' => strtotime( 'next Tuesday 10am' ),
- 'location' => array(
- 'location' => 'San Diego, CA',
- 'country' => 'US',
- 'latitude' => 32.7220419,
- 'longitude' => -117.1534513,
- ),
- ),
- array(
- 'type' => 'meetup',
- 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
- 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
- 'meetup' => 'WordPress Bay Area Foothills Group',
- 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
- 'start_unix_timestamp' => strtotime( 'next Wednesday 1:30pm' ),
- 'end_unix_timestamp' => strtotime( 'next Wednesday 2:30pm' ),
- 'location' => array(
- 'location' => 'Milpitas, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.432813,
- 'longitude' => -121.907095,
- ),
- ),
- array(
- 'type' => 'meetup',
- 'title' => 'WordPress Q&A',
- 'url' => 'https://www.meetup.com/sanjosewp/events/245419844/',
- 'meetup' => 'The San Jose WordPress Meetup',
- 'meetup_url' => 'https://www.meetup.com/sanjosewp/',
- 'start_unix_timestamp' => strtotime( 'next Thursday 5:30pm' ),
- 'end_unix_timestamp' => strtotime( 'next Thursday 6:30pm' ),
- 'location' => array(
- 'location' => 'Milpitas, CA, USA',
- 'country' => 'us',
- 'latitude' => 37.244194,
- 'longitude' => -121.889313,
- ),
- ),
- array(
- 'type' => 'wordcamp',
- 'title' => 'WordCamp Los Angeles',
- 'url' => 'https://2018.la.wordcamp.org',
- 'meetup' => null,
- 'meetup_url' => null,
- 'start_unix_timestamp' => strtotime( 'next Friday 9am' ),
- 'end_unix_timestamp' => strtotime( 'next Friday 10am' ),
- 'location' => array(
- 'location' => 'Los Angeles, CA',
- 'country' => 'US',
- 'latitude' => 34.050888,
- 'longitude' => -118.285426,
- ),
- ),
- );
- }
- /**
- * Test that get_unsafe_client_ip() properly anonymizes all possible address formats
- *
- * @dataProvider data_get_unsafe_client_ip
- *
- * @ticket 41083
- */
- public function test_get_unsafe_client_ip( $raw_ip, $expected_result ) {
- $_SERVER['REMOTE_ADDR'] = 'this should not be used';
- $_SERVER['HTTP_CLIENT_IP'] = $raw_ip;
- $actual_result = WP_Community_Events::get_unsafe_client_ip();
- $this->assertSame( $expected_result, $actual_result );
- }
- /**
- * Provide test cases for `test_get_unsafe_client_ip()`.
- *
- * @return array
- */
- public function data_get_unsafe_client_ip() {
- return array(
- // Handle '::' returned from `wp_privacy_anonymize_ip()`.
- array(
- 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001',
- false,
- ),
- // Handle '0.0.0.0' returned from `wp_privacy_anonymize_ip()`.
- array(
- 'unknown',
- false,
- ),
- // Valid IPv4.
- array(
- '198.143.164.252',
- '198.143.164.0',
- ),
- // Valid IPv6.
- array(
- '2a03:2880:2110:df07:face:b00c::1',
- '2a03:2880:2110:df07::',
- ),
- );
- }
- }
|