This commit is contained in:
alorig
2025-12-01 09:32:06 +05:00
parent aeaac01990
commit 71a38435b1
46 changed files with 75 additions and 17130 deletions

View File

@@ -1,116 +0,0 @@
<?php
/**
* Test API Authentication
*
* Tests the fixed API authentication flow.
* Run via: php tests/test-api-authentication.php
*
* @package Igny8Bridge
*/
// Mock WordPress constants and functions if running standalone
if (!defined('ABSPATH')) {
define('ABSPATH', dirname(dirname(__FILE__)) . '/');
}
// Include plugin files
require_once ABSPATH . 'igny8-bridge.php';
require_once ABSPATH . 'includes/functions.php';
require_once ABSPATH . 'includes/class-igny8-api.php';
echo "=== IGNY8 API Authentication Test ===\n\n";
// Test 1: Check if Igny8API class exists
echo "Test 1: Check Igny8API class exists\n";
if (class_exists('Igny8API')) {
echo "✓ PASS: Igny8API class loaded\n\n";
} else {
echo "✗ FAIL: Igny8API class not found\n\n";
exit(1);
}
// Test 2: Check Igny8API instantiation
echo "Test 2: Instantiate Igny8API\n";
try {
$api = new Igny8API();
echo "✓ PASS: Igny8API instantiated successfully\n\n";
} catch (Exception $e) {
echo "✗ FAIL: " . $e->getMessage() . "\n\n";
exit(1);
}
// Test 3: Check is_authenticated method exists
echo "Test 3: Check is_authenticated() method\n";
if (method_exists($api, 'is_authenticated')) {
echo "✓ PASS: is_authenticated() method exists\n";
$is_auth = $api->is_authenticated();
echo " Status: " . ($is_auth ? "Authenticated" : "Not authenticated") . "\n\n";
} else {
echo "✗ FAIL: is_authenticated() method not found\n\n";
exit(1);
}
// Test 4: Check connect method exists
echo "Test 4: Check connect() method\n";
if (method_exists($api, 'connect')) {
echo "✓ PASS: connect() method exists\n\n";
} else {
echo "✗ FAIL: connect() method not found\n\n";
exit(1);
}
// Test 5: Check get method exists
echo "Test 5: Check get() method\n";
if (method_exists($api, 'get')) {
echo "✓ PASS: get() method exists\n\n";
} else {
echo "✗ FAIL: get() method not found\n\n";
exit(1);
}
// Test 6: Test endpoint path normalization (mock test)
echo "Test 6: Endpoint path normalization logic\n";
// This is a conceptual test - in reality we'd need to mock HTTP calls
echo "✓ PASS: Endpoint normalization should convert:\n";
echo " /auth/sites/ → https://api.igny8.com/api/v1/auth/sites/\n";
echo " /v1/auth/sites/ → https://api.igny8.com/api/v1/auth/sites/\n";
echo " auth/sites → https://api.igny8.com/api/v1/auth/sites/\n\n";
// Test 7: Check if functions exist
echo "Test 7: Check helper functions\n";
$functions = array(
'igny8_store_secure_option',
'igny8_get_secure_option',
'igny8_is_connection_enabled'
);
$missing = array();
foreach ($functions as $func) {
if (!function_exists($func)) {
$missing[] = $func;
}
}
if (empty($missing)) {
echo "✓ PASS: All helper functions available\n\n";
} else {
echo "⚠ WARNING: Missing functions: " . implode(', ', $missing) . "\n";
echo " These may be optional depending on WordPress configuration\n\n";
}
// Test 8: Summary
echo "=== Test Summary ===\n";
echo "✓ API authentication module is properly structured\n";
echo "✓ All required methods exist\n";
echo "✓ Ready for integration testing\n\n";
echo "Next Steps:\n";
echo "1. Go to WordPress Admin → IGNY8 API Settings\n";
echo "2. Get your API key from: https://app.igny8.com/sites/{id}/settings?tab=integrations\n";
echo "3. Paste the API key and click 'Connect to IGNY8'\n";
echo "4. Check the debug.log for connection details if it fails\n";
echo "\nExpected successful response:\n";
echo " Authorization: Bearer sk_live_xxxxxxxxxxxxx\n";
echo " GET https://api.igny8.com/api/v1/auth/sites/\n";
echo " Response: {\"success\": true, \"data\": [{\"id\": 1, ...}]}\n";
?>

View File

@@ -1,28 +0,0 @@
<?php
/**
* Unit test for API key revoke handler
*
* @package Igny8Bridge
*/
class Test_Revoke_Api_Key extends WP_UnitTestCase {
public function test_revoke_api_key_clears_options() {
// Simulate stored API key and tokens
update_option('igny8_api_key', 'test-key-123');
update_option('igny8_access_token', 'test-key-123');
update_option('igny8_refresh_token', 'refresh-123');
update_option('igny8_token_refreshed_at', time());
// Call revoke
Igny8Admin::revoke_api_key();
// Assert removed
$this->assertFalse(get_option('igny8_api_key'));
$this->assertFalse(get_option('igny8_access_token'));
$this->assertFalse(get_option('igny8_refresh_token'));
$this->assertFalse(get_option('igny8_token_refreshed_at'));
}
}

View File

@@ -1,36 +0,0 @@
<?php
/**
* Basic unit test for site-metadata endpoint
*
* @package Igny8Bridge
*/
class Test_Site_Metadata_Endpoint extends WP_UnitTestCase {
public function test_site_metadata_endpoint_returns_success() {
// Ensure connection enabled
update_option('igny8_connection_enabled', 1);
// Create a fake API key so permission checks pass via Igny8API
update_option('igny8_api_key', 'test-api-key-123');
update_option('igny8_access_token', 'test-api-key-123');
// Build request
$request = new WP_REST_Request('GET', '/igny8/v1/site-metadata/');
$request->set_header('Authorization', 'Bearer test-api-key-123');
$server = rest_get_server();
$response = $server->dispatch($request);
$this->assertEquals(200, $response->get_status());
$data = $response->get_data();
$this->assertNotEmpty($data);
$this->assertArrayHasKey('success', $data);
$this->assertTrue($data['success']);
$this->assertArrayHasKey('data', $data);
$this->assertArrayHasKey('post_types', $data['data']);
$this->assertArrayHasKey('taxonomies', $data['data']);
}
}

View File

@@ -1,163 +0,0 @@
<?php
/**
* Test Site Structure Sync
*
* Run this test to verify site structure sync is working correctly
* Usage: wp eval-file tests/test-sync-structure.php
* Or: http://your-site.com/wp-admin/admin-ajax.php?action=igny8_test_structure_sync
*
* @package Igny8Bridge
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
echo "=== IGNY8 Site Structure Sync Test ===\n\n";
// Test 1: Check site ID
echo "Test 1: Checking Site ID...\n";
$site_id = get_option('igny8_site_id');
if ($site_id) {
echo "✅ Site ID found: $site_id\n";
} else {
echo "❌ Site ID not found. Run connection setup first.\n";
exit;
}
echo "\n";
// Test 2: Check authentication
echo "Test 2: Checking Authentication...\n";
$api = new Igny8API();
if ($api->is_authenticated()) {
echo "✅ API is authenticated\n";
} else {
echo "❌ API is not authenticated. Credentials missing.\n";
exit;
}
echo "\n";
// Test 3: Get site structure
echo "Test 3: Gathering Site Structure...\n";
$structure = igny8_get_site_structure();
echo " Post Types Found: " . count($structure['post_types']) . "\n";
foreach ($structure['post_types'] as $type => $data) {
echo " - $type: {$data['label']} ({$data['count']} items)\n";
}
echo "\n Taxonomies Found: " . count($structure['taxonomies']) . "\n";
foreach ($structure['taxonomies'] as $tax => $data) {
echo " - $tax: {$data['label']} ({$data['count']} items)\n";
}
if (empty($structure['post_types']) && empty($structure['taxonomies'])) {
echo "❌ No content found to sync\n";
exit;
}
echo "✅ Site structure gathered successfully\n";
echo "\n";
// Test 4: Query for integration
echo "Test 4: Querying for Integration...\n";
$query_response = $api->get('/v1/integration/integrations/?site=' . $site_id . '&platform=wordpress');
echo " API Response Status: " . ($query_response['success'] ? 'Success' : 'Failed') . "\n";
echo " HTTP Status: " . (isset($query_response['http_status']) ? $query_response['http_status'] : 'N/A') . "\n";
// Extract integration
$integration = null;
if (isset($query_response['data'])) {
$data = $query_response['data'];
if (isset($data['results']) && !empty($data['results'])) {
$integration = $data['results'][0];
echo " Response Format: Paginated (DRF)\n";
} elseif (is_array($data) && isset($data[0])) {
$integration = $data[0];
echo " Response Format: Direct Array\n";
} elseif (is_array($data) && isset($data['id'])) {
$integration = $data;
echo " Response Format: Single Object\n";
}
}
if (!$integration || empty($integration['id'])) {
echo "❌ No integration found\n";
if (isset($query_response['error'])) {
echo " Error: " . $query_response['error'] . "\n";
}
exit;
}
echo "✅ Integration found: ID {$integration['id']}\n";
echo "\n";
// Test 5: Sync structure to backend
echo "Test 5: Syncing Structure to Backend...\n";
$payload = array(
'post_types' => $structure['post_types'],
'taxonomies' => $structure['taxonomies'],
'timestamp' => $structure['timestamp'],
'plugin_connection_enabled' => (bool) igny8_is_connection_enabled(),
'two_way_sync_enabled' => (bool) get_option('igny8_enable_two_way_sync', 1),
);
$endpoint = '/v1/integration/integrations/' . $integration['id'] . '/update-structure/';
echo " Endpoint: $endpoint\n";
echo " Payload Size: " . strlen(json_encode($payload)) . " bytes\n";
$sync_response = $api->post($endpoint, $payload);
echo " API Response Status: " . ($sync_response['success'] ? 'Success' : 'Failed') . "\n";
echo " HTTP Status: " . (isset($sync_response['http_status']) ? $sync_response['http_status'] : 'N/A') . "\n";
if ($sync_response['success']) {
echo "✅ Structure synced successfully\n";
if (isset($sync_response['data']['message'])) {
echo " Message: " . $sync_response['data']['message'] . "\n";
}
if (isset($sync_response['data']['post_types_count'])) {
echo " Post Types Synced: " . $sync_response['data']['post_types_count'] . "\n";
}
if (isset($sync_response['data']['taxonomies_count'])) {
echo " Taxonomies Synced: " . $sync_response['data']['taxonomies_count'] . "\n";
}
} else {
echo "❌ Structure sync failed\n";
if (isset($sync_response['error'])) {
echo " Error: " . $sync_response['error'] . "\n";
}
if (isset($sync_response['raw_error'])) {
echo " Details: " . json_encode($sync_response['raw_error']) . "\n";
}
exit;
}
echo "\n";
// Test 6: Verify backend stored the data
echo "Test 6: Verifying Backend Stored Data...\n";
$verify_response = $api->get('/v1/integration/integrations/' . $integration['id'] . '/content-types/');
if ($verify_response['success'] && isset($verify_response['data'])) {
$data = $verify_response['data'];
echo " Post Types in Backend: " . count($data['post_types'] ?? []) . "\n";
echo " Taxonomies in Backend: " . count($data['taxonomies'] ?? []) . "\n";
echo " Last Structure Fetch: " . ($data['last_structure_fetch'] ?? 'Unknown') . "\n";
echo "✅ Backend data verified\n";
} else {
echo "⚠️ Could not verify backend data\n";
if (isset($verify_response['error'])) {
echo " Error: " . $verify_response['error'] . "\n";
}
}
echo "\n";
echo "=== Test Complete ===\n";
echo "✅ All tests passed! Site structure sync is working.\n\n";