You are here

commerce_webform.test in Commerce Webform 7.2

Commerce Webform tests.

File

tests/commerce_webform.test
View source
<?php

/**
 * @file
 * Commerce Webform tests.
 */

/**
 * Test Commerce Webform module's features.
 */
class CommerceWebformTestCase extends CommerceBaseTestCase {

  /**
   * User with admin rights.
   */
  protected $privileged_user;

  /**
   * User with normal rights who can checkout.
   */
  protected $basic_user;

  /**
   * getInfo() returns properties that are displayed in the test selection form.
   */
  public static function getInfo() {
    return array(
      'name' => 'Commerce Webform Tests',
      'description' => 'Ensure that the commerce webform module functions as expected',
      'group' => 'Commerce Webform',
    );
  }

  /**
   * setUp() performs any pre-requisite tasks that need to happen.
   */
  public function setUp() {
    $modules = parent::setUpHelper('all');
    $modules[] = 'commerce_webform';
    parent::setUp($modules);

    // Create and log in our privileged user.
    $this->privileged_user = $this
      ->drupalCreateUser(array(
      'access content',
      'administer site configuration',
      'access administration pages',
      'access content overview',
      'bypass node access',
      'administer content types',
      'administer nodes',
      'access all webform results',
      'access own webform results',
      'edit all webform submissions',
      'delete all webform submissions',
      'access own webform submissions',
      'edit own webform submissions',
      'delete own webform submissions',
      'edit webform components',
      'alter productfield on paid webform',
      'administer checkout',
      'access checkout',
      'configure store',
      'administer commerce_order entities',
      'administer product types',
      'administer commerce_product entities',
      'create commerce_product entities',
      'edit any commerce_product entity',
      'view own commerce_product entities',
      'view any commerce_product entity',
    ));
    $this->basic_user = $this
      ->drupalCreateUser(array(
      'access content',
      'access checkout',
    ));
    cache_clear_all();
  }

  /**
   * Create a webform node.
   *
   * @return object
   */
  public function createWebformNode() {
    return $this
      ->drupalCreateNode(array(
      'title' => 'Commerce Webform Node',
      'type' => 'webform',
      'status' => '1',
    ));
  }

  /**
   * Add a product field to a webform node.
   *
   * @param $node
   * @param $label
   * @param $key
   * @param array $product_types
   * @param array $product_skus
   * @param string $default_sku
   * @param bool|FALSE $multiple
   * @param bool|FALSE $choose_quantity
   */
  public function addProductFieldToWebformNode($node, $label, $key, array $product_types = array(), $product_skus = array(), $default_sku = '', $multiple = FALSE, $choose_quantity = FALSE, $listbox = FALSE) {
    $settings = array(
      'name' => $label,
      'form_key' => $key,
      'extra[description]' => 'This is a description',
      'extra[items]' => implode(',', $product_skus),
      'extra[multiple]' => $multiple,
      'extra[choose_quantity]' => $choose_quantity,
      'value' => $default_sku,
      'extra[aslist]' => $listbox,
    );
    $this
      ->drupalPost("node/{$node->nid}/webform/components/new/productfield", $settings, 'Save component', array(
      'query' => array(
        'name' => $label,
        'required' => 1,
        'pid' => 0,
        'weight' => 0,
      ),
    ));
  }

  /**
   * @param $node
   * @param $label
   * @param $key
   * @param string $default_value
   * @param bool|FALSE $required
   * @param bool|FALSE $unique
   */
  public function addTextFieldToWebform($node, $label, $key, $default_value = '', $required = FALSE, $unique = FALSE) {
    $settings = array(
      'name' => $label,
      'form_key' => $key,
      'extra[description]' => 'This is a description',
      'value' => $default_value,
      'required' => $required,
      'extra[unique]' => $unique,
    );
    $this
      ->drupalPost("node/{$node->nid}/webform/components/new/textfield", $settings, 'Save component', array(
      'query' => array(
        'name' => $label,
        'required' => $required ? 1 : 0,
        'pid' => 0,
        'weight' => 0,
      ),
    ));
  }

  /**
   * Test the ability to create a webform and add a product to it.
   */
  public function testSingleProductNoQuantitiesCommerceWebform() {
    $product = $this
      ->createDummyProduct('SKU101', 'Special Product', 1000, 'GBP', 1, 'product');
    if (empty($product)) {
      throw new Exception('Could not make product');
    }
    $this
      ->drupalLogin($this->privileged_user);

    // Create a webform node with a product field and text field.
    $webform_node = $this
      ->createWebformNode();
    $this
      ->addProductFieldToWebformNode($webform_node, 'Product', 'product', array(), array(
      'SKU101',
    ), 'SKU101');
    $this
      ->addTextFieldToWebform($webform_node, 'Name', 'name', '', TRUE);

    // Logout and purchase a product.
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($this->basic_user);
    $settings = array(
      'submitted[name]' => 'My name',
      'submitted[product][0]' => $product->product_id,
    );
    $this
      ->drupalPost("node/{$webform_node->nid}", $settings, 'Submit');

    // Click through the cart screen.
    $this
      ->drupalPost('cart', array(), 'Checkout');

    // Click through the billing address.
    $this
      ->drupalPost('checkout/1', array(
      'customer_profile_billing[commerce_customer_address][und][0][country]' => 'AF',
      'customer_profile_billing[commerce_customer_address][und][0][name_line]' => 'Example Name',
      'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => 'Address Line 1',
      'customer_profile_billing[commerce_customer_address][und][0][premise]' => 'Address Line 2',
      'customer_profile_billing[commerce_customer_address][und][0][locality]' => 'City',
    ), 'Continue to next step');

    // Make a full example payment.
    $this
      ->drupalPost('checkout/1/review', array(
      'commerce_payment[payment_method]' => 'commerce_payment_example|commerce_payment_commerce_payment_example',
      'commerce_payment[payment_details][credit_card][number]' => '4111111111111111',
      'commerce_payment[payment_details][credit_card][exp_month]' => '12',
      'commerce_payment[payment_details][credit_card][exp_year]' => date('Y'),
    ), 'Continue to next step');

    // Logout to check its all worked.
    $this
      ->drupalLogout();
    $this
      ->drupalLogin($this->privileged_user);

    // Asset that the submission and order are linked.
    $this
      ->drupalGet('node/1/submission/1');
    $this
      ->clickLink('Paid');
    $this
      ->assertUrl('/admin/commerce/orders/1/view');
    $this
      ->assertText('Webform submission');
  }

}

Classes

Namesort descending Description
CommerceWebformTestCase Test Commerce Webform module's features.