Repository navigation
Expand file tree
/
Copy pathseed-woocommerce-e2e.php
More file actions
105 lines (81 loc) · 2.88 KB
/
Copy pathseed-woocommerce-e2e.php
File metadata and controls
105 lines (81 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
/**
* Seed a deterministic WooCommerce storefront for browser E2E tests.
*
* This file runs through `wp eval-file` after WordPress, WooCommerce, and
* Basicrum are active in an isolated test stack.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit( 1 );
}
$product = new WC_Product_Simple();
$product->set_name( 'Basicrum E2E Product' );
$product->set_regular_price( '10.00' );
$product->set_status( 'publish' );
$product->set_catalog_visibility( 'visible' );
$product_id = $product->save();
if ( ! $product_id ) {
throw new RuntimeException( 'Could not create the WooCommerce E2E product.' );
}
wp_update_post(
array(
'ID' => $product_id,
'post_name' => 'basicrum-e2e-product',
)
);
$product = wc_get_product( $product_id );
if ( ! $product ) {
throw new RuntimeException( 'Could not load the WooCommerce E2E product.' );
}
add_filter( 'pre_wp_mail', '__return_true' );
$order = wc_create_order();
if ( ! $order ) {
throw new RuntimeException( 'Could not create the WooCommerce E2E order.' );
}
$order_item_id = $order->add_product( $product, 1 );
if ( ! $order_item_id ) {
throw new RuntimeException( 'Could not add the product to the WooCommerce E2E order.' );
}
$order->calculate_totals();
$order->set_status( 'completed' );
$order->save();
$payment_order = wc_create_order();
if ( ! $payment_order ) {
throw new RuntimeException( 'Could not create the WooCommerce E2E payment order.' );
}
$payment_order_item_id = $payment_order->add_product( $product, 1 );
if ( ! $payment_order_item_id ) {
throw new RuntimeException( 'Could not add the product to the WooCommerce E2E payment order.' );
}
$payment_order->calculate_totals();
$payment_order->set_status( 'pending' );
$payment_order->save();
$routes = array(
'shop' => wc_get_page_permalink( 'shop' ),
'product' => get_permalink( $product_id ),
'cart' => wc_get_cart_url(),
'cart_add' => add_query_arg( 'add-to-cart', $product_id, wc_get_cart_url() ),
'checkout' => wc_get_checkout_url(),
'order_pay' => $payment_order->get_checkout_payment_url(),
'order_received' => $order->get_checkout_order_received_url(),
);
foreach ( $routes as $route_name => $route_url ) {
if ( empty( $route_url ) ) {
throw new RuntimeException( sprintf( 'Could not determine the %s test route.', $route_name ) );
}
}
$upload_directory = wp_upload_dir();
if ( ! empty( $upload_directory['error'] ) ) {
throw new RuntimeException( 'Could not access the WordPress uploads directory.' );
}
$fixture_path = trailingslashit( $upload_directory['basedir'] ) . 'basicrum-woocommerce-e2e.json';
$fixture = wp_json_encode(
array(
'routes' => $routes,
),
JSON_UNESCAPED_SLASHES
);
if ( false === $fixture || false === file_put_contents( $fixture_path, $fixture ) ) {
throw new RuntimeException( 'Could not write the WooCommerce E2E fixture.' );
}
WP_CLI::log( "Seeded WooCommerce E2E routes in $fixture_path" );