Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add billingStateProvince and standardized property names #21583

Merged
merged 2 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions Civi/Payment/PropertyBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ class PropertyBag implements \ArrayAccess {

protected static $propMap = [
'amount' => TRUE,
'total_amount' => 'amount',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line may have caused a regression - https://lab.civicrm.org/dev/core/-/issues/3413 - it appears to be treating total_amount as amount when both are passed - I think this was probably done by accident since I think amount was consolidated quite a long time ago

'billingStreetAddress' => TRUE,
'billing_street_address' => 'billingStreetAddress',
'street_address' => 'billingStreetAddress',
'billingSupplementalAddress1' => TRUE,
'billingSupplementalAddress2' => TRUE,
'billingSupplementalAddress3' => TRUE,
'billingCity' => TRUE,
'billing_city' => 'billingCity',
'city' => 'billingCity',
'billingPostalCode' => TRUE,
'billing_postal_code' => 'billingPostalCode',
'postal_code' => 'billingPostalCode',
'billingCounty' => TRUE,
'billingStateProvince' => TRUE,
'billing_state_province' => 'billingStateProvince',
'state_province' => 'billingStateProvince',
'billingCountry' => TRUE,
'contactID' => TRUE,
'contact_id' => 'contactID',
Expand Down Expand Up @@ -258,13 +268,18 @@ protected function handleLegacyPropNames($prop, $silent = FALSE) {
if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType::getBilling()
&& isset(static::$propMap[substr($prop, 0, -2)])
) {
$newName = substr($prop, 0, -2);
$billingAddressProp = substr($prop, 0, -2);
$newName = static::$propMap[$billingAddressProp] ?? NULL;
if ($newName === TRUE) {
// Good, modern name.
return $billingAddressProp;
}
Comment on lines -261 to +276
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It turns out we already have good test coverage on this - if you add the properties without this change you get errors like invalid function getBilling_state_province

}

if ($newName === NULL) {
if ($silent) {
// Only for use by offsetExists
return;
return NULL;
}
throw new \InvalidArgumentException("Unknown property '$prop'.");
}
Expand Down Expand Up @@ -300,7 +315,7 @@ protected function get($prop, $label) {
*
* @return PropertyBag $this object so you can chain set setters.
*/
protected function set($prop, $label, $value) {
protected function set($prop, $label = 'default', $value) {
$this->props[$label][$prop] = $value;
return $this;
}
Expand Down Expand Up @@ -591,6 +606,27 @@ public function setBillingCounty($input, $label = 'default') {
return $this->set('billingCounty', $label, (string) $input);
}

/**
* BillingStateProvince getter.
*
* @return string
*/
public function getBillingStateProvince($label = 'default') {
return $this->get('billingStateProvince', $label);
}

/**
* BillingStateProvince setter.
*
* Nb. we can't validate this unless we have the country ID too, so we don't.
*
* @param string $input
* @param string $label e.g. 'default'
*/
public function setBillingStateProvince($input, $label = 'default') {
return $this->set('billingStateProvince', $label, (string) $input);
}

/**
* BillingCountry getter.
*
Expand Down
10 changes: 6 additions & 4 deletions tests/phpunit/Civi/Payment/PropertyBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,10 @@ public function testEmpty() {
}

/**
*
* Data provider for testOtherParams
* $prop, $legacy_names, $valid_values, $invalid_values
*
* return array
*/
public function otherParamsDataProvider() {
$valid_bools = [['0' , FALSE], ['', FALSE], [0, FALSE], [FALSE, FALSE], [TRUE, TRUE], [1, TRUE], ['1', TRUE]];
Expand All @@ -420,13 +421,14 @@ public function otherParamsDataProvider() {
$valid_ints = [[123, 123], ['123', 123]];
$invalid_ints = [-1, 0, NULL, ''];
return [
['billingStreetAddress', [], $valid_strings_inc_null, []],
['billingStreetAddress', ['billing_street_address', 'street_address', 'billing_street_address-5'], $valid_strings_inc_null, []],
['billingSupplementalAddress1', [], $valid_strings_inc_null, []],
['billingSupplementalAddress2', [], $valid_strings_inc_null, []],
['billingSupplementalAddress3', [], $valid_strings_inc_null, []],
['billingCity', [], $valid_strings_inc_null, []],
['billingPostalCode', [], $valid_strings_inc_null, []],
['billingCity', ['billing_city', 'city', 'billing_city-5'], $valid_strings_inc_null, []],
['billingPostalCode', ['billing_postal_code', 'postal_code', 'billing_postal_code-5'], $valid_strings_inc_null, []],
['billingCounty', [], $valid_strings_inc_null, []],
['billingStateProvince', ['billing_state_province', 'state_province', 'billing_state_province-5'], $valid_strings_inc_null, []],
['billingCountry', [], [['GB', 'GB'], ['NZ', 'NZ']], ['XX', '', NULL, 0]],
['contributionID', ['contribution_id'], $valid_ints, $invalid_ints],
['contributionRecurID', ['contribution_recur_id'], $valid_ints, $invalid_ints],
Expand Down