-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathverify-brevo-integration.php
More file actions
346 lines (305 loc) · 8.83 KB
/
verify-brevo-integration.php
File metadata and controls
346 lines (305 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* Verification Script for Brevo Integration
*
* Run this from command line: php verify-brevo-integration.php
*
* @package FormsCRM
*/
echo "🧪 FormsCRM Brevo Integration - Verification Script\n";
echo str_repeat( '=', 70 ) . "\n\n";
$tests_passed = 0;
$tests_failed = 0;
$base_path = dirname( __DIR__ );
/**
* Run a test and output result
*/
function run_test( $name, $condition, $success_msg = '', $fail_msg = '' ) {
global $tests_passed, $tests_failed;
echo "Testing: {$name}... ";
if ( $condition ) {
echo "✅ PASS\n";
if ( $success_msg ) {
echo " → {$success_msg}\n";
}
$tests_passed++;
} else {
echo "❌ FAIL\n";
if ( $fail_msg ) {
echo " → {$fail_msg}\n";
}
$tests_failed++;
}
echo "\n";
}
/**
* Simulate the create_entry logic
*/
function simulate_brevo_create_entry( $merge_vars, $list_id = 3 ) {
$standard_fields = array(
'email',
'ext_id',
'emailBlacklisted',
'smsBlacklisted',
'listIds',
'unlinkListIds',
'updateEnabled',
'smtpBlacklistSender',
);
$subscriber = array();
$subscriber['listIds'] = array( $list_id );
foreach ( $merge_vars as $element ) {
$field_name = $element['name'];
$field_value = $element['value'];
if ( false === strpos( $field_name, '|' ) ) {
if ( in_array( $field_name, $standard_fields, true ) ) {
$subscriber[ $field_name ] = $field_value;
} else {
$subscriber['attributes'][ $field_name ] = $field_value;
}
} else {
$key = str_replace( 'attributes|', '', $field_name );
$subscriber['attributes'][ $key ] = $field_value;
}
}
return $subscriber;
}
echo "📁 FILE STRUCTURE TEST\n";
echo str_repeat( '-', 70 ) . "\n";
// Test 1: Brevo class file exists.
$brevo_file = $base_path . '/includes/crm-library/class-crmlib-brevo.php';
run_test(
'Brevo Class File',
file_exists( $brevo_file ),
'File found at: ' . $brevo_file,
'File not found at: ' . $brevo_file
);
if ( ! file_exists( $brevo_file ) ) {
die( "Cannot continue without Brevo class file.\n" );
}
$brevo_content = file_get_contents( $brevo_file );
// Test 2: Standard fields list exists.
run_test(
'Standard Fields List',
strpos( $brevo_content, '$standard_fields' ) !== false,
'Standard fields array found in code',
'Standard fields array not found'
);
// Test 3: Check for all required standard fields.
$required_fields = array( 'email', 'ext_id', 'emailBlacklisted', 'smsBlacklisted' );
$all_found = true;
foreach ( $required_fields as $field ) {
if ( strpos( $brevo_content, "'{$field}'" ) === false ) {
$all_found = false;
break;
}
}
run_test(
'All Standard Fields Present',
$all_found,
'All required standard fields found',
'Some standard fields missing'
);
echo "\n🔬 LOGIC TESTS\n";
echo str_repeat( '-', 70 ) . "\n";
// Test 4: Email goes to root level.
$test_data = simulate_brevo_create_entry(
array(
array(
'name' => 'email',
'value' => 'test@example.com',
),
)
);
run_test(
'Email at Root Level',
isset( $test_data['email'] ) && $test_data['email'] === 'test@example.com',
'Email correctly placed at root level',
'Email not at root level'
);
// Test 5: Custom fields go to attributes.
$test_data = simulate_brevo_create_entry(
array(
array(
'name' => 'email',
'value' => 'test@example.com',
),
array(
'name' => 'FIRSTNAME',
'value' => 'John',
),
array(
'name' => 'LASTNAME',
'value' => 'Doe',
),
)
);
run_test(
'Custom Fields in Attributes',
isset( $test_data['attributes']['FIRSTNAME'] ) &&
isset( $test_data['attributes']['LASTNAME'] ) &&
$test_data['attributes']['FIRSTNAME'] === 'John' &&
$test_data['attributes']['LASTNAME'] === 'Doe',
'Custom fields correctly placed in attributes object',
'Custom fields not in attributes'
);
// Test 6: Standard fields NOT in attributes.
run_test(
'Email NOT in Attributes',
! isset( $test_data['attributes']['email'] ),
'Email correctly excluded from attributes',
'Email incorrectly placed in attributes'
);
// Test 7: Backwards compatibility with attributes| prefix.
$test_data = simulate_brevo_create_entry(
array(
array(
'name' => 'email',
'value' => 'test@example.com',
),
array(
'name' => 'attributes|FIRSTNAME',
'value' => 'Jane',
),
array(
'name' => 'attributes|LASTNAME',
'value' => 'Smith',
),
)
);
run_test(
'Backwards Compatibility (attributes| prefix)',
isset( $test_data['attributes']['FIRSTNAME'] ) &&
isset( $test_data['attributes']['LASTNAME'] ) &&
$test_data['attributes']['FIRSTNAME'] === 'Jane' &&
$test_data['attributes']['LASTNAME'] === 'Smith',
'Prefix attributes| still works correctly',
'Prefix attributes| not working'
);
// Test 8: Mixed standard and custom fields.
$test_data = simulate_brevo_create_entry(
array(
array(
'name' => 'email',
'value' => 'max@example.com',
),
array(
'name' => 'ext_id',
'value' => '12345',
),
array(
'name' => 'FIRSTNAME',
'value' => 'Max',
),
array(
'name' => 'LASTNAME',
'value' => 'Mustermann',
),
array(
'name' => 'SMS',
'value' => '+34600000000',
),
)
);
$mixed_test = isset( $test_data['email'] ) &&
isset( $test_data['ext_id'] ) &&
isset( $test_data['attributes']['FIRSTNAME'] ) &&
isset( $test_data['attributes']['LASTNAME'] ) &&
isset( $test_data['attributes']['SMS'] ) &&
! isset( $test_data['attributes']['email'] ) &&
! isset( $test_data['attributes']['ext_id'] );
run_test(
'Mixed Standard and Custom Fields',
$mixed_test,
'Standard fields at root, custom fields in attributes',
'Mixed fields not correctly separated'
);
echo "\n📋 JSON STRUCTURE VALIDATION\n";
echo str_repeat( '-', 70 ) . "\n";
// Test 9: JSON structure matches Brevo API.
$json_string = json_encode( $test_data, JSON_PRETTY_PRINT );
echo "Generated JSON Structure:\n";
echo str_repeat( '-', 70 ) . "\n";
echo $json_string . "\n";
echo str_repeat( '-', 70 ) . "\n\n";
run_test(
'Valid JSON Structure',
json_decode( $json_string ) !== null,
'JSON is valid and can be decoded',
'JSON is invalid'
);
// Test 10: JSON has required keys.
$decoded = json_decode( $json_string, true );
$required_keys = array( 'listIds', 'email', 'attributes' );
$has_all_keys = true;
foreach ( $required_keys as $key ) {
if ( ! isset( $decoded[ $key ] ) ) {
$has_all_keys = false;
break;
}
}
run_test(
'Required Keys Present',
$has_all_keys,
'JSON has listIds, email, and attributes',
'Some required keys missing'
);
echo "\n📊 EXAMPLE USE CASES\n";
echo str_repeat( '-', 70 ) . "\n";
// Example 1: Contact Form 7 typical fields.
echo "Example 1: Contact Form 7 Typical Submission\n";
$example1 = simulate_brevo_create_entry(
array(
array( 'name' => 'email', 'value' => 'customer@example.com' ),
array( 'name' => 'FIRSTNAME', 'value' => 'María' ),
array( 'name' => 'LASTNAME', 'value' => 'García' ),
array( 'name' => 'SMS', 'value' => '+34666777888' ),
array( 'name' => 'COMPANY', 'value' => 'Example Corp' ),
),
5
);
echo json_encode( $example1, JSON_PRETTY_PRINT ) . "\n\n";
// Example 2: With attributes| prefix (old style).
echo "Example 2: With attributes| Prefix (Backwards Compatible)\n";
$example2 = simulate_brevo_create_entry(
array(
array( 'name' => 'email', 'value' => 'oldstyle@example.com' ),
array( 'name' => 'attributes|FIRSTNAME', 'value' => 'Pedro' ),
array( 'name' => 'attributes|LASTNAME', 'value' => 'Sánchez' ),
)
);
echo json_encode( $example2, JSON_PRETTY_PRINT ) . "\n\n";
// Example 3: Multiple standard fields.
echo "Example 3: Multiple Standard Fields\n";
$example3 = simulate_brevo_create_entry(
array(
array( 'name' => 'email', 'value' => 'vip@example.com' ),
array( 'name' => 'ext_id', 'value' => 'CRM-12345' ),
array( 'name' => 'emailBlacklisted', 'value' => false ),
array( 'name' => 'FIRSTNAME', 'value' => 'VIP' ),
array( 'name' => 'LASTNAME', 'value' => 'Customer' ),
)
);
echo json_encode( $example3, JSON_PRETTY_PRINT ) . "\n\n";
echo "\n📊 TEST SUMMARY\n";
echo str_repeat( '=', 70 ) . "\n";
$total_tests = $tests_passed + $tests_failed;
$pass_rate = $total_tests > 0 ? round( ( $tests_passed / $total_tests ) * 100, 1 ) : 0;
echo "Total Tests: {$total_tests}\n";
echo "Passed: " . "\033[32m{$tests_passed}\033[0m\n";
echo "Failed: " . ( $tests_failed > 0 ? "\033[31m{$tests_failed}\033[0m" : $tests_failed ) . "\n";
echo "Pass Rate: {$pass_rate}%\n\n";
if ( 0 === $tests_failed ) {
echo "\033[32m✅ ALL TESTS PASSED!\033[0m\n";
echo "The Brevo integration is working correctly.\n\n";
echo "✅ Custom attributes are correctly placed in 'attributes' object\n";
echo "✅ Standard fields remain at root level\n";
echo "✅ Backwards compatibility maintained\n";
echo "✅ JSON structure matches Brevo API documentation\n\n";
echo "The bug described in the issue is FIXED. ✓\n";
} else {
echo "\033[31m❌ SOME TESTS FAILED\033[0m\n";
echo "Please review the failed tests above.\n";
}
echo "\n";
exit( $tests_failed > 0 ? 1 : 0 );