wp-md-rest-import/test-enhanced-functionality.php

147 lines
5.1 KiB
PHP

<?php
/**
* Test script for enhanced functionality
*
* This script tests the enhanced functionality of the Markdown Parser WP plugin:
* 1. Relative image paths in content
* 2. Image import for content images
* 3. Field mapping configuration
*/
// Load WordPress functions
require_once __DIR__ . '/vendor/autoload.php';
use MarkdownParserWP\MarkdownParser;
use MarkdownParserWP\BlocksConverter;
use MarkdownParserWP\PostCreator;
// 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: Parse URL and check for relative image paths
echo "Test 1: Parsing URL and checking for relative image paths\n";
try {
$result = MarkdownParser::parse_url($test_url);
echo "✓ Successfully parsed URL\n";
// Check if markdown contains image references
if (strpos($result['markdown'], '![') !== false) {
echo "✓ Found image references in markdown content\n";
// Extract image references
preg_match_all('/!\[(.*?)\]\((.*?)(?:\s+"(.*?)")?\)/', $result['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 "✗ Failed to parse URL: " . $e->getMessage() . "\n";
}
echo "\n";
// Test 2: Convert markdown to blocks with image import
echo "Test 2: Converting markdown to blocks with image import\n";
try {
// Get markdown content
$markdown = $result['markdown'];
// Convert markdown to blocks with image import
$blocks = BlocksConverter::convert_markdown_to_blocks($markdown, true, $test_url);
echo "✓ Successfully converted markdown to blocks\n";
// Check if blocks contain image blocks
if (strpos($blocks, '<!-- wp:image') !== false) {
echo "✓ Found image blocks in converted content\n";
// Count image blocks
preg_match_all('/<!-- wp:image/', $blocks, $matches);
$image_blocks_count = count($matches[0]);
echo " Found $image_blocks_count image blocks\n";
// Check if image blocks have attachment IDs
preg_match_all('/class="wp-image-(\d+)"/', $blocks, $matches);
$attachment_ids = $matches[1];
if (count($attachment_ids) > 0) {
echo "✓ Found " . count($attachment_ids) . " image blocks with attachment IDs\n";
foreach ($attachment_ids as $id) {
echo " - Attachment ID: $id\n";
}
} else {
echo "✗ No image blocks with attachment IDs found\n";
}
} else {
echo "✗ No image blocks found in converted content\n";
}
} catch (Exception $e) {
echo "✗ Failed to convert markdown to blocks: " . $e->getMessage() . "\n";
}
echo "\n";
// Test 3: Field mapping configuration
echo "Test 3: Field mapping configuration\n";
try {
// Define custom field mapping
$field_mapping = [
'post_title' => 'title',
'post_excerpt' => 'summary',
'post_date' => 'datePublished',
'post_name' => 'url',
'post_tag' => 'keywords'
];
// Map metadata to post fields with custom mapping
$post_data = MarkdownParser::map_metadata_to_post_fields($result['metadata'], $field_mapping);
echo "✓ Successfully mapped metadata to post fields with custom mapping\n";
echo "Mapped fields:\n";
foreach ($post_data as $field => $value) {
if (is_array($value)) {
$value = json_encode($value);
}
echo " - $field: $value\n";
}
// Test taxonomies mapping
$taxonomies = MarkdownParser::map_metadata_to_taxonomies($result['metadata']);
echo "✓ Successfully mapped metadata to taxonomies\n";
echo "Mapped taxonomies:\n";
foreach ($taxonomies as $taxonomy => $terms) {
echo " - $taxonomy: " . json_encode($terms) . "\n";
}
} catch (Exception $e) {
echo "✗ Failed to test field mapping: " . $e->getMessage() . "\n";
}
echo "\n";
echo "All tests completed.\n";