includesCommunityEvents.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. <?php
  2. /**
  3. * Unit tests for methods in WP_Community_Events.
  4. *
  5. * @package WordPress
  6. * @subpackage UnitTests
  7. * @since 4.8.0
  8. */
  9. /**
  10. * Class Test_WP_Community_Events.
  11. *
  12. * @group admin
  13. * @group community-events
  14. *
  15. * @since 4.8.0
  16. */
  17. class Test_WP_Community_Events extends WP_UnitTestCase {
  18. /**
  19. * An instance of the class to test.
  20. *
  21. * @access private
  22. * @since 4.8.0
  23. *
  24. * @var WP_Community_Events
  25. */
  26. private $instance;
  27. /**
  28. * Performs setup tasks for every test.
  29. *
  30. * @since 4.8.0
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. require_once ABSPATH . 'wp-admin/includes/class-wp-community-events.php';
  35. $this->instance = new WP_Community_Events( 1, $this->get_user_location() );
  36. }
  37. /**
  38. * Simulates a stored user location.
  39. *
  40. * @access private
  41. * @since 4.8.0
  42. *
  43. * @return array The mock location.
  44. */
  45. private function get_user_location() {
  46. return array(
  47. 'description' => 'San Francisco',
  48. 'latitude' => '37.7749300',
  49. 'longitude' => '-122.4194200',
  50. 'country' => 'US',
  51. );
  52. }
  53. /**
  54. * Test: get_events() should return an instance of WP_Error if the response code is not 200.
  55. *
  56. * @since 4.8.0
  57. */
  58. public function test_get_events_bad_response_code() {
  59. add_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
  60. $this->assertWPError( $this->instance->get_events() );
  61. remove_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
  62. }
  63. /**
  64. * Test: The response body should not be cached if the response code is not 200.
  65. *
  66. * @since 4.8.0
  67. */
  68. public function test_get_cached_events_bad_response_code() {
  69. add_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
  70. $this->instance->get_events();
  71. $this->assertFalse( $this->instance->get_cached_events() );
  72. remove_filter( 'pre_http_request', array( $this, '_http_request_bad_response_code' ) );
  73. }
  74. /**
  75. * Simulates an HTTP response with a non-200 response code.
  76. *
  77. * @since 4.8.0
  78. *
  79. * @return array A mock response with a 404 HTTP status code
  80. */
  81. public function _http_request_bad_response_code() {
  82. return array(
  83. 'headers' => '',
  84. 'body' => '',
  85. 'response' => array(
  86. 'code' => 404,
  87. ),
  88. 'cookies' => '',
  89. 'filename' => '',
  90. );
  91. }
  92. /**
  93. * Test: get_events() should return an instance of WP_Error if the response body does not have
  94. * the required properties.
  95. *
  96. * @since 4.8.0
  97. */
  98. public function test_get_events_invalid_response() {
  99. add_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
  100. $this->assertWPError( $this->instance->get_events() );
  101. remove_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
  102. }
  103. /**
  104. * Test: The response body should not be cached if it does not have the required properties.
  105. *
  106. * @since 4.8.0
  107. */
  108. public function test_get_cached_events_invalid_response() {
  109. add_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
  110. $this->instance->get_events();
  111. $this->assertFalse( $this->instance->get_cached_events() );
  112. remove_filter( 'pre_http_request', array( $this, '_http_request_invalid_response' ) );
  113. }
  114. /**
  115. * Simulates an HTTP response with a body that does not have the required properties.
  116. *
  117. * @since 4.8.0
  118. *
  119. * @return array A mock response that's missing required properties.
  120. */
  121. public function _http_request_invalid_response() {
  122. return array(
  123. 'headers' => '',
  124. 'body' => wp_json_encode( array() ),
  125. 'response' => array(
  126. 'code' => 200,
  127. ),
  128. 'cookies' => '',
  129. 'filename' => '',
  130. );
  131. }
  132. /**
  133. * Test: With a valid response, get_events() should return an associative array containing a location array and
  134. * an events array with individual events that have Unix start/end timestamps.
  135. *
  136. * @since 4.8.0
  137. */
  138. public function test_get_events_valid_response() {
  139. add_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
  140. $response = $this->instance->get_events();
  141. $this->assertNotWPError( $response );
  142. $this->assertSameSetsWithIndex( $this->get_user_location(), $response['location'] );
  143. $this->assertSame( strtotime( 'next Sunday 1pm' ), $response['events'][0]['start_unix_timestamp'] );
  144. $this->assertSame( strtotime( 'next Sunday 2pm' ), $response['events'][0]['end_unix_timestamp'] );
  145. remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
  146. }
  147. /**
  148. * Test: `get_cached_events()` should return the same data as get_events(), including Unix start/end
  149. * timestamps for each event.
  150. *
  151. * @since 4.8.0
  152. */
  153. public function test_get_cached_events_valid_response() {
  154. add_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
  155. $this->instance->get_events();
  156. $cached_events = $this->instance->get_cached_events();
  157. $this->assertNotWPError( $cached_events );
  158. $this->assertSameSetsWithIndex( $this->get_user_location(), $cached_events['location'] );
  159. $this->assertSame( strtotime( 'next Sunday 1pm' ), $cached_events['events'][0]['start_unix_timestamp'] );
  160. $this->assertSame( strtotime( 'next Sunday 2pm' ), $cached_events['events'][0]['end_unix_timestamp'] );
  161. remove_filter( 'pre_http_request', array( $this, '_http_request_valid_response' ) );
  162. }
  163. /**
  164. * Simulates an HTTP response with valid location and event data.
  165. *
  166. * @since 4.8.0
  167. *
  168. * @return array A mock HTTP response with valid data.
  169. */
  170. public function _http_request_valid_response() {
  171. return array(
  172. 'headers' => '',
  173. 'body' => wp_json_encode(
  174. array(
  175. 'location' => $this->get_user_location(),
  176. 'events' => $this->get_valid_events(),
  177. )
  178. ),
  179. 'response' => array(
  180. 'code' => 200,
  181. ),
  182. 'cookies' => '',
  183. 'filename' => '',
  184. );
  185. }
  186. /**
  187. * Get a sample of valid events.
  188. *
  189. * @return array[]
  190. */
  191. protected function get_valid_events() {
  192. return array(
  193. array(
  194. 'type' => 'meetup',
  195. 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
  196. 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
  197. 'meetup' => 'The East Bay WordPress Meetup Group',
  198. 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
  199. 'start_unix_timestamp' => strtotime( 'next Sunday 1pm' ),
  200. 'end_unix_timestamp' => strtotime( 'next Sunday 2pm' ),
  201. 'location' => array(
  202. 'location' => 'Oakland, CA, USA',
  203. 'country' => 'us',
  204. 'latitude' => 37.808453,
  205. 'longitude' => -122.26593,
  206. ),
  207. ),
  208. array(
  209. 'type' => 'meetup',
  210. 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
  211. 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
  212. 'meetup' => 'WordPress Bay Area Foothills Group',
  213. 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
  214. 'start_unix_timestamp' => strtotime( 'next Wednesday 1:30pm' ),
  215. 'end_unix_timestamp' => strtotime( 'next Wednesday 2:30pm' ),
  216. 'location' => array(
  217. 'location' => 'Milpitas, CA, USA',
  218. 'country' => 'us',
  219. 'latitude' => 37.432813,
  220. 'longitude' => -121.907095,
  221. ),
  222. ),
  223. array(
  224. 'type' => 'wordcamp',
  225. 'title' => 'WordCamp San Francisco',
  226. 'url' => 'https://sf.wordcamp.org/2020/',
  227. 'meetup' => null,
  228. 'meetup_url' => null,
  229. 'start_unix_timestamp' => strtotime( 'next Saturday' ),
  230. 'end_unix_timestamp' => strtotime( 'next Saturday 8pm' ),
  231. 'location' => array(
  232. 'location' => 'San Francisco, CA',
  233. 'country' => 'US',
  234. 'latitude' => 37.432813,
  235. 'longitude' => -121.907095,
  236. ),
  237. ),
  238. );
  239. }
  240. /**
  241. * Test: `trim_events()` should immediately remove expired events.
  242. *
  243. * @covers WP_Community_Events::trim_events
  244. *
  245. * @since 5.5.2
  246. */
  247. public function test_trim_expired_events() {
  248. $trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
  249. $trim_events->setAccessible( true );
  250. $events = $this->get_valid_events();
  251. // This should be removed because it's already ended.
  252. $events[0]['start_unix_timestamp'] = strtotime( '1 hour ago' );
  253. $events[0]['end_unix_timestamp'] = strtotime( '2 seconds ago' );
  254. // This should remain because it hasn't ended yet.
  255. $events[1]['start_unix_timestamp'] = strtotime( '2 seconds ago' );
  256. $events[1]['end_unix_timestamp'] = strtotime( '+1 hour' );
  257. $actual = $trim_events->invoke( $this->instance, $events );
  258. $this->assertCount( 2, $actual );
  259. $this->assertSame( $actual[0]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
  260. $this->assertSame( $actual[1]['title'], 'WordCamp San Francisco' );
  261. }
  262. /**
  263. * Test: get_events() should return the events with the WordCamp pinned in the prepared list.
  264. *
  265. * @covers WP_Community_Events::trim_events
  266. *
  267. * @since 4.9.7
  268. * @since 5.5.2 Tests `trim_events()` directly instead of indirectly via `get_events()`.
  269. */
  270. public function test_trim_events_pin_wordcamp() {
  271. $trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
  272. $trim_events->setAccessible( true );
  273. $actual = $trim_events->invoke( $this->instance, $this->_events_with_unpinned_wordcamp() );
  274. /*
  275. * San Diego was at index 3 in the mock API response, but pinning puts it at index 2,
  276. * so that it remains in the list. The other events should remain unchanged.
  277. */
  278. $this->assertCount( 3, $actual );
  279. $this->assertSame( $actual[0]['title'], 'Flexbox + CSS Grid: Magic for Responsive Layouts' );
  280. $this->assertSame( $actual[1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
  281. $this->assertSame( $actual[2]['title'], 'WordCamp San Diego' );
  282. }
  283. /**
  284. * Simulates a scenario where a WordCamp needs to be pinned higher than it's default position.
  285. *
  286. * @since 4.9.7
  287. * @since 5.5.2 Accepts and returns only the events, rather than an entire HTTP response.
  288. *
  289. * @return array A list of mock events.
  290. */
  291. public function _events_with_unpinned_wordcamp() {
  292. return array(
  293. array(
  294. 'type' => 'meetup',
  295. 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
  296. 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
  297. 'meetup' => 'The East Bay WordPress Meetup Group',
  298. 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
  299. 'start_unix_timestamp' => strtotime( 'next Monday 1pm' ),
  300. 'end_unix_timestamp' => strtotime( 'next Monday 2pm' ),
  301. 'location' => array(
  302. 'location' => 'Oakland, CA, USA',
  303. 'country' => 'us',
  304. 'latitude' => 37.808453,
  305. 'longitude' => -122.26593,
  306. ),
  307. ),
  308. array(
  309. 'type' => 'meetup',
  310. 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
  311. 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
  312. 'meetup' => 'WordPress Bay Area Foothills Group',
  313. 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
  314. 'start_unix_timestamp' => strtotime( 'next Tuesday 1:30pm' ),
  315. 'end_unix_timestamp' => strtotime( 'next Tuesday 2:30pm' ),
  316. 'location' => array(
  317. 'location' => 'Milpitas, CA, USA',
  318. 'country' => 'us',
  319. 'latitude' => 37.432813,
  320. 'longitude' => -121.907095,
  321. ),
  322. ),
  323. array(
  324. 'type' => 'meetup',
  325. 'title' => 'WordPress Q&A',
  326. 'url' => 'https://www.meetup.com/sanjosewp/events/245419844/',
  327. 'meetup' => 'The San Jose WordPress Meetup',
  328. 'meetup_url' => 'https://www.meetup.com/sanjosewp/',
  329. 'start_unix_timestamp' => strtotime( 'next Wednesday 5:30pm' ),
  330. 'end_unix_timestamp' => strtotime( 'next Wednesday 6:30pm' ),
  331. 'location' => array(
  332. 'location' => 'Milpitas, CA, USA',
  333. 'country' => 'us',
  334. 'latitude' => 37.244194,
  335. 'longitude' => -121.889313,
  336. ),
  337. ),
  338. array(
  339. 'type' => 'wordcamp',
  340. 'title' => 'WordCamp San Diego',
  341. 'url' => 'https://2018.sandiego.wordcamp.org',
  342. 'meetup' => null,
  343. 'meetup_url' => null,
  344. 'start_unix_timestamp' => strtotime( 'next Thursday 9am' ),
  345. 'end_unix_timestamp' => strtotime( 'next Thursday 10am' ),
  346. 'location' => array(
  347. 'location' => 'San Diego, CA',
  348. 'country' => 'US',
  349. 'latitude' => 32.7220419,
  350. 'longitude' => -117.1534513,
  351. ),
  352. ),
  353. );
  354. }
  355. /**
  356. * Test: get_events() shouldn't stick an extra WordCamp when there's already one that naturally
  357. * falls into the list.
  358. *
  359. * @covers WP_Community_Events::trim_events
  360. *
  361. * @since 4.9.7
  362. * @since 5.5.2 Tests `trim_events()` directly instead of indirectly via `get_events()`.
  363. */
  364. public function test_trim_events_dont_pin_multiple_wordcamps() {
  365. $trim_events = new ReflectionMethod( $this->instance, 'trim_events' );
  366. $trim_events->setAccessible( true );
  367. $actual = $trim_events->invoke( $this->instance, $this->_events_with_multiple_wordcamps() );
  368. /*
  369. * The first meetup should be removed because it's expired, while the next 3 events are selected.
  370. * WordCamp LA should not be stuck to the list, because San Diego already appears naturally.
  371. */
  372. $this->assertCount( 3, $actual );
  373. $this->assertSame( $actual[0]['title'], 'WordCamp San Diego' );
  374. $this->assertSame( $actual[1]['title'], 'Part 3- Site Maintenance - Tools to Make It Easy' );
  375. $this->assertSame( $actual[2]['title'], 'WordPress Q&A' );
  376. }
  377. /**
  378. * Simulates a valid HTTP response where a WordCamp needs to be pinned higher than it's default position.
  379. * no need to pin extra camp b/c one already exists in response
  380. *
  381. * @since 4.9.7
  382. * @since 5.5.2 Tests `trim_events()` directly instead of indirectly via `get_events()`.
  383. *
  384. * @return array A mock HTTP response.
  385. */
  386. public function _events_with_multiple_wordcamps() {
  387. return array(
  388. array(
  389. 'type' => 'meetup',
  390. 'title' => 'Flexbox + CSS Grid: Magic for Responsive Layouts',
  391. 'url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/events/236031233/',
  392. 'meetup' => 'The East Bay WordPress Meetup Group',
  393. 'meetup_url' => 'https://www.meetup.com/Eastbay-WordPress-Meetup/',
  394. 'start_unix_timestamp' => strtotime( '2 days ago' ) - HOUR_IN_SECONDS,
  395. 'end_unix_timestamp' => strtotime( '2 days ago' ),
  396. 'location' => array(
  397. 'location' => 'Oakland, CA, USA',
  398. 'country' => 'us',
  399. 'latitude' => 37.808453,
  400. 'longitude' => -122.26593,
  401. ),
  402. ),
  403. array(
  404. 'type' => 'wordcamp',
  405. 'title' => 'WordCamp San Diego',
  406. 'url' => 'https://2018.sandiego.wordcamp.org',
  407. 'meetup' => null,
  408. 'meetup_url' => null,
  409. 'start_unix_timestamp' => strtotime( 'next Tuesday 9am' ),
  410. 'end_unix_timestamp' => strtotime( 'next Tuesday 10am' ),
  411. 'location' => array(
  412. 'location' => 'San Diego, CA',
  413. 'country' => 'US',
  414. 'latitude' => 32.7220419,
  415. 'longitude' => -117.1534513,
  416. ),
  417. ),
  418. array(
  419. 'type' => 'meetup',
  420. 'title' => 'Part 3- Site Maintenance - Tools to Make It Easy',
  421. 'url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/events/237706839/',
  422. 'meetup' => 'WordPress Bay Area Foothills Group',
  423. 'meetup_url' => 'https://www.meetup.com/Wordpress-Bay-Area-CA-Foothills/',
  424. 'start_unix_timestamp' => strtotime( 'next Wednesday 1:30pm' ),
  425. 'end_unix_timestamp' => strtotime( 'next Wednesday 2:30pm' ),
  426. 'location' => array(
  427. 'location' => 'Milpitas, CA, USA',
  428. 'country' => 'us',
  429. 'latitude' => 37.432813,
  430. 'longitude' => -121.907095,
  431. ),
  432. ),
  433. array(
  434. 'type' => 'meetup',
  435. 'title' => 'WordPress Q&A',
  436. 'url' => 'https://www.meetup.com/sanjosewp/events/245419844/',
  437. 'meetup' => 'The San Jose WordPress Meetup',
  438. 'meetup_url' => 'https://www.meetup.com/sanjosewp/',
  439. 'start_unix_timestamp' => strtotime( 'next Thursday 5:30pm' ),
  440. 'end_unix_timestamp' => strtotime( 'next Thursday 6:30pm' ),
  441. 'location' => array(
  442. 'location' => 'Milpitas, CA, USA',
  443. 'country' => 'us',
  444. 'latitude' => 37.244194,
  445. 'longitude' => -121.889313,
  446. ),
  447. ),
  448. array(
  449. 'type' => 'wordcamp',
  450. 'title' => 'WordCamp Los Angeles',
  451. 'url' => 'https://2018.la.wordcamp.org',
  452. 'meetup' => null,
  453. 'meetup_url' => null,
  454. 'start_unix_timestamp' => strtotime( 'next Friday 9am' ),
  455. 'end_unix_timestamp' => strtotime( 'next Friday 10am' ),
  456. 'location' => array(
  457. 'location' => 'Los Angeles, CA',
  458. 'country' => 'US',
  459. 'latitude' => 34.050888,
  460. 'longitude' => -118.285426,
  461. ),
  462. ),
  463. );
  464. }
  465. /**
  466. * Test that get_unsafe_client_ip() properly anonymizes all possible address formats
  467. *
  468. * @dataProvider data_get_unsafe_client_ip
  469. *
  470. * @ticket 41083
  471. */
  472. public function test_get_unsafe_client_ip( $raw_ip, $expected_result ) {
  473. $_SERVER['REMOTE_ADDR'] = 'this should not be used';
  474. $_SERVER['HTTP_CLIENT_IP'] = $raw_ip;
  475. $actual_result = WP_Community_Events::get_unsafe_client_ip();
  476. $this->assertSame( $expected_result, $actual_result );
  477. }
  478. /**
  479. * Provide test cases for `test_get_unsafe_client_ip()`.
  480. *
  481. * @return array
  482. */
  483. public function data_get_unsafe_client_ip() {
  484. return array(
  485. // Handle '::' returned from `wp_privacy_anonymize_ip()`.
  486. array(
  487. 'or=\"[1000:0000:0000:0000:0000:0000:0000:0001',
  488. false,
  489. ),
  490. // Handle '0.0.0.0' returned from `wp_privacy_anonymize_ip()`.
  491. array(
  492. 'unknown',
  493. false,
  494. ),
  495. // Valid IPv4.
  496. array(
  497. '198.143.164.252',
  498. '198.143.164.0',
  499. ),
  500. // Valid IPv6.
  501. array(
  502. '2a03:2880:2110:df07:face:b00c::1',
  503. '2a03:2880:2110:df07::',
  504. ),
  505. );
  506. }
  507. }