From 959528d239b6bdcf410bccf003191f5bc648b21e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Thu, 26 Jun 2014 23:48:47 +1200 Subject: [PATCH] create smartgroup test --- CRM/Contact/BAO/SavedSearch.php | 2 +- CRM/Core/BAO/Mapping.php | 6 +-- tests/phpunit/CRM/Contact/BAO/GroupTest.php | 54 ++++++++++++++++++- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 38 +++++++++++++ .../801_pre44_billing_email_smartgroup.sql | 13 +++++ 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 tests/phpunit/CRM/Contact/BAO/SavedSearchDataSets/801_pre44_billing_email_smartgroup.sql diff --git a/CRM/Contact/BAO/SavedSearch.php b/CRM/Contact/BAO/SavedSearch.php index c9fe096769..b13a2f35fd 100644 --- a/CRM/Contact/BAO/SavedSearch.php +++ b/CRM/Contact/BAO/SavedSearch.php @@ -137,7 +137,7 @@ class CRM_Contact_BAO_SavedSearch extends CRM_Contact_DAO_SavedSearch { */ static function getSearchParams($id) { $fv = self::getFormValues($id); - //check if the saved seach has mapping id + //check if the saved search has mapping id if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $id, 'mapping_id')) { return CRM_Core_BAO_Mapping::formattedFields($fv); } diff --git a/CRM/Core/BAO/Mapping.php b/CRM/Core/BAO/Mapping.php index 828c046648..32ac611cc3 100644 --- a/CRM/Core/BAO/Mapping.php +++ b/CRM/Core/BAO/Mapping.php @@ -100,13 +100,13 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { * contact object. the params array could contain additional unused name/value * pairs * - * @param array $params (reference) an assoc array of name/value pairs + * @param array $params an array of name/value pairs * * @return object CRM_Core_DAO_Mapper object on success, otherwise null * @access public * @static */ - static function add(&$params) { + static function add($params) { $mapping = new CRM_Core_DAO_Mapping(); $mapping->copyValues($params); $mapping->save(); @@ -988,7 +988,7 @@ class CRM_Core_BAO_Mapping extends CRM_Core_DAO_Mapping { * @return array $returnFields formatted associated array of elements@static * @public */ - static function &formattedFields(&$params, $row = FALSE) { + static function formattedFields(&$params, $row = FALSE) { $fields = array(); if (empty($params) || !isset($params['mapper'])) { diff --git a/tests/phpunit/CRM/Contact/BAO/GroupTest.php b/tests/phpunit/CRM/Contact/BAO/GroupTest.php index 7e30269481..7d38c3e50f 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupTest.php @@ -51,7 +51,9 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { * * @access protected */ - protected function tearDown() {} + protected function tearDown() { + $this->quickCleanup(array('civicrm_mapping_field', 'civicrm_mapping', 'civicrm_group', 'civicrm_saved_search')); + } /** * test case for add( ) @@ -93,5 +95,55 @@ class CRM_Contact_BAO_GroupTest extends CiviUnitTestCase { $checkParams ); } + + /** + * Load all sql data sets & return an array of saved searches + * @return array + */ + function dataProviderSavedSearch() { + + $this->loadSavedSearches(); + $results = CRM_Core_DAO::singleValueQuery('SELECT GROUP_CONCAT(id) FROM civicrm_group WHERE saved_search_id IS NOT NULL'); + return array(explode(',', $results)); + } + + /** + * Load saved search sql files into the DB + */ + function loadSavedSearches() { + $dsn = CRM_Core_Config::singleton()->dsn; + foreach (glob(dirname(__FILE__) . "/SavedSearchDataSets/*.sql") as $file) { + CRM_Utils_File::sourceSQLFile($dsn, $file); + } + } + + /** + * 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 + * + * To add to this dataset do + * + * SET @groupID = x; + * SELECT mapping_id FROM civicrm_group g LEFT JOIN civicrm_saved_search s ON saved_search_id = s.id WHERE g.id = @groupID INTO @mappingID; + + * SELECT * FROM civicrm_mapping WHERE id = @mappingID; + * SELECT * FROM civicrm_mapping_field WHERE mapping_id = @mappingID; + * SELECT * FROM civicrm_saved_search WHERE mapping_id = @mappingID; + * SELECT g.* FROM civicrm_saved_search s LEFT JOIN civicrm_group g ON g.saved_search_id = s.id WHERE mapping_id = @mappingID; + * + * Copy the output to a single xml file and place in the SavedSearchDataSets folder - use the group number as the prefix. + * Try to keep as much of the real world irregular glory as you can! Don't change the table ids to be number 1 as this can hide errors + */ + function testGroupData() { + $groups = $this->dataProviderSavedSearch(); + foreach ($groups[0] as $groupID) { + $group = new CRM_Contact_BAO_Group(); + $group->id = $groupID; + $group->find(TRUE); + CRM_Contact_BAO_GroupContactCache::load($group, TRUE); + } + } } diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 85463182aa..af5c1e70c4 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -185,5 +185,43 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { } } + /** + * CRM-14263 search builder failure with search profile & address in criteria + * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has + * so few tests detecting any change seems good here :-) + */ + function testSearchProfilePrimaryCityCRM14263() + { + $contactID = $this->individualCreate(); + CRM_Core_Config::singleton()->defaultSearchProfileID = 1; + $this->callAPISuccess('address', 'create', array('contact_id' => $contactID, 'city' => 'Cool City', 'location_type_id' => 1,)); + $params = array( + 0 => array( + 0 => 'city', + 1 => '=', + 2 => 'Cool City', + 3 => 1, + 4 => 0, + ) + ); + $returnProperties = array( + 'contact_type' => 1, + 'contact_sub_type' => 1, + 'sort_name' => 1, + ); + $expectedSQL = "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, civicrm_address.id as address_id, civicrm_address.city as `city` FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) WHERE ( ( LOWER(civicrm_address.city) = 'cool city' ) ) AND (contact_a.is_deleted = 0) ORDER BY contact_a.sort_name asc, contact_a.id "; + $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties); + try { + $this->assertEquals($expectedSQL ,$queryObj->searchQuery(0, 0, NULL, + FALSE, FALSE, + FALSE, FALSE, + TRUE)); + } + catch (PEAR_Exception $e) { + $err = $e->getCause(); + $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo); + + } + } } diff --git a/tests/phpunit/CRM/Contact/BAO/SavedSearchDataSets/801_pre44_billing_email_smartgroup.sql b/tests/phpunit/CRM/Contact/BAO/SavedSearchDataSets/801_pre44_billing_email_smartgroup.sql new file mode 100644 index 0000000000..c41e347e6a --- /dev/null +++ b/tests/phpunit/CRM/Contact/BAO/SavedSearchDataSets/801_pre44_billing_email_smartgroup.sql @@ -0,0 +1,13 @@ +INSERT IGNORE INTO `civicrm_mapping` (`id`, `name`, `description`, `mapping_type_id`) VALUES (315, NULL, NULL, NULL); + +INSERT IGNORE INTO `civicrm_mapping_field` (`id`, `mapping_id`, `name`, `contact_type`, `column_number`, `location_type_id`, `phone_type_id`, `im_provider_id`, `relationship_type_id`, `relationship_direction`, `grouping`, `operator`, `value`, `website_type_id`) VALUES (7455, 315, 'email', 'Individual', 0, 5, NULL, NULL, NULL, NULL, 1, 'IS NOT EMPTY', '', NULL); +INSERT IGNORE INTO `civicrm_mapping_field` (`id`, `mapping_id`, `name`, `contact_type`, `column_number`, `location_type_id`, `phone_type_id`, `im_provider_id`, `relationship_type_id`, `relationship_direction`, `grouping`, `operator`, `value`, `website_type_id`) VALUES (7456, 315, 'do_not_email', 'Individual', 1, NULL, NULL, NULL, NULL, NULL, 1, '=', '0', NULL); +INSERT IGNORE INTO `civicrm_mapping_field` (`id`, `mapping_id`, `name`, `contact_type`, `column_number`, `location_type_id`, `phone_type_id`, `im_provider_id`, `relationship_type_id`, `relationship_direction`, `grouping`, `operator`, `value`, `website_type_id`) VALUES (7457, 315, 'state_province', 'Individual', 2, 5, NULL, NULL, NULL, NULL, 1, '=', 'Idaho', NULL); +INSERT IGNORE INTO `civicrm_mapping_field` (`id`, `mapping_id`, `name`, `contact_type`, `column_number`, `location_type_id`, `phone_type_id`, `im_provider_id`, `relationship_type_id`, `relationship_direction`, `grouping`, `operator`, `value`, `website_type_id`) VALUES (7458, 315, 'is_opt_out', 'Individual', 3, NULL, NULL, NULL, NULL, NULL, 1, '=', '0', NULL); +INSERT IGNORE INTO `civicrm_mapping_field` (`id`, `mapping_id`, `name`, `contact_type`, `column_number`, `location_type_id`, `phone_type_id`, `im_provider_id`, `relationship_type_id`, `relationship_direction`, `grouping`, `operator`, `value`, `website_type_id`) VALUES (7459, 315, 'on_hold', 'Individual', 4, 5, NULL, NULL, NULL, NULL, 1, '=', '0', NULL); + + +INSERT IGNORE INTO `civicrm_saved_search` (`id`, `form_values`, `mapping_id`, `search_custom_id`, `where_clause`, `select_tables`, `where_tables`) VALUES (286, 'a:8:{s:5:"qfKey";s:37:"92bf8874b4cd856436833fba5526a10f_9684";s:6:"mapper";a:2:{i:1;a:5:{i:0;a:3:{i:0;s:10:"Individual";i:1;s:5:"email";i:2;s:1:"5";}i:1;a:2:{i:0;s:10:"Individual";i:1;s:12:"do_not_email";}i:2;a:3:{i:0;s:10:"Individual";i:1;s:14:"state_province";i:2;s:1:"5";}i:3;a:2:{i:0;s:10:"Individual";i:1;s:10:"is_opt_out";}i:4;a:3:{i:0;s:10:"Individual";i:1;s:7:"on_hold";i:2;s:1:"5";}}i:2;a:1:{i:0;a:1:{i:0;s:0:"";}}}s:8:"operator";a:2:{i:1;a:5:{i:0;s:11:"IS NOT NULL";i:1;s:1:"=";i:2;s:1:"=";i:3;s:1:"=";i:4;s:1:"=";}i:2;a:1:{i:0;s:0:"";}}s:5:"value";a:2:{i:1;a:5:{i:0;s:0:"";i:1;s:1:"0";i:2;s:5:"Idaho";i:3;s:1:"0";i:4;s:1:"0";}i:2;a:1:{i:0;s:0:"";}}s:4:"task";s:2:"13";s:8:"radio_ts";s:6:"ts_all";s:11:"uf_group_id";s:0:"";s:14:"component_mode";i:1;}', 315, NULL, ' ( ( ( civicrm_email.email IS NOT NULL ) AND contact_a.do_not_email = 0 AND civicrm_state_province.name IN (\'Idaho\') AND contact_a.is_opt_out = 0 AND LOWER(civicrm_email.on_hold) = \'0\' AND contact_a.contact_type IN (\'Individual\') ) ) ', 'a:11:{s:15:"civicrm_contact";i:1;s:15:"civicrm_address";i:1;s:22:"civicrm_state_province";i:1;s:15:"civicrm_country";i:1;s:13:"civicrm_email";i:1;s:13:"civicrm_phone";i:1;s:10:"civicrm_im";i:1;s:19:"civicrm_worldregion";i:1;s:6:"gender";i:1;s:17:"individual_prefix";i:1;s:17:"individual_suffix";i:1;}', 'a:4:{s:15:"civicrm_contact";i:1;s:15:"civicrm_address";i:1;s:22:"civicrm_state_province";i:1;s:13:"civicrm_email";i:1;}'); + +INSERT IGNORE INTO `civicrm_group` (`id`, `name`, `title`, `description`, `source`, `saved_search_id`, `is_active`, `visibility`, `where_clause`, `select_tables`, `where_tables`, `group_type`, `cache_date`, `refresh_date`, `parents`, `children`, `is_hidden`, `is_reserved`, `created_id`) VALUES (801, 'Idaho EmailContacts', 'Idaho Email Contacts', NULL, NULL, 286, 1, 'User and User Admin Only', ' ( `civicrm_group_contact_cache_801`.group_id = 801 ) ', 'a:12:{s:15:"civicrm_contact";i:1;s:15:"civicrm_address";i:1;s:22:"civicrm_state_province";i:1;s:15:"civicrm_country";i:1;s:13:"civicrm_email";i:1;s:13:"civicrm_phone";i:1;s:10:"civicrm_im";i:1;s:19:"civicrm_worldregion";i:1;s:33:"`civicrm_group_contact_cache_801`";s:136:" LEFT JOIN civicrm_group_contact_cache `civicrm_group_contact_cache_801` ON contact_a.id = `civicrm_group_contact_cache_801`.contact_id ";s:6:"gender";i:1;s:17:"individual_prefix";i:1;s:17:"individual_suffix";i:1;}', 'a:2:{s:15:"civicrm_contact";i:1;s:33:"`civicrm_group_contact_cache_801`";s:136:" LEFT JOIN civicrm_group_contact_cache `civicrm_group_contact_cache_801` ON contact_a.id = `civicrm_group_contact_cache_801`.contact_id ";}', '2', '2014-06-26 03:31:03', NULL, NULL, NULL, 0, 0, NULL); + -- 2.25.1