From 9b1e446971894c31d95acb5e6b698ee1593785e7 Mon Sep 17 00:00:00 2001 From: eileenmcnaughton Date: Mon, 12 Oct 2015 04:14:24 +0000 Subject: [PATCH] CRM-14720 add test for postal smart groups --- .../CRM/Contact/BAO/GroupContactTest.php | 3 +- tests/phpunit/CRM/Contact/BAO/GroupTest.php | 14 ++++++-- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 36 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php index 42a8364356..043035fe67 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupContactTest.php @@ -46,13 +46,14 @@ class CRM_Contact_BAO_GroupContactTest extends CiviUnitTestCase { /** * Tears down the fixture, for example, closes a network connection. + * * This method is called after a test is executed. */ protected function tearDown() { } /** - * Test case for add( ) + * Test case for add( ). */ public function testAdd() { diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index 9acdb33e52..3125da6f09 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -37,6 +37,7 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { /** * Sets up the fixture, for example, opens a network connection. + * * This method is called before a test is executed. */ protected function setUp() { @@ -45,6 +46,7 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { /** * Tears down the fixture, for example, closes a network connection. + * * This method is called after a test is executed. */ protected function tearDown() { @@ -52,7 +54,7 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { } /** - * Test case for add( ) + * Test case for add( ). */ public function testAddSimple() { @@ -72,6 +74,9 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { ); } + /** + * Test adding a smart group. + */ public function testAddSmart() { $checkParams = $params = array( @@ -93,7 +98,8 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { } /** - * Load all sql data sets & return an array of saved searches + * Load all sql data sets & return an array of saved searches. + * * @return array */ public function dataProviderSavedSearch() { @@ -114,7 +120,9 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { } /** - * Check we can load smart groups based on config from 'real DBs' without fatal errors - note that we are only testing lack of errors at this stage + * Check we can load smart groups based on config from 'real DBs' without fatal errors. + * + * Note that we are only testing lack of errors at this stage * @todo - for some reason the data was getting truncated from the group table using dataprovider - would be preferable to get that working * //@notdataProvider dataProviderSavedSearch * //@notparam integer $groupID diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 1ff3bc78a7..4daff9d813 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -26,6 +26,7 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { 'civicrm_entity_tag', 'civicrm_tag', 'civicrm_contact', + 'civicrm_address', ); $this->quickCleanup($tablesToTruncate); } @@ -212,4 +213,39 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { $this->assertEquals('SELECT contact_a.id as contact_id', $select); } + /** + * Test smart groups with non-numeric don't fail on range queries. + * + * CRM-14720 + */ + public function testNumericPostal() { + $this->individualCreate(array('api.address.create' => array('postal_code' => 5, 'location_type_id' => 'Main'))); + $this->individualCreate(array('api.address.create' => array('postal_code' => 'EH10 4RB-889', 'location_type_id' => 'Main'))); + $this->individualCreate(array('api.address.create' => array('postal_code' => '4', 'location_type_id' => 'Main'))); + $this->individualCreate(array('api.address.create' => array('postal_code' => '6', 'location_type_id' => 'Main'))); + + $params = array(array('postal_code_low', '=', 5, 0, 0)); + CRM_Contact_BAO_Query::convertFormValues($params); + + $query = new CRM_Contact_BAO_Query( + $params, array('contact_id'), + NULL, TRUE, FALSE, 1, + TRUE, + TRUE, FALSE + ); + + $sql = $query->query(FALSE); + $result = CRM_Core_DAO::executeQuery(implode(' ', $sql)); + $this->assertEquals(2, $result->N); + + // We save this as a smart group and then load it. With mysql warnings on & CRM-14720 this + // results in mysql warnings & hence fatal errors. + /// I was unable to get mysql warnings to activate in the context of the unit tests - but + // felt this code still provided a useful bit of coverage as it runs the various queries to load + // the group & could generate invalid sql if a bug were introduced. + $groupParams = array('title' => 'postal codes', 'formValues' => $params, 'is_active' => 1); + $group = CRM_Contact_BAO_Group::createSmartGroup($groupParams); + CRM_Contact_BAO_GroupContactCache::load($group, TRUE); + } + } -- 2.25.1