| 1 |
<?php |
| 2 |
|
| 3 |
namespace Extendify\Tests\Integration; |
| 4 |
|
| 5 |
use Extendify\Shared\Services\Import\Post; |
| 6 |
use WP_UnitTestCase; |
| 7 |
|
| 8 |
class CronTest extends WP_UnitTestCase |
| 9 |
{ |
| 10 |
public function test_cron_executes() |
| 11 |
{ |
| 12 |
$isLocked = false; |
| 13 |
$cronUserId = null; |
| 14 |
$user = self::factory()->user->create(); |
| 15 |
wp_set_current_user($user); |
| 16 |
$postId = self::factory()->post->create(['post_content' => '<p>original</p>']); |
| 17 |
wp_set_post_lock($postId); |
| 18 |
|
| 19 |
// Cron runs as no user, so we need to reset the current user to 0 to simulate that. |
| 20 |
wp_set_current_user(0); |
| 21 |
add_action('my_cron_hook', function () use ($postId, &$isLocked, &$cronUserId) { |
| 22 |
$isLocked = wp_check_post_lock($postId); |
| 23 |
$cronUserId = get_current_user_id(); |
| 24 |
}); |
| 25 |
|
| 26 |
wp_schedule_single_event(time() - 10, 'my_cron_hook'); |
| 27 |
|
| 28 |
$crons = _get_cron_array(); |
| 29 |
foreach ($crons as $timestamp => $hooks) { |
| 30 |
if ($timestamp > time()) continue; |
| 31 |
|
| 32 |
foreach ($hooks as $hook => $events) { |
| 33 |
foreach ($events as $event) { |
| 34 |
do_action_ref_array($hook, $event['args']); |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
$this->assertTrue(Post::isLocked($postId)); |
| 40 |
$this->assertNotFalse(wp_check_post_lock($postId)); |
| 41 |
$this->assertNotNull($postId); |
| 42 |
$this->assertIsNumeric($isLocked); |
| 43 |
$this->assertSame($user, $isLocked); |
| 44 |
$this->assertSame(0, $cronUserId); |
| 45 |
} |
| 46 |
} |
| 47 |
|