summaryrefslogtreecommitdiffstats
path: root/test/setup.sh
blob: 69253865dd10286424f2c844d6af1995e5de519e (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/sh
#
# Stand up the WordPress + WooCommerce store the browser tests drive.
#
# Idempotent: running it again on a live stack reconfigures rather than
# rebuilds. `make clean` is what throws the volumes away.

set -eu

cd "$(dirname "$0")"

SITE="http://127.0.0.1:8090"

wp() {
	docker compose exec -T cli wp "$@"
}

printf 'bringing the stack up\n'
docker compose up -d >/dev/null

printf 'waiting for the web server\n'
i=0
while [ "$i" -lt 90 ]; do
	code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 2 "$SITE/" 2>/dev/null || echo 000)
	[ "$code" != "000" ] && break
	i=$((i + 1))
	sleep 2
done
[ "$code" = "000" ] && { printf 'web server never answered\n' >&2; exit 1; }

if ! wp core is-installed >/dev/null 2>&1; then
	printf 'installing WordPress\n'
	wp core install \
		--url="$SITE" \
		--title="CEP Test Store" \
		--admin_user=admin \
		--admin_password=admin \
		[email protected] \
		--skip-email >/dev/null
fi

wp rewrite structure '/%postname%/' >/dev/null 2>&1
wp rewrite flush >/dev/null 2>&1

if ! wp plugin is-installed woocommerce >/dev/null 2>&1; then
	printf 'installing WooCommerce\n'
	wp plugin install woocommerce >/dev/null
fi
wp plugin activate woocommerce >/dev/null

printf 'configuring the store\n'
wp option update woocommerce_store_address "Praça da Sé" >/dev/null
wp option update woocommerce_store_city "São Paulo" >/dev/null
wp option update woocommerce_default_country "BR:SP" >/dev/null
wp option update woocommerce_store_postcode "01001000" >/dev/null
wp option update woocommerce_currency BRL >/dev/null
wp option update woocommerce_allowed_countries all >/dev/null
wp option update woocommerce_enable_guest_checkout yes >/dev/null
wp option update woocommerce_enable_checkout_login_reminder no >/dev/null
wp option update woocommerce_calc_taxes no >/dev/null
wp option update woocommerce_cod_settings \
	'{"enabled":"yes","title":"Pagamento na entrega","description":"","instructions":""}' \
	--format=json >/dev/null
wp eval 'if ( ! WC_Shipping_Zones::get_zone( 0 )->get_shipping_methods() ) { WC_Shipping_Zones::get_zone( 0 )->add_shipping_method( "flat_rate" ); }' >/dev/null

wp eval '
$id = (int) get_option( "cepis_test_product", 0 );
if ( ! $id || ! wc_get_product( $id ) ) {
	$product = new WC_Product_Simple();
	$product->set_name( "Caneca" );
	$product->set_regular_price( "25" );
	$product->set_virtual( false );
	$id = $product->save();
	update_option( "cepis_test_product", $id );
}
' >/dev/null

wp eval '
$blocks = 0;
foreach ( get_pages( array( "post_status" => "publish" ) ) as $page ) {
	if ( has_block( "woocommerce/checkout", $page ) ) {
		$blocks = (int) $page->ID;
		break;
	}
}
if ( ! $blocks ) {
	WP_CLI::error( "no page holds the woocommerce/checkout block" );
}
update_option( "cepis_test_blocks_page", $blocks );
update_option( "woocommerce_checkout_page_id", $blocks );

$classic = get_page_by_path( "checkout-classico" );
if ( ! $classic ) {
	$id = wp_insert_post( array(
		"post_title"   => "Checkout classico",
		"post_name"    => "checkout-classico",
		"post_content" => "[woocommerce_checkout]",
		"post_status"  => "publish",
		"post_type"    => "page",
	) );
} else {
	$id = (int) $classic->ID;
}
update_option( "cepis_test_classic_page", $id );
' >/dev/null

printf 'installing the fixture that imitates Brazilian Market on WooCommerce\n'
docker compose exec -T cli sh -c 'mkdir -p /var/www/html/wp-content/mu-plugins && cat > /var/www/html/wp-content/mu-plugins/cepis-test-br-fields.php' <<'PHP'
<?php
/**
 * Adds the house-number and neighbourhood fields that "Brazilian Market on
 * WooCommerce" adds, so the plugin can be tested with and without them.
 * Switched by the `cepis_test_br_fields` option.
 */

function cepis_test_br_fields( $fields, $prefix ) {
	if ( 'yes' !== get_option( 'cepis_test_br_fields' ) ) {
		return $fields;
	}

	$fields[ $prefix . '_number' ] = array(
		'label'    => 'Número',
		'required' => false,
		'class'    => array( 'form-row-first', 'address-field' ),
		'priority' => 51,
	);
	$fields[ $prefix . '_neighborhood' ] = array(
		'label'    => 'Bairro',
		'required' => false,
		'class'    => array( 'form-row-last', 'address-field' ),
		'priority' => 52,
	);

	return $fields;
}

add_filter( 'woocommerce_billing_fields', function ( $fields ) {
	return cepis_test_br_fields( $fields, 'billing' );
} );

add_filter( 'woocommerce_shipping_fields', function ( $fields ) {
	return cepis_test_br_fields( $fields, 'shipping' );
} );

/**
 * Force the site locale from an option, so the shipped translation can be
 * exercised without downloading WordPress's own language pack.
 */
add_filter( 'locale', function ( $locale ) {
	$forced = get_option( 'cepis_test_locale' );

	return $forced ? $forced : $locale;
} );
PHP

wp plugin activate cepis-woocommerce >/dev/null

printf 'ready: %s\n' "$SITE"