cron.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. <?php
  2. /**
  3. * Test the cron scheduling functions
  4. *
  5. * @group cron
  6. */
  7. class Tests_Cron extends WP_UnitTestCase {
  8. /**
  9. * @var array Cron array for testing preflight filters.
  10. */
  11. private $preflight_cron_array;
  12. /**
  13. * @var int Timestamp of now() + 30 minutes;
  14. */
  15. private $plus_thirty_minutes;
  16. function setUp() {
  17. parent::setUp();
  18. // Make sure the schedule is clear.
  19. _set_cron_array( array() );
  20. $this->preflight_cron_array = array();
  21. $this->plus_thirty_minutes = strtotime( '+30 minutes' );
  22. }
  23. function tearDown() {
  24. // Make sure the schedule is clear.
  25. _set_cron_array( array() );
  26. parent::tearDown();
  27. }
  28. function test_wp_get_schedule_empty() {
  29. // Nothing scheduled.
  30. $hook = __FUNCTION__;
  31. $this->assertFalse( wp_get_schedule( $hook ) );
  32. }
  33. function test_schedule_event_single() {
  34. // Schedule an event and make sure it's returned by wp_next_scheduled().
  35. $hook = __FUNCTION__;
  36. $timestamp = strtotime( '+1 hour' );
  37. $scheduled = wp_schedule_single_event( $timestamp, $hook );
  38. $this->assertTrue( $scheduled );
  39. $this->assertSame( $timestamp, wp_next_scheduled( $hook ) );
  40. // It's a non-recurring event.
  41. $this->assertFalse( wp_get_schedule( $hook ) );
  42. }
  43. function test_schedule_event_single_args() {
  44. // Schedule an event with arguments and make sure it's returned by wp_next_scheduled().
  45. $hook = 'event';
  46. $timestamp = strtotime( '+1 hour' );
  47. $args = array( 'foo' );
  48. $scheduled = wp_schedule_single_event( $timestamp, $hook, $args );
  49. $this->assertTrue( $scheduled );
  50. // This returns the timestamp only if we provide matching args.
  51. $this->assertSame( $timestamp, wp_next_scheduled( $hook, $args ) );
  52. // These don't match so return nothing.
  53. $this->assertFalse( wp_next_scheduled( $hook ) );
  54. $this->assertFalse( wp_next_scheduled( $hook, array( 'bar' ) ) );
  55. // It's a non-recurring event.
  56. $this->assertFalse( wp_get_schedule( $hook, $args ) );
  57. }
  58. function test_schedule_event() {
  59. // Schedule an event and make sure it's returned by wp_next_scheduled().
  60. $hook = __FUNCTION__;
  61. $recur = 'hourly';
  62. $timestamp = strtotime( '+1 hour' );
  63. $scheduled = wp_schedule_event( $timestamp, $recur, $hook );
  64. $this->assertTrue( $scheduled );
  65. // It's scheduled for the right time.
  66. $this->assertSame( $timestamp, wp_next_scheduled( $hook ) );
  67. // It's a recurring event.
  68. $this->assertSame( $recur, wp_get_schedule( $hook ) );
  69. }
  70. function test_schedule_event_args() {
  71. // Schedule an event and make sure it's returned by wp_next_scheduled().
  72. $hook = 'event';
  73. $timestamp = strtotime( '+1 hour' );
  74. $recur = 'hourly';
  75. $args = array( 'foo' );
  76. $scheduled = wp_schedule_event( $timestamp, 'hourly', $hook, $args );
  77. $this->assertTrue( $scheduled );
  78. // This returns the timestamp only if we provide matching args.
  79. $this->assertSame( $timestamp, wp_next_scheduled( $hook, $args ) );
  80. // These don't match so return nothing.
  81. $this->assertFalse( wp_next_scheduled( $hook ) );
  82. $this->assertFalse( wp_next_scheduled( $hook, array( 'bar' ) ) );
  83. $this->assertSame( $recur, wp_get_schedule( $hook, $args ) );
  84. }
  85. function test_unschedule_event() {
  86. // Schedule an event and make sure it's returned by wp_next_scheduled().
  87. $hook = __FUNCTION__;
  88. $timestamp = strtotime( '+1 hour' );
  89. wp_schedule_single_event( $timestamp, $hook );
  90. $this->assertSame( $timestamp, wp_next_scheduled( $hook ) );
  91. // Now unschedule it and make sure it's gone.
  92. $unscheduled = wp_unschedule_event( $timestamp, $hook );
  93. $this->assertTrue( $unscheduled );
  94. $this->assertFalse( wp_next_scheduled( $hook ) );
  95. }
  96. function test_clear_schedule() {
  97. $hook = __FUNCTION__;
  98. $args = array( 'arg1' );
  99. // Schedule several events with and without arguments.
  100. wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
  101. wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
  102. wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
  103. wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
  104. // Make sure they're returned by wp_next_scheduled().
  105. $this->assertTrue( wp_next_scheduled( $hook ) > 0 );
  106. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  107. // Clear the schedule for the no args events and make sure it's gone.
  108. $hook_unscheduled = wp_clear_scheduled_hook( $hook );
  109. $this->assertSame( 2, $hook_unscheduled );
  110. $this->assertFalse( wp_next_scheduled( $hook ) );
  111. // The args events should still be there.
  112. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  113. // Clear the schedule for the args events and make sure they're gone too.
  114. // Note: wp_clear_scheduled_hook() expects args passed directly, rather than as an array.
  115. wp_clear_scheduled_hook( $hook, $args );
  116. $this->assertFalse( wp_next_scheduled( $hook, $args ) );
  117. }
  118. function test_clear_undefined_schedule() {
  119. $hook = __FUNCTION__;
  120. $args = array( 'arg1' );
  121. wp_schedule_single_event( strtotime( '+1 hour' ), $hook, $args );
  122. wp_schedule_single_event( strtotime( '+2 hour' ), $hook, $args );
  123. // Clear the schedule for no args events and ensure no events are cleared.
  124. $hook_unscheduled = wp_clear_scheduled_hook( $hook );
  125. $this->assertSame( 0, $hook_unscheduled );
  126. }
  127. function test_clear_schedule_multiple_args() {
  128. $hook = __FUNCTION__;
  129. $args = array( 'arg1', 'arg2' );
  130. // Schedule several events with and without arguments.
  131. wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
  132. wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
  133. wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
  134. wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
  135. // Make sure they're returned by wp_next_scheduled().
  136. $this->assertTrue( wp_next_scheduled( $hook ) > 0 );
  137. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  138. // Clear the schedule for the no args events and make sure it's gone.
  139. wp_clear_scheduled_hook( $hook );
  140. $this->assertFalse( wp_next_scheduled( $hook ) );
  141. // The args events should still be there.
  142. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  143. // Clear the schedule for the args events and make sure they're gone too.
  144. // Note: wp_clear_scheduled_hook() used to expect args passed directly, rather than as an array pre WP 3.0.
  145. wp_clear_scheduled_hook( $hook, $args );
  146. $this->assertFalse( wp_next_scheduled( $hook, $args ) );
  147. }
  148. /**
  149. * @ticket 10468
  150. */
  151. function test_clear_schedule_new_args() {
  152. $hook = __FUNCTION__;
  153. $args = array( 'arg1' );
  154. $multi_hook = __FUNCTION__ . '_multi';
  155. $multi_args = array( 'arg2', 'arg3' );
  156. // Schedule several events with and without arguments.
  157. wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
  158. wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
  159. wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
  160. wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
  161. wp_schedule_single_event( strtotime( '+5 hour' ), $multi_hook, $multi_args );
  162. wp_schedule_single_event( strtotime( '+6 hour' ), $multi_hook, $multi_args );
  163. // Make sure they're returned by wp_next_scheduled().
  164. $this->assertTrue( wp_next_scheduled( $hook ) > 0 );
  165. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  166. // Clear the schedule for the no args events and make sure it's gone.
  167. wp_clear_scheduled_hook( $hook );
  168. $this->assertFalse( wp_next_scheduled( $hook ) );
  169. // The args events should still be there.
  170. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  171. // Clear the schedule for the args events and make sure they're gone too.
  172. // wp_clear_scheduled_hook() should take args as an array like the other functions.
  173. wp_clear_scheduled_hook( $hook, $args );
  174. $this->assertFalse( wp_next_scheduled( $hook, $args ) );
  175. // Clear the schedule for the args events and make sure they're gone too.
  176. // wp_clear_scheduled_hook() should take args as an array like the other functions and does from WP 3.0.
  177. wp_clear_scheduled_hook( $multi_hook, $multi_args );
  178. $this->assertFalse( wp_next_scheduled( $multi_hook, $multi_args ) );
  179. }
  180. /**
  181. * @ticket 18997
  182. */
  183. function test_unschedule_hook() {
  184. $hook = __FUNCTION__;
  185. $args = array( rand_str() );
  186. // Schedule several events with and without arguments.
  187. wp_schedule_single_event( strtotime( '+1 hour' ), $hook );
  188. wp_schedule_single_event( strtotime( '+2 hour' ), $hook );
  189. wp_schedule_single_event( strtotime( '+3 hour' ), $hook, $args );
  190. wp_schedule_single_event( strtotime( '+4 hour' ), $hook, $args );
  191. // Make sure they're returned by wp_next_scheduled().
  192. $this->assertTrue( wp_next_scheduled( $hook ) > 0 );
  193. $this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
  194. // Clear the schedule and make sure it's gone.
  195. $unschedule_hook = wp_unschedule_hook( $hook );
  196. $this->assertSame( 4, $unschedule_hook );
  197. $this->assertFalse( wp_next_scheduled( $hook ) );
  198. }
  199. function test_unschedule_undefined_hook() {
  200. $hook = __FUNCTION__;
  201. $unrelated_hook = __FUNCTION__ . '_two';
  202. // Attempt to clear schedule on non-existant hook.
  203. $unschedule_hook = wp_unschedule_hook( $hook );
  204. $this->assertSame( 0, $unschedule_hook );
  205. $this->assertFalse( wp_next_scheduled( $hook ) );
  206. // Repeat tests with populated cron array.
  207. wp_schedule_single_event( strtotime( '+1 hour' ), $unrelated_hook );
  208. wp_schedule_single_event( strtotime( '+2 hour' ), $unrelated_hook );
  209. $unschedule_hook = wp_unschedule_hook( $hook );
  210. $this->assertSame( 0, $unschedule_hook );
  211. $this->assertFalse( wp_next_scheduled( $hook ) );
  212. }
  213. /**
  214. * @ticket 6966
  215. */
  216. function test_duplicate_event() {
  217. // Duplicate events close together should be skipped.
  218. $hook = __FUNCTION__;
  219. $args = array( 'arg1' );
  220. $ts1 = strtotime( '+5 minutes' );
  221. $ts2 = strtotime( '+3 minutes' );
  222. // First one works.
  223. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  224. // Subsequent ones are ignored.
  225. $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
  226. $subsequent = wp_schedule_single_event( $ts2, $hook, $args, true );
  227. $this->assertWPError( $subsequent );
  228. $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
  229. // The next event should be at +5 minutes, not +3.
  230. $this->assertSame( $ts1, wp_next_scheduled( $hook, $args ) );
  231. }
  232. /**
  233. * @ticket 6966
  234. */
  235. function test_not_duplicate_event() {
  236. // Duplicate events far apart should work normally.
  237. $hook = __FUNCTION__;
  238. $args = array( 'arg1' );
  239. $ts1 = strtotime( '+30 minutes' );
  240. $ts2 = strtotime( '+3 minutes' );
  241. // First one works.
  242. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  243. // Second works too.
  244. $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
  245. // The next event should be at +3 minutes, even though that one was scheduled second.
  246. $this->assertSame( $ts2, wp_next_scheduled( $hook, $args ) );
  247. wp_unschedule_event( $ts2, $hook, $args );
  248. // Following event at +30 minutes should be there too.
  249. $this->assertSame( $ts1, wp_next_scheduled( $hook, $args ) );
  250. }
  251. function test_not_duplicate_event_reversed() {
  252. // Duplicate events far apart should work normally regardless of order.
  253. $hook = __FUNCTION__;
  254. $args = array( 'arg1' );
  255. $ts1 = strtotime( '+3 minutes' );
  256. $ts2 = strtotime( '+30 minutes' );
  257. // First one works.
  258. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  259. // Second works too.
  260. $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
  261. // The next event should be at +3 minutes.
  262. $this->assertSame( $ts1, wp_next_scheduled( $hook, $args ) );
  263. wp_unschedule_event( $ts1, $hook, $args );
  264. // Following event should be there too.
  265. $this->assertSame( $ts2, wp_next_scheduled( $hook, $args ) );
  266. }
  267. /**
  268. * Ensure the pre_scheduled_event filter prevents
  269. * modification of the cron_array_option.
  270. *
  271. * @ticket 32656
  272. */
  273. function test_pre_schedule_event_filter() {
  274. $hook = __FUNCTION__;
  275. $args = array( 'arg1' );
  276. $ts1 = strtotime( '+30 minutes' );
  277. $ts2 = strtotime( '+3 minutes' );
  278. $expected = _get_cron_array();
  279. add_filter( 'pre_schedule_event', array( $this, '_filter_pre_schedule_event_filter' ), 10, 2 );
  280. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  281. $this->assertTrue( wp_schedule_event( $ts2, 'hourly', $hook ) );
  282. // Check cron option is unchanged.
  283. $this->assertSame( $expected, _get_cron_array() );
  284. $expected_preflight[ $ts2 ][ $hook ][ md5( serialize( array() ) ) ] = array(
  285. 'schedule' => 'hourly',
  286. 'interval' => HOUR_IN_SECONDS,
  287. 'args' => array(),
  288. );
  289. $expected_preflight[ $ts1 ][ $hook ][ md5( serialize( $args ) ) ] = array(
  290. 'schedule' => false,
  291. 'interval' => 0,
  292. 'args' => $args,
  293. );
  294. $this->assertSame( $expected_preflight, $this->preflight_cron_array );
  295. }
  296. /**
  297. * Filter the scheduling of events to use the preflight array.
  298. */
  299. function _filter_pre_schedule_event_filter( $null, $event ) {
  300. $key = md5( serialize( $event->args ) );
  301. $this->preflight_cron_array[ $event->timestamp ][ $event->hook ][ $key ] = array(
  302. 'schedule' => $event->schedule,
  303. 'interval' => isset( $event->interval ) ? $event->interval : 0,
  304. 'args' => $event->args,
  305. );
  306. uksort( $this->preflight_cron_array, 'strnatcasecmp' );
  307. return true;
  308. }
  309. /**
  310. * Ensure the pre_reschedule_event filter prevents
  311. * modification of the cron_array_option.
  312. *
  313. * @ticket 32656
  314. */
  315. function test_pre_reschedule_event_filter() {
  316. $hook = __FUNCTION__;
  317. $ts1 = strtotime( '+30 minutes' );
  318. // Add an event.
  319. $this->assertTrue( wp_schedule_event( $ts1, 'hourly', $hook ) );
  320. $expected = _get_cron_array();
  321. // Add preflight filter.
  322. add_filter( 'pre_reschedule_event', '__return_true' );
  323. // Reschedule event with preflight filter in place.
  324. wp_reschedule_event( $ts1, 'daily', $hook );
  325. // Check cron option is unchanged.
  326. $this->assertSame( $expected, _get_cron_array() );
  327. }
  328. /**
  329. * Ensure the pre_unschedule_event filter prevents
  330. * modification of the cron_array_option.
  331. *
  332. * @ticket 32656
  333. */
  334. function test_pre_unschedule_event_filter() {
  335. $hook = __FUNCTION__;
  336. $ts1 = strtotime( '+30 minutes' );
  337. // Add an event.
  338. $this->assertTrue( wp_schedule_event( $ts1, 'hourly', $hook ) );
  339. $expected = _get_cron_array();
  340. // Add preflight filter.
  341. add_filter( 'pre_unschedule_event', '__return_true' );
  342. // Unschedule event with preflight filter in place.
  343. wp_unschedule_event( $ts1, $hook );
  344. // Check cron option is unchanged.
  345. $this->assertSame( $expected, _get_cron_array() );
  346. }
  347. /**
  348. * Ensure the clearing scheduled hooks filter prevents
  349. * modification of the cron_array_option.
  350. *
  351. * @ticket 32656
  352. */
  353. function test_pre_clear_scheduled_hook_filters() {
  354. $hook = __FUNCTION__;
  355. $ts1 = strtotime( '+30 minutes' );
  356. // Add an event.
  357. $this->assertTrue( wp_schedule_event( $ts1, 'hourly', $hook ) );
  358. $expected = _get_cron_array();
  359. // Add preflight filters.
  360. add_filter( 'pre_clear_scheduled_hook', '__return_true' );
  361. add_filter( 'pre_unschedule_hook', '__return_zero' );
  362. // Unschedule event with preflight filter in place.
  363. wp_clear_scheduled_hook( $hook );
  364. // Check cron option is unchanged.
  365. $this->assertSame( $expected, _get_cron_array() );
  366. // Unschedule all events with preflight filter in place.
  367. wp_unschedule_hook( $hook );
  368. // Check cron option is unchanged.
  369. $this->assertSame( $expected, _get_cron_array() );
  370. }
  371. /**
  372. * Ensure the preflight hooks for scheduled events
  373. * return a filtered value as expected.
  374. *
  375. * @ticket 32656
  376. */
  377. function test_pre_scheduled_event_hooks() {
  378. add_filter( 'pre_get_scheduled_event', array( $this, 'filter_pre_scheduled_event_hooks' ) );
  379. $actual = wp_get_scheduled_event( 'preflight_event', array(), $this->plus_thirty_minutes );
  380. $actual2 = wp_next_scheduled( 'preflight_event', array() );
  381. $expected = (object) array(
  382. 'hook' => 'preflight_event',
  383. 'timestamp' => $this->plus_thirty_minutes,
  384. 'schedule' => false,
  385. 'args' => array(),
  386. );
  387. $this->assertEquals( $expected, $actual );
  388. $this->assertSame( $expected->timestamp, $actual2 );
  389. }
  390. function filter_pre_scheduled_event_hooks() {
  391. return (object) array(
  392. 'hook' => 'preflight_event',
  393. 'timestamp' => $this->plus_thirty_minutes,
  394. 'schedule' => false,
  395. 'args' => array(),
  396. );
  397. }
  398. /**
  399. * Ensure wp_get_scheduled_event() returns the expected one off events.
  400. *
  401. * When no timestamp is specified, the next event should be returned.
  402. * When a timestamp is specified, a particular event should be returned.
  403. *
  404. * @ticket 45976.
  405. */
  406. function test_get_scheduled_event_singles() {
  407. $hook = __FUNCTION__;
  408. $args = array( 'arg1' );
  409. $ts_late = strtotime( '+30 minutes' );
  410. $ts_next = strtotime( '+3 minutes' );
  411. $expected1 = (object) array(
  412. 'hook' => $hook,
  413. 'timestamp' => $ts_late,
  414. 'schedule' => false,
  415. 'args' => $args,
  416. );
  417. $expected2 = (object) array(
  418. 'hook' => $hook,
  419. 'timestamp' => $ts_next,
  420. 'schedule' => false,
  421. 'args' => $args,
  422. );
  423. // Schedule late running event.
  424. wp_schedule_single_event( $ts_late, $hook, $args );
  425. // Schedule next running event.
  426. wp_schedule_single_event( $ts_next, $hook, $args );
  427. // Late running, timestamp specified.
  428. $this->assertEquals( $expected1, wp_get_scheduled_event( $hook, $args, $ts_late ) );
  429. // Next running, timestamp specified.
  430. $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args, $ts_next ) );
  431. // Next running, no timestamp specified.
  432. $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
  433. }
  434. /**
  435. * Ensure wp_get_scheduled_event() returns the expected recurring events.
  436. *
  437. * When no timestamp is specified, the next event should be returned.
  438. * When a timestamp is specified, a particular event should be returned.
  439. *
  440. * @ticket 45976.
  441. */
  442. function test_get_scheduled_event_recurring() {
  443. $hook = __FUNCTION__;
  444. $args = array( 'arg1' );
  445. $ts_late = strtotime( '+30 minutes' );
  446. $ts_next = strtotime( '+3 minutes' );
  447. $schedule = 'hourly';
  448. $interval = HOUR_IN_SECONDS;
  449. $expected1 = (object) array(
  450. 'hook' => $hook,
  451. 'timestamp' => $ts_late,
  452. 'schedule' => $schedule,
  453. 'args' => $args,
  454. 'interval' => $interval,
  455. );
  456. $expected2 = (object) array(
  457. 'hook' => $hook,
  458. 'timestamp' => $ts_next,
  459. 'schedule' => $schedule,
  460. 'args' => $args,
  461. 'interval' => $interval,
  462. );
  463. // Schedule late running event.
  464. wp_schedule_event( $ts_late, $schedule, $hook, $args );
  465. // Schedule next running event.
  466. wp_schedule_event( $ts_next, $schedule, $hook, $args );
  467. // Late running, timestamp specified.
  468. $this->assertEquals( $expected1, wp_get_scheduled_event( $hook, $args, $ts_late ) );
  469. // Next running, timestamp specified.
  470. $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args, $ts_next ) );
  471. // Next running, no timestamp specified.
  472. $this->assertEquals( $expected2, wp_get_scheduled_event( $hook, $args ) );
  473. }
  474. /**
  475. * Ensure wp_get_scheduled_event() returns false when expected.
  476. *
  477. * @ticket 45976.
  478. */
  479. function test_get_scheduled_event_false() {
  480. $hook = __FUNCTION__;
  481. $args = array( 'arg1' );
  482. $ts = strtotime( '+3 minutes' );
  483. // No scheduled events.
  484. // - With timestamp.
  485. $this->assertFalse( wp_get_scheduled_event( $hook, $args, $ts ) );
  486. // - Get next, none scheduled.
  487. $this->assertFalse( wp_get_scheduled_event( $hook, $args ) );
  488. // Schedule an event.
  489. wp_schedule_event( $ts, $hook, $args );
  490. // - Unregistered timestamp.
  491. $this->assertFalse( wp_get_scheduled_event( $hook, $args, strtotime( '+30 minutes' ) ) );
  492. // - Invalid timestamp.
  493. $this->assertFalse( wp_get_scheduled_event( $hook, $args, 'Words Fail!' ) );
  494. }
  495. /**
  496. * Ensure any past event counts as a duplicate.
  497. *
  498. * @ticket 44818
  499. */
  500. function test_duplicate_past_event() {
  501. $hook = __FUNCTION__;
  502. $args = array( 'arg1' );
  503. $ts1 = strtotime( '-14 minutes' );
  504. $ts2 = strtotime( '+5 minutes' );
  505. $ts3 = strtotime( '-2 minutes' );
  506. // First event scheduled successfully.
  507. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  508. // Second event fails.
  509. $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
  510. // Third event fails.
  511. $this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
  512. // Fourth event fails.
  513. $subsequent = wp_schedule_single_event( $ts3, $hook, $args, true );
  514. $this->assertWPError( $subsequent );
  515. $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
  516. }
  517. /**
  518. * Ensure any near future event counts as a duplicate.
  519. *
  520. * @ticket 44818
  521. */
  522. function test_duplicate_near_future_event() {
  523. $hook = __FUNCTION__;
  524. $args = array( 'arg1' );
  525. $ts1 = strtotime( '+4 minutes' );
  526. $ts2 = strtotime( '-15 minutes' );
  527. $ts3 = strtotime( '+12 minutes' );
  528. // First event scheduled successfully.
  529. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  530. // Second event fails.
  531. $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
  532. // Third event fails.
  533. $this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
  534. // Fourth event fails.
  535. $subsequent = wp_schedule_single_event( $ts3, $hook, $args, true );
  536. $this->assertWPError( $subsequent );
  537. $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
  538. }
  539. /**
  540. * Duplicate future events are disallowed.
  541. *
  542. * @ticket 44818
  543. */
  544. function test_duplicate_future_event() {
  545. $hook = __FUNCTION__;
  546. $args = array( 'arg1' );
  547. $ts1 = strtotime( '+15 minutes' );
  548. $ts2 = strtotime( '-600 seconds', $ts1 );
  549. $ts3 = strtotime( '+600 seconds', $ts1 );
  550. // First event scheduled successfully.
  551. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  552. // Events within ten minutes should fail.
  553. $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
  554. $this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
  555. $subsequent = wp_schedule_single_event( $ts3, $hook, $args, true );
  556. $this->assertWPError( $subsequent );
  557. $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
  558. }
  559. /**
  560. * Future events are allowed.
  561. *
  562. * @ticket 44818
  563. */
  564. function test_not_duplicate_future_event() {
  565. $hook = __FUNCTION__;
  566. $args = array( 'arg1' );
  567. $ts1 = strtotime( '+15 minutes' );
  568. $ts2 = strtotime( '-601 seconds', $ts1 );
  569. $ts3 = strtotime( '+601 seconds', $ts1 );
  570. // First event scheduled successfully.
  571. $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
  572. // Events over ten minutes should work.
  573. $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
  574. $this->assertTrue( wp_schedule_single_event( $ts3, $hook, $args ) );
  575. }
  576. /**
  577. * @ticket 49961
  578. */
  579. public function test_invalid_timestamp_for_event_returns_error() {
  580. $single_event = wp_schedule_single_event( -50, 'hook', array(), true );
  581. $event = wp_schedule_event( -50, 'daily', 'hook', array(), true );
  582. $rescheduled_event = wp_reschedule_event( -50, 'daily', 'hook', array(), true );
  583. $unscheduled_event = wp_unschedule_event( -50, 'hook', array(), true );
  584. $this->assertWPError( $single_event );
  585. $this->assertSame( 'invalid_timestamp', $single_event->get_error_code() );
  586. $this->assertWPError( $event );
  587. $this->assertSame( 'invalid_timestamp', $event->get_error_code() );
  588. $this->assertWPError( $rescheduled_event );
  589. $this->assertSame( 'invalid_timestamp', $rescheduled_event->get_error_code() );
  590. $this->assertWPError( $unscheduled_event );
  591. $this->assertSame( 'invalid_timestamp', $unscheduled_event->get_error_code() );
  592. }
  593. /**
  594. * @ticket 49961
  595. */
  596. public function test_invalid_recurrence_for_event_returns_error() {
  597. $event = wp_schedule_event( time(), 'invalid', 'hook', array(), true );
  598. $rescheduled_event = wp_reschedule_event( time(), 'invalid', 'hook', array(), true );
  599. $this->assertWPError( $event );
  600. $this->assertSame( 'invalid_schedule', $event->get_error_code() );
  601. $this->assertWPError( $rescheduled_event );
  602. $this->assertSame( 'invalid_schedule', $rescheduled_event->get_error_code() );
  603. }
  604. /**
  605. * @ticket 49961
  606. */
  607. public function test_disallowed_event_returns_false_when_wp_error_is_set_to_false() {
  608. add_filter( 'schedule_event', '__return_false' );
  609. $single_event = wp_schedule_single_event( time(), 'hook', array() );
  610. $event = wp_schedule_event( time(), 'daily', 'hook', array() );
  611. $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array() );
  612. $this->assertFalse( $single_event );
  613. $this->assertFalse( $event );
  614. $this->assertFalse( $rescheduled_event );
  615. }
  616. /**
  617. * @ticket 49961
  618. */
  619. public function test_disallowed_event_returns_error_when_wp_error_is_set_to_true() {
  620. add_filter( 'schedule_event', '__return_false' );
  621. $single_event = wp_schedule_single_event( time(), 'hook', array(), true );
  622. $event = wp_schedule_event( time(), 'daily', 'hook', array(), true );
  623. $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array(), true );
  624. $this->assertWPError( $single_event );
  625. $this->assertSame( 'schedule_event_false', $single_event->get_error_code() );
  626. $this->assertWPError( $event );
  627. $this->assertSame( 'schedule_event_false', $event->get_error_code() );
  628. $this->assertWPError( $rescheduled_event );
  629. $this->assertSame( 'schedule_event_false', $rescheduled_event->get_error_code() );
  630. }
  631. /**
  632. * @ticket 49961
  633. */
  634. public function test_schedule_short_circuit_with_error_returns_false_when_wp_error_is_set_to_false() {
  635. $return_error = function( $pre, $event, $wp_error ) {
  636. $this->assertFalse( $wp_error );
  637. return new WP_Error(
  638. 'my_error',
  639. 'An error ocurred'
  640. );
  641. };
  642. // Add filters which return a WP_Error:
  643. add_filter( 'pre_schedule_event', $return_error, 10, 3 );
  644. add_filter( 'pre_reschedule_event', $return_error, 10, 3 );
  645. // Schedule events without the `$wp_error` parameter:
  646. $single_event = wp_schedule_single_event( time(), 'hook', array() );
  647. $event = wp_schedule_event( time(), 'daily', 'hook', array() );
  648. $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array() );
  649. // Ensure boolean false is returned:
  650. $this->assertFalse( $single_event );
  651. $this->assertFalse( $event );
  652. $this->assertFalse( $rescheduled_event );
  653. }
  654. /**
  655. * @ticket 49961
  656. */
  657. public function test_schedule_short_circuit_with_error_returns_error_when_wp_error_is_set_to_true() {
  658. $return_error = function( $pre, $event, $wp_error ) {
  659. $this->assertTrue( $wp_error );
  660. return new WP_Error(
  661. 'my_error',
  662. 'An error ocurred'
  663. );
  664. };
  665. // Add filters which return a WP_Error:
  666. add_filter( 'pre_schedule_event', $return_error, 10, 3 );
  667. add_filter( 'pre_reschedule_event', $return_error, 10, 3 );
  668. // Schedule events with the `$wp_error` parameter:
  669. $single_event = wp_schedule_single_event( time(), 'hook', array(), true );
  670. $event = wp_schedule_event( time(), 'daily', 'hook', array(), true );
  671. $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array(), true );
  672. // Ensure the error object is returned:
  673. $this->assertWPError( $single_event );
  674. $this->assertSame( 'my_error', $single_event->get_error_code() );
  675. $this->assertWPError( $event );
  676. $this->assertSame( 'my_error', $event->get_error_code() );
  677. $this->assertWPError( $rescheduled_event );
  678. $this->assertSame( 'my_error', $rescheduled_event->get_error_code() );
  679. }
  680. /**
  681. * @ticket 49961
  682. */
  683. public function test_schedule_short_circuit_with_false_returns_false_when_wp_error_is_set_to_false() {
  684. // Add filters which return false:
  685. add_filter( 'pre_schedule_event', '__return_false' );
  686. add_filter( 'pre_reschedule_event', '__return_false' );
  687. // Schedule events without the `$wp_error` parameter:
  688. $single_event = wp_schedule_single_event( time(), 'hook', array() );
  689. $event = wp_schedule_event( time(), 'daily', 'hook', array() );
  690. $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array() );
  691. // Ensure false is returned:
  692. $this->assertFalse( $single_event );
  693. $this->assertFalse( $event );
  694. $this->assertFalse( $rescheduled_event );
  695. }
  696. /**
  697. * @ticket 49961
  698. */
  699. public function test_schedule_short_circuit_with_false_returns_error_when_wp_error_is_set_to_true() {
  700. // Add filters which return false:
  701. add_filter( 'pre_schedule_event', '__return_false' );
  702. add_filter( 'pre_reschedule_event', '__return_false' );
  703. // Schedule events with the `$wp_error` parameter:
  704. $single_event = wp_schedule_single_event( time(), 'hook', array(), true );
  705. $event = wp_schedule_event( time(), 'daily', 'hook', array(), true );
  706. $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array(), true );
  707. // Ensure an error object is returned:
  708. $this->assertWPError( $single_event );
  709. $this->assertSame( 'pre_schedule_event_false', $single_event->get_error_code() );
  710. $this->assertWPError( $event );
  711. $this->assertSame( 'pre_schedule_event_false', $event->get_error_code() );
  712. $this->assertWPError( $rescheduled_event );
  713. $this->assertSame( 'pre_reschedule_event_false', $rescheduled_event->get_error_code() );
  714. }
  715. /**
  716. * @ticket 49961
  717. * @expectedDeprecated wp_clear_scheduled_hook
  718. */
  719. public function test_deprecated_argument_usage_of_wp_clear_scheduled_hook() {
  720. $return_pre = function( $pre, $hook, $args, $wp_error ) {
  721. $this->assertSame( array( 1, 2, 3 ), $args );
  722. $this->assertFalse( $wp_error );
  723. return $pre;
  724. };
  725. add_filter( 'pre_clear_scheduled_hook', $return_pre, 10, 4 );
  726. $cleared = wp_clear_scheduled_hook( 'hook', 1, 2, 3 );
  727. $this->assertSame( 0, $cleared );
  728. }
  729. /**
  730. * @ticket 49961
  731. */
  732. public function test_clear_scheduled_hook_returns_default_pre_filter_error_when_wp_error_is_set_to_true() {
  733. add_filter( 'pre_unschedule_event', '__return_false' );
  734. wp_schedule_single_event( strtotime( '+1 hour' ), 'test_hook' );
  735. wp_schedule_single_event( strtotime( '+2 hours' ), 'test_hook' );
  736. $cleared = wp_clear_scheduled_hook( 'test_hook', array(), true );
  737. $this->assertWPError( $cleared );
  738. $this->assertSame(
  739. array(
  740. 'pre_unschedule_event_false',
  741. ),
  742. $cleared->get_error_codes()
  743. );
  744. $this->assertCount( 2, $cleared->get_error_messages() );
  745. }
  746. /**
  747. * @ticket 49961
  748. */
  749. public function test_clear_scheduled_hook_returns_custom_pre_filter_error_when_wp_error_is_set_to_true() {
  750. $return_error = function( $pre, $timestamp, $hook, $args, $wp_error ) {
  751. $this->assertTrue( $wp_error );
  752. return new WP_Error( 'error_code', 'error message' );
  753. };
  754. add_filter( 'pre_unschedule_event', $return_error, 10, 5 );
  755. wp_schedule_single_event( strtotime( '+1 hour' ), 'test_hook' );
  756. wp_schedule_single_event( strtotime( '+2 hours' ), 'test_hook' );
  757. $cleared = wp_clear_scheduled_hook( 'test_hook', array(), true );
  758. $this->assertWPError( $cleared );
  759. $this->assertSame(
  760. array(
  761. 'error_code',
  762. ),
  763. $cleared->get_error_codes()
  764. );
  765. $this->assertSame(
  766. array(
  767. 'error message',
  768. 'error message',
  769. ),
  770. $cleared->get_error_messages()
  771. );
  772. }
  773. /**
  774. * @ticket 49961
  775. */
  776. public function test_unschedule_short_circuit_with_error_returns_false_when_wp_error_is_set_to_false() {
  777. $return_error = function( $pre, $hook, $wp_error ) {
  778. $this->assertFalse( $wp_error );
  779. return new WP_Error(
  780. 'my_error',
  781. 'An error ocurred'
  782. );
  783. };
  784. // Add a filter which returns a WP_Error:
  785. add_filter( 'pre_unschedule_hook', $return_error, 10, 3 );
  786. // Unschedule a hook without the `$wp_error` parameter:
  787. $result = wp_unschedule_hook( 'hook' );
  788. // Ensure boolean false is returned:
  789. $this->assertFalse( $result );
  790. }
  791. /**
  792. * @ticket 49961
  793. */
  794. public function test_unschedule_short_circuit_with_error_returns_error_when_wp_error_is_set_to_true() {
  795. $return_error = function( $pre, $hook, $wp_error ) {
  796. $this->assertTrue( $wp_error );
  797. return new WP_Error(
  798. 'my_error',
  799. 'An error ocurred'
  800. );
  801. };
  802. // Add a filter which returns a WP_Error:
  803. add_filter( 'pre_unschedule_hook', $return_error, 10, 3 );
  804. // Unschedule a hook with the `$wp_error` parameter:
  805. $result = wp_unschedule_hook( 'hook', true );
  806. // Ensure the error object is returned:
  807. $this->assertWPError( $result );
  808. $this->assertSame( 'my_error', $result->get_error_code() );
  809. }
  810. /**
  811. * @ticket 49961
  812. */
  813. public function test_unschedule_short_circuit_with_false_returns_false_when_wp_error_is_set_to_false() {
  814. // Add a filter which returns false:
  815. add_filter( 'pre_unschedule_hook', '__return_false' );
  816. // Unschedule a hook without the `$wp_error` parameter:
  817. $result = wp_unschedule_hook( 'hook' );
  818. // Ensure false is returned:
  819. $this->assertFalse( $result );
  820. }
  821. /**
  822. * @ticket 49961
  823. */
  824. public function test_unschedule_short_circuit_with_false_returns_error_when_wp_error_is_set_to_true() {
  825. // Add a filter which returns false:
  826. add_filter( 'pre_unschedule_hook', '__return_false' );
  827. // Unchedule a hook with the `$wp_error` parameter:
  828. $result = wp_unschedule_hook( 'hook', true );
  829. // Ensure an error object is returned:
  830. $this->assertWPError( $result );
  831. $this->assertSame( 'pre_unschedule_hook_false', $result->get_error_code() );
  832. }
  833. /**
  834. * @ticket 49961
  835. */
  836. public function test_cron_array_error_is_returned_when_scheduling_single_event() {
  837. // Force update_option() to fail by setting the new value to match the existing:
  838. add_filter(
  839. 'pre_update_option_cron',
  840. function() {
  841. return get_option( 'cron' );
  842. }
  843. );
  844. // Attempt to schedule a valid event:
  845. $event = wp_schedule_single_event( time(), 'hook', array(), true );
  846. // Ensure an error object is returned:
  847. $this->assertWPError( $event );
  848. $this->assertSame( 'could_not_set', $event->get_error_code() );
  849. }
  850. /**
  851. * @ticket 49961
  852. */
  853. public function test_cron_array_error_is_returned_when_scheduling_event() {
  854. // Force update_option() to fail by setting the new value to match the existing:
  855. add_filter(
  856. 'pre_update_option_cron',
  857. function() {
  858. return get_option( 'cron' );
  859. }
  860. );
  861. // Attempt to schedule a valid event:
  862. $event = wp_schedule_event( time(), 'daily', 'hook', array(), true );
  863. // Ensure an error object is returned:
  864. $this->assertWPError( $event );
  865. $this->assertSame( 'could_not_set', $event->get_error_code() );
  866. }
  867. /**
  868. * @ticket 49961
  869. */
  870. public function test_cron_array_error_is_returned_when_unscheduling_hook() {
  871. // Schedule a valid event:
  872. $event = wp_schedule_event( strtotime( '+1 hour' ), 'daily', 'hook', array(), true );
  873. // Force update_option() to fail by setting the new value to match the existing:
  874. add_filter(
  875. 'pre_update_option_cron',
  876. function() {
  877. return get_option( 'cron' );
  878. }
  879. );
  880. // Attempt to unschedule the hook:
  881. $unscheduled = wp_unschedule_hook( 'hook', true );
  882. // Ensure an error object is returned:
  883. $this->assertTrue( $event );
  884. $this->assertWPError( $unscheduled );
  885. $this->assertSame( 'could_not_set', $unscheduled->get_error_code() );
  886. }
  887. /**
  888. * @ticket 49961
  889. */
  890. public function test_cron_array_error_is_returned_when_unscheduling_event() {
  891. // Schedule a valid event:
  892. $event = wp_schedule_event( strtotime( '+1 hour' ), 'daily', 'hook', array(), true );
  893. // Force update_option() to fail by setting the new value to match the existing:
  894. add_filter(
  895. 'pre_update_option_cron',
  896. function() {
  897. return get_option( 'cron' );
  898. }
  899. );
  900. // Attempt to unschedule the event:
  901. $unscheduled = wp_unschedule_event( wp_next_scheduled( 'hook' ), 'hook', array(), true );
  902. // Ensure an error object is returned:
  903. $this->assertTrue( $event );
  904. $this->assertWPError( $unscheduled );
  905. $this->assertSame( 'could_not_set', $unscheduled->get_error_code() );
  906. }
  907. }