array( 'email' => 'test@example.com', 'first_name' => '测试', 'last_name' => '用户', 'billing' => array( 'first_name' => '测试', 'last_name' => '用户', 'company' => '', 'address_1' => '测试地址1', 'address_2' => '', 'city' => '测试城市', 'state' => 'ON', 'postcode' => 'M5V 3A8', 'country' => 'CA', 'email' => 'test@example.com', 'phone' => '416-555-0123' ) ), 'products' => array( 'subscription' => array( 'name' => '测试订阅产品', 'price' => 29.99, 'billing_period' => 'month', 'billing_interval' => 1, 'trial_period' => 7 ), 'bundle' => array( 'name' => '测试捆绑产品', 'items' => array( array('product_id' => 100, 'quantity' => 1, 'discount' => 10), array('product_id' => 101, 'quantity' => 2, 'discount' => 15) ) ) ), 'payment' => array( 'card_number' => '4242424242424242', 'expiry_month' => '12', 'expiry_year' => '2025', 'cvv' => '123', 'cardholder_name' => '测试用户' ) ); } /** * 创建测试客户 */ public static function create_test_customer() { $test_data = self::get_test_data(); $customer_data = $test_data['customer']; // 检查客户是否已存在 $existing_customer = get_user_by('email', $customer_data['email']); if ($existing_customer) { return $existing_customer->ID; } // 创建新客户 $customer_id = wp_create_user( $customer_data['email'], wp_generate_password(), $customer_data['email'] ); if (is_wp_error($customer_id)) { return false; } // 更新客户信息 wp_update_user(array( 'ID' => $customer_id, 'first_name' => $customer_data['first_name'], 'last_name' => $customer_data['last_name'] )); // 添加账单地址 foreach ($customer_data['billing'] as $key => $value) { update_user_meta($customer_id, 'billing_' . $key, $value); } return $customer_id; } /** * 获取捆绑产品测试数据 */ public static function get_bundle_products() { return array( array( 'name' => '测试产品A', 'price' => 19.99, 'type' => 'simple' ), array( 'name' => '测试产品B', 'price' => 29.99, 'type' => 'simple' ), array( 'name' => '测试产品C', 'price' => 39.99, 'type' => 'simple' ) ); } /** * 创建测试产品 */ public static function create_test_product($product_data = null) { if (!$product_data) { $test_data = self::get_test_data(); $product_data = $test_data['products']['subscription']; } // 创建产品 $product = new WC_Product_Simple(); $product->set_name($product_data['name']); $product->set_status('publish'); $product->set_catalog_visibility('visible'); $product->set_price($product_data['price']); $product->set_regular_price($product_data['price']); $product->set_manage_stock(false); $product->set_stock_status('instock'); // 保存产品 $product_id = $product->save(); // 如果是订阅产品,添加订阅元数据 if (isset($product_data['billing_period'])) { update_post_meta($product_id, '_yoone_subscription_enabled', 'yes'); update_post_meta($product_id, '_yoone_billing_period', $product_data['billing_period']); update_post_meta($product_id, '_yoone_billing_interval', $product_data['billing_interval']); if (isset($product_data['trial_period'])) { update_post_meta($product_id, '_yoone_trial_period', $product_data['trial_period']); } } return $product_id; } /** * 清理测试数据 */ public static function cleanup_test_data() { global $wpdb; // 删除测试客户 $test_email = self::get_test_data()['customer']['email']; $customer = get_user_by('email', $test_email); if ($customer) { wp_delete_user($customer->ID); } // 删除测试订阅 $wpdb->delete( $wpdb->prefix . 'yoone_subscriptions', array('customer_id' => 0), // 使用0作为测试标识 array('%d') ); // 删除测试支付令牌 $wpdb->delete( $wpdb->prefix . 'yoone_payment_tokens', array('customer_id' => 0), array('%d') ); // 删除测试产品 $products = get_posts(array( 'post_type' => 'product', 'meta_query' => array( array( 'key' => '_test_product', 'value' => 'yes' ) ), 'posts_per_page' => -1 )); foreach ($products as $product) { wp_delete_post($product->ID, true); } } /** * 获取测试环境信息 */ public static function get_environment_info() { return array( 'php_version' => PHP_VERSION, 'wordpress_version' => get_bloginfo('version'), 'woocommerce_version' => class_exists('WooCommerce') ? WC()->version : 'Not installed', 'plugin_version' => YOONE_SUBSCRIPTIONS_VERSION, 'memory_limit' => ini_get('memory_limit'), 'max_execution_time' => ini_get('max_execution_time'), 'database_version' => $GLOBALS['wpdb']->db_version() ); } }