| 1 |
<?php |
| 2 |
/** |
| 3 |
* Test endpoints for Playwright testing |
| 4 |
* This file provides WordPress endpoints to test AIContentHelper functionality |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace Templately\Core\Developer; |
| 8 |
|
| 9 |
use Templately\Core\Importer\Utils\AIContentHelper; |
| 10 |
use Exception; |
| 11 |
use ArgumentCountError; |
| 12 |
|
| 13 |
class TestEndpoints { |
| 14 |
|
| 15 |
public function __construct() { |
| 16 |
add_action('wp_ajax_templately_test_ai_content', [$this, 'test_ai_content_validation']); |
| 17 |
add_action('wp_ajax_nopriv_templately_test_ai_content', [$this, 'test_ai_content_validation']); |
| 18 |
add_action('admin_init', [$this, 'handle_test_requests']); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Handle test requests via query parameters |
| 23 |
*/ |
| 24 |
public function handle_test_requests() { |
| 25 |
if (!isset($_GET['test']) || !current_user_can('manage_options')) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$test_type = sanitize_text_field($_GET['test']); |
| 30 |
|
| 31 |
switch ($test_type) { |
| 32 |
case 'ai-content-validation': |
| 33 |
$this->run_ai_content_validation_tests(); |
| 34 |
break; |
| 35 |
case 'wordpress-core-errors': |
| 36 |
$this->test_wordpress_core_error_scenarios(); |
| 37 |
break; |
| 38 |
case 'optional-parameters': |
| 39 |
$this->test_optional_parameters(); |
| 40 |
break; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Run AI content validation tests |
| 46 |
*/ |
| 47 |
public function run_ai_content_validation_tests() { |
| 48 |
// Set content type to JSON for easier parsing in Playwright |
| 49 |
header('Content-Type: application/json'); |
| 50 |
|
| 51 |
$results = []; |
| 52 |
|
| 53 |
// Test 1: Valid block structure validation |
| 54 |
$valid_blocks = [ |
| 55 |
[ |
| 56 |
'blockName' => 'core/paragraph', |
| 57 |
'attrs' => ['blockId' => 'test'], |
| 58 |
'innerBlocks' => [], |
| 59 |
'innerHTML' => '<p>Test</p>', |
| 60 |
'innerContent' => ['<p>Test</p>'] |
| 61 |
] |
| 62 |
]; |
| 63 |
|
| 64 |
$results['valid_blocks'] = [ |
| 65 |
'test' => 'validateBlockStructure with valid blocks', |
| 66 |
'result' => AIContentHelper::validateBlockStructure($valid_blocks), |
| 67 |
'expected' => true, |
| 68 |
'passed' => AIContentHelper::validateBlockStructure($valid_blocks) === true |
| 69 |
]; |
| 70 |
|
| 71 |
// Test 2: Invalid block structure validation |
| 72 |
$invalid_blocks = [ |
| 73 |
[ |
| 74 |
// Missing blockName |
| 75 |
'attrs' => ['blockId' => 'test'], |
| 76 |
'innerBlocks' => [], |
| 77 |
'innerHTML' => '<p>Test</p>', |
| 78 |
'innerContent' => null // Null innerContent |
| 79 |
] |
| 80 |
]; |
| 81 |
|
| 82 |
$results['invalid_blocks'] = [ |
| 83 |
'test' => 'validateBlockStructure with invalid blocks', |
| 84 |
'result' => AIContentHelper::validateBlockStructure($invalid_blocks), |
| 85 |
'expected' => false, |
| 86 |
'passed' => AIContentHelper::validateBlockStructure($invalid_blocks) === false |
| 87 |
]; |
| 88 |
|
| 89 |
// Test 3: Test mergeAiContentWithOriginalGutenberg with 2 parameters |
| 90 |
$ai_template = [ |
| 91 |
'test-block' => [ |
| 92 |
'blockName' => 'core/paragraph', |
| 93 |
'contents' => [ |
| 94 |
['attribute' => 'content', 'content' => 'AI generated content'] |
| 95 |
] |
| 96 |
] |
| 97 |
]; |
| 98 |
|
| 99 |
$original_template = [ |
| 100 |
'content' => '<!-- wp:paragraph {"blockId":"test-block"} --> |
| 101 |
<p>Original content</p> |
| 102 |
<!-- /wp:paragraph -->' |
| 103 |
]; |
| 104 |
|
| 105 |
try { |
| 106 |
$merge_result = AIContentHelper::mergeAiContentWithOriginalGutenberg($ai_template, $original_template); |
| 107 |
$results['merge_2_params'] = [ |
| 108 |
'test' => 'mergeAiContentWithOriginalGutenberg with 2 parameters', |
| 109 |
'result' => is_array($merge_result) && isset($merge_result['content']), |
| 110 |
'expected' => true, |
| 111 |
'passed' => is_array($merge_result) && isset($merge_result['content']), |
| 112 |
'error' => null |
| 113 |
]; |
| 114 |
} catch (Exception $e) { |
| 115 |
$results['merge_2_params'] = [ |
| 116 |
'test' => 'mergeAiContentWithOriginalGutenberg with 2 parameters', |
| 117 |
'result' => false, |
| 118 |
'expected' => true, |
| 119 |
'passed' => false, |
| 120 |
'error' => $e->getMessage() |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
// Test 4: Test with malformed content that would cause WordPress errors |
| 125 |
$malformed_template = [ |
| 126 |
'content' => '<!-- wp:paragraph --> |
| 127 |
<p>Malformed content without proper closing' |
| 128 |
]; |
| 129 |
|
| 130 |
try { |
| 131 |
$malformed_result = AIContentHelper::mergeAiContentWithOriginalGutenberg([], $malformed_template); |
| 132 |
$results['malformed_content'] = [ |
| 133 |
'test' => 'mergeAiContentWithOriginalGutenberg with malformed content', |
| 134 |
'result' => is_array($malformed_result), |
| 135 |
'expected' => true, |
| 136 |
'passed' => is_array($malformed_result), |
| 137 |
'error' => null, |
| 138 |
'note' => 'Should handle gracefully without WordPress core errors' |
| 139 |
]; |
| 140 |
} catch (Exception $e) { |
| 141 |
$results['malformed_content'] = [ |
| 142 |
'test' => 'mergeAiContentWithOriginalGutenberg with malformed content', |
| 143 |
'result' => false, |
| 144 |
'expected' => true, |
| 145 |
'passed' => false, |
| 146 |
'error' => $e->getMessage() |
| 147 |
]; |
| 148 |
} |
| 149 |
|
| 150 |
// Calculate overall test results |
| 151 |
$total_tests = count($results); |
| 152 |
$passed_tests = array_sum(array_column($results, 'passed')); |
| 153 |
|
| 154 |
$summary = [ |
| 155 |
'total_tests' => $total_tests, |
| 156 |
'passed_tests' => $passed_tests, |
| 157 |
'failed_tests' => $total_tests - $passed_tests, |
| 158 |
'success_rate' => $total_tests > 0 ? round(($passed_tests / $total_tests) * 100, 2) : 0, |
| 159 |
'overall_status' => $passed_tests === $total_tests ? 'PASS' : 'FAIL' |
| 160 |
]; |
| 161 |
|
| 162 |
$response = [ |
| 163 |
'test_type' => 'ai-content-validation', |
| 164 |
'timestamp' => current_time('mysql'), |
| 165 |
'summary' => $summary, |
| 166 |
'results' => $results, |
| 167 |
'wordpress_version' => get_bloginfo('version'), |
| 168 |
'php_version' => PHP_VERSION |
| 169 |
]; |
| 170 |
|
| 171 |
echo json_encode($response, JSON_PRETTY_PRINT); |
| 172 |
exit; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Test WordPress core error scenarios |
| 177 |
*/ |
| 178 |
public function test_wordpress_core_error_scenarios() { |
| 179 |
header('Content-Type: application/json'); |
| 180 |
|
| 181 |
$results = []; |
| 182 |
|
| 183 |
// Test scenarios that previously caused WordPress core errors |
| 184 |
$error_scenarios = [ |
| 185 |
'missing_blockname' => [ |
| 186 |
[ |
| 187 |
'attrs' => ['blockId' => 'test'], |
| 188 |
'innerBlocks' => [], |
| 189 |
'innerHTML' => '<p>Test</p>', |
| 190 |
'innerContent' => ['<p>Test</p>'] |
| 191 |
] |
| 192 |
], |
| 193 |
'null_innercontent' => [ |
| 194 |
[ |
| 195 |
'blockName' => 'core/paragraph', |
| 196 |
'attrs' => ['blockId' => 'test'], |
| 197 |
'innerBlocks' => [], |
| 198 |
'innerHTML' => '<p>Test</p>', |
| 199 |
'innerContent' => null |
| 200 |
] |
| 201 |
], |
| 202 |
'null_innerblocks' => [ |
| 203 |
[ |
| 204 |
'blockName' => 'core/paragraph', |
| 205 |
'attrs' => ['blockId' => 'test'], |
| 206 |
'innerBlocks' => null, |
| 207 |
'innerHTML' => '<p>Test</p>', |
| 208 |
'innerContent' => ['<p>Test</p>'] |
| 209 |
] |
| 210 |
] |
| 211 |
]; |
| 212 |
|
| 213 |
foreach ($error_scenarios as $scenario_name => $blocks) { |
| 214 |
$is_valid = AIContentHelper::validateBlockStructure($blocks); |
| 215 |
$results[$scenario_name] = [ |
| 216 |
'test' => "WordPress core error scenario: $scenario_name", |
| 217 |
'blocks_valid' => $is_valid, |
| 218 |
'expected_valid' => false, |
| 219 |
'passed' => $is_valid === false, |
| 220 |
'note' => 'Should be rejected to prevent WordPress core errors' |
| 221 |
]; |
| 222 |
} |
| 223 |
|
| 224 |
$total_tests = count($results); |
| 225 |
$passed_tests = array_sum(array_column($results, 'passed')); |
| 226 |
|
| 227 |
$response = [ |
| 228 |
'test_type' => 'wordpress-core-errors', |
| 229 |
'timestamp' => current_time('mysql'), |
| 230 |
'summary' => [ |
| 231 |
'total_tests' => $total_tests, |
| 232 |
'passed_tests' => $passed_tests, |
| 233 |
'overall_status' => $passed_tests === $total_tests ? 'PASS' : 'FAIL' |
| 234 |
], |
| 235 |
'results' => $results |
| 236 |
]; |
| 237 |
|
| 238 |
echo json_encode($response, JSON_PRETTY_PRINT); |
| 239 |
exit; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Test optional parameters functionality |
| 244 |
*/ |
| 245 |
public function test_optional_parameters() { |
| 246 |
header('Content-Type: application/json'); |
| 247 |
|
| 248 |
$results = []; |
| 249 |
|
| 250 |
$ai_template = [ |
| 251 |
'test-block' => [ |
| 252 |
'blockName' => 'core/paragraph', |
| 253 |
'contents' => [ |
| 254 |
['attribute' => 'content', 'content' => 'AI content'] |
| 255 |
] |
| 256 |
] |
| 257 |
]; |
| 258 |
|
| 259 |
$original_template = [ |
| 260 |
'content' => '<!-- wp:paragraph {"blockId":"test-block"} --><p>Original</p><!-- /wp:paragraph -->' |
| 261 |
]; |
| 262 |
|
| 263 |
// Test 2 parameters |
| 264 |
try { |
| 265 |
$result_2_params = AIContentHelper::mergeAiContentWithOriginalGutenberg($ai_template, $original_template); |
| 266 |
$results['two_parameters'] = [ |
| 267 |
'test' => 'Call with 2 parameters', |
| 268 |
'success' => true, |
| 269 |
'passed' => is_array($result_2_params), |
| 270 |
'error' => null |
| 271 |
]; |
| 272 |
} catch (ArgumentCountError $e) { |
| 273 |
$results['two_parameters'] = [ |
| 274 |
'test' => 'Call with 2 parameters', |
| 275 |
'success' => false, |
| 276 |
'passed' => false, |
| 277 |
'error' => 'ArgumentCountError: ' . $e->getMessage() |
| 278 |
]; |
| 279 |
} catch (Exception $e) { |
| 280 |
$results['two_parameters'] = [ |
| 281 |
'test' => 'Call with 2 parameters', |
| 282 |
'success' => false, |
| 283 |
'passed' => false, |
| 284 |
'error' => 'Exception: ' . $e->getMessage() |
| 285 |
]; |
| 286 |
} |
| 287 |
|
| 288 |
// Test 3 parameters |
| 289 |
try { |
| 290 |
$result_3_params = AIContentHelper::mergeAiContentWithOriginalGutenberg($ai_template, $original_template, 'test.json'); |
| 291 |
$results['three_parameters'] = [ |
| 292 |
'test' => 'Call with 3 parameters', |
| 293 |
'success' => true, |
| 294 |
'passed' => is_array($result_3_params), |
| 295 |
'error' => null |
| 296 |
]; |
| 297 |
} catch (Exception $e) { |
| 298 |
$results['three_parameters'] = [ |
| 299 |
'test' => 'Call with 3 parameters', |
| 300 |
'success' => false, |
| 301 |
'passed' => false, |
| 302 |
'error' => $e->getMessage() |
| 303 |
]; |
| 304 |
} |
| 305 |
|
| 306 |
$total_tests = count($results); |
| 307 |
$passed_tests = array_sum(array_column($results, 'passed')); |
| 308 |
|
| 309 |
$response = [ |
| 310 |
'test_type' => 'optional-parameters', |
| 311 |
'timestamp' => current_time('mysql'), |
| 312 |
'summary' => [ |
| 313 |
'total_tests' => $total_tests, |
| 314 |
'passed_tests' => $passed_tests, |
| 315 |
'overall_status' => $passed_tests === $total_tests ? 'PASS' : 'FAIL' |
| 316 |
], |
| 317 |
'results' => $results |
| 318 |
]; |
| 319 |
|
| 320 |
echo json_encode($response, JSON_PRETTY_PRINT); |
| 321 |
exit; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* AJAX endpoint for testing |
| 326 |
*/ |
| 327 |
public function test_ai_content_validation() { |
| 328 |
if (!current_user_can('manage_options')) { |
| 329 |
wp_die('Unauthorized'); |
| 330 |
} |
| 331 |
|
| 332 |
$test_type = sanitize_text_field($_POST['test_type'] ?? 'ai-content-validation'); |
| 333 |
|
| 334 |
switch ($test_type) { |
| 335 |
case 'ai-content-validation': |
| 336 |
$this->run_ai_content_validation_tests(); |
| 337 |
break; |
| 338 |
case 'wordpress-core-errors': |
| 339 |
$this->test_wordpress_core_error_scenarios(); |
| 340 |
break; |
| 341 |
case 'optional-parameters': |
| 342 |
$this->test_optional_parameters(); |
| 343 |
break; |
| 344 |
default: |
| 345 |
wp_send_json_error('Invalid test type'); |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|