You are here

public function CommerceProductReferenceAdminTest::testCommerceProductReferenceTestReferenceableTypes in Commerce Core 7

Test the limit of referenceable product types.

File

modules/product_reference/tests/commerce_product_reference.test, line 217
Tests for adding and displaying product reference fields.

Class

CommerceProductReferenceAdminTest
Functional tests for the Product Reference module.

Code

public function testCommerceProductReferenceTestReferenceableTypes() {

  // Create an additional product type and a product for it.
  $this
    ->createDummyProductType('additional_type', 'Additional Type', '', '', FALSE);
  $add_product = $this
    ->createDummyProduct('ADD-01', 'Additional One', -1, 'USD', 1, 'additional_type');
  $product_title = t('@sku: @title', array(
    '@sku' => $add_product->sku,
    '@title' => $add_product->title,
  ));

  // Check if the additional type is available in the product display.
  $this
    ->drupalGet('node/add/' . strtr($this->display_type->type, '_', '-'));
  $select_options = $this
    ->xpath("//select[@id='edit-field-product-und']//option");
  $this
    ->assertTrue(in_array($product_title, $select_options), t('Additional product is available in the select'));

  // Check if the additional type is available in the product field settings.
  $this
    ->drupalGet('admin/structure/types/manage/' . strtr($this->display_type->type, '_', '-') . '/fields/' . $this->field_name);
  $this
    ->assertFieldById('edit-instance-settings-referenceable-types-additional-type', NULL, t('Additional product type is present'));

  // Select only the additional type.
  $this
    ->pass(t('Configure the display product reference field to only accept products from "Additional" type:'));
  $this
    ->drupalPost(NULL, array(
    'instance[settings][referenceable_types][additional_type]' => 1,
  ), t('Save settings'));

  // Check the saved message and the checkbox.
  $this
    ->assertRaw(t('Saved %label configuration.', array(
    '%label' => $this->field_instance['label'],
  )), t('Message of saved field displayed'));
  $this
    ->drupalGet('admin/structure/types/manage/' . strtr($this->display_type->type, '_', '-') . '/fields/' . $this->field_name);
  $this
    ->assertFieldChecked('edit-instance-settings-referenceable-types-additional-type', t('Required field is checked'));

  // Clear field's cache and reload field instance information just saved.
  field_cache_clear();
  $field_instance = field_info_instance('node', $this->field_name, $this->display_type->type);

  // Check field instance settings.
  $this
    ->assertTrue($field_instance['settings']['referenceable_types']['additional_type'] == 'additional_type', t('Product type: Additional type is referenceable'));
  $this
    ->assertTrue($field_instance['settings']['referenceable_types']['product'] == 0, t('Product type: Product is not referenceable'));

  // Check if the additional type is available in the product display.
  $this
    ->drupalGet('node/add/' . strtr($this->display_type->type, '_', '-'));
  $select_options = $this
    ->xpath("//select[@id='edit-field-product-und']//option");
  foreach ($this->products as $product) {
    $this
      ->assertFalse(in_array($product->title, $select_options), t('Product "!product_title" of regular type is not available in the product reference select', array(
      '!product_title' => $product->title,
    )));
  }
}