/**
  * Tests creating a product via the admin.
  */
 function testAddProductAdmin()
 {
     $title = $this->randomMachineName();
     $store_ids = array_map(function ($store) {
         return $store->id();
     }, $this->stores);
     $this->drupalGet('admin/commerce/products');
     $this->clickLink('Add product');
     $product_variation_values = ['variations[form][inline_entity_form][sku][0][value]' => $this->randomMachineName(), 'variations[form][inline_entity_form][status][value]' => 1];
     $this->drupalPostForm(NULL, $product_variation_values, t('Create variation'));
     $edit = ['title[0][value]' => $title];
     foreach ($store_ids as $store_id) {
         $edit['stores[target_id][value][' . $store_id . ']'] = $store_id;
     }
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $result = \Drupal::entityQuery('commerce_product')->condition("title", $edit['title[0][value]'])->range(0, 1)->execute();
     $product_id = reset($result);
     $product = Product::load($product_id);
     $this->assertNotNull($product, 'The new product has been created in the database.');
     $this->assertText(t('The product @title has been successfully saved', ['@title' => $title]), 'Product success text is shown');
     $this->assertText($title, 'Created product name exists on this page.');
     $this->assertFieldValues($product->getStores(), $this->stores, 'Created product has the correct associated stores.');
     $this->assertFieldValues($product->getStoreIds(), $store_ids, 'Created product has the correct associated store ids.');
     // Assert that the frontend product page is displaying.
     $this->drupalGet('product/' . $product->id());
     $this->assertResponse(200);
     $this->assertText($product->getTitle(), 'Product title exists');
     // Test product variations
     $product_variation = \Drupal::entityQuery('commerce_product_variation')->condition("sku", $product_variation_values['variations[form][inline_entity_form][sku][0][value]'])->range(0, 1)->execute();
     $product_variation = ProductVariation::load(current($product_variation));
     $this->assertNotNull($product_variation, 'The new product variation has been created in the database.');
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     // Create a store.
     $values = ['name' => t('Default store'), 'uid' => 1, 'mail' => \Drupal::config('system.site')->get('mail'), 'type' => 'default', 'default_currency' => 'USD', 'address' => ['country_code' => 'GB', 'locality' => 'London', 'postal_code' => 'NW1 6XE', 'address_line1' => '221B Baker Street']];
     $this->store = Store::create($values);
     $this->store->save();
     // Set as default store.
     \Drupal::configFactory()->getEditable('commerce_store.settings')->set('default_store', $this->store->uuid())->save();
     // Create a product variation.
     $values = ['type' => 'default', 'sku' => $this->randomMachineName(), 'price' => ['amount' => 999, 'currency_code' => 'USD']];
     $this->variation = ProductVariation::create($values);
     $this->variation->save();
     // We need a product too otherwise tests complain about the missing
     // backreference.
     $this->createEntity('commerce_product', ['type' => 'default', 'title' => $this->randomMachineName(), 'stores' => [$this->store], 'variations' => [$this->variation]]);
 }
Exemplo n.º 3
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.');
 }
Exemplo n.º 4
0
 protected function setUp()
 {
     parent::setUp();
     $this->drupalPlaceBlock('local_tasks_block');
     $this->drupalPlaceBlock('local_actions_block');
     $this->drupalPlaceBlock('page_title_block');
     $this->adminUser = $this->drupalCreateUser(['administer orders', 'administer order types', 'administer line item types', 'access administration pages']);
     // Create a store
     $values = ['name' => t('Default store'), 'uid' => 1, 'mail' => \Drupal::config('system.site')->get('mail'), 'type' => 'default', 'default_currency' => 'USD', 'address' => ['country_code' => 'GB', 'locality' => 'London', 'postal_code' => 'NW1 6XE', 'address_line1' => '221B Baker Street']];
     $this->store = Store::create($values);
     $this->store->save();
     // Set as default store.
     \Drupal::configFactory()->getEditable('commerce_store.settings')->set('default_store', $this->store->uuid())->save();
     // Create a product variation.
     $values = ['type' => 'default', 'sku' => $this->randomMachineName()];
     $this->variation = ProductVariation::create($values);
     $this->variation->save();
     // We need a product too otherwise tests complain about the missing
     // backreference.
     $this->createEntity('commerce_product', ['type' => 'default', 'title' => $this->randomMachineName(), 'variations' => [$this->variation]]);
     $this->drupalLogin($this->adminUser);
 }
Exemplo n.º 5
0
 /**
  * Tests editing a product.
  */
 function testEditProduct()
 {
     $variation = $this->createEntity('commerce_product_variation', ['type' => 'default', 'sku' => strtolower($this->randomMachineName())]);
     $product = $this->createEntity('commerce_product', ['type' => 'default', 'variations' => [$variation]]);
     // Check the integrity of the edit form.
     $this->drupalGet($product->toUrl('edit-form'));
     $this->assertResponse(200, 'The product edit form can be accessed.');
     $this->assertFieldByName('title[0][value]', NULL, 'Title field is present');
     $this->assertFieldById('edit-variations-entities-0-actions-ief-entity-edit', NULL, 'The edit button for product variation is present');
     $this->drupalPostForm(NULL, [], t('Edit'));
     $this->assertFieldByName('variations[form][inline_entity_form][entities][0][form][sku][0][value]', NULL, 'SKU field is present');
     $this->assertFieldByName('variations[form][inline_entity_form][entities][0][form][price][0][amount]', NULL, 'Price field is present');
     $this->assertFieldByName('variations[form][inline_entity_form][entities][0][form][status][value]', NULL, 'Status field is present');
     $this->assertFieldsByValue(t('Update variation'), NULL, 'Update variation button is present');
     $title = $this->randomMachineName();
     $store_ids = array_map(function ($store) {
         return $store->id();
     }, $this->stores);
     $edit = ['title[0][value]' => $title];
     foreach ($store_ids as $store_id) {
         $edit['stores[target_id][value][' . $store_id . ']'] = $store_id;
     }
     $new_sku = strtolower($this->randomMachineName());
     $new_price_amount = '1.11';
     $variations_edit = ['variations[form][inline_entity_form][entities][0][form][sku][0][value]' => $new_sku, 'variations[form][inline_entity_form][entities][0][form][price][0][amount]' => $new_price_amount, 'variations[form][inline_entity_form][entities][0][form][status][value]' => 1];
     $this->drupalPostForm(NULL, $variations_edit, t('Update variation'));
     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
     \Drupal::service('entity_type.manager')->getStorage('commerce_product_variation')->resetCache([$variation->id()]);
     $variation = ProductVariation::load($variation->id());
     $this->assertEqual($variation->getSku(), $new_sku, 'The variation sku successfully updated.');
     $this->assertEqual($variation->get('price')->amount, $new_price_amount, 'The variation price successfully updated.');
     \Drupal::service('entity_type.manager')->getStorage('commerce_product')->resetCache([$product->id()]);
     $product = Product::load($product->id());
     $this->assertEqual($product->getTitle(), $title, 'The product title successfully updated.');
     $this->assertFieldValues($product->getStores(), $this->stores, 'Updated product has the correct associated stores.');
     $this->assertFieldValues($product->getStoreIds(), $store_ids, 'Updated product has the correct associated store ids.');
 }
Exemplo n.º 6
0
 public function migrateNcdProductVariation($import)
 {
     $query = \Drupal::entityQuery('commerce_product_variation')->condition('field_old_id', $import->product_id);
     $newId = $query->execute();
     if (!empty($newId)) {
         $newId = array_pop($newId);
         $targetEntity = ProductVariation::load($newId);
         // dpm($targetEntity);
     } else {
         $targetEntity = ProductVariation::create(array('type' => $import->type == 'bundle' ? 'device' : 'accessory', 'sku' => $import->sku));
     }
     $targetEntity->field_picture = $this->processFile($import->field_picture[$import->language]);
     $targetEntity->field_wiring_diagram = $this->processFile($import->field_wiring_diagram[$import->language]);
     $targetEntity->field_mechanical_drawing = $this->processFile($import->field_mechanical_drawing[$import->language]);
     $targetEntity->field_unique_datasheets = $this->processFile($import->field_unique_datasheets[$import->language]);
     $targetEntity->field_documents = $this->processFile($import->field_documents[$import->language]);
     $targetEntity->sku = $import->sku;
     $targetEntity->field_title = $import->title ?? 'No Title';
     $targetEntity->price = array('amount' => $import->commerce_price[$import->language]['0']['amount'] / 100.0 ?? '0', 'currency_code' => 'USD');
     $targetEntity->cost = array('amount' => $import->field_cost[$import->language]['0']['amount'] / 100.0 ?? '0', 'currency_code' => 'USD');
     $targetEntity->field_assigned_categories = $this->convertCategoryAssignment($import->field_assigned_categories[$import->language]);
     $targetEntity->field_at_a_glance_items = $this->constructArray($import->field_at_a_glance_items[$import->language]);
     $targetEntity->status = $import->status;
     $targetEntity->field_prod_bod = $import->field_prod_bod[$import->language]['0']['value'] ?? '';
     $targetEntity->field_ac_or_dc_dimming = $import->field_ac_or_dc_dimming[$import->language]['0']['value'] ?? '';
     $targetEntity->field_board_location = $import->field_board_location[$import->language]['0']['value'] ?? '';
     $targetEntity->field_github_repository = $import->field_github_repository[$import->language]['0']['value'] ?? '';
     $targetEntity->field_maximum_board_current_cons = $import->field_maximum_board_current_cons[$import->language]['0']['value'] ?? '';
     $targetEntity->field_maximum_potentiometer_ampe = $import->field_maximum_potentiometer_ampe[$import->language]['0']['value'] ?? '';
     $targetEntity->field_maximum_potentiometer_volt = $import->field_maximum_potentiometer_volt[$import->language]['0']['value'] ?? '';
     $targetEntity->field_maximum_voltage = $import->field_maximum_voltage[$import->language]['0']['value'] ?? '';
     $targetEntity->field_minimum_board_current_cons = $import->field_minimum_board_current_cons[$import->language]['0']['value'] ?? '';
     $targetEntity->field_minimum_voltage = $import->field_minimum_voltage[$import->language]['0']['value'] ?? '';
     $targetEntity->field_nominal_voltage = $import->field_nominal_voltage[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_adc_inputs = $import->field_number_of_adc_inputs[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_contact_closures = $import->field_number_of_contact_closures[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_current_sensors = $import->field_number_of_current_sensors[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_dacs = $import->field_number_of_dacs[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_digital_io_ports = $import->field_number_of_digital_io_ports[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_dimmers = $import->field_number_of_dimmers[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_fets = $import->field_number_of_fets[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_motor_controller = $import->field_number_of_motor_controller[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_open_collectors = $import->field_number_of_open_collectors[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_potentiometers = $import->field_number_of_potentiometers[$import->language]['0']['value'] ?? '';
     $targetEntity->field_number_of_relays = $import->field_number_of_relays[$import->language]['0']['value'] ?? '';
     $targetEntity->field_part_bins = $import->field_part_bins[$import->language]['0']['value'] ?? '';
     $targetEntity->field_filepath = $import->field_filepath[$import->language]['0']['value'] ?? '';
     $targetEntity->field_potentiometer_resistance = $import->field_potentiometer_resistance[$import->language]['0']['value'] ?? '';
     $targetEntity->field_relay_ac_amperage = $import->field_relay_ac_amperage[$import->language]['0']['value'] ?? '';
     $targetEntity->field_relay_type = $import->field_relay_type[$import->language]['0']['value'] ?? '';
     $targetEntity->field_resolution_of_adc_inputs = $import->field_resolution_of_adc_inputs[$import->language]['0']['value'] ?? '';
     $targetEntity->field_resolution_of_dac_inputs = $import->field_resolution_of_dac_inputs[$import->language]['0']['value'] ?? '';
     //    $targetEntity->field_uses_power_supply = $import->field_uses_power_supply[$import->language]['0']['value'] ?? 0;
     $targetEntity->field_wattage_per_channel = $import->field_wattage_per_channel[$import->language]['0']['value'] ?? '';
     $targetEntity->field_old_id = $import->product_id;
     $targetEntity->save();
 }
Exemplo n.º 7
0
 /**
  * Tests adding a product to the cart when there are multiple variations.
  */
 public function testMultipleVariationsMultipleAttributes()
 {
     /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
     $variation_type = ProductVariationType::load($this->variation->bundle());
     $this->createAttributeField($variation_type, 'test_size_attribute');
     /** @var \Drupal\taxonomy\TermInterface[] $size_attributes */
     $size_attributes = ['small' => $this->createAttributeOption('test_color_attribute', 'Small'), 'medium' => $this->createAttributeOption('test_color_attribute', 'Medium')];
     $this->createAttributeField($variation_type, 'test_color_attribute');
     /** @var \Drupal\taxonomy\TermInterface[] $color_attributes */
     $color_attributes = ['red' => $this->createAttributeOption('test_color_attribute', 'Red'), 'blue' => $this->createAttributeOption('test_color_attribute', 'Blue')];
     // Reload the variation since we have a new fields.
     $this->variation = ProductVariation::load($this->variation->id());
     // Get the product so we can append new variations
     $product = $this->variation->getProduct();
     /**
      * +--------------------+------------------+
      * | Variation          | Attributes       |
      * +--------------------+------------------+
      * | 1                  | Small, Red       |
      * | 2                  | Medium, Red      |
      * | 3                  | Medium, Blue     |
      * +--------------------+------------------+
      */
     // Update first variation to have the attribute's value.
     $this->variation->set('test_size_attribute', $size_attributes['small']->id());
     $this->variation->set('test_color_attribute', $color_attributes['red']->id());
     $this->variation->save();
     $variation2 = $this->createEntity('commerce_product_variation', ['type' => $variation_type->id(), 'sku' => $this->randomMachineName(), 'price' => ['amount' => 999, 'currency_code' => 'USD'], 'test_size_attribute' => $size_attributes['medium']->id(), 'test_color_attribute' => $color_attributes['red']->id()]);
     $variation2->save();
     $variation3 = $this->createEntity('commerce_product_variation', ['type' => $variation_type->id(), 'sku' => $this->randomMachineName(), 'price' => ['amount' => 999, 'currency_code' => 'USD'], 'test_size_attribute' => $size_attributes['medium']->id(), 'test_color_attribute' => $color_attributes['blue']->id()]);
     $variation3->save();
     $product->variations->appendItem($variation2);
     $product->variations->appendItem($variation3);
     $product->save();
     $this->postAddToCart($this->variation->getProduct());
     // Trigger AJAX by changing size attribute
     $this->drupalPostAjaxForm(NULL, ['purchased_entity[0][attributes][test_size_attribute]' => $size_attributes['medium']->id()], 'purchased_entity[0][attributes][test_size_attribute]');
     // Trigger AJAX by changing color attribute
     $this->drupalPostAjaxForm(NULL, ['purchased_entity[0][attributes][test_color_attribute]' => $color_attributes['blue']->id()], 'purchased_entity[0][attributes][test_color_attribute]');
     // We can't assert an option doesn't exist using AssertContentTrait, since
     // our ID is dynamic. Version of assertNoOption using data-drupal-selector.
     // @see \Drupal\simpletest\AssertContentTrait::assertNoOption
     $selects = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]', [':data_drupal_selector' => 'edit-purchased-entity-0-attributes-test-size-attribute']);
     $options = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => 'edit-purchased-entity-0-attributes-test-size-attribute', ':option' => $size_attributes['small']->id()]);
     $this->assertTrue(isset($selects[0]) && !isset($options[0]), NULL, 'Browser');
     $selects = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector and @disabled]', [':data_drupal_selector' => 'edit-purchased-entity-0-attributes-test-size-attribute']);
     $this->assertTrue(isset($selects[0]));
     // Since we do not have a Small, Blue. Should only see variation 3.
     $this->postAddToCart($product, ['purchased_entity[0][attributes][test_color_attribute]' => $color_attributes['blue']->id(), 'purchased_entity[0][attributes][test_size_attribute]' => $size_attributes['medium']->id()]);
     // Check if the quantity was increased for the existing line item.
     $this->cart = Order::load($this->cart->id());
     $line_items = $this->cart->getLineItems();
     /** @var \Drupal\commerce_order\Entity\LineItemInterface $line_item */
     $line_item = $line_items[0];
     $this->assertEqual($line_item->getTitle(), $this->variation->getLineItemTitle());
     $this->assertTrue($line_item->getQuantity() == 1, t('The product @product has been added to cart.', ['@product' => $line_item->getTitle()]));
     $line_item = $line_items[1];
     $this->assertEqual($line_item->getTitle(), $variation3->getLineItemTitle());
     $this->assertTrue($line_item->getQuantity() == 1, t('The product @product has been added to cart.', ['@product' => $line_item->getTitle()]));
 }