|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Scanner; |
| 4 | + |
| 5 | +use Brain\Monkey\Functions; |
| 6 | + |
| 7 | +final class LinkScanControllerTest extends ScannerTestCase |
| 8 | +{ |
| 9 | + public function test_acquire_lock_generates_token_when_unlocked(): void |
| 10 | + { |
| 11 | + Functions\when('blc_generate_lock_token')->alias(fn() => 'lock-token'); |
| 12 | + |
| 13 | + $token = blc_acquire_link_scan_lock(300); |
| 14 | + |
| 15 | + $this->assertSame('lock-token', $token); |
| 16 | + $this->assertSame( |
| 17 | + [ |
| 18 | + 'token' => 'lock-token', |
| 19 | + 'locked_at' => $this->currentTime, |
| 20 | + ], |
| 21 | + $this->options['blc_link_scan_lock'] ?? null, |
| 22 | + 'Lock state should be stored with the generated token.' |
| 23 | + ); |
| 24 | + $this->assertSame( |
| 25 | + 'lock-token', |
| 26 | + $this->options['blc_link_scan_lock_token'] ?? null, |
| 27 | + 'Token mirror option should be updated for watchdog checks.' |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + public function test_acquire_lock_returns_empty_string_when_lock_is_active(): void |
| 32 | + { |
| 33 | + $this->options['blc_link_scan_lock'] = [ |
| 34 | + 'token' => 'existing-token', |
| 35 | + 'locked_at' => $this->currentTime, |
| 36 | + ]; |
| 37 | + |
| 38 | + $token = blc_acquire_link_scan_lock(120); |
| 39 | + |
| 40 | + $this->assertSame('', $token); |
| 41 | + $this->assertSame( |
| 42 | + 'existing-token', |
| 43 | + $this->options['blc_link_scan_lock']['token'] ?? null, |
| 44 | + 'Existing lock token should remain untouched when lock is active.' |
| 45 | + ); |
| 46 | + $this->assertArrayNotHasKey( |
| 47 | + 'blc_link_scan_lock_token', |
| 48 | + $this->options, |
| 49 | + 'Mirror token option should not be overwritten when acquisition fails.' |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + public function test_release_lock_clears_stored_options(): void |
| 54 | + { |
| 55 | + $this->options['blc_link_scan_lock'] = [ |
| 56 | + 'token' => 'release-me', |
| 57 | + 'locked_at' => $this->currentTime, |
| 58 | + ]; |
| 59 | + $this->options['blc_link_scan_lock_token'] = 'release-me'; |
| 60 | + |
| 61 | + blc_release_link_scan_lock('release-me'); |
| 62 | + |
| 63 | + $this->assertArrayNotHasKey('blc_link_scan_lock', $this->options); |
| 64 | + $this->assertArrayNotHasKey('blc_link_scan_lock_token', $this->options); |
| 65 | + } |
| 66 | + |
| 67 | + public function test_perform_check_reschedules_batch_inside_rest_window(): void |
| 68 | + { |
| 69 | + $this->options['blc_rest_start_hour'] = '08'; |
| 70 | + $this->options['blc_rest_end_hour'] = '20'; |
| 71 | + $this->options['blc_batch_delay'] = 300; |
| 72 | + $this->options['blc_debug_mode'] = false; |
| 73 | + |
| 74 | + $this->setCurrentTime(strtotime('2023-01-01 12:34:00 UTC')); |
| 75 | + |
| 76 | + Functions\when('blc_acquire_link_scan_lock')->alias(fn() => 'token-123'); |
| 77 | + Functions\expect('blc_release_link_scan_lock')->once()->with('token-123'); |
| 78 | + Functions\when('current_filter')->alias(fn() => 'blc_check_links'); |
| 79 | + |
| 80 | + blc_perform_check(0, true); |
| 81 | + |
| 82 | + $this->assertCount(1, $this->scheduledEvents, 'A follow-up batch should be scheduled during the rest window.'); |
| 83 | + $scheduled = $this->scheduledEvents[0]; |
| 84 | + $this->assertSame('blc_check_batch', $scheduled['hook']); |
| 85 | + $this->assertSame([0, true, false], $scheduled['args']); |
| 86 | + $this->assertSame( |
| 87 | + strtotime('2023-01-01 20:00:00 UTC'), |
| 88 | + $scheduled['timestamp'], |
| 89 | + 'Next run should align with the configured rest window exit.' |
| 90 | + ); |
| 91 | + } |
| 92 | +} |
0 commit comments