Skip to content

Commit 81fb6d5

Browse files
Merge pull request #279 from JLG-WOCFR-DEV/codex/update-fallback-frequency-in-blc_activate_site
Fix activation fallback schedule handling
2 parents e5ea6df + ae68b29 commit 81fb6d5

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

liens-morts-detector-jlg/includes/blc-activation.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,17 @@ function blc_activate_site() {
426426
update_option('blc_activation_schedule_failure', $notice_payload);
427427
}
428428

429-
$fallback_frequency = 'daily';
430-
if ($frequency !== $fallback_frequency && isset($schedules[$fallback_frequency])) {
429+
$fallback_frequency = 'daily';
430+
$requested_frequency = isset($schedule_result['schedule']) && $schedule_result['schedule'] !== ''
431+
? $schedule_result['schedule']
432+
: $frequency_option;
433+
if (function_exists('wp_get_schedules')) {
434+
$schedules = wp_get_schedules();
435+
} else {
436+
$schedules = array();
437+
}
438+
439+
if ($requested_frequency !== $fallback_frequency && isset($schedules[$fallback_frequency])) {
431440
$fallback_timestamp = time() + (defined('HOUR_IN_SECONDS') ? (int) HOUR_IN_SECONDS : 3600);
432441
$fallback = wp_schedule_event($fallback_timestamp, $fallback_frequency, 'blc_check_links');
433442

tests/BlcSettingsPageTest.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,41 @@ class BlcSettingsPageTest extends TestCase
2222
*/
2323
private array $settingsErrors = [];
2424

25+
private ?string $upgradeStubPath = null;
26+
27+
private bool $createdUpgradeStub = false;
28+
2529
protected function setUp(): void
2630
{
2731
parent::setUp();
2832
require_once __DIR__ . '/../vendor/autoload.php';
2933
Monkey\setUp();
3034

35+
$this->createdUpgradeStub = false;
36+
3137
if (!defined('ABSPATH')) {
3238
define('ABSPATH', __DIR__ . '/../');
3339
}
3440

41+
$this->upgradeStubPath = ABSPATH . 'wp-admin/includes/upgrade.php';
42+
$upgradeDirectory = dirname((string) $this->upgradeStubPath);
43+
44+
if (!is_dir($upgradeDirectory)) {
45+
mkdir($upgradeDirectory, 0777, true);
46+
}
47+
48+
if (!file_exists((string) $this->upgradeStubPath)) {
49+
$stub = "<?php\n";
50+
$stub .= "if (!function_exists('dbDelta')) {\n";
51+
$stub .= " function dbDelta(\$sql) {\n";
52+
$stub .= " return true;\n";
53+
$stub .= " }\n";
54+
$stub .= "}\n";
55+
56+
file_put_contents((string) $this->upgradeStubPath, $stub);
57+
$this->createdUpgradeStub = true;
58+
}
59+
3560
if (!defined('HOUR_IN_SECONDS')) {
3661
define('HOUR_IN_SECONDS', 3600);
3762
}
@@ -142,6 +167,7 @@ protected function setUp(): void
142167
Functions\when('error_log')->justReturn(null);
143168
Functions\when('do_action')->justReturn(null);
144169
Functions\when('wp_unslash')->alias(static fn($value) => $value);
170+
Functions\when('set_transient')->justReturn(true);
145171
Functions\when('add_settings_error')->alias(function ($setting, $code, $message, $type = 'error') use ($test_case) {
146172
$test_case->settingsErrors[] = [
147173
'setting' => $setting,
@@ -150,13 +176,22 @@ protected function setUp(): void
150176
'type' => $type,
151177
];
152178
});
179+
require_once __DIR__ . '/../liens-morts-detector-jlg/includes/blc-activation.php';
153180
}
154181

155182
protected function tearDown(): void
156183
{
157184
Monkey\tearDown();
158185
parent::tearDown();
159186
$this->settingsErrors = [];
187+
188+
if ($this->createdUpgradeStub && $this->upgradeStubPath && file_exists($this->upgradeStubPath)) {
189+
unlink($this->upgradeStubPath);
190+
}
191+
192+
if (isset($GLOBALS['wpdb'])) {
193+
unset($GLOBALS['wpdb']);
194+
}
160195
}
161196

162197
public function test_settings_page_uses_settings_api_calls(): void
@@ -179,6 +214,73 @@ public function test_settings_page_uses_settings_api_calls(): void
179214
$this->assertStringContainsString('<div class="wrap">', (string) $output);
180215
}
181216

217+
public function test_activation_schedules_daily_fallback_when_initial_schedule_fails(): void
218+
{
219+
$currentTime = time();
220+
221+
$GLOBALS['wpdb'] = new class () {
222+
public string $prefix = 'wp_';
223+
224+
public function get_charset_collate(): string
225+
{
226+
return 'utf8mb4_general_ci';
227+
}
228+
229+
public function esc_like($text)
230+
{
231+
return $text;
232+
}
233+
234+
public function prepare($query, ...$args)
235+
{
236+
if (!empty($args)) {
237+
return vsprintf(str_replace('%s', '%s', $query), $args);
238+
}
239+
240+
return $query;
241+
}
242+
243+
public function get_var($query = null)
244+
{
245+
return '';
246+
}
247+
};
248+
249+
Functions\expect('blc_reset_link_check_schedule')
250+
->once()
251+
->andReturn([
252+
'success' => false,
253+
'error_code' => 'missing_schedule',
254+
'error_message' => '',
255+
'schedule' => 'weekly',
256+
]);
257+
258+
$scheduledEvents = [];
259+
260+
Functions\when('wp_schedule_event')->alias(function ($timestamp, $recurrence, $hook) use (&$scheduledEvents) {
261+
$scheduledEvents[] = [
262+
'timestamp' => $timestamp,
263+
'recurrence' => $recurrence,
264+
'hook' => $hook,
265+
];
266+
267+
return true;
268+
});
269+
270+
blc_activate_site();
271+
272+
$this->assertCount(1, $scheduledEvents, 'Exactly one fallback event should be scheduled.');
273+
274+
$event = $scheduledEvents[0];
275+
$upperBound = $currentTime + (defined('HOUR_IN_SECONDS') ? HOUR_IN_SECONDS : 3600) + 5;
276+
277+
$this->assertIsInt($event['timestamp']);
278+
$this->assertGreaterThanOrEqual($currentTime, $event['timestamp']);
279+
$this->assertLessThanOrEqual($upperBound, $event['timestamp']);
280+
$this->assertSame('daily', $event['recurrence']);
281+
$this->assertSame('blc_check_links', $event['hook']);
282+
}
283+
182284
public function test_invalid_frequency_falls_back_to_previous_value(): void
183285
{
184286
$expected_frequency = 'weekly';

0 commit comments

Comments
 (0)