You are here

public function CommerceProductReferenceSelectSynonymsWebTestCase::testWidget in Synonyms 7

Test main functionality of the widget.

Overrides AbstractSelectSynonymsWebTestCase::testWidget

File

synonyms_commerce/synonyms_commerce.test, line 369
Tests for the Synonyms Commerce module.

Class

CommerceProductReferenceSelectSynonymsWebTestCase
Test synonyms-friendly select widget for commerce product reference field.

Code

public function testWidget() {
  $name = $this
    ->randomName();
  $this
    ->drupalPost('node/add/synonyms-test-content', array(
    'title' => $name,
    $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
      $this
        ->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
      $this->terms['product2']->product_id,
      $this->terms['product3']->product_id,
      $this
        ->synonymSelectKey($this->terms['product3'], $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
    ),
  ), 'Save');
  $node = $this
    ->drupalGetNodeByTitle($name);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($this->terms['product1']->title, 'Product is saved when its synonym is submitted through synonyms friendly select for the unlimited cardinality.');
  $this
    ->assertText($this->terms['product2']->title, 'Product is saved when it is submitted through synonyms friendly select for the unlimited cardinality.');
  $this
    ->assertText($this->terms['product3']->title, 'Product is saved only once when the term and its synonym are submitted through synonyms friendly select for the unlimited cardinality.');
  $options = array();
  $options[] = array(
    'entity' => $this->terms['product3'],
    'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product3'],
    'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'selected' => TRUE,
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'selected' => TRUE,
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product3'],
    'selected' => TRUE,
  );
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $this
    ->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with field cardinality more than 1.');
  $this->reference_field['cardinality'] = 2;
  field_update_field($this->reference_field);
  $name = $this
    ->randomName();
  $this
    ->drupalPost('node/add/synonyms-test-content', array(
    'title' => $name,
    $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
      $this
        ->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
      $this->terms['product2']->product_id,
      $this->terms['product3']->product_id,
    ),
  ), 'Save');
  $this
    ->assertText('this field cannot hold more than 2 values.', 'Submitting 3 entries into a field with cardinality of 2, that refer to 3 products, results in a form error.');
  $this
    ->drupalPost('node/add/synonyms-test-content', array(
    'title' => $name,
    $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . '][]' => array(
      $this
        ->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
      $this->terms['product2']->product_id,
      $this
        ->synonymSelectKey($this->terms['product2'], $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
    ),
  ), 'Save');
  $node = $this
    ->drupalGetNodeByTitle($name);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($this->terms['product1']->title, 'Submitting 3 entries into a field with cardinality of 2, that refer to only 2 products, results in form getting submitted. Product #1 is saved.');
  $this
    ->assertText($this->terms['product2']->title, 'Product #2 is saved.');
  $this->reference_field['cardinality'] = 1;
  field_update_field($this->reference_field);

  // We need to invoke this one. Without it some caching within Entity
  // metadata wrappers interferes. See https://www.drupal.org/node/2717019 for
  // details.
  entity_flush_caches();
  $name = $this
    ->randomName();
  $this
    ->drupalPost('node/add/synonyms-test-content', array(
    'title' => $name,
    $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this
      ->synonymSelectKey($this->terms['product1'], $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value']),
  ), 'Save');
  $node = $this
    ->drupalGetNodeByTitle($name);
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertText($this->terms['product1']->title, 'Product is saved when its synonym is submitted through synonyms friendly select for the cardinality of 1.');
  $options = array();
  $options[] = array(
    'entity' => $this->terms['product3'],
    'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product3'],
    'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'selected' => TRUE,
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product3'],
  );
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $this
    ->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
  $this
    ->drupalPost('node/' . $node->nid . '/edit', array(
    $this->reference_field['field_name'] . '[' . LANGUAGE_NONE . ']' => $this->terms['product2']->product_id,
  ), 'Save');
  $this
    ->drupalGet('node/' . $node->nid);
  $this
    ->assertNoText($this->terms['product1']->title, 'After updating entity the old product is removed.');
  $this
    ->assertText($this->terms['product2']->title, 'Product is saved when it is submitted through synonyms friendly select for the cardinality of 1.');
  $options = array();
  $options[] = array(
    'entity' => $this->terms['product3'],
    'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product3'],
    'synonym' => $this->terms['product3']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product1'],
    'synonym' => $this->terms['product1']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'selected' => TRUE,
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][1]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product2'],
    'synonym' => $this->terms['product2']->{$this->fields['enabled']['field']['field_name']}[LANGUAGE_NONE][0]['value'],
  );
  $options[] = array(
    'entity' => $this->terms['product3'],
  );
  $this
    ->drupalGet('node/' . $node->nid . '/edit');
  $this
    ->assertSynonymsSelect($options, 'Default values are set correctly in the synonyms friendly select widget when working with the field cardinality of 1.');
}