You are here

protected function ProfileFieldCopyTest::setUp in Commerce Shipping 8.2

Overrides CommerceWebDriverTestBase::setUp

File

tests/src/FunctionalJavascript/ProfileFieldCopyTest.php, line 105

Class

ProfileFieldCopyTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

protected function setUp() : void {
  parent::setUp();
  $this->store
    ->set('billing_countries', [
    'FR',
    'US',
  ]);
  $this->store
    ->save();

  // Turn off verification via external services.
  $tax_number_field = FieldConfig::loadByName('profile', 'customer', 'tax_number');
  $tax_number_field
    ->setSetting('verify', FALSE);
  $tax_number_field
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
  $payment_gateway = PaymentGateway::create([
    'id' => 'cod',
    'label' => 'Manual',
    'plugin' => 'manual',
    'configuration' => [
      'display_label' => 'Cash on delivery',
      'instructions' => [
        'value' => 'Sample payment instructions.',
        'format' => 'plain_text',
      ],
    ],
  ]);
  $payment_gateway
    ->save();
  $variation_type = ProductVariationType::load('default');
  $variation_type
    ->setTraits([
    'purchasable_entity_shippable',
  ]);
  $variation_type
    ->save();
  $order_type = OrderType::load('default');
  $order_type
    ->setThirdPartySetting('commerce_checkout', 'checkout_flow', 'shipping');
  $order_type
    ->setThirdPartySetting('commerce_shipping', 'shipment_type', 'default');
  $order_type
    ->save();

  // Create the order field.
  $field_definition = commerce_shipping_build_shipment_field_definition($order_type
    ->id());
  $this->container
    ->get('commerce.configurable_field_manager')
    ->createField($field_definition);

  // Install the variation trait.
  $trait_manager = $this->container
    ->get('plugin.manager.commerce_entity_trait');
  $trait = $trait_manager
    ->createInstance('purchasable_entity_shippable');
  $trait_manager
    ->installTrait($trait, 'commerce_product_variation', 'default');

  // Create a non-shippable product/variation type set.
  $variation_type = ProductVariationType::create([
    'id' => 'digital',
    'label' => 'Digital',
    'orderItemType' => 'default',
    'generateTitle' => TRUE,
  ]);
  $variation_type
    ->save();
  $product_type = ProductType::create([
    'id' => 'Digital',
    'label' => 'Digital',
    'variationType' => $variation_type
      ->id(),
  ]);
  $product_type
    ->save();

  // Create two products. One shippable, one non-shippable.
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => '7.99',
      'currency_code' => 'USD',
    ],
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $this->firstProduct = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'Conference hat',
    'variations' => [
      $variation,
    ],
    'stores' => [
      $this->store,
    ],
  ]);
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'digital',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => '8.99',
      'currency_code' => 'USD',
    ],
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $this->secondProduct = $this
    ->createEntity('commerce_product', [
    'type' => 'digital',
    'title' => 'Conference ticket',
    'variations' => [
      $variation,
    ],
    'stores' => [
      $this->store,
    ],
  ]);
  $order_item = $this
    ->createEntity('commerce_order_item', [
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'quantity' => 1,
    'unit_price' => new Price('7.99', 'USD'),
    'purchased_entity' => $this->firstProduct
      ->getDefaultVariation(),
  ]);
  $order_item
    ->save();

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $this->order = $this
    ->createEntity('commerce_order', [
    'type' => 'default',
    'order_number' => '2020/01',
    'store_id' => $this->store,
    'uid' => $this->adminUser
      ->id(),
    'order_items' => [
      $order_item,
    ],
    'state' => 'draft',
    'payment_gateway' => $payment_gateway
      ->id(),
  ]);

  /** @var \Drupal\commerce_shipping\Entity\PackageType $package_type */
  $package_type = $this
    ->createEntity('commerce_package_type', [
    'id' => 'package_type_a',
    'label' => 'Package Type A',
    'dimensions' => [
      'length' => '20',
      'width' => '20',
      'height' => '20',
      'unit' => 'mm',
    ],
    'weight' => [
      'number' => '20',
      'unit' => 'g',
    ],
  ]);
  $this->container
    ->get('plugin.manager.commerce_package_type')
    ->clearCachedDefinitions();
  $shipping_method = $this
    ->createEntity('commerce_shipping_method', [
    'name' => 'Standard shipping',
    'stores' => [
      $this->store
        ->id(),
    ],
    // Ensure that Standard shipping shows before overnight shipping.
    'weight' => -10,
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Standard shipping',
        'rate_amount' => [
          'number' => '9.99',
          'currency_code' => 'USD',
        ],
      ],
    ],
  ]);
  $this->shippingOrderManager = $this->container
    ->get('commerce_shipping.order_manager');
  $checkout_flow = CheckoutFlow::load('shipping');
  $checkout_flow_configuration = $checkout_flow
    ->get('configuration');
  $checkout_flow_configuration['panes']['shipping_information']['auto_recalculate'] = FALSE;
  $checkout_flow
    ->set('configuration', $checkout_flow_configuration);
  $checkout_flow
    ->save();
}