wp-md-rest-import/test-standalone.php

183 lines
7.2 KiB
PHP

<?php
/**
* Test script for enhanced functionality (without WordPress dependencies)
*
* This script tests the enhanced functionality of the Markdown Parser WP plugin:
* 1. Relative image paths in content
* 2. Field mapping configuration
*/
// Mock WordPress functions
function get_headers($url) {
$headers = @get_headers($url);
return $headers ?: ['HTTP/1.1 404 Not Found'];
}
// Test URL with relative image paths
$test_url = 'https://git.rpi-virtuell.de/Comenius-Institut/FOERBICO/raw/branch/main/Website/content/posts/2024-12-03-Konzeptionstag/index.md';
echo "Testing enhanced functionality with URL: $test_url\n\n";
// Test 1: Check for relative image paths in markdown content
echo "Test 1: Checking for relative image paths in markdown content\n";
try {
// Get markdown content
$content = file_get_contents($test_url);
if ($content === false) {
echo "✗ Failed to fetch content from URL\n";
} else {
echo "✓ Successfully fetched content from URL\n";
// Extract YAML frontmatter and markdown content
$parts = explode('---', $content);
if (count($parts) < 3) {
echo "✗ Invalid markdown format - YAML frontmatter not found\n";
} else {
$markdown = trim($parts[2]);
echo "✓ Successfully extracted markdown content\n";
// Check if markdown contains image references
if (strpos($markdown, '![') !== false) {
echo "✓ Found image references in markdown content\n";
// Extract image references
preg_match_all('/!\[(.*?)\]\((.*?)(?:\s+"(.*?)")?\)/', $markdown, $matches);
$image_refs = $matches[2];
echo "Found " . count($image_refs) . " image references:\n";
foreach ($image_refs as $index => $ref) {
echo " - Image " . ($index + 1) . ": $ref\n";
// Check if it's a relative path
if (strpos($ref, 'http') !== 0) {
echo " ✓ This is a relative path\n";
// Resolve relative path
$base_url = dirname($test_url) . '/';
$full_url = $base_url . ltrim($ref, '/');
echo " ✓ Resolved to: $full_url\n";
// Check if URL is accessible
$headers = get_headers($full_url);
if (strpos($headers[0], '200') !== false) {
echo " ✓ Image is accessible\n";
} else {
echo " ✗ Image is not accessible\n";
}
} else {
echo " ✓ This is an absolute path\n";
}
}
} else {
echo "✗ No image references found in markdown content\n";
}
}
}
} catch (Exception $e) {
echo "✗ Error: " . $e->getMessage() . "\n";
}
echo "\n";
// Test 2: Verify BlocksConverter code for handling relative image paths
echo "Test 2: Verifying BlocksConverter code for handling relative image paths\n";
// Check if the BlocksConverter class has the necessary code
$blocks_converter_file = file_get_contents(__DIR__ . '/src/BlocksConverter.php');
if ($blocks_converter_file === false) {
echo "✗ Failed to read BlocksConverter.php\n";
} else {
echo "✓ Successfully read BlocksConverter.php\n";
// Check if convert_markdown_to_blocks accepts original_url parameter
if (strpos($blocks_converter_file, 'convert_markdown_to_blocks($markdown, $import_images = true, $original_url = \'\')') !== false) {
echo "✓ convert_markdown_to_blocks method accepts original_url parameter\n";
} else {
echo "✗ convert_markdown_to_blocks method does not accept original_url parameter\n";
}
// Check if process_images_in_html handles relative paths
if (strpos($blocks_converter_file, 'process_images_in_html($html, $original_url = \'\')') !== false) {
echo "✓ process_images_in_html method accepts original_url parameter\n";
} else {
echo "✗ process_images_in_html method does not accept original_url parameter\n";
}
// Check for relative path handling code
if (strpos($blocks_converter_file, 'if (!filter_var($src, FILTER_VALIDATE_URL) && !empty($original_url))') !== false) {
echo "✓ Code for handling relative paths is present\n";
} else {
echo "✗ Code for handling relative paths is not present\n";
}
}
echo "\n";
// Test 3: Verify PostCreator code for passing original_url to BlocksConverter
echo "Test 3: Verifying PostCreator code for passing original_url to BlocksConverter\n";
// Check if the PostCreator class has the necessary code
$post_creator_file = file_get_contents(__DIR__ . '/src/PostCreator.php');
if ($post_creator_file === false) {
echo "✗ Failed to read PostCreator.php\n";
} else {
echo "✓ Successfully read PostCreator.php\n";
// Check if create_post_from_data passes original_url to BlocksConverter
if (strpos($post_creator_file, '$options[\'original_url\']') !== false) {
echo "✓ create_post_from_data method stores original_url in options\n";
} else {
echo "✗ create_post_from_data method does not store original_url in options\n";
}
// Check if BlocksConverter is called with original_url
if (strpos($post_creator_file, 'BlocksConverter::convert_markdown_to_blocks(
$markdown_content,
$options[\'import_images\'],
$options[\'original_url\']') !== false) {
echo "✓ BlocksConverter is called with original_url parameter\n";
} else {
echo "✗ BlocksConverter is not called with original_url parameter\n";
}
}
echo "\n";
// Test 4: Verify Admin code for field mapping configuration UI
echo "Test 4: Verifying Admin code for field mapping configuration UI\n";
// Check if the Admin class has the necessary code
$admin_file = file_get_contents(__DIR__ . '/src/Admin.php');
if ($admin_file === false) {
echo "✗ Failed to read Admin.php\n";
} else {
echo "✓ Successfully read Admin.php\n";
// Check for field mapping UI
if (strpos($admin_file, '<div id="field-mapping-container">') !== false) {
echo "✓ Field mapping UI is present in Admin.php\n";
} else {
echo "✗ Field mapping UI is not present in Admin.php\n";
}
// Check for AJAX handler for field mapping preview
if (strpos($admin_file, 'ajax_get_field_mapping_preview') !== false) {
echo "✓ AJAX handler for field mapping preview is present\n";
} else {
echo "✗ AJAX handler for field mapping preview is not present\n";
}
// Check if field mapping is passed to PostCreator
if (strpos($admin_file, '$options[\'field_mapping\'] = $field_mapping;') !== false) {
echo "✓ Field mapping is passed to PostCreator\n";
} else {
echo "✗ Field mapping is not passed to PostCreator\n";
}
}
echo "\n";
echo "All tests completed.\n";