You are here

mailchimp_ecommerce.test in Mailchimp E-Commerce 7

Test class and methods for the Mailchimp eCommerce module.

File

tests/mailchimp_ecommerce.test
View source
<?php

/**
 * @file
 * Test class and methods for the Mailchimp eCommerce module.
 */

/**
 * Tests for Mailchimp eCommerce core integration.
 */
class MailchimpEcommerceTestCase extends DrupalWebTestCase {

  /**
   * Returns info displayed in the test interface.
   *
   * @return array
   *   Formatted as specified by simpletest.
   */
  public static function getInfo() {
    return [
      'name' => 'Mailchimp eCommerce',
      'description' => 'Test Mailchimp eCommerce core integration.',
      'group' => 'Mailchimp',
    ];
  }

  /**
   * Pre-test setup function.
   *
   * Enables dependencies.
   * Sets the mailchimp_api_key to the test-mode key.
   * Sets test mode to TRUE.
   */
  protected function setUp() {
    $this->profile = drupal_get_profile();

    // Enable modules required for the test.
    $enabled_modules = [
      'commerce',
      'entity',
      'libraries',
      'mailchimp',
      'mailchimp_ecommerce',
    ];
    parent::setUp($enabled_modules);
    variable_set('mailchimp_api_key', 'MAILCHIMP_TEST_API_KEY');
    variable_set('mailchimp_test_mode', TRUE);
    variable_set('mailchimp_ecommerce_store_id', 1);
  }

  /**
   * Post-test function.
   *
   * Sets test mode to FALSE.
   */
  protected function tearDown() {
    parent::tearDown();
    variable_del('mailchimp_test_mode');
    variable_del('mailchimp_ecommerce_store_id');
  }

  /**
   * Tests adding a store to Mailchimp.
   */
  public function testAddStore() {
    $store_id = 1;
    $store = [
      'list_id' => '57afe96172',
      'name' => 'Freddie\'s Merchandise',
      'currency_code' => 'USD',
    ];
    mailchimp_ecommerce_add_store($store_id, $store, 'Drupal Commerce');
    $saved_store = mailchimp_ecommerce_get_store($store_id);
    $this
      ->assertEqual($saved_store->id, $store_id);
    $this
      ->assertEqual($saved_store->name, $store['name']);
    $this
      ->assertEqual($saved_store->currency_code, $store['currency_code']);
  }

  /**
   * Tests adding an order to Mailchimp.
   */
  public function testAddOrder() {
    $store_id = 1;
    $order_id = 1;
    $customer = [
      'id' => 1,
      'email_address' => 'testuser@example.com',
      'first_name' => 'Test',
      'last_name' => 'User',
    ];
    $order = [
      'currency_code' => 'USD',
      'order_total' => 29.98,
      'lines' => [
        (object) [
          'id' => 1,
          'product_id' => 11,
          'product_variant_id' => 11,
          'quantity' => 1,
          'price' => 9.99,
        ],
        'lines' => [
          (object) [
            'id' => 2,
            'product_id' => 12,
            'product_variant_id' => 12,
            'quantity' => 1,
            'price' => 19.99,
          ],
        ],
      ],
    ];
    mailchimp_ecommerce_add_order($order_id, $customer, $order);

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    $saved_order = $mc_ecommerce
      ->getOrder($store_id, $order_id);
    $this
      ->assertEqual($saved_order->id, $order_id);
    $this
      ->assertEqual($saved_order->currency_code, $order['currency_code']);
    $this
      ->assertEqual($saved_order->order_total, $order['order_total']);
  }

  /**
   * Tests adding an order to Mailchimp with no line items.
   */
  public function testAddOrderNoLineItems() {
    $store_id = 1;
    $order_id = 1;
    $customer = [
      'id' => 1,
      'email_address' => 'testuser@example.com',
      'first_name' => 'Test',
      'last_name' => 'User',
    ];
    $order = [
      'currency_code' => 'USD',
      'order_total' => 19.99,
    ];
    mailchimp_ecommerce_add_order($order_id, $customer, $order);

    /* @var \Mailchimp\MailchimpEcommerce $mc_ecommerce */
    $mc_ecommerce = mailchimp_get_api_object('MailchimpEcommerce');
    $saved_order = $mc_ecommerce
      ->getOrder($store_id, $order_id);
    $this
      ->assertNull($saved_order);
  }

}

Classes

Namesort descending Description
MailchimpEcommerceTestCase Tests for Mailchimp eCommerce core integration.