/** * Checks the right calls are made by the getMCInterestGroupings. * * This is a dependency of some other tests because it also caches the result, * which means that we don't have to duplicate prophecies for this behaviour * in other tests. * * @group interests */ public function testGetMCInterestGroupings() { // Get Mock API. $api_prophecy = $this->prophesize('CRM_Mailchimp_Api3'); CRM_Mailchimp_Utils::setMailchimpApi($api_prophecy->reveal()); // Creating a sync object requires some calls to Mailchimp's API to find out // details about the list and interest groupings. These are cached during // runtime. $api_prophecy->get("/lists/dummylistid/interest-categories", Argument::any())->shouldBeCalled()->willReturn(json_decode('{"http_code":200,"data":{"categories":[{"id":"categoryid","title":"' . static::MC_INTEREST_CATEGORY_TITLE . '"}]}}')); $api_prophecy->get("/lists/dummylistid/interest-categories/categoryid/interests", Argument::any())->shouldBeCalled()->willReturn(json_decode('{"http_code":200,"data":{"interests":[{"id":"interestId1","name":"' . static::MC_INTEREST_NAME_1 . '"},{"id":"interestId2","name":"' . static::MC_INTEREST_NAME_2 . '"}]}}')); $interests = CRM_Mailchimp_Utils::getMCInterestGroupings('dummylistid'); $this->assertEquals(['categoryid' => ['id' => 'categoryid', 'name' => static::MC_INTEREST_CATEGORY_TITLE, 'interests' => ['interestId1' => ['id' => 'interestId1', 'name' => static::MC_INTEREST_NAME_1], 'interestId2' => ['id' => 'interestId2', 'name' => static::MC_INTEREST_NAME_2]]]], $interests); // Also ensure we have this in cache: $api_prophecy->get("/lists", Argument::any())->shouldBeCalled()->willReturn(json_decode('{"http_code":200,"data":{"lists":[{"id":"dummylistid","name":"' . static::MC_TEST_LIST_NAME . '"}]}}')); CRM_Mailchimp_Utils::getMCListName('dummylistid'); }
/** * Look up an array of CiviCRM groups linked to Maichimp groupings. * * * * @param $groupIDs mixed array of CiviCRM group Ids to fetch data for; or empty to return ALL mapped groups. * @param $mc_list_id mixed Fetch for a specific Mailchimp list only, or null. * @param $membership_only bool. Only fetch mapped membership groups (i.e. NOT linked to a MC grouping). * @return array keyed by CiviCRM group id whose values are arrays of details * including: * // Details about Mailchimp * 'list_id' * 'list_name' * 'category_id' * 'category_name' * 'interest_id' * 'interest_name' * // Details from CiviCRM * 'civigroup_title' * 'civigroup_uses_cache' * 'is_mc_update_grouping' bool: is the subscriber allowed to update this * via MC interface? * // Deprecated DO NOT USE from Mailchimp. * 'grouping_id' * 'grouping_name' * 'group_id' * 'group_name' */ public static function getGroupsToSync($groupIDs = array(), $mc_list_id = null, $membership_only = FALSE) { $params = $groups = $temp = array(); $groupIDs = array_filter(array_map('intval', $groupIDs)); if (!empty($groupIDs)) { $groupIDs = implode(',', $groupIDs); $whereClause = "entity_id IN ({$groupIDs})"; } else { $whereClause = "1 = 1"; } $whereClause .= " AND mc_list_id IS NOT NULL AND mc_list_id <> ''"; if ($mc_list_id) { // just want results for a particular MC list. $whereClause .= " AND mc_list_id = %1 "; $params[1] = array($mc_list_id, 'String'); } if ($membership_only) { $whereClause .= " AND (mc_grouping_id IS NULL OR mc_grouping_id = '')"; } $query = "\n SELECT entity_id, mc_list_id, mc_grouping_id, mc_group_id, is_mc_update_grouping, cg.title as civigroup_title, cg.saved_search_id, cg.children\n FROM civicrm_value_mailchimp_settings mcs\n INNER JOIN civicrm_group cg ON mcs.entity_id = cg.id\n WHERE {$whereClause}"; $dao = CRM_Core_DAO::executeQuery($query, $params); while ($dao->fetch()) { $list_name = CRM_Mailchimp_Utils::getMCListName($dao->mc_list_id); $interest_name = CRM_Mailchimp_Utils::getMCInterestName($dao->mc_list_id, $dao->mc_grouping_id, $dao->mc_group_id); $category_name = CRM_Mailchimp_Utils::getMCCategoryName($dao->mc_list_id, $dao->mc_grouping_id); $groups[$dao->entity_id] = array('list_id' => $dao->mc_list_id, 'list_name' => $list_name, 'category_id' => $dao->mc_grouping_id, 'category_name' => $category_name, 'interest_id' => $dao->mc_group_id, 'interest_name' => $interest_name, 'is_mc_update_grouping' => $dao->is_mc_update_grouping, 'civigroup_title' => $dao->civigroup_title, 'civigroup_uses_cache' => (bool) ($dao->saved_search_id > 0 || (bool) $dao->children), 'grouping_id' => $dao->mc_grouping_id, 'grouping_name' => $category_name, 'group_id' => $dao->mc_group_id, 'group_name' => $interest_name); } CRM_Mailchimp_Utils::checkDebug('End-CRM_Mailchimp_Utils getGroupsToSync $groups', $groups); return $groups; }