<commit_before><?php

require_once 'CiviTest/CiviUnitTestCase.php';

/**
 * Tests for Mailchimp Webhooks.
 */
class CRM_CiviMailchimp_Page_WebhookTest extends CiviUnitTestCase {
  function setUp() {
    // If your test manipulates any SQL tables, then you should truncate
    // them to ensure a consisting starting point for all tests
    // $this->quickCleanup(array('example_table_name'));
    parent::setUp();
  }

  function tearDown() {
    parent::tearDown();
  }

  static function sampleRequestSubscribeOrProfileUpdate() {
    $rand = rand();
    return array(
      'email' => "<EMAIL>+{<EMAIL>",
      'merges' => array(
        'EMAIL' => "<EMAIL>+{<EMAIL>",
        'FNAME' => "Civi{$rand}",
        'LNAME' => "Mailchimp{$rand}",
      ),
      'list_id' => 'MailchimpListsTestListA',
    );
  }

  static function sampleRequestUnsubscribeOrCleaned() {
    $rand = rand();
    return array(
      'email' => "<EMAIL>+{<EMAIL>",
      'list_id' => 'MailchimpListsTestListA',
    );
  }
  
  static function sampleRequestUpemail() {
    $rand = rand();
    return array(
      'new_email' => "<EMAIL>+{<EMAIL>",
      'old_email' => "<EMAIL>+{<EMAIL>",
      'list_id' => 'MailchimpListsTestListA',
    );
  }
}<commit_msg>Add Webhook tests for unsubscribe and subscribe.<commit_after><?php

require_once 'CiviTest/CiviUnitTestCase.php';

/**
 * Tests for Mailchimp Webhooks.
 */
class CRM_CiviMailchimp_Page_WebhookTest extends CiviUnitTestCase {

  static function setUpBeforeClass() {
    // Use the Mailchimp API test class for all the tests.
    CRM_Core_BAO_Setting::setItem('CRM_MailchimpMock', 'CiviMailchimp Preferences', 'mailchimp_api_class');
  }

  function setUp() {
    // If your test manipulates any SQL tables, then you should truncate
    // them to ensure a consisting starting point for all tests
    // $this->quickCleanup(array('example_table_name'));
    parent::setUp();
  }

  function tearDown() {
    parent::tearDown();
  }

  function testMailchimpWebhookSubscribe() {
    $sync_settings = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('test_group_mailchimp_webhook_subscribe');
    $sample_data = self::sampleRequestSubscribeOrProfileUpdate();
    CRM_CiviMailchimp_Page_Webhook::mailchimpWebhookSubscribe($sample_data);
    $new_contact = CRM_Contact_BAO_Contact::matchContactOnEmail($sample_data['email']);
    $this->assertTrue(CRM_Contact_BAO_GroupContact::isContactInGroup($new_contact->contact_id, $sync_settings->civicrm_group_id));

    $sample_data = self::sampleRequestSubscribeOrProfileUpdate();
    $initial_contact = self::addContactFromSampleData($sample_data);
    CRM_CiviMailchimp_Page_Webhook::mailchimpWebhookSubscribe($sample_data);
    $this->assertTrue(CRM_Contact_BAO_GroupContact::isContactInGroup($initial_contact->id, $sync_settings->civicrm_group_id));
  }

  function testMailchimpWebhookUnsubscribe() {
    $sync_settings = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('test_group_mailchimp_webhook_unsubscribe');
    $sample_data = self::sampleRequestSubscribeOrProfileUpdate();
    $contact = self::addContactFromSampleData($sample_data);
    CRM_Contact_BAO_GroupContact::addContactsToGroup(array($contact->id, ), $sync_settings->civicrm_group_id);
    CRM_CiviMailchimp_Page_Webhook::mailchimpWebhookUnsubscribe($sample_data);
    $this->assertFalse(CRM_Contact_BAO_GroupContact::isContactInGroup($contact->id, $sync_settings->civicrm_group_id));
  }

  static function addContactFromSampleData($sample_data) {
    $params = array(
      'first_name' => $sample_data['merges']['FNAME'],
      'last_name' => $sample_data['merges']['LNAME'],
      'contact_type' => 'Individual',
      'do_not_email' => FALSE,
    );
    $contact = CRM_Contact_BAO_Contact::add($params);
    $primary_email = new CRM_Core_BAO_Email();
    $primary_email->contact_id = $contact->id;
    $primary_email->email = $sample_data['email'];
    $primary_email->is_primary = TRUE;
    $primary_email->save();
    return $contact;
  }

  static function sampleRequestSubscribeOrProfileUpdate() {
    $rand = rand();
    return array(
      'email' => "<EMAIL>+{<EMAIL>",
      'merges' => array(
        'EMAIL' => "<EMAIL>+{<EMAIL>",
        'FNAME' => "Civi{$rand}",
        'LNAME' => "Mailchimp{$rand}",
      ),
      'list_id' => 'MailchimpListsTestListA',
    );
  }

  static function sampleRequestUnsubscribeOrCleaned() {
    $rand = rand();
    return array(
      'email' => "<EMAIL>+{<EMAIL>",
      'list_id' => 'MailchimpListsTestListA',
    );
  }

  static function sampleRequestUpemail() {
    $rand = rand();
    return array(
      'new_email' => "<EMAIL>+{<EMAIL>",
      'old_email' => "<EMAIL>+{<EMAIL>",
      'list_id' => 'MailchimpListsTestListA',
    );
  }
}
{'fraction_non_alphanumeric': 0.14211076280041798, 'fraction_numerical': 0.0, 'mean_word_length': 4.532947976878613, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2008763', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1574, 'n_words': 245}