Use enums for countries
[MAILPOET-5169]
This commit is contained in:
@@ -79,11 +79,14 @@ class OrderFieldsFactory {
|
|||||||
),
|
),
|
||||||
new Field(
|
new Field(
|
||||||
'woocommerce:order:billing-country',
|
'woocommerce:order:billing-country',
|
||||||
Field::TYPE_STRING,
|
Field::TYPE_ENUM,
|
||||||
__('Billing country', 'mailpoet'),
|
__('Billing country', 'mailpoet'),
|
||||||
function (OrderPayload $payload) {
|
function (OrderPayload $payload) {
|
||||||
return $payload->getOrder()->get_billing_country();
|
return $payload->getOrder()->get_billing_country();
|
||||||
}
|
},
|
||||||
|
[
|
||||||
|
'options' => $this->getBillingCountryOptions(),
|
||||||
|
]
|
||||||
),
|
),
|
||||||
new Field(
|
new Field(
|
||||||
'woocommerce:order:shipping-company',
|
'woocommerce:order:shipping-company',
|
||||||
@@ -127,11 +130,14 @@ class OrderFieldsFactory {
|
|||||||
),
|
),
|
||||||
new Field(
|
new Field(
|
||||||
'woocommerce:order:shipping-country',
|
'woocommerce:order:shipping-country',
|
||||||
Field::TYPE_STRING,
|
Field::TYPE_ENUM,
|
||||||
__('Shipping country', 'mailpoet'),
|
__('Shipping country', 'mailpoet'),
|
||||||
function (OrderPayload $payload) {
|
function (OrderPayload $payload) {
|
||||||
return $payload->getOrder()->get_shipping_country();
|
return $payload->getOrder()->get_shipping_country();
|
||||||
}
|
},
|
||||||
|
[
|
||||||
|
'options' => $this->getShippingCountryOptions(),
|
||||||
|
]
|
||||||
),
|
),
|
||||||
new Field(
|
new Field(
|
||||||
'woocommerce:order:created-date',
|
'woocommerce:order:created-date',
|
||||||
@@ -259,6 +265,22 @@ class OrderFieldsFactory {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getBillingCountryOptions(): array {
|
||||||
|
$options = [];
|
||||||
|
foreach (WC()->countries->get_allowed_countries() as $code => $name) {
|
||||||
|
$options[] = ['id' => $code, 'name' => $name];
|
||||||
|
}
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getShippingCountryOptions(): array {
|
||||||
|
$options = [];
|
||||||
|
foreach (WC()->countries->get_shipping_countries() as $code => $name) {
|
||||||
|
$options[] = ['id' => $code, 'name' => $name];
|
||||||
|
}
|
||||||
|
return $options;
|
||||||
|
}
|
||||||
|
|
||||||
private function getOrderPaymentOptions(): array {
|
private function getOrderPaymentOptions(): array {
|
||||||
$gateways = WC()->payment_gateways()->get_available_payment_gateways();
|
$gateways = WC()->payment_gateways()->get_available_payment_gateways();
|
||||||
$options = [];
|
$options = [];
|
||||||
|
@@ -15,6 +15,11 @@ use WP_Term;
|
|||||||
*/
|
*/
|
||||||
class OrderFieldsFactoryTest extends \MailPoetTest {
|
class OrderFieldsFactoryTest extends \MailPoetTest {
|
||||||
public function testBillingInfo(): void {
|
public function testBillingInfo(): void {
|
||||||
|
// set specific countries
|
||||||
|
$wp = $this->diContainer->get(WPFunctions::class);
|
||||||
|
$wp->updateOption('woocommerce_allowed_countries', 'specific');
|
||||||
|
$wp->updateOption('woocommerce_specific_allowed_countries', ['CZ', 'DE']);
|
||||||
|
|
||||||
$fields = $this->getFieldsMap();
|
$fields = $this->getFieldsMap();
|
||||||
|
|
||||||
// check definitions
|
// check definitions
|
||||||
@@ -45,8 +50,13 @@ class OrderFieldsFactoryTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$countryField = $fields['woocommerce:order:billing-country'];
|
$countryField = $fields['woocommerce:order:billing-country'];
|
||||||
$this->assertSame('Billing country', $countryField->getName());
|
$this->assertSame('Billing country', $countryField->getName());
|
||||||
$this->assertSame('string', $countryField->getType());
|
$this->assertSame('enum', $countryField->getType());
|
||||||
$this->assertSame([], $countryField->getArgs());
|
$this->assertSame([
|
||||||
|
'options' => [
|
||||||
|
['id' => 'CZ', 'name' => 'Czech Republic'],
|
||||||
|
['id' => 'DE', 'name' => 'Germany'],
|
||||||
|
],
|
||||||
|
], $countryField->getArgs());
|
||||||
|
|
||||||
// check values
|
// check values
|
||||||
$order = new WC_Order();
|
$order = new WC_Order();
|
||||||
@@ -55,7 +65,7 @@ class OrderFieldsFactoryTest extends \MailPoetTest {
|
|||||||
$order->set_billing_city('Test billing city');
|
$order->set_billing_city('Test billing city');
|
||||||
$order->set_billing_postcode('12345');
|
$order->set_billing_postcode('12345');
|
||||||
$order->set_billing_state('Test billing state');
|
$order->set_billing_state('Test billing state');
|
||||||
$order->set_billing_country('Test billing country');
|
$order->set_billing_country('CZ');
|
||||||
|
|
||||||
$payload = new OrderPayload($order);
|
$payload = new OrderPayload($order);
|
||||||
$this->assertSame('Test billing company', $companyField->getValue($payload));
|
$this->assertSame('Test billing company', $companyField->getValue($payload));
|
||||||
@@ -63,10 +73,15 @@ class OrderFieldsFactoryTest extends \MailPoetTest {
|
|||||||
$this->assertSame('Test billing city', $cityField->getValue($payload));
|
$this->assertSame('Test billing city', $cityField->getValue($payload));
|
||||||
$this->assertSame('12345', $postcodeField->getValue($payload));
|
$this->assertSame('12345', $postcodeField->getValue($payload));
|
||||||
$this->assertSame('Test billing state', $stateField->getValue($payload));
|
$this->assertSame('Test billing state', $stateField->getValue($payload));
|
||||||
$this->assertSame('Test billing country', $countryField->getValue($payload));
|
$this->assertSame('CZ', $countryField->getValue($payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testShippingInfo(): void {
|
public function testShippingInfo(): void {
|
||||||
|
// set specific countries
|
||||||
|
$wp = $this->diContainer->get(WPFunctions::class);
|
||||||
|
$wp->updateOption('woocommerce_ship_to_countries', 'specific');
|
||||||
|
$wp->updateOption('woocommerce_specific_ship_to_countries', ['GR', 'SK']);
|
||||||
|
|
||||||
$fields = $this->getFieldsMap();
|
$fields = $this->getFieldsMap();
|
||||||
|
|
||||||
// check definitions
|
// check definitions
|
||||||
@@ -97,8 +112,13 @@ class OrderFieldsFactoryTest extends \MailPoetTest {
|
|||||||
|
|
||||||
$countryField = $fields['woocommerce:order:shipping-country'];
|
$countryField = $fields['woocommerce:order:shipping-country'];
|
||||||
$this->assertSame('Shipping country', $countryField->getName());
|
$this->assertSame('Shipping country', $countryField->getName());
|
||||||
$this->assertSame('string', $countryField->getType());
|
$this->assertSame('enum', $countryField->getType());
|
||||||
$this->assertSame([], $countryField->getArgs());
|
$this->assertSame([
|
||||||
|
'options' => [
|
||||||
|
['id' => 'GR', 'name' => 'Greece'],
|
||||||
|
['id' => 'SK', 'name' => 'Slovakia'],
|
||||||
|
],
|
||||||
|
], $countryField->getArgs());
|
||||||
|
|
||||||
// check values
|
// check values
|
||||||
$order = new WC_Order();
|
$order = new WC_Order();
|
||||||
@@ -107,7 +127,7 @@ class OrderFieldsFactoryTest extends \MailPoetTest {
|
|||||||
$order->set_shipping_city('Test shipping city');
|
$order->set_shipping_city('Test shipping city');
|
||||||
$order->set_shipping_postcode('12345');
|
$order->set_shipping_postcode('12345');
|
||||||
$order->set_shipping_state('Test shipping state');
|
$order->set_shipping_state('Test shipping state');
|
||||||
$order->set_shipping_country('Test shipping country');
|
$order->set_shipping_country('GR');
|
||||||
|
|
||||||
$payload = new OrderPayload($order);
|
$payload = new OrderPayload($order);
|
||||||
$this->assertSame('Test shipping company', $companyField->getValue($payload));
|
$this->assertSame('Test shipping company', $companyField->getValue($payload));
|
||||||
@@ -115,7 +135,7 @@ class OrderFieldsFactoryTest extends \MailPoetTest {
|
|||||||
$this->assertSame('Test shipping city', $cityField->getValue($payload));
|
$this->assertSame('Test shipping city', $cityField->getValue($payload));
|
||||||
$this->assertSame('12345', $postcodeField->getValue($payload));
|
$this->assertSame('12345', $postcodeField->getValue($payload));
|
||||||
$this->assertSame('Test shipping state', $stateField->getValue($payload));
|
$this->assertSame('Test shipping state', $stateField->getValue($payload));
|
||||||
$this->assertSame('Test shipping country', $countryField->getValue($payload));
|
$this->assertSame('GR', $countryField->getValue($payload));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCreatedDateField(): void {
|
public function testCreatedDateField(): void {
|
||||||
|
Reference in New Issue
Block a user