/** * Function to actually build the form * * @return None * @access public */ public function buildQuickForm() { $this->addFormRule(array('CRM_Mailchimp_Form_Setting', 'formRule'), $this); CRM_Core_Resources::singleton()->addStyleFile('uk.co.vedaconsulting.mailchimp', 'css/mailchimp.css'); $webhook_url = CRM_Utils_System::url('civicrm/mailchimp/webhook', 'reset=1', TRUE, NULL, FALSE, TRUE); $this->assign('webhook_url', 'Webhook URL - ' . $webhook_url); // Add the API Key Element $this->addElement('text', 'api_key', ts('API Key'), array('size' => 48)); // Add the User Security Key Element $this->addElement('text', 'security_key', ts('Security Key'), array('size' => 24)); // Add Enable or Disable Debugging $enableOptions = array(1 => ts('Yes'), 0 => ts('No')); $this->addRadio('enable_debugging', ts('Enable Debugging'), $enableOptions, NULL); // Create the Submit Button. $buttons = array(array('type' => 'submit', 'name' => ts('Save & Test'))); // Add the Buttons. $this->addButtons($buttons); try { // Initially we won't be able to do this as we don't have an API key. $api = CRM_Mailchimp_Utils::getMailchimpApi(); // Check for warnings and output them as status messages. $warnings = CRM_Mailchimp_Utils::checkGroupsConfig(); foreach ($warnings as $message) { CRM_Core_Session::setStatus($message); } } catch (Exception $e) { CRM_Core_Session::setStatus('Could not use the Mailchimp API - ' . $e->getMessage() . ' You will see this message If you have not yet configured your Mailchimp acccount.'); } }
/** * Check that list problems are spotted. * * 1. Test for missing webhooks. * 2. Test for error if the list is not found at Mailchimp. * 3. Test for network error. * * @depends testGetMCInterestGroupings */ public function testCheckGroupsConfig() { // // Test 1 // // The default mock list does not have any webhooks set. $api_prophecy = $this->prophesize('CRM_Mailchimp_Api3'); CRM_Mailchimp_Utils::setMailchimpApi($api_prophecy->reveal()); $api_prophecy->get('/lists/dummylistid/webhooks'); $groups = CRM_Mailchimp_Utils::getGroupsToSync([static::$civicrm_group_id_membership]); $warnings = CRM_Mailchimp_Utils::checkGroupsConfig($groups); $this->assertEquals(1, count($warnings)); $this->assertContains(ts('Need to create a webhook'), $warnings[0]); // // Test 2 // $api_prophecy = $this->prophesize('CRM_Mailchimp_Api3'); CRM_Mailchimp_Utils::setMailchimpApi($api_prophecy->reveal()); $api_prophecy->get('/lists/dummylistid/webhooks')->will(function ($args) { // Need to mock a 404 response. $this->response = (object) ['http_code' => 404, 'data' => []]; $this->request = (object) ['method' => 'GET']; throw new CRM_Mailchimp_RequestErrorException($this->reveal(), "Not found"); }); $groups = CRM_Mailchimp_Utils::getGroupsToSync([static::$civicrm_group_id_membership]); $warnings = CRM_Mailchimp_Utils::checkGroupsConfig($groups); $this->assertEquals(1, count($warnings)); $this->assertContains(ts('The Mailchimp list that this once worked with has been deleted'), $warnings[0]); // // Test 3 // $api_prophecy = $this->prophesize('CRM_Mailchimp_Api3'); CRM_Mailchimp_Utils::setMailchimpApi($api_prophecy->reveal()); $api_prophecy->get('/lists/dummylistid/webhooks')->will(function ($args) { // Need to mock a network error $this->response = (object) ['http_code' => 500, 'data' => []]; throw new CRM_Mailchimp_NetworkErrorException($this->reveal(), "Someone unplugged internet"); }); $groups = CRM_Mailchimp_Utils::getGroupsToSync([static::$civicrm_group_id_membership]); $warnings = CRM_Mailchimp_Utils::checkGroupsConfig($groups); $this->assertEquals(1, count($warnings)); $this->assertContains(ts('Problems (possibly temporary)'), $warnings[0]); $this->assertContains(ts('Someone unplugged internet'), $warnings[0]); // We did not change anything on the fixture. static::$fixture_should_be_reset = FALSE; }