Skip to content
Snippets Groups Projects
Commit 02a12ae8 authored by DaveD's avatar DaveD
Browse files

fix country list

parent 9199f241
Branches
Tags
No related merge requests found
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
namespace Civi\Schema\Entity;
use Civi\Schema\SqlEntityMetadata;
class AddressMetadata extends SqlEntityMetadata {
public function getOptions(string $fieldName, array $values = [], bool $includeDisabled = FALSE, bool $checkPermissions = FALSE, ?int $userId = NULL): ?array {
$options = parent::getOptions($fieldName, $values, $includeDisabled, $checkPermissions, $userId);
if ($fieldName == 'country_id') {
// The general idea is call the function that does all the stuff, but it
// wants a different format, so we convert, then merge back in the
// original format data.
$map = [];
$originalReindexed = [];
foreach ($options as $opt) {
$map[$opt['id']] = $opt['label'];
// It's way more efficient later to be able to get the original by id.
// It's currently indexed sequentially.
$originalReindexed[$opt['id']] = $opt;
}
// At the moment it's unsorted. The pre-entity output used the db for
// sorting. We don't know what all the local mysql settings are set to,
// but the strings right now are still all en_US, and it will get
// re-sorted in a minute according to locale if the civi locale is
// different anyway, so use en_US.
// If we just use regular asort() here, then e.g. Åland Islands is wrong.
$collator = new \Collator('en_US.utf8');
$collator->asort($map);
// Do all the things
$map = \CRM_Core_BAO_Country::_defaultContactCountries($map);
// Now merge the format it wants back in
$newOptions = [];
foreach ($map as $id => $possiblyTranslatedLabel) {
// We may have translated the label, so we want that label.
$newOptions[] = array_merge($originalReindexed[$id], ['label' => $possiblyTranslatedLabel]);
}
$options = $newOptions;
}
return $options;
}
}
......@@ -4,6 +4,7 @@ return [
'name' => 'Address',
'table' => 'civicrm_address',
'class' => 'CRM_Core_DAO_Address',
'metaProvider' => '\Civi\Schema\Entity\AddressMetadata',
'getInfo' => fn() => [
'title' => ts('Address'),
'title_plural' => ts('Addresses'),
......
......@@ -24,6 +24,12 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase {
$this->quickCleanup(['civicrm_contact', 'civicrm_address']);
}
public function tearDown(): void {
\Civi::settings()->set('pinnedContactCountries', []);
CRM_Core_I18n::singleton()->setLocale('en_US');
parent::tearDown();
}
/**
* Create() method (create and update modes)
*/
......@@ -930,6 +936,58 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase {
$this->assertEquals(1228, $availableCountries[2]);
}
public function testPinnedCountryWithEntity(): void {
\Civi::settings()->set('pinnedContactCountries', ['1228']);
$countries = \Civi::entity('Address')->getOptions('country_id');
$this->assertEquals('US', $countries[0]['name']);
}
public function testCountryLabelTranslation(): void {
CRM_Core_I18n::singleton()->setLocale('nl_NL');
$countries = \Civi::entity('Address')->getOptions('country_id');
$checked = [];
foreach ($countries as $country) {
switch ($country['name']) {
case 'NL':
$this->assertEquals('Nederland', $country['label']);
$checked[] = 'NL';
break;
case 'US':
$this->assertEquals('Verenigde Staten', $country['label']);
$checked[] = 'US';
break;
}
}
$this->assertCount(2, $checked, 'Country list incomplete');
}
public function testCountrySorting(): void {
$countries = \Civi::entity('Address')->getOptions('country_id');
$this->assertEquals('AF', $countries[0]['name']);
// Åland Islands should sort second in en_US locale
$this->assertEquals('AX', $countries[1]['name']);
$this->assertEquals('AL', $countries[2]['name']);
$this->assertEquals('ZW', array_pop($countries)['name']);
CRM_Core_I18n::singleton()->setLocale('nl_NL');
$countries = \Civi::entity('Address')->getOptions('country_id');
$this->assertEquals('AF', $countries[0]['name']);
$this->assertEquals('AL', $countries[1]['name']);
$this->assertEquals('DZ', $countries[2]['name']);
// Åland Islands
$this->assertEquals('AX', array_pop($countries)['name']);
CRM_Core_I18n::singleton()->setLocale('it_IT');
$countries = \Civi::entity('Address')->getOptions('country_id');
$this->assertEquals('AF', $countries[0]['name']);
$this->assertEquals('AL', $countries[1]['name']);
$this->assertEquals('DZ', $countries[2]['name']);
// Åland Islands
$this->assertEquals('AX', $countries[114]['name']);
$this->assertEquals('ZW', array_pop($countries)['name']);
}
/**
* Test dev/core#2379 fix - geocodes shouldn't be > 14 characters.
*/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment