You are here

protected function ShippingIntegrationTest::setUp in Commerce Currency Resolver 8

Overrides CommerceWebDriverTestBase::setUp

File

modules/shipping/tests/src/FunctionalJavascript/ShippingIntegrationTest.php, line 57

Class

ShippingIntegrationTest
Tests integration with the shipping module.

Namespace

Drupal\Tests\commerce_currency_resolver_shipping\FunctionalJavascript

Code

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

  // Add additional currency.
  // The parent has already imported USD.
  $currency_importer = $this->container
    ->get('commerce_price.currency_importer');
  $currency_importer
    ->import('HRK');

  // Create new exchange rates.
  $exchange_rates = ExchangeRates::create([
    'id' => 'testing',
    'label' => 'Manual',
    'plugin' => 'manual',
    'status' => TRUE,
    'configuration' => [
      'cron' => FALSE,
      'use_cross_sync' => FALSE,
      'demo_amount' => 100,
      'base_currency' => 'USD',
      'mode' => 'live',
    ],
  ]);
  $exchange_rates
    ->save();
  $this
    ->config($exchange_rates
    ->getExchangerConfigName())
    ->setData([
    'rates' => [
      'HRK' => [
        'USD' => [
          'value' => 0.15,
          'sync' => 0,
        ],
      ],
      'USD' => [
        'HRK' => [
          'value' => 6.85,
          'sync' => 0,
        ],
      ],
    ],
  ])
    ->save();
  $this
    ->config('commerce_currency_resolver.settings')
    ->set('currency_exchange_rates', 'testing')
    ->save();
  $this->store
    ->setDefaultCurrencyCode('USD');

  /** @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 product.
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => 'default',
    'sku' => strtolower($this
      ->randomMachineName()),
    'price' => [
      'number' => '10',
      'currency_code' => 'HRK',
    ],
    'weight' => [
      'number' => '20',
      'unit' => 'g',
    ],
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $product */
  $this->firstProduct = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'Conference hat',
    '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 a flat rate per item shipping method to make testing adjustments
  // in items easier.
  $this
    ->createEntity('commerce_shipping_method', [
    'name' => 'Flat Rate Per Item',
    'stores' => [
      $this->store
        ->id(),
    ],
    'plugin' => [
      'target_plugin_id' => 'flat_rate_per_item',
      'target_plugin_configuration' => [
        'rate_label' => 'Flat Rate Per Item',
        'rate_amount' => [
          'number' => '10.00',
          'currency_code' => 'HRK',
        ],
      ],
    ],
    'conditions' => [
      [
        'target_plugin_id' => 'shipment_weight',
        'target_plugin_configuration' => [
          'operator' => '<',
          'weight' => [
            'number' => '120',
            'unit' => 'g',
          ],
        ],
      ],
    ],
  ]);
}