Example #1
0
 public function testTaxDisplay()
 {
     $this->drupalLogin($this->adminUser);
     // Enable a payment method for the payment preview checkout pane.
     $edit = array('methods[check][status]' => 1);
     $this->drupalPostForm('admin/store/config/payment', $edit, t('Save configuration'));
     // Create a 20% inclusive tax rate.
     $rate = (object) array('name' => $this->randomMachineName(8), 'rate' => 0.2, 'taxed_product_types' => array('product'), 'taxed_line_items' => array(), 'weight' => 0, 'shippable' => 0, 'display_include' => 1, 'inclusion_text' => '');
     uc_tax_rate_save($rate);
     $this->drupalGet('admin/store/config/taxes');
     $this->assertText($rate->name, 'Tax was saved successfully.');
     // $this->drupalGet("admin/store/config/taxes/manage/uc_tax_$rate->id");
     // $this->assertText(t('Conditions'), 'Rules configuration linked to tax.');
     $this->addToCart($this->product);
     // Manually step through checkout. $this->checkout() doesn't know about taxes.
     $this->drupalPostForm('cart', array(), 'Checkout');
     $this->assertText(t('Enter your billing address and information here.'), 'Viewed cart page: Billing pane has been displayed.');
     $this->assertRaw($rate->name, 'Tax line item displayed.');
     $this->assertRaw(uc_currency_format($rate->rate * $this->product->price->value), 'Correct tax amount displayed.');
     // Submit the checkout page.
     $edit = $this->populateCheckoutForm();
     $this->drupalPostForm('cart/checkout', $edit, t('Review order'));
     $this->assertRaw(t('Your order is almost complete.'));
     $this->assertRaw($rate->name, 'Tax line item displayed.');
     $this->assertRaw(uc_currency_format($rate->rate * $this->product->price->value), 'Correct tax amount displayed.');
     // Complete the review page.
     $this->drupalPostForm(NULL, array(), t('Submit order'));
     $order_ids = \Drupal::entityQuery('uc_order')->condition('delivery_first_name', $edit['panes[delivery][first_name]'])->execute();
     $order_id = reset($order_ids);
     if ($order_id) {
         $this->pass(SafeMarkup::format('Order %order_id has been created', ['%order_id' => $order_id]));
         $this->drupalGet('admin/store/orders/' . $order_id . '/edit');
         $this->assertTaxLineCorrect($this->loadTaxLine($order_id), $rate->rate, 'on initial order load');
         $this->drupalPostForm('admin/store/orders/' . $order_id . '/edit', array(), t('Save changes'));
         $this->assertText(t('Order changes saved.'));
         $this->assertTaxLineCorrect($this->loadTaxLine($order_id), $rate->rate, 'after saving order');
         // Change tax rate and ensure order doesn't change.
         $oldrate = $rate->rate;
         $rate->rate = 0.1;
         $rate = uc_tax_rate_save($rate);
         // Save order because tax changes are only updated on save.
         $this->drupalPostForm('admin/store/orders/' . $order_id . '/edit', array(), t('Save changes'));
         $this->assertText(t('Order changes saved.'));
         $this->assertTaxLineCorrect($this->loadTaxLine($order_id), $oldrate, 'after rate change');
         // Change taxable products and ensure order doesn't change.
         $class = $this->createProductClass();
         $rate->taxed_product_types = array($class->getEntityTypeId());
         uc_tax_rate_save($rate);
         // entity_flush_caches();
         $this->drupalPostForm('admin/store/orders/' . $order_id . '/edit', array(), t('Save changes'));
         $this->assertText(t('Order changes saved.'));
         $this->assertTaxLineCorrect($this->loadTaxLine($order_id), $oldrate, 'after applicable product change');
         // Change order Status back to in_checkout and ensure tax-rate changes now update the order.
         \Drupal\uc_order\Entity\Order::load($order_id)->setStatusId('in_checkout')->save();
         $this->drupalPostForm('admin/store/orders/' . $order_id . '/edit', array(), t('Save changes'));
         $this->assertText(t('Order changes saved.'));
         $this->assertFalse($this->loadTaxLine($order_id), 'The tax line was removed from the order when order status changed back to in_checkout.');
         // Restore taxable product and ensure new tax is added.
         $rate->taxed_product_types = array('product');
         uc_tax_rate_save($rate);
         $this->drupalPostForm('admin/store/orders/' . $order_id . '/edit', array(), t('Save changes'));
         $this->assertText(t('Order changes saved.'));
         $this->assertTaxLineCorrect($this->loadTaxLine($order_id), $rate->rate, 'when order status changed back to in_checkout');
     } else {
         $this->fail('No order was created.');
     }
 }
 public function testProductKitAttributes()
 {
     $this->drupalLogin($this->adminUser);
     // Create a 20% inclusive tax rate.
     $rate = (object) array('name' => $this->randomMachineName(8), 'rate' => 0.2, 'taxed_product_types' => array('product'), 'taxed_line_items' => [], 'weight' => 0, 'shippable' => 0, 'display_include' => 1, 'inclusion_text' => $this->randomMachineName(6));
     uc_tax_rate_save($rate);
     // Ensure Rules picks up the new condition.
     // entity_flush_caches();
     // Create a $10 product.
     $product = $this->createProduct(array('price' => 10));
     // Create an attribute.
     $attribute = (object) array('name' => $this->randomMachineName(8), 'label' => $this->randomMachineName(8), 'description' => $this->randomMachineName(8), 'required' => TRUE, 'display' => 1, 'ordering' => 0);
     uc_attribute_save($attribute);
     // Create an option with a price adjustment of $5.
     $option = (object) array('aid' => $attribute->aid, 'name' => $this->randomMachineName(8), 'cost' => 0, 'price' => 5, 'weight' => 0, 'ordering' => 0);
     uc_attribute_option_save($option);
     // Attach the attribute to the product.
     $attribute = uc_attribute_load($attribute->aid);
     uc_attribute_subject_save($attribute, 'product', $product->id(), TRUE);
     // Create a product kit containing the product.
     $kit = $this->drupalCreateNode(array('type' => 'product_kit', 'products' => array($product->id()), 'default_qty' => 1, 'mutable' => UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST));
     // Set the kit total to $9 to automatically apply a discount.
     $kit = node_load($kit->id());
     $kit->kit_total = 9;
     $kit->save();
     $kit = node_load($kit->id());
     $this->assertEqual($kit->products[$product->id()]->discount, -1, 'Product kit component has correct discount applied.');
     // Ensure the price is displayed tax-inclusively on the add-to-cart form.
     $this->drupalGet('node/' . $kit->id());
     $this->assertText('$10.80' . $rate->inclusion_text, 'Tax inclusive price on node-view form is accurate.');
     // $10.80 = $9.00 + 20%
     $this->assertRaw($option->name . ', +$6.00</option>', 'Tax inclusive option price on node view form is accurate.');
     // $6.00 = $5.00 + 20%
     // Add the product kit to the cart, selecting the option.
     $attribute_key = 'products[' . $product->id() . '][attributes][' . $attribute->aid . ']';
     $this->addToCart($kit, array($attribute_key => $option->oid));
     // Check that the subtotal is $16.80 ($10 base + $5 option - $1 discount, with 20% tax)
     $this->drupalGet('cart');
     $this->assertText('Subtotal: $16.80', 'Order subtotal is correct on cart page.');
     // @todo: disable rest of test, see [#2306379]
     return;
     // Make sure that the subtotal is also correct on the checkout page.
     $this->drupalPostForm('cart', [], 'Checkout');
     $this->assertText('Subtotal: $16.80', 'Order subtotal is correct on checkout page.');
     // Manually proceed to checkout review.
     $edit = $this->populateCheckoutForm();
     $this->drupalPostForm('cart/checkout', $edit, t('Review order'));
     $this->assertRaw(t('Your order is almost complete.'));
     // Make sure the price is still listed tax-inclusively.
     // @TODO This could be handled more specifically with a regex.
     $this->assertText('$16.80' . $rate->inclusion_text, 'Tax inclusive price appears in cart pane on checkout review page');
     // Ensure the tax-inclusive price is listed on the order admin page.
     $order_ids = \Drupal::entityQuery('uc_order')->condition('delivery_first_name', $edit['panes[delivery][first_name]'])->execute();
     $order_id = reset($order_ids);
     $this->assertTrue($order_id, 'Order was created successfully');
     $this->drupalGet('admin/store/orders/' . $order_id);
     $this->assertText('$16.80' . $rate->inclusion_text, 'Tax inclusive price appears on the order view page.');
     // And on the invoice.
     $this->drupalGet('admin/store/orders/' . $order_id . '/invoice');
     $this->assertText('$16.80' . $rate->inclusion_text, 'Tax inclusive price appears on the invoice.');
     // And on the printable invoice.
     $this->drupalGet('admin/store/orders/' . $order_id . '/invoice');
     $this->assertText('$16.80' . $rate->inclusion_text, 'Tax inclusive price appears on the printable invoice.');
 }