Exemple #1
0
 /**
  * Tests deleting a product.
  */
 function testDeleteProduct()
 {
     $variation = $this->createEntity('commerce_product_variation', ['type' => 'default', 'sku' => strtolower($this->randomMachineName())]);
     $product = $this->createEntity('commerce_product', ['type' => 'default', 'title' => $this->randomMachineName(), 'variations' => [$variation]]);
     // Delete the product and verify deletion.
     $product->delete();
     $product_exists = (bool) Product::load($product->id());
     $variation_exists = (bool) ProductVariation::load($variation->id());
     $this->assertFalse($product_exists, 'The new product has been deleted from the database.');
     $this->assertFalse($variation_exists, 'The matching product variation has been deleted from the database.');
 }
 /**
  * Tests deleting a product via the admin.
  */
 function testDeleteProductAdmin()
 {
     $product = $this->createEntity('commerce_product', ['title' => $this->randomMachineName(), 'type' => 'default']);
     $this->drupalGet('product/' . $product->id() . '/delete');
     $this->assertText(t("Are you sure you want to delete the product @product?", ['@product' => $product->getTitle()]), "Commerce Product deletion confirmation text is showing");
     $this->assertText(t('This action cannot be undone.'), 'The product deletion confirmation form is available');
     $this->drupalPostForm(NULL, NULL, t('Delete'));
     \Drupal::service('entity_type.manager')->getStorage('commerce_product')->resetCache();
     $product_exists = (bool) Product::load($product->id());
     $this->assertFalse($product_exists, 'The new product has been deleted from the database.');
 }
 /**
  * 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]);
 }
Exemple #4
0
 public function migrateNcdProduct($import)
 {
     $query = \Drupal::entityQuery('commerce_product')->condition('field_old_id', $import->nid);
     $newId = $query->execute();
     if (!empty($newId)) {
         $newId = array_pop($newId);
         $targetEntity = Product::load($newId);
     } else {
         $targetEntity = Product::create(array('type' => 'default', 'title' => $import->title));
     }
     $targetEntity->title = $import->title ?? 'No Title';
     $targetEntity->status = $import->status ?? '0';
     $targetEntity->body = $import->body ?? '';
     dpm($import->field_products[$import->language]);
     $targetEntity->variations = $this->buildProductAssignment($import->field_products[$import->language]) ?? '';
     dpm($targetEntity->field_product_variations);
     $targetEntity->field_old_id = $import->nid ?? '';
     $targetEntity->save();
 }