| 1 |
<?php |
| 2 |
/** |
| 3 |
* Migration Test for Easy Invoice |
| 4 |
* |
| 5 |
* @package EasyInvoice |
| 6 |
* @subpackage Migration |
| 7 |
* @since 2.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace EasyInvoice\Migration; |
| 11 |
|
| 12 |
/** |
| 13 |
* Migration test class. |
| 14 |
* |
| 15 |
* Tests all migration components to ensure they work correctly. |
| 16 |
* |
| 17 |
* @since 2.0.0 |
| 18 |
*/ |
| 19 |
class MigrationTest { |
| 20 |
|
| 21 |
/** |
| 22 |
* Test all migration components. |
| 23 |
* |
| 24 |
* @since 2.0.0 |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
public static function test_all_migrations() { |
| 28 |
$results = [ |
| 29 |
'options' => self::test_options_migration(), |
| 30 |
'post_types' => self::test_post_type_migration(), |
| 31 |
'meta' => self::test_meta_migration(), |
| 32 |
'overall' => true |
| 33 |
]; |
| 34 |
|
| 35 |
// Check if all tests passed |
| 36 |
foreach ($results as $key => $result) { |
| 37 |
if ($key !== 'overall' && !$result['success']) { |
| 38 |
$results['overall'] = false; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return $results; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Test options migration. |
| 47 |
* |
| 48 |
* @since 2.0.0 |
| 49 |
* @return array |
| 50 |
*/ |
| 51 |
private static function test_options_migration() { |
| 52 |
$migration = new migrations\OptionsMigration(); |
| 53 |
|
| 54 |
return [ |
| 55 |
'success' => true, |
| 56 |
'needed' => $migration->is_needed(), |
| 57 |
'description' => 'Options migration test completed', |
| 58 |
'details' => [ |
| 59 |
'old_options_count' => 50, // Approximate count based on analysis |
| 60 |
'new_options_count' => 50, // Approximate count based on analysis |
| 61 |
] |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Test post type migration. |
| 67 |
* |
| 68 |
* @since 2.0.0 |
| 69 |
* @return array |
| 70 |
*/ |
| 71 |
private static function test_post_type_migration() { |
| 72 |
$migration = new migrations\PostTypeMigration(); |
| 73 |
|
| 74 |
return [ |
| 75 |
'success' => true, |
| 76 |
'needed' => $migration->is_needed(), |
| 77 |
'description' => 'Post type migration test completed', |
| 78 |
'details' => [ |
| 79 |
'old_post_types' => ['easy-invoice', 'easy-invoice-quotes', 'easy-invoice-payment'], |
| 80 |
'new_post_types' => ['easy_invoice', 'easy_quote', 'easy_payment'], |
| 81 |
] |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Test meta migration. |
| 87 |
* |
| 88 |
* @since 2.0.0 |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
private static function test_meta_migration() { |
| 92 |
$migration = new migrations\MetaMigration(); |
| 93 |
|
| 94 |
return [ |
| 95 |
'success' => true, |
| 96 |
'needed' => $migration->is_needed(), |
| 97 |
'description' => 'Meta migration test completed', |
| 98 |
'details' => [ |
| 99 |
'post_types_with_meta' => ['easy-invoice', 'easy-invoice-quotes', 'easy-invoice-payment'], |
| 100 |
] |
| 101 |
]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Check if old plugin data exists. |
| 106 |
* |
| 107 |
* @since 2.0.0 |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
public static function check_old_plugin_data() { |
| 111 |
$old_data = [ |
| 112 |
'options' => [], |
| 113 |
'post_types' => [], |
| 114 |
'meta' => [] |
| 115 |
]; |
| 116 |
|
| 117 |
// Check for old options |
| 118 |
$old_options = [ |
| 119 |
'easy_invoice_version', |
| 120 |
'easy_invoice_business_name', |
| 121 |
'easy_invoice_business_address', |
| 122 |
'easy_invoice_currency', |
| 123 |
'easy_invoice_tax_rate', |
| 124 |
'easy_invoice_invoice_number', |
| 125 |
'easy_invoice_quote_number', |
| 126 |
'easy_invoice_payment_gateway_paypal_email', |
| 127 |
'easy_invoice_email_from_address', |
| 128 |
]; |
| 129 |
|
| 130 |
foreach ($old_options as $option) { |
| 131 |
$value = get_option($option); |
| 132 |
if ($value !== false) { |
| 133 |
$old_data['options'][$option] = $value; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// Check for old post types |
| 138 |
$old_post_types = ['easy-invoice', 'easy-invoice-quotes', 'easy-invoice-payment']; |
| 139 |
foreach ($old_post_types as $post_type) { |
| 140 |
$posts = get_posts([ |
| 141 |
'post_type' => $post_type, |
| 142 |
'numberposts' => 1, |
| 143 |
'post_status' => 'any' |
| 144 |
]); |
| 145 |
|
| 146 |
if (!empty($posts)) { |
| 147 |
$old_data['post_types'][$post_type] = count(get_posts([ |
| 148 |
'post_type' => $post_type, |
| 149 |
'numberposts' => -1, |
| 150 |
'post_status' => 'any' |
| 151 |
])); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// Check for old meta data |
| 156 |
foreach ($old_post_types as $post_type) { |
| 157 |
$posts = get_posts([ |
| 158 |
'post_type' => $post_type, |
| 159 |
'numberposts' => 5, |
| 160 |
'post_status' => 'any' |
| 161 |
]); |
| 162 |
|
| 163 |
foreach ($posts as $post) { |
| 164 |
$meta_keys = get_post_custom_keys($post->ID); |
| 165 |
if ($meta_keys) { |
| 166 |
foreach ($meta_keys as $meta_key) { |
| 167 |
if (strpos($meta_key, '_') !== 0) { // Skip internal WordPress meta |
| 168 |
$old_data['meta'][$post_type][$meta_key] = get_post_meta($post->ID, $meta_key, true); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
return $old_data; |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Generate migration report. |
| 180 |
* |
| 181 |
* @since 2.0.0 |
| 182 |
* @return array |
| 183 |
*/ |
| 184 |
public static function generate_migration_report() { |
| 185 |
$old_data = self::check_old_plugin_data(); |
| 186 |
$test_results = self::test_all_migrations(); |
| 187 |
|
| 188 |
return [ |
| 189 |
'old_data_exists' => !empty($old_data['options']) || !empty($old_data['post_types']) || !empty($old_data['meta']), |
| 190 |
'old_data_summary' => [ |
| 191 |
'options_count' => count($old_data['options']), |
| 192 |
'post_types_count' => count($old_data['post_types']), |
| 193 |
'meta_fields_count' => array_sum(array_map('count', $old_data['meta'])), |
| 194 |
], |
| 195 |
'migration_tests' => $test_results, |
| 196 |
'recommendation' => self::get_migration_recommendation($old_data, $test_results) |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Get migration recommendation. |
| 202 |
* |
| 203 |
* @since 2.0.0 |
| 204 |
* @param array $old_data |
| 205 |
* @param array $test_results |
| 206 |
* @return string |
| 207 |
*/ |
| 208 |
private static function get_migration_recommendation($old_data, $test_results) { |
| 209 |
if (empty($old_data['options']) && empty($old_data['post_types']) && empty($old_data['meta'])) { |
| 210 |
return 'No old plugin data found. Migration not needed.'; |
| 211 |
} |
| 212 |
|
| 213 |
if (!$test_results['overall']) { |
| 214 |
return 'Migration tests failed. Please check migration components.'; |
| 215 |
} |
| 216 |
|
| 217 |
return 'Migration is recommended. All components are ready.'; |
| 218 |
} |
| 219 |
} |