You are here

public function OrderAdminTest::testEditOrder in Commerce Core 8.2

Tests editing an order.

File

modules/order/tests/src/FunctionalJavascript/OrderAdminTest.php, line 186

Class

OrderAdminTest
Tests the order admin UI.

Namespace

Drupal\Tests\commerce_order\FunctionalJavascript

Code

public function testEditOrder() {
  $address = [
    'country_code' => 'US',
    'postal_code' => '53177',
    'locality' => 'Milwaukee',
    'address_line1' => 'Pabst Blue Ribbon Dr',
    'administrative_area' => 'WI',
    'given_name' => 'Frederick',
    'family_name' => 'Pabst',
  ];
  $profile = Profile::create([
    'type' => 'customer',
    'uid' => 0,
    'address' => $address,
  ]);
  $profile
    ->save();
  $order_item = OrderItem::create([
    'type' => 'default',
    'unit_price' => [
      'number' => '999',
      'currency_code' => 'USD',
    ],
  ]);
  $order_item
    ->save();
  $order = Order::create([
    'type' => 'default',
    'store_id' => $this->store,
    'uid' => $this->adminUser,
    'billing_profile' => $profile,
    'order_items' => [
      $order_item,
    ],
    'state' => 'completed',
  ]);
  $order
    ->save();
  $adjustments = [];
  $adjustments[] = new Adjustment([
    'type' => 'custom',
    'label' => '10% off',
    'amount' => new Price('-1.00', 'USD'),
    'percentage' => '0.1',
  ]);
  $adjustments[] = new Adjustment([
    'type' => 'custom',
    'label' => 'Handling fee',
    'amount' => new Price('10.00', 'USD'),
  ]);
  $order
    ->addAdjustment($adjustments[0]);
  $order
    ->addAdjustment($adjustments[1]);
  $order
    ->save();
  $this
    ->drupalGet($order
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->buttonNotExists('hide_profile_form');
  $this
    ->assertSession()
    ->fieldValueEquals('adjustments[0][definition][label]', '10% off');
  $this
    ->assertSession()
    ->fieldValueEquals('adjustments[1][definition][label]', 'Handling fee');
  $this
    ->assertSession()
    ->optionExists('adjustments[2][type]', 'Custom');
  $this
    ->assertSession()
    ->optionNotExists('adjustments[2][type]', 'Test order adjustment type');
  $this
    ->assertRenderedAddress($address, 'billing_profile[0][profile]');

  // Select the default profile instead.
  $this
    ->getSession()
    ->getPage()
    ->fillField('billing_profile[0][profile][select_address]', $this->defaultProfile
    ->id());
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->assertRenderedAddress($this->defaultAddress, 'billing_profile[0][profile]');

  // Edit the default profile and change the street.
  $this
    ->getSession()
    ->getPage()
    ->pressButton('billing_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  foreach ($this->defaultAddress as $property => $value) {
    $prefix = 'billing_profile[0][profile][address][0][address]';
    $this
      ->assertSession()
      ->fieldValueEquals($prefix . '[' . $property . ']', $value);
  }

  // The copy checkbox should be hidden and checked.
  $this
    ->assertSession()
    ->fieldNotExists('billing_profile[0][profile][copy_to_address_book]');
  $this
    ->submitForm([
    'billing_profile[0][profile][address][0][address][address_line1]' => '10 Drupal Ave',
  ], 'Save');

  /** @var \Drupal\profile\Entity\ProfileInterface $profile */
  $profile = $this
    ->reloadEntity($profile);
  $this->defaultProfile = $this
    ->reloadEntity($this->defaultProfile);
  $expected_address = [
    'address_line1' => '10 Drupal Ave',
  ] + $this->defaultAddress;
  $this
    ->assertEquals($expected_address, array_filter($profile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertEquals($expected_address, array_filter($this->defaultProfile
    ->get('address')
    ->first()
    ->toArray()));
  $this
    ->assertEquals($this->defaultProfile
    ->id(), $profile
    ->getData('address_book_profile_id'));
}