You are here

commerce_reference_table_formatter.test in Reference Table Formatter 7

File

tests/commerce_reference_table_formatter.test
View source
<?php

class CommerceReferenceTableFormatterTest extends CommerceBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Reference Table Formatter Commerce',
      'description' => 'Test the table formatter functionality with commerce.',
      'group' => 'Reference Table Formatter',
    );
  }
  function setUp() {
    $modules = parent::setUpHelper('all');
    $modules[] = 'reference_table_formatter';
    parent::setUp($modules);

    // Create a site admin + store admin user and login.
    $this->site_admin = $this
      ->createUserWithPermissionHelper(array(
      'site admin',
      'store admin',
    ));
    $this
      ->drupalLogin($this->site_admin);

    // Create a dummy product display content type without product reference
    // fields.
    $this->display_type = $this
      ->createDummyProductDisplayContentType('product_display', FALSE);

    // Create dummy product entities.
    $this->products[1] = $this
      ->createDummyProduct('PROD-01', 'Product One', 2500);
    $this->products[2] = $this
      ->createDummyProduct('PROD-02', 'Product Two', 3500);

    // Access to the manage fields screen.
    $this
      ->drupalGet('admin/structure/types/manage/' . strtr($this->display_type->type, '_', '-') . '/fields');

    // Add a new product reference field
    $edit = array(
      'fields[_add_new_field][label]' => 'Product',
      'fields[_add_new_field][field_name]' => 'product',
      'fields[_add_new_field][type]' => 'commerce_product_reference',
      'fields[_add_new_field][widget_type]' => 'options_select',
    );
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->drupalPost(NULL, array(), t('Save field settings'));

    // Save the default field instance settings.
    $this
      ->drupalPost(NULL, array(
      'field[cardinality]' => FIELD_CARDINALITY_UNLIMITED,
    ), t('Save settings'));

    // Clear field's cache.
    field_cache_clear();
    cache_clear_all();

    // Get the field information just saved.
    $this->field_name = 'field_product';
    $this->field = field_info_field($this->field_name);
    $this->field_instance = field_info_instance('node', $this->field_name, $this->display_type->type);
  }
  public function testCommerceTableFormatter() {
    $this
      ->drupalGet('admin/structure/types/manage/' . strtr($this->display_type->type, '_', '-') . '/display');
    $edit = array(
      'fields[field_product][type]' => 'reference_table_formatter',
      'refresh_rows' => 'field_product',
    );
    $this
      ->drupalPostAJAX(NULL, $edit, array(
      'op' => t('Refresh'),
    ));
    $this
      ->drupalPost(NULL, array(), t('Save'));

    // Create a prodcut display.
    $this
      ->drupalPost('node/add/' . strtr($this->display_type->type, '_', '-'), array(
      'title' => 'A Product Display Node',
      'field_product[und][]' => array(
        $this->products[1]->product_id,
        $this->products[2]->product_id,
      ),
    ), 'Save');
    $this
      ->assertRaw('<th>Product ID</th><th>SKU</th><th>Title</th><th>Price</th>');
    $this
      ->assertRaw('<td>1</td><td>PROD-01</td><td>Product One</td><td><div class="field field-name-commerce-price field-type-commerce-price field-label-hidden"><div class="field-items"><div class="field-item even">$25.00</div></div></div></td>');
    $this
      ->assertRaw('<td>2</td><td>PROD-02</td><td>Product Two</td><td><div class="field field-name-commerce-price field-type-commerce-price field-label-hidden"><div class="field-items"><div class="field-item even">$35.00</div></div></div></td>');
  }

}