You are here

protected function CheckoutPaneTest::setUp in Commerce Shipping 8.2

Overrides CommerceWebDriverTestBase::setUp

File

tests/src/FunctionalJavascript/CheckoutPaneTest.php, line 98

Class

CheckoutPaneTest
Tests the "Shipping information" checkout pane.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

protected function setUp() : void {
  parent::setUp();

  // Limit the available countries.
  $this->store->shipping_countries = [
    'US',
    'FR',
    'DE',
  ];
  $this->store
    ->save();

  /** @var \Drupal\commerce_payment\Entity\PaymentGateway $gateway */
  $gateway = PaymentGateway::create([
    'id' => 'example_onsite',
    'label' => 'Example',
    'plugin' => 'example_onsite',
  ]);
  $gateway
    ->getPlugin()
    ->setConfiguration([
    'api_key' => '2342fewfsfs',
    'payment_method_types' => [
      'credit_card',
    ],
  ]);
  $gateway
    ->save();
  $product_variation_type = ProductVariationType::load('default');
  $product_variation_type
    ->setTraits([
    'purchasable_entity_shippable',
  ]);
  $product_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 two products.
  $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' => 'default',
    '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' => 'default',
    'title' => 'Conference bow tie',
    'variations' => [
      $variation,
    ],
    'stores' => [
      $this->store,
    ],
  ]);

  /** @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();

  // Create two flat rate shipping methods.
  $this->firstShippingMethod = $this
    ->createEntity('commerce_shipping_method', [
    'name' => 'Overnight shipping',
    'stores' => [
      $this->store
        ->id(),
    ],
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'default_package_type' => 'commerce_package_type:' . $package_type
          ->get('uuid'),
        'rate_label' => 'Overnight shipping',
        'rate_description' => 'At your door tomorrow morning',
        'rate_amount' => [
          'number' => '19.99',
          'currency_code' => 'USD',
        ],
      ],
    ],
  ]);
  $this->secondShippingMethod = $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',
        ],
      ],
    ],
  ]);
  $second_store = $this
    ->createStore();

  // Should never be shown cause it doesn't belong to the order's store.
  $third_shipping_method = $this
    ->createEntity('commerce_shipping_method', [
    'name' => 'Secret shipping',
    'stores' => [
      $second_store
        ->id(),
    ],
    'plugin' => [
      'target_plugin_id' => 'flat_rate',
      'target_plugin_configuration' => [
        'rate_label' => 'Secret shipping',
        'rate_amount' => [
          'number' => '9.99',
          'currency_code' => 'USD',
        ],
      ],
    ],
  ]);
  $promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      'default',
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'shipment_fixed_amount_off',
      'target_plugin_configuration' => [
        'display_inclusive' => TRUE,
        'filter' => 'include',
        'shipping_methods' => [
          [
            'shipping_method' => $this->firstShippingMethod
              ->uuid(),
          ],
        ],
        'amount' => [
          'number' => '3.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $promotion
    ->save();

  // Create a different shipping profile type, which also has a Phone field.
  $bundle_entity_duplicator = $this->container
    ->get('entity.bundle_entity_duplicator');
  $customer_profile_type = ProfileType::load('customer');
  $shipping_profile_type = $bundle_entity_duplicator
    ->duplicate($customer_profile_type, [
    'id' => 'customer_shipping',
    'label' => 'Customer (Shipping)',
  ]);

  // Add a telephone field to the new profile type.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'field_phone',
    'entity_type' => 'profile',
    'type' => 'telephone',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $shipping_profile_type
      ->id(),
    'label' => 'Phone',
  ]);
  $field
    ->save();
  $form_display = commerce_get_entity_display('profile', 'customer_shipping', 'form');
  $form_display
    ->setComponent('field_phone', [
    'type' => 'telephone_default',
  ]);
  $form_display
    ->save();
  $view_display = commerce_get_entity_display('profile', 'customer_shipping', 'view');
  $view_display
    ->setComponent('field_phone', [
    'type' => 'basic_string',
  ]);
  $view_display
    ->save();
  $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();
}