275 lines
9.7 KiB
PHP
275 lines
9.7 KiB
PHP
<?php
|
|
/**
|
|
* Comprehensive Plugin Test
|
|
* Tests all plugin functionality: database, snippets, saving, deleting, etc.
|
|
*/
|
|
|
|
// Include WordPress
|
|
require_once dirname(__FILE__) . '/../../../../wp-load.php';
|
|
|
|
echo '<h1>Headers & Footers Plugin - Comprehensive Test</h1>';
|
|
echo '<p>Test Date: ' . date('Y-m-d H:i:s') . '</p>';
|
|
|
|
$all_passed = true;
|
|
|
|
// Test 1: Include Classes
|
|
echo '<h2>Test 1: Include Plugin Classes</h2>';
|
|
try {
|
|
require_once dirname(__FILE__) . '/includes/class-database.php';
|
|
require_once dirname(__FILE__) . '/includes/class-snippet.php';
|
|
echo '<p style="color: green;">✓ Classes loaded successfully</p>';
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Failed to load classes: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
|
|
// Test 2: Database Table Creation
|
|
echo '<h2>Test 2: Database Table Creation</h2>';
|
|
try {
|
|
PC_HFAP_Database::create_tables();
|
|
global $wpdb;
|
|
$table_name = PC_HFAP_Database::get_table_name();
|
|
$table_exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table_name));
|
|
|
|
if ($table_name === $table_exists) {
|
|
echo '<p style="color: green;">✓ Table exists: ' . $table_name . '</p>';
|
|
} else {
|
|
echo '<p style="color: red;">✗ Table does not exist!</p>';
|
|
$all_passed = false;
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Database error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
|
|
// Test 3: Insert Snippet
|
|
echo '<h2>Test 3: Insert New Snippet</h2>';
|
|
$test_snippet_id = null;
|
|
try {
|
|
$data = array(
|
|
'title' => 'Test Snippet ' . date('Y-m-d H:i:s'),
|
|
'location' => 'header',
|
|
'code' => '<!-- Test header code -->'
|
|
);
|
|
|
|
$snippet = new PC_HFAP_Snippet($data);
|
|
$result = $snippet->save();
|
|
|
|
if ($result) {
|
|
$test_snippet_id = $result;
|
|
echo '<p style="color: green;">✓ Snippet created with ID: ' . $result . '</p>';
|
|
} else {
|
|
echo '<p style="color: red;">✗ Failed to create snippet</p>';
|
|
global $wpdb;
|
|
echo '<p>Error: ' . $wpdb->last_error . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Insert error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
|
|
// Test 4: Retrieve Snippet
|
|
echo '<h2>Test 4: Retrieve Snippet by ID</h2>';
|
|
if ($test_snippet_id) {
|
|
try {
|
|
$snippet = PC_HFAP_Snippet::get_by_id($test_snippet_id);
|
|
|
|
if ($snippet && $snippet->get_id()) {
|
|
echo '<p style="color: green;">✓ Snippet retrieved successfully</p>';
|
|
echo '<ul>';
|
|
echo '<li>ID: ' . $snippet->get_id() . '</li>';
|
|
echo '<li>Title: ' . esc_html($snippet->get_title()) . '</li>';
|
|
echo '<li>Location: ' . $snippet->get_location() . '</li>';
|
|
echo '<li>Code: ' . esc_html($snippet->get_code()) . '</li>';
|
|
echo '</ul>';
|
|
} else {
|
|
echo '<p style="color: red;">✗ Failed to retrieve snippet</p>';
|
|
$all_passed = false;
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Retrieve error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: orange;">⚠ Skipped (no test snippet ID)</p>';
|
|
}
|
|
|
|
// Test 5: Update Snippet
|
|
echo '<h2>Test 5: Update Snippet</h2>';
|
|
if ($test_snippet_id) {
|
|
try {
|
|
$snippet = PC_HFAP_Snippet::get_by_id($test_snippet_id);
|
|
|
|
if ($snippet) {
|
|
$snippet->set_title('Updated Test Snippet');
|
|
$snippet->set_code('<!-- Updated test code -->');
|
|
$result = $snippet->save();
|
|
|
|
if ($result !== false) {
|
|
// Verify update
|
|
$updated_snippet = PC_HFAP_Snippet::get_by_id($test_snippet_id);
|
|
if ($updated_snippet->get_title() === 'Updated Test Snippet') {
|
|
echo '<p style="color: green;">✓ Snippet updated successfully</p>';
|
|
} else {
|
|
echo '<p style="color: red;">✗ Update did not persist</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: red;">✗ Update failed</p>';
|
|
global $wpdb;
|
|
echo '<p>Error: ' . $wpdb->last_error . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: red;">✗ Snippet not found for update</p>';
|
|
$all_passed = false;
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Update error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: orange;">⚠ Skipped (no test snippet ID)</p>';
|
|
}
|
|
|
|
// Test 6: Get All Snippets
|
|
echo '<h2>Test 6: Get All Snippets</h2>';
|
|
try {
|
|
$snippets = PC_HFAP_Snippet::get_all();
|
|
echo '<p style="color: green;">✓ Retrieved ' . count($snippets) . ' snippet(s)</p>';
|
|
|
|
if (!empty($snippets)) {
|
|
echo '<table border="1" cellpadding="5">';
|
|
echo '<tr><th>ID</th><th>Title</th><th>Location</th></tr>';
|
|
foreach ($snippets as $s) {
|
|
echo '<tr>';
|
|
echo '<td>' . $s->get_id() . '</td>';
|
|
echo '<td>' . esc_html($s->get_title()) . '</td>';
|
|
echo '<td>' . $s->get_location() . '</td>';
|
|
echo '</tr>';
|
|
}
|
|
echo '</table>';
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Get all error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
|
|
// Test 7: Delete Snippet
|
|
echo '<h2>Test 7: Delete Snippet</h2>';
|
|
if ($test_snippet_id) {
|
|
try {
|
|
$snippet = PC_HFAP_Snippet::get_by_id($test_snippet_id);
|
|
|
|
if ($snippet) {
|
|
$result = $snippet->delete();
|
|
|
|
if ($result) {
|
|
// Verify deletion
|
|
$deleted_snippet = PC_HFAP_Snippet::get_by_id($test_snippet_id);
|
|
if (!$deleted_snippet) {
|
|
echo '<p style="color: green;">✓ Snippet deleted successfully</p>';
|
|
} else {
|
|
echo '<p style="color: red;">✗ Snippet still exists after delete</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: red;">✗ Delete failed</p>';
|
|
global $wpdb;
|
|
echo '<p>Error: ' . $wpdb->last_error . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: red;">✗ Snippet not found for delete</p>';
|
|
$all_passed = false;
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Delete error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
} else {
|
|
echo '<p style="color: orange;">⚠ Skipped (no test snippet ID)</p>';
|
|
}
|
|
|
|
// Test 8: Test with Different Locations
|
|
echo '<h2>Test 8: Test Different Locations</h2>';
|
|
$location_test_ids = array();
|
|
try {
|
|
foreach (array('header', 'body', 'footer') as $location) {
|
|
$data = array(
|
|
'title' => 'Location Test - ' . ucfirst($location),
|
|
'location' => $location,
|
|
'code' => '<!-- Test ' . $location . ' code -->'
|
|
);
|
|
|
|
$snippet = new PC_HFAP_Snippet($data);
|
|
$result = $snippet->save();
|
|
|
|
if ($result) {
|
|
$location_test_ids[$location] = $result;
|
|
echo '<p style="color: green;">✓ Created ' . $location . ' snippet (ID: ' . $result . ')</p>';
|
|
} else {
|
|
echo '<p style="color: red;">✗ Failed to create ' . $location . ' snippet</p>';
|
|
$all_passed = false;
|
|
}
|
|
}
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Location test error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
|
|
// Test 9: Get Snippets by Location
|
|
echo '<h2>Test 9: Get Snippets by Location</h2>';
|
|
try {
|
|
$headers = PC_HFAP_Snippet::get_headers();
|
|
$bodies = PC_HFAP_Snippet::get_bodies();
|
|
$footers = PC_HFAP_Snippet::get_footers();
|
|
|
|
echo '<p>Header snippets: ' . count($headers) . '</p>';
|
|
echo '<p>Body snippets: ' . count($bodies) . '</p>';
|
|
echo '<p>Footer snippets: ' . count($footers) . '</p>';
|
|
|
|
echo '<p style="color: green;">✓ Location queries work correctly</p>';
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Location query error: ' . $e->getMessage() . '</p>';
|
|
$all_passed = false;
|
|
}
|
|
|
|
// Clean up test snippets
|
|
echo '<h2>Cleanup</h2>';
|
|
try {
|
|
global $wpdb;
|
|
$table_name = PC_HFAP_Database::get_table_name();
|
|
|
|
// Delete test snippets
|
|
$deleted = $wpdb->query("DELETE FROM $table_name WHERE title LIKE 'Test Snippet%' OR title LIKE 'Updated Test%' OR title LIKE 'Location Test%' OR title LIKE 'Database Test%'");
|
|
|
|
if ($deleted !== false) {
|
|
echo '<p style="color: green;">✓ Cleaned up ' . $deleted . ' test snippet(s)</p>';
|
|
}
|
|
|
|
// Delete location test snippets
|
|
foreach ($location_test_ids as $location => $id) {
|
|
$wpdb->delete($table_name, array('id' => $id), array('%d'));
|
|
}
|
|
echo '<p style="color: green;">✓ Cleaned up location test snippets</p>';
|
|
} catch (Exception $e) {
|
|
echo '<p style="color: red;">✗ Cleanup error: ' . $e->getMessage() . '</p>';
|
|
}
|
|
|
|
// Final Result
|
|
echo '<h2>Test Summary</h2>';
|
|
if ($all_passed) {
|
|
echo '<p style="color: green; font-size: 20px;">✓ ALL TESTS PASSED!</p>';
|
|
} else {
|
|
echo '<p style="color: red; font-size: 20px;">✗ SOME TESTS FAILED</p>';
|
|
}
|
|
|
|
echo '<h3>Next Steps</h3>';
|
|
echo '<ul>';
|
|
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-snippets') . '">View Snippets Admin Page</a></li>';
|
|
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-add-snippet') . '">Add New Snippet</a></li>';
|
|
echo '<li><a href="' . admin_url('admin.php?page=pc-hfap-settings') . '">Plugin Settings</a></li>';
|
|
echo '</ul>';
|