/**
  * Tests widget's hidden input type.
  */
 function testWidget()
 {
     $form_url = 'product/' . $this->product->id() . '/edit';
     // Create the first store. Since the field is required, the widget
     // should be a hidden element.
     $this->createStores(1);
     $store_id = $this->stores[0]->id();
     $this->drupalGet($form_url);
     $this->assertFieldByXpath('//input[@type="hidden" and @name="stores[target_id][value]" and @value="' . $store_id . '"]', NULL, 'Stores field is displayed as a hidden element.');
     // Create another store. The widget should now be a set of checkboxes.
     $this->createStores(1);
     $store_ids = array_map(function ($store) {
         return $store->id();
     }, $this->stores);
     $this->drupalGet($form_url);
     $this->assertTrue((bool) $this->xpath('//input[@type="checkbox" and starts-with(@name,"stores")]'), 'Stores field is displayed as a checkboxes element.');
     $this->assertNoFieldChecked('edit-stores-target-id-value-1');
     $this->assertNoFieldChecked('edit-stores-target-id-value-2');
     // Check store 1.
     $edit['stores[target_id][value][' . $store_ids[0] . ']'] = $store_ids[0];
     $edit['stores[target_id][value][' . $store_ids[1] . ']'] = FALSE;
     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
     $this->assertResponse(200);
     \Drupal::entityManager()->getStorage('commerce_product')->resetCache();
     $this->product = Product::load($this->product->id());
     $this->assertFieldValues($this->product->getStoreIds(), [$store_ids[0]], 'The correct store has been set on the product.');
     $this->drupalGet($form_url);
     $this->assertFieldChecked('edit-stores-target-id-value-' . $store_ids[0]);
     $this->assertNoFieldChecked('edit-stores-target-id-value-' . $store_ids[1]);
     // Reduce the cardinality to 1. Checkboxes should now be radios.
     $this->referenceField->setCardinality(1)->save();
     $this->drupalGet($form_url);
     $this->assertTrue((bool) $this->xpath('//input[@type="radio" and @name="stores[target_id][value]"]'), 'Stores field is displayed as a radio element.');
     $this->assertFieldChecked('edit-stores-target-id-value-' . $store_ids[0], 'Radio field for store ' . $store_ids[0] . ' is checked.');
     $this->assertNoFieldChecked('edit-stores-target-id-value-' . $store_ids[1], 'Radio field for store ' . $store_ids[1] . ' is unchecked.');
     // Create the final store. The widget should now be an autocomplete field.
     $this->createStores(1);
     $store_labels = array_map(function ($store) {
         return $store->label() . ' (' . $store->id() . ')';
     }, $this->stores);
     $this->referenceField->setCardinality(FieldStorageConfig::CARDINALITY_UNLIMITED)->save();
     $this->drupalGet($form_url);
     $this->assertTrue((bool) $this->xpath('//input[@id="edit-stores-target-id-value" and starts-with(@class, "form-autocomplete")]'), 'Stores field is displayed as an autocomplete element.');
     $this->assertFieldByName('stores[target_id][value]', $store_labels[0]);
     // Reference both stores 1 and 2.
     $edit = [];
     $edit['stores[target_id][value]'] = $store_labels[0] . ', ' . $store_labels[1];
     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
     $this->assertResponse(200);
     \Drupal::entityManager()->getStorage('commerce_product')->resetCache();
     $this->product = Product::load($this->product->id());
     $this->assertFieldValues($this->product->getStoreIds(), [$store_ids[0], $store_ids[1]], 'The correct stores have been set on the product.');
     $this->drupalGet($form_url);
     $this->assertFieldByName('stores[target_id][value]', $store_labels[0] . ', ' . $store_labels[1]);
 }
Ejemplo n.º 2
0
 /**
  * Entity builder: updates the product status with the submitted value.
  *
  * @param string $entity_type
  *   The entity type.
  * @param \Drupal\commerce_product\Entity\ProductInterface $product
  *   The product updated with the submitted values.
  * @param array $form
  *   The complete form array.
  * @param \Drupal\Core\Form\FormStateInterface $form_state
  *   The current state of the form.
  *
  * @see \Drupal\node\NodeForm::form()
  */
 function updateStatus($entity_type, ProductInterface $product, array $form, FormStateInterface $form_state) {
   $element = $form_state->getTriggeringElement();
   if (isset($element['#published_status'])) {
     $product->setPublished($element['#published_status']);
   }
 }
Ejemplo n.º 3
0
 /**
  * Posts the add to cart form for a product.
  *
  * @param \Drupal\commerce_product\Entity\ProductInterface $product
  * @param array $edit
  *
  * @throws \Exception
  */
 protected function postAddToCart(ProductInterface $product, array $edit = [])
 {
     $this->drupalGet('product/' . $product->id());
     $this->assertField('edit-submit', t('Add to cart button exists.'));
     $this->drupalPostForm(NULL, $edit, t('Add to cart'));
 }