You are here

protected function ShipmentAdminTest::setUp in Commerce Shipping 8.2

Overrides CommerceWebDriverTestBase::setUp

File

tests/src/FunctionalJavascript/ShipmentAdminTest.php, line 92

Class

ShipmentAdminTest
Tests the shipment admin UI.

Namespace

Drupal\Tests\commerce_shipping\FunctionalJavascript

Code

protected function setUp() : void {
  parent::setUp();
  $product_variation_type = ProductVariationType::load('default');
  $product_variation_type
    ->setTraits([
    'purchasable_entity_shippable',
  ]);
  $product_variation_type
    ->setGenerateTitle(FALSE);
  $product_variation_type
    ->save();
  $order_type = OrderType::load('default');
  $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());
  \Drupal::service('commerce.configurable_field_manager')
    ->createField($field_definition);

  // Install the variation trait.
  $trait_manager = \Drupal::service('plugin.manager.commerce_entity_trait');
  $trait = $trait_manager
    ->createInstance('purchasable_entity_shippable');
  $trait_manager
    ->installTrait($trait, 'commerce_product_variation', 'default');
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'title' => $this
      ->randomMachineName(),
    'type' => 'default',
    'sku' => 'sku-' . $this
      ->randomMachineName(),
    'price' => [
      'number' => '7.99',
      'currency_code' => 'USD',
    ],
  ]);
  $order_item = $this
    ->createEntity('commerce_order_item', [
    'title' => $this
      ->randomMachineName(),
    'type' => 'default',
    'quantity' => 1,
    'unit_price' => new Price('10', 'USD'),
    'purchased_entity' => $variation,
  ]);
  $order_item
    ->save();
  $this->order = $this
    ->createEntity('commerce_order', [
    'uid' => $this->loggedInUser
      ->id(),
    'order_number' => '6',
    'type' => 'default',
    'state' => 'completed',
    'order_items' => [
      $order_item,
    ],
    'store_id' => $this->store,
    'mail' => $this->loggedInUser
      ->getEmail(),
  ]);
  $this->shipmentUri = Url::fromRoute('entity.commerce_shipment.collection', [
    'commerce_order' => $this->order
      ->id(),
  ])
    ->toString();

  /** @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',
    ],
  ]);
  \Drupal::service('plugin.manager.commerce_package_type')
    ->clearCachedDefinitions();
  $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
    ->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',
        ],
      ],
    ],
  ]);

  // 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();
  $shipment_type = ShipmentType::load('default');
  $shipment_type
    ->setProfileTypeId('customer_shipping');
  $shipment_type
    ->save();
  $this->defaultProfile = Profile::create([
    'type' => 'customer_shipping',
    'uid' => $this->adminUser,
    'address' => $this->defaultAddress,
    'field_phone' => '202-555-0108',
  ]);
  $this->defaultProfile
    ->save();
}