Commit | Line | Data |
---|---|---|
61ca7d1f SL |
1 | <?php |
2 | ||
3 | /** | |
4 | * Class CRM_Utils_AddressTest | |
5 | * @group headless | |
6 | */ | |
7 | class CRM_Utils_AddressTest extends CiviUnitTestCase { | |
8 | ||
9 | public function setUp() { | |
10 | parent::setUp(); | |
11 | } | |
12 | ||
13 | public function testAddressFormat() { | |
14 | $contact = $this->callAPISuccess('contact', 'create', array( | |
15 | 'first_name' => 'Micky', | |
16 | 'last_name' => 'mouse', | |
17 | 'contact_type' => 'Individual', | |
18 | )); | |
19 | $address = $this->callAPISuccess('address', 'create', array( | |
20 | 'street_address' => '1 Happy Place', | |
21 | 'city' => 'Miami', | |
22 | 'state_province' => 'Flordia', | |
23 | 'country' => 'United States', | |
24 | 'postal_code' => 33101, | |
25 | 'contact_id' => $contact['id'], | |
26 | 'location_type_id' => 5, | |
27 | 'is_primary' => 1, | |
28 | )); | |
29 | $addressDetails = $address['values'][$address['id']]; | |
30 | $countries = CRM_Core_PseudoConstant::country(); | |
31 | $addressDetails['country'] = $countries[$addressDetails['country_id']]; | |
32 | $formatted_address = CRM_Utils_Address::format($addressDetails, 'mailing_format', FALSE, TRUE); | |
33 | $this->assertTrue((bool) strstr($formatted_address, 'UNITED STATES')); | |
34 | } | |
35 | ||
7a2c4cfa | 36 | /** |
37 | * Test state/province field's state_province_name token on getFormattedBillingAddressFieldsFromParameters | |
38 | * and test using alternate names for state_province field | |
39 | */ | |
40 | public function testStateProvinceFormattedBillingAddress() { | |
41 | $params = array( | |
42 | 'billing_street_address-99' => '123 Happy Place', | |
43 | 'billing_city-99' => 'Miami', | |
44 | 'billing_postal_code-99' => 33101, | |
39b959db SL |
45 | // 1000 => Alabama (AL) |
46 | 'state_province-99' => '1000', | |
7a2c4cfa | 47 | 'country-99' => 'United States', |
48 | ); | |
49 | ||
50 | // set address_format (we are only interested in state_province & state_province_name) | |
51 | $addFormat = '{contact.state_province}'; | |
52 | Civi::settings()->set('address_format', $addFormat); | |
53 | $formatted_address = CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters($params, '99'); | |
54 | $this->assertTrue((bool) $formatted_address == 'AL'); | |
55 | ||
56 | $addFormat = '{contact.state_province_name}'; | |
57 | Civi::settings()->set('address_format', $addFormat); | |
58 | $formatted_address = CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters($params, '99'); | |
59 | $this->assertTrue((bool) $formatted_address == 'Alabama'); | |
60 | ||
61 | // test using alternate names for state/province field | |
62 | unset($params['state_province-99']); | |
39b959db SL |
63 | // alternate name 1 |
64 | $params['billing_state_province-99'] = '1000'; | |
7a2c4cfa | 65 | $addFormat = '{contact.state_province_name}'; |
66 | Civi::settings()->set('address_format', $addFormat); | |
67 | $formatted_address = CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters($params, '99'); | |
68 | $this->assertTrue((bool) $formatted_address == 'Alabama'); | |
69 | ||
70 | unset($params['state_province-99']); | |
39b959db SL |
71 | // alternate name 2 |
72 | $params['billing_state_province_id-99'] = '1000'; | |
7a2c4cfa | 73 | $addFormat = '{contact.state_province_name}'; |
74 | Civi::settings()->set('address_format', $addFormat); | |
75 | $formatted_address = CRM_Utils_Address::getFormattedBillingAddressFieldsFromParameters($params, '99'); | |
76 | $this->assertTrue((bool) $formatted_address == 'Alabama'); | |
77 | } | |
78 | ||
61ca7d1f | 79 | } |