|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | namespace { |
| 4 | + require_once __DIR__ . '/../vendor/autoload.php'; |
4 | 5 | require_once __DIR__ . '/translation-stubs.php'; |
5 | 6 |
|
6 | 7 | if (!class_exists('WP_Error')) { |
@@ -72,6 +73,21 @@ function is_email($email) |
72 | 73 | } |
73 | 74 | } |
74 | 75 |
|
| 76 | + if (!function_exists('esc_sql')) { |
| 77 | + function esc_sql($value) |
| 78 | + { |
| 79 | + if (is_array($value)) { |
| 80 | + return array_map('esc_sql', $value); |
| 81 | + } |
| 82 | + |
| 83 | + if (is_float($value) || is_int($value)) { |
| 84 | + return $value; |
| 85 | + } |
| 86 | + |
| 87 | + return addslashes((string) $value); |
| 88 | + } |
| 89 | + } |
| 90 | + |
75 | 91 |
|
76 | 92 | if (!class_exists('WP_Query')) { |
77 | 93 | class WP_Query |
@@ -4035,6 +4051,101 @@ public function test_blc_perform_image_check_records_remote_cdn_when_option_enab |
4035 | 4051 | } |
4036 | 4052 | } |
4037 | 4053 |
|
| 4054 | + public function test_blc_perform_image_check_records_metrics_history_and_triggers_hook(): void |
| 4055 | + { |
| 4056 | + global $wpdb; |
| 4057 | + $wpdb = $this->createWpdbStub(); |
| 4058 | + |
| 4059 | + $initialStatus = blc_get_default_image_scan_status(); |
| 4060 | + $initialStatus['state'] = 'running'; |
| 4061 | + $initialStatus['job_id'] = 'img-job-123'; |
| 4062 | + $initialStatus['attempt'] = 2; |
| 4063 | + $initialStatus['processed_batches'] = 1; |
| 4064 | + $initialStatus['total_batches'] = 4; |
| 4065 | + $initialStatus['processed_items'] = 10; |
| 4066 | + $initialStatus['total_items'] = 40; |
| 4067 | + $initialStatus['is_full_scan'] = true; |
| 4068 | + $this->options['blc_image_scan_status'] = $initialStatus; |
| 4069 | + $this->options['blc_image_scan_history'] = []; |
| 4070 | + |
| 4071 | + $controllerResult = ['processed' => 40]; |
| 4072 | + $controller = new class($controllerResult) { |
| 4073 | + /** @var array<string, mixed> */ |
| 4074 | + private array $result; |
| 4075 | + |
| 4076 | + /** |
| 4077 | + * @param array<string, mixed> $result |
| 4078 | + */ |
| 4079 | + public function __construct(array $result) |
| 4080 | + { |
| 4081 | + $this->result = $result; |
| 4082 | + } |
| 4083 | + |
| 4084 | + /** |
| 4085 | + * @param int $batch |
| 4086 | + * @param bool $is_full_scan |
| 4087 | + * |
| 4088 | + * @return array<string, mixed> |
| 4089 | + */ |
| 4090 | + public function run($batch, $is_full_scan) |
| 4091 | + { |
| 4092 | + blc_update_image_scan_status([ |
| 4093 | + 'state' => 'completed', |
| 4094 | + 'processed_batches' => 4, |
| 4095 | + 'total_batches' => 4, |
| 4096 | + 'processed_items' => 40, |
| 4097 | + 'total_items' => 40, |
| 4098 | + 'message' => 'Scan terminé', |
| 4099 | + ]); |
| 4100 | + |
| 4101 | + return $this->result; |
| 4102 | + } |
| 4103 | + }; |
| 4104 | + |
| 4105 | + \Brain\Monkey\Functions\when('blc_make_image_scan_controller')->alias(fn($queue) => $controller); |
| 4106 | + |
| 4107 | + $result = blc_perform_image_check(0, true); |
| 4108 | + |
| 4109 | + $this->assertSame($controllerResult, $result, 'Le contrôleur stub doit retourner le payload attendu.'); |
| 4110 | + |
| 4111 | + $this->assertArrayHasKey('blc_image_scan_metrics_history', $this->updatedOptions, 'Les métriques doivent être historisées.'); |
| 4112 | + $history = $this->updatedOptions['blc_image_scan_metrics_history']; |
| 4113 | + $this->assertIsArray($history); |
| 4114 | + $this->assertNotEmpty($history, 'Le premier lot doit enregistrer une entrée de métriques.'); |
| 4115 | + $metrics = $history[0]; |
| 4116 | + $this->assertSame('img-job-123', $metrics['job_id']); |
| 4117 | + $this->assertSame(0, $metrics['batch']); |
| 4118 | + $this->assertArrayHasKey('duration_ms', $metrics); |
| 4119 | + $this->assertIsInt($metrics['duration_ms']); |
| 4120 | + $this->assertGreaterThanOrEqual(0, $metrics['duration_ms']); |
| 4121 | + $this->assertLessThan(5000, $metrics['duration_ms']); |
| 4122 | + $this->assertSame($this->utcNow, $metrics['timestamp']); |
| 4123 | + $this->assertSame('completed', $metrics['state']); |
| 4124 | + $this->assertTrue($metrics['success']); |
| 4125 | + $this->assertSame(4, $metrics['processed_batches']); |
| 4126 | + $this->assertSame(4, $metrics['total_batches']); |
| 4127 | + $this->assertSame(40, $metrics['processed_items']); |
| 4128 | + $this->assertSame(40, $metrics['total_items']); |
| 4129 | + $this->assertSame(2, $metrics['attempt']); |
| 4130 | + $this->assertTrue($metrics['is_full_scan']); |
| 4131 | + |
| 4132 | + $actions = array_filter( |
| 4133 | + $this->triggeredActions, |
| 4134 | + static fn(array $action) => $action['hook'] === 'blc_image_scan_metrics' |
| 4135 | + ); |
| 4136 | + $this->assertNotEmpty($actions, 'Le hook blc_image_scan_metrics doit être déclenché.'); |
| 4137 | + $lastAction = array_pop($actions); |
| 4138 | + $this->assertSame($metrics, $lastAction['args'][0]); |
| 4139 | + $this->assertSame($controllerResult, $lastAction['args'][1]); |
| 4140 | + |
| 4141 | + $this->assertArrayHasKey('blc_image_scan_history', $this->options, 'Le journal des jobs doit être maintenu.'); |
| 4142 | + $historyEntries = $this->options['blc_image_scan_history']; |
| 4143 | + $this->assertNotEmpty($historyEntries); |
| 4144 | + $this->assertSame('img-job-123', $historyEntries[0]['job_id']); |
| 4145 | + $this->assertArrayHasKey('metrics', $historyEntries[0], 'Les métriques doivent être attachées à l’entrée du job.'); |
| 4146 | + $this->assertSame($metrics, $historyEntries[0]['metrics']); |
| 4147 | + } |
| 4148 | + |
4038 | 4149 | public function test_blc_perform_image_check_detects_missing_srcset_variant(): void |
4039 | 4150 | { |
4040 | 4151 | global $wpdb; |
|
0 commit comments