public function ShipmentAdminTest::testShipmentEdit in Commerce Shipping 8.2
Tests editing a shipment.
File
- tests/
src/ FunctionalJavascript/ ShipmentAdminTest.php, line 363
Class
- ShipmentAdminTest
- Tests the shipment admin UI.
Namespace
Drupal\Tests\commerce_shipping\FunctionalJavascriptCode
public function testShipmentEdit() {
$shipping_method = $this
->createEntity('commerce_shipping_method', [
'name' => 'The best shipping',
'stores' => [
$this->store
->id(),
],
'plugin' => [
'target_plugin_id' => 'dynamic',
'target_plugin_configuration' => [
'rate_label' => 'The best shipping',
'rate_amount' => [
'number' => '7.99',
'currency_code' => 'USD',
],
],
],
]);
$this
->createEntity('commerce_shipping_method', [
'name' => 'Wisconsin Express',
'stores' => [
$this->store
->id(),
],
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Wisconsin Express',
'rate_amount' => [
'number' => '2.99',
'currency_code' => 'USD',
],
],
],
'conditions' => [
[
'target_plugin_id' => 'shipment_address',
'target_plugin_configuration' => [
'zone' => [
'territories' => [
[
'country_code' => 'US',
'administrative_area' => 'WI',
],
],
],
],
],
],
]);
$this
->createEntity('commerce_shipping_method', [
'name' => 'Carolina Special',
'stores' => [
$this->store
->id(),
],
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Carolina Special',
'rate_amount' => [
'number' => '2.99',
'currency_code' => 'USD',
],
],
],
'conditions' => [
[
'target_plugin_id' => 'shipment_address',
'target_plugin_configuration' => [
'zone' => [
'territories' => [
[
'country_code' => 'US',
'administrative_area' => 'SC',
],
],
],
],
],
],
]);
$address = [
'country_code' => 'US',
'postal_code' => '53177',
'locality' => 'Milwaukee',
'address_line1' => 'Pabst Blue Ribbon Dr',
'administrative_area' => 'WI',
'given_name' => 'Frederick',
'family_name' => 'Pabst',
];
$shipping_profile = Profile::create([
'type' => 'customer_shipping',
'uid' => 0,
'address' => $address,
]);
$shipping_profile
->save();
/** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
$shipment = $this
->createEntity('commerce_shipment', [
'type' => 'default',
'title' => 'Test shipment',
'order_id' => $this->order
->id(),
'amount' => new Price('10', 'USD'),
'items' => [
new ShipmentItem([
'order_item_id' => 1,
'title' => 'Test shipment item label',
'quantity' => 1,
'weight' => new Weight(0, 'g'),
'declared_value' => new Price('1', 'USD'),
]),
],
'shipping_profile' => $shipping_profile,
]);
/** @var \Drupal\commerce_shipping_test\Plugin\Commerce\ShippingMethod\DynamicRate $shipping_method_plugin */
$shipping_method_plugin = \Drupal::service('plugin.manager.commerce_shipping_method')
->createInstance('dynamic');
$shipping_services = $shipping_method_plugin
->getServices();
$shipment
->setData('owned_by_packer', TRUE)
->setShippingMethod($shipping_method)
->setShippingService(key($shipping_services))
->save();
$this
->assertEquals(new Price('10', 'USD'), $shipment
->getAmount());
// Edit the shipment.
$this
->assertSession()
->linkExists('Edit');
$this
->drupalGet($shipment
->toUrl('edit-form'));
$this
->assertSession()
->fieldValueEquals('title[0][value]', $shipment
->label());
$shipment_item_title = $shipment
->getItems()[0]
->getTitle();
$this
->assertSession()
->fieldExists($shipment_item_title);
// Assert shipping rates available on load and not lost when recalculated.
$this
->assertNotEmpty($this
->getSession()
->getPage()
->findField('The best shipping: $7.99')
->getAttribute('checked'));
$this
->assertSession()
->fieldExists('Overnight shipping: $19.99');
$this
->assertSession()
->fieldExists('Standard shipping: $9.99');
$this
->assertSession()
->fieldExists('The best shipping: $7.99');
$this
->assertSession()
->fieldNotExists('Carolina Special: $2.99');
$this
->assertSession()
->fieldExists('Wisconsin Express: $2.99');
$this
->getSession()
->getPage()
->pressButton('Recalculate shipping');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertNotEmpty($this
->getSession()
->getPage()
->findField('The best shipping: $7.99')
->getAttribute('checked'));
$this
->assertSession()
->fieldExists('Overnight shipping: $19.99');
$this
->assertSession()
->fieldExists('Standard shipping: $9.99');
$this
->assertSession()
->fieldExists('The best shipping: $7.99');
$this
->assertSession()
->fieldNotExists('Carolina Special: $2.99');
$this
->assertSession()
->fieldExists('Wisconsin Express: $2.99');
$wi_express = $this
->getSession()
->getPage()
->findField('Wisconsin Express: $2.99');
$this
->getSession()
->getPage()
->selectFieldOption($wi_express
->getAttribute('id'), $wi_express
->getAttribute('value'));
$this
->assertRenderedAddress($address, 'shipping_profile[0][profile]');
// Select the default profile instead.
$this
->getSession()
->getPage()
->fillField('shipping_profile[0][profile][select_address]', $this->defaultProfile
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertRenderedAddress($this->defaultAddress, 'shipping_profile[0][profile]');
// Edit the default profile and change the street.
$this
->getSession()
->getPage()
->pressButton('shipping_edit');
$this
->assertSession()
->assertWaitOnAjaxRequest();
foreach ($this->defaultAddress as $property => $value) {
$prefix = 'shipping_profile[0][profile][address][0][address]';
$this
->assertSession()
->fieldValueEquals($prefix . '[' . $property . ']', $value);
}
$this
->getSession()
->getPage()
->fillField('shipping_profile[0][profile][address][0][address][address_line1]', '10 Drupal Ave');
// The copy checkbox should be hidden and checked.
$this
->assertSession()
->fieldNotExists('shipping_profile[0][profile][copy_to_address_book]');
// Change the package type.
$package_type = PackageType::load('package_type_a');
$this
->getSession()
->getPage()
->fillField('package_type', 'commerce_package_type:' . $package_type
->uuid());
$this
->getSession()
->getPage()
->pressButton('Recalculate shipping');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldExists('Overnight shipping: $19.99');
$this
->assertSession()
->fieldExists('Standard shipping: $9.99');
$this
->assertSession()
->fieldExists('The best shipping: $159.80');
$this
->assertSession()
->fieldExists('Carolina Special: $2.99');
$this
->assertSession()
->fieldNotExists('Wisconsin Express: $2.99');
$this
->createScreenshot('../shipment_default_method.png');
$this
->assertNotEmpty($this
->getSession()
->getPage()
->findField('The best shipping: $159.80')
->getAttribute('checked'));
$this
->submitForm([], 'Save');
// Ensure the shipment has been updated.
$shipment = $this
->reloadEntity($shipment);
$this
->assertEquals('commerce_package_type:' . $package_type
->uuid(), $shipment
->getPackageType()
->getId());
$this
->assertFalse($shipment
->getData('owned_by_packer', TRUE));
$this
->assertEquals(new Price('159.80', 'USD'), $shipment
->getAmount());
$shipping_profile = $this
->reloadEntity($shipping_profile);
$this
->assertEquals('customer_shipping', $shipping_profile
->bundle());
$expected_address = [
'address_line1' => '10 Drupal Ave',
] + $this->defaultAddress;
$this
->assertEquals($expected_address, array_filter($shipping_profile
->get('address')
->first()
->toArray()));
$this
->assertEquals($this->defaultProfile
->id(), $shipping_profile
->getData('address_book_profile_id'));
// Confirm that the address book profile was updated.
$this->defaultProfile = $this
->reloadEntity($this->defaultProfile);
$this
->assertEquals($expected_address, array_filter($this->defaultProfile
->get('address')
->first()
->toArray()));
}