public function testShipmentsUI()
 {
     $this->drupalLogin($this->adminUser);
     $method = $this->createPaymentMethod('other');
     // Process an anonymous, shippable order.
     $order = Order::create(['uid' => 0, 'primary_email' => $this->randomString() . '@example.org', 'payment_method' => $method['id']]);
     // Add three more products to use for our tests.
     $products = array();
     for ($i = 1; $i <= 4; $i++) {
         $product = $this->createProduct(array('uid' => $this->adminUser->id(), 'promote' => 0));
         $order->products[$i] = OrderProduct::create(array('nid' => $product->nid->target_id, 'title' => $product->title->value, 'model' => $product->model, 'qty' => 1, 'cost' => $product->cost->value, 'price' => $product->price->value, 'weight' => $product->weight, 'data' => []));
         $order->products[$i]->data->shippable = 1;
     }
     $order->save();
     $order = Order::load($order->id());
     uc_payment_enter($order->id(), $method['id'], $order->getTotal());
     // Now quickly package all the products in this order.
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages');
     $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Create one package'));
     // Test "Ship" operations for this package.
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages');
     $this->assertLink(t('Ship'));
     $this->clickLink(t('Ship'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=1');
     foreach ($order->products as $sequence => $item) {
         $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.');
         // Test for weight here too? How do we compute this?
     }
     // We're shipping a specific package, so it should already be checked.
     foreach ($order->products as $sequence => $item) {
         $this->assertFieldByName('shipping_types[small_package][table][1][checked]', 1, 'Package is available for shipping.');
     }
     $this->assertFieldByName('method', 'manual', 'Manual shipping method selected.');
     //
     // Test presence and operation of ship operation on order admin View.
     //
     $this->drupalGet('admin/store/orders/view');
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/shipments');
     // Test action.
     $this->clickLink(t('Ship'));
     $this->assertResponse(200);
     $this->assertUrl('admin/store/orders/' . $order->id() . '/shipments/new');
     $this->assertText('No shipments have been made for this order.', 'Ship action found.');
     $this->assertText($order->products[1]->qty->value . ' x ' . $order->products[1]->model->value, 'Product quantity x SKU found.');
     $this->assertFieldByName('method', 'manual', 'Manual shipping method selected.');
     // Test reaching this through the shipments tab too ...
 }
 /**
  * Creates a new order directly, without going through checkout.
  */
 protected function createOrder($edit = [])
 {
     if (empty($edit['primary_email'])) {
         $edit['primary_email'] = $this->randomString() . '@example.org';
     }
     $order = Order::create($edit);
     if (!isset($edit['products'])) {
         $order->products[] = OrderProduct::create(array('nid' => $this->product->nid->target_id, 'title' => $this->product->title->value, 'model' => $this->product->model, 'qty' => 1, 'cost' => $this->product->cost->value, 'price' => $this->product->price->value, 'weight' => $this->product->weight, 'data' => []));
     }
     $order->save();
     return Order::load($order->id());
 }
Example #3
0
 /**
  * Form submit callback: add a product to an order.
  */
 public function addProductSubmit($form, FormStateInterface $form_state)
 {
     $form_state->set('products_action', 'products_select');
     $form_state->set('refresh_products', TRUE);
     $form_state->setRebuild();
     $order = $form['#order'];
     $data = \Drupal::moduleHandler()->invokeAll('uc_add_to_cart_data', array($form_state->getValue('product_controls')));
     $values = uc_product_load_variant(intval($form_state->getValue(['product_controls', 'nid'])), $data)->toArray();
     $values['qty'] = $form_state->getValue(['product_controls', 'qty']);
     $values['order_id'] = $order->id();
     $product = OrderProduct::create($values);
     \Drupal::moduleHandler()->alter('uc_order_product', $product, $order);
     $product->save();
     $order->products[] = $product;
     $order->logChanges([$this->t('Added (@qty) @title to order.', ['@qty' => $product->qty->value, '@title' => $product->title->value])]);
     // Decrement stock.
     if (\Drupal::moduleHandler()->moduleExists('uc_stock')) {
         uc_stock_adjust_product_stock($product, 0, $order);
     }
     // Add this product to the form values for accurate tax calculations.
     $products = $form_state->getValue('products');
     $products[] = $product;
     $form_state->setValue('products', $products);
 }
Example #4
0
 /**
  * Converts a cart item into an order product.
  */
 public function toOrderProduct()
 {
     $order_product = \Drupal\uc_order\Entity\OrderProduct::create(array('nid' => $this->nid->target_id, 'title' => $this->title, 'model' => $this->model, 'qty' => $this->qty->value, 'cost' => $this->cost, 'price' => $this->price, 'weight' => $this->weight, 'data' => $this->data));
     $order_product->weight->units = $this->weight_units;
     return $order_product;
 }
 public function testPackagesUI()
 {
     $this->drupalLogin($this->adminUser);
     $method = $this->createPaymentMethod('other');
     // Process an anonymous, shippable order.
     $order = Order::create(['uid' => 0, 'primary_email' => $this->randomString() . '@example.org', 'payment_method' => $method['id']]);
     // Add three more products to use for our tests.
     $products = array();
     for ($i = 1; $i <= 4; $i++) {
         $product = $this->createProduct(array('uid' => $this->adminUser->id(), 'promote' => 0));
         $order->products[$i] = OrderProduct::create(array('nid' => $product->nid->target_id, 'title' => $product->title->value, 'model' => $product->model, 'qty' => 1, 'cost' => $product->cost->value, 'price' => $product->price->value, 'weight' => $product->weight, 'data' => []));
         $order->products[$i]->data->shippable = 1;
     }
     $order->save();
     $order = Order::load($order->id());
     uc_payment_enter($order->id(), $method['id'], $order->getTotal());
     // Order with 4 products shippable products. (where do we test not-shippable?)
     // Check all, make one package, verify we're on packages page with only one packge.
     // Try create package link, should see there are no products message.
     // Delete package.
     // Check all, make shipment, verify we're on packages page with N packages.
     // Delete packages.
     // How does Sep work? how does making 2 packages out of 4 products work?
     // Check all, cancel, verify we're on order page.
     // After packages made and check for # (check make one and make shipment, use sep. as well)
     // Can use edit/delete actions to package then start over with the same order.
     // and check for full table at /packages and check for action on /packages page,
     // goto shipments tab and look for No shipments have been made for this order.  as well as a list of all the packages.
     //
     // Test presence and operation of package operation on order admin View.
     //
     $this->drupalGet('admin/store/orders/view');
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages');
     // Test action.
     $this->clickLink(t('Package'));
     $this->assertResponse(200);
     $this->assertText('This order\'s products have not been organized into packages.', 'Package action found.');
     // Now package the products in this order.
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages');
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     // First time through we'll be verbose - skip this on subsequent tests.
     foreach ($order->products as $sequence => $item) {
         $this->assertText($item->title->value, 'Product title found.');
         $this->assertText($item->model->value, 'Product SKU found.');
         $this->assertFieldByName('shipping_types[small_package][table][' . $sequence . '][checked]', 0, 'Product is available for packaging.');
     }
     // Select all products and test the "Cancel" button.
     $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Cancel'));
     // Go back to Packages tab and try something else.
     $this->assertUrl('admin/store/orders/' . $order->id());
     $this->clickLink(t('Packages'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     $this->assertText('This order\'s products have not been organized into packages.', 'Package action found.');
     // Now test the "Create one package" button without selecting anything.
     $this->drupalPostForm(NULL, array(), t('Create one package'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     $this->assertText('Packages must contain at least one product.', 'Validation that there must be products in a package.');
     // Now test the "Create one package" button with all products selected.
     $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Create one package'));
     // Check that we're now on the package list page.
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages');
     foreach ($order->products as $sequence => $item) {
         $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.');
     }
     // The "Create packages" local action should now be available too.
     $this->assertLink(t('Create packages'));
     $this->clickLink(t('Create packages'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     // But we've already packaged everything...
     $this->assertText('There are no products available for this type of package.', 'Create packages local action found.');
     //
     // Test "Ship", "Edit", and "Delete" operations for this package.
     //
     // First "Ship".
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages');
     $this->assertLink(t('Ship'));
     $this->clickLink(t('Ship'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=1');
     foreach ($order->products as $sequence => $item) {
         $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.');
     }
     // Second, "Edit".
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages');
     // (Use Href to distinguish Edit operation from Edit tab.)
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages/1/edit');
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages/1/edit');
     // We're editing the package we already made, so all the
     // products should be checked.
     foreach ($order->products as $sequence => $item) {
         $this->assertFieldByName('products[' . $sequence . '][checked]', 1, 'Product is available for packaging.');
     }
     // Third, "Delete".
     $this->drupalGet('admin/store/orders/' . $order->id() . '/packages');
     $this->assertLink(t('Delete'));
     $this->clickLink(t('Delete'));
     // Delete takes us to confirm page.
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/1/delete');
     $this->assertText('The products it contains will be available for repackaging.', 'Deletion confirm question found.');
     // "Cancel" returns to the package list page.
     $this->clickLink(t('Cancel'));
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages');
     // Again with the "Delete".
     $this->clickLink(t('Delete'));
     $this->drupalPostForm(NULL, array(), t('Delete'));
     // Delete returns to new packages page with all packages unchecked.
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     $this->assertText('Package 1 has been deleted.', 'Package deleted message found.');
     foreach ($order->products as $sequence => $item) {
         $this->assertFieldByName('shipping_types[small_package][table][' . $sequence . '][checked]', 0, 'Product is available for packaging.');
     }
     // Back to no packages. Now test making more than one package.
     // Now test the "Create one package" button with all products selected.
     $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1), t('Create one package'));
     // Check that we're now on the package list page.
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages');
     $this->assertText($order->products[1]->qty->value . ' x ' . $order->products[1]->model->value, 'Product quantity x SKU found.');
     $this->assertText($order->products[2]->qty->value . ' x ' . $order->products[2]->model->value, 'Product quantity x SKU found.');
     $this->assertNoText($order->products[3]->qty->value . ' x ' . $order->products[3]->model->value, 'Product quantity x SKU not found.');
     $this->assertNoText($order->products[4]->qty->value . ' x ' . $order->products[4]->model->value, 'Product quantity x SKU not found.');
     // Use "Create packages" to create a second package.
     $this->assertLink(t('Create packages'));
     $this->clickLink(t('Create packages'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     $this->assertNoText($order->products[1]->model->value, 'Product SKU not found.');
     $this->assertNoText($order->products[2]->model->value, 'Product SKU not found.');
     $this->assertText($order->products[3]->model->value, 'Product SKU found.');
     $this->assertText($order->products[4]->model->value, 'Product SKU found.');
     $this->drupalPostForm(NULL, array('shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Create one package'));
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/packages');
     foreach ($order->products as $sequence => $item) {
         $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.');
     }
     // How do we test for two packages? Look for two "Ship" links
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=2');
     $this->assertLinkByHref('admin/store/orders/' . $order->id() . '/shipments/new?pkgs=3');
     // Now delete both packages.
     $this->clickLink(t('Delete'));
     $this->drupalPostForm(NULL, array(), t('Delete'));
     $this->assertText('Package 2 has been deleted.', 'Package deleted message found.');
     // There's still one left to delete...
     $this->clickLink(t('Delete'));
     $this->drupalPostForm(NULL, array(), t('Delete'));
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages/new');
     $this->assertText('Package 3 has been deleted.', 'Package deleted message found.');
     // Back to no packages. Now test "Make packages" button.
     $this->drupalPostForm(NULL, array('shipping_types[small_package][table][1][checked]' => 1, 'shipping_types[small_package][table][2][checked]' => 1, 'shipping_types[small_package][table][3][checked]' => 1, 'shipping_types[small_package][table][4][checked]' => 1), t('Make packages'));
     // Check that we're now on the package list page.
     $this->assertUrl('admin/store/orders/' . $order->id() . '/packages');
     foreach ($order->products as $sequence => $item) {
         $this->assertText($item->qty->value . ' x ' . $item->model->value, 'Product quantity x SKU found.');
     }
 }