Skip to content

Commit

Permalink
CRM-14720 add test for postal smart groups
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 12, 2015
1 parent ea62dcb commit f2a28c5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion tests/phpunit/CRM/Contact/BAO/GroupContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ protected function setUp() {

/**
* 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() {

Expand Down
14 changes: 11 additions & 3 deletions tests/phpunit/CRM/Contact/BAO/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -45,14 +46,15 @@ protected function setUp() {

/**
* Tears down the fixture, for example, closes a network connection.
*
* This method is called after a test is executed.
*/
protected function tearDown() {
$this->quickCleanup(array('civicrm_mapping_field', 'civicrm_mapping', 'civicrm_group', 'civicrm_saved_search'));
}

/**
* Test case for add( )
* Test case for add( ).
*/
public function testAddSimple() {

Expand All @@ -72,6 +74,9 @@ public function testAddSimple() {
);
}

/**
* Test adding a smart group.
*/
public function testAddSmart() {

$checkParams = $params = array(
Expand All @@ -93,7 +98,8 @@ public function testAddSmart() {
}

/**
* 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() {
Expand All @@ -114,7 +120,9 @@ public function loadSavedSearches() {
}

/**
* 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
Expand Down
36 changes: 36 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function tearDown() {
'civicrm_entity_tag',
'civicrm_tag',
'civicrm_contact',
'civicrm_address',
);
$this->quickCleanup($tablesToTruncate);
}
Expand Down Expand Up @@ -212,4 +213,39 @@ public function testGroupContactCacheAddSearch() {
$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);
}

}

0 comments on commit f2a28c5

Please sign in to comment.