/**
 * Get Mailchimp Interests.
 *
 * Returns an array whose keys are interest hashes and whose values are
 * arrays. Nb. Mailchimp now (2016) talks "Interest Categories" which each
 * contain "Interests". It used to talk of "groupings and groups" which was much
 * more confusing!
 *
 * @param array $params
 * @return array API result descriptor
 * @see civicrm_api3_create_success
 * @see civicrm_api3_create_error
 * @throws API_Exception
 */
function civicrm_api3_mailchimp_getinterests($params)
{
    try {
        $list_id = $params['id'];
        $results = CRM_Mailchimp_Utils::getMCInterestGroupings($list_id);
    } catch (Exception $e) {
        return array();
    }
    $interests = [];
    foreach ($results as $category_id => $category_details) {
        $interests[$category_id]['id'] = $category_id;
        $interests[$category_id]['name'] = $category_details['name'];
        foreach ($category_details['interests'] as $interest_id => $interest_details) {
            $interests[$category_id]['interests'][$interest_id] = "{$category_details['name']}::{$interest_details['name']}";
        }
    }
    return civicrm_api3_create_success($interests);
}
 /**
  * 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');
 }