diff --git a/uc_product_kit/uc_product_kit.module b/uc_product_kit/uc_product_kit.module index 3cb237c..f7b42c3 100644 --- a/uc_product_kit/uc_product_kit.module +++ b/uc_product_kit/uc_product_kit.module @@ -139,6 +139,7 @@ function uc_product_kit_insert(&$node) { $obj->weight = 0; $obj->weight_units = variable_get('uc_weight_unit', 'lb'); + $obj->default_qty = $node->default_qty; $obj->ordering = $node->ordering; $obj->shippable = FALSE; @@ -306,11 +307,11 @@ function uc_product_kit_update(&$node) { // If we're not making a new revision, delete information for the current // revision so we can overwrite it. - if (!$node->revision) { + if (empty($node->revision)) { db_query("DELETE FROM {uc_product_kits} WHERE vid = %d", $node->vid); } db_query("INSERT INTO {uc_product_kits} (vid, nid, product_id, mutable, qty, discount, ordering, synchronized) VALUES ". implode(',', $placeholders), $values); - if ($node->revision) { + if (!empty($node->revision)) { db_query("INSERT INTO {uc_products} (vid, nid, model, list_price, cost, sell_price, weight, weight_units, default_qty, unique_hash, ordering, shippable) VALUES (%d, %d, '%s', %f, %f, %f, %f, '%s', %d, '%s', %d, %d)", $obj->vid, $obj->nid, $obj->model, $obj->list_price, $obj->cost, $obj->sell_price, $obj->weight, $obj->weight_units, $obj->default_qty, md5($obj->vid . $obj->nid . $obj->model . $obj->list_price . $obj->cost . $obj->sell_price . $obj->weight . $obj->weight_units . $obj->default_qty . $obj->ordering . time()), @@ -584,7 +585,7 @@ function uc_product_kit_form(&$node) { $form['base']['default_qty'] = array( '#type' => 'uc_quantity', '#title' => t('Default quantity to add to cart'), - '#default_value' => !is_null($node->default_qty) ? $node->default_qty : 1, + '#default_value' => isset($node->default_qty) ? $node->default_qty : 1, '#description' => t('Use 0 to disable the quantity field next to the add to cart button.'), '#weight' => 27, '#allow_zero' => TRUE, @@ -722,7 +723,8 @@ function uc_product_kit_view($node, $teaser = 0, $page = 0) { * @ingroup themeable */ function theme_uc_product_kit_list_item($product) { - if (node_access('view', $product)) { + $node = node_load($product->nid); + if (node_access('view', $node)) { $title = l($product->title, 'node/'. $product->nid); } else { diff --git a/uc_product_kit/uc_product_kit.test b/uc_product_kit/uc_product_kit.test new file mode 100644 index 0000000..eb84909 --- /dev/null +++ b/uc_product_kit/uc_product_kit.test @@ -0,0 +1,214 @@ + 'Product kits', + 'description' => 'Ensure that the product kit functions properly.', + 'group' => 'Ubercart', + ); + } + + /** + * Overrides DrupalWebTestCase::setUp(). + */ + public function setUp() { + parent::setUp(array('uc_product_kit'), array('create product kits', 'edit all product kits')); + } + + public function testProductKitNodeForm() { + $this->drupalLogin($this->adminUser); + + // Create some test products. + $products = array(); + for ($i = 0; $i < 3; $i++) { + $products[$i] = $this->createProduct(); + } + + // Test the product kit fields. + $this->drupalGet('node/add/product-kit'); + foreach (array('mutable', 'products[]', 'ordering') as $field) { + $this->assertFieldByName($field); + } + + // Test creation of a basic kit. + $body_key = 'body'; + $edit = array( + 'title' => $this->randomName(32), + $body_key => $this->randomName(64), + 'products[]' => array( + $products[0]->nid, + $products[1]->nid, + $products[2]->nid, + ), + ); + $this->drupalPost('node/add/product-kit', $edit, 'Save'); + $this->assertText(t('Product kit @title has been created.', array('@title' => $edit['title']))); + $this->assertText($edit[$body_key], 'Product kit body found.'); + $this->assertText('1 × ' . $products[0]->title, 'Product 1 title found.'); + $this->assertText('1 × ' . $products[1]->title, 'Product 2 title found.'); + $this->assertText('1 × ' . $products[2]->title, 'Product 3 title found.'); + $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price; + $this->assertText(uc_currency_format($total), 'Product kit total found.'); + } + + public function testProductKitDiscounts() { + $this->drupalLogin($this->adminUser); + + // Create some test products and a kit. + $products = array(); + for ($i = 0; $i < 3; $i++) { + $products[$i] = $this->createProduct(); + } + $kit = $this->drupalCreateNode(array( + 'type' => 'product_kit', + 'title' => $this->randomName(32), + 'products' => array( + $products[0]->nid, + $products[1]->nid, + $products[2]->nid, + ), + 'mutable' => UC_PRODUCT_KIT_UNMUTABLE_NO_LIST, + 'default_qty' => 1, + 'ordering' => 0, + )); + + // Test the product kit extra fields available to configure discounts. + $this->drupalGet('node/' . $kit->nid . '/edit'); + $this->assertFieldByName('kit_total'); + foreach ($products as $product) { + $this->assertFieldByName('items[' . $product->nid . '][qty]'); + $this->assertFieldByName('items[' . $product->nid . '][ordering]'); + $this->assertFieldByName('items[' . $product->nid . '][discount]'); + } + + // Set some discounts. + $discounts = array( + mt_rand(-100, 100), + mt_rand(-100, 100), + mt_rand(-100, 100), + ); + $edit = array( + 'items[' . $products[0]->nid . '][discount]' => $discounts[0], + 'items[' . $products[1]->nid . '][discount]' => $discounts[1], + 'items[' . $products[2]->nid . '][discount]' => $discounts[2], + ); + $this->drupalPost('node/' . $kit->nid . '/edit', $edit, 'Save'); + + // Check the discounted total. + $total = $products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price; + $total += array_sum($discounts); + $this->assertText(uc_currency_format($total), 'Discounted product kit total found.'); + + // Check the discounts on the edit page. + $this->drupalGet('node/' . $kit->nid . '/edit'); + $this->assertText('Currently, the total sell price is ' . uc_currency_format($total), 'Discounted product kit total found.'); + $this->assertFieldByName('items[' . $products[0]->nid . '][discount]', $discounts[0]); + $this->assertFieldByName('items[' . $products[1]->nid . '][discount]', $discounts[1]); + $this->assertFieldByName('items[' . $products[2]->nid . '][discount]', $discounts[2]); + + // Set the kit total. + $total = 2 * ($products[0]->sell_price + $products[1]->sell_price + $products[2]->sell_price); + $this->drupalPost('node/' . $kit->nid . '/edit', array('kit_total' => $total), 'Save'); + + // Check the fixed total. + $this->assertText(uc_currency_format($total), 'Fixed product kit total found.'); + + // Check the discounts on the edit page. + $this->drupalGet('node/' . $kit->nid . '/edit'); + $this->assertFieldByName('kit_total', $total); + $this->assertFieldByName('items[' . $products[0]->nid . '][discount]', $products[0]->sell_price); + $this->assertFieldByName('items[' . $products[1]->nid . '][discount]', $products[1]->sell_price); + $this->assertFieldByName('items[' . $products[2]->nid . '][discount]', $products[2]->sell_price); + } + + public function testProductKitMutability() { + $this->drupalLogin($this->adminUser); + + // Create some test products and prepare a kit. + $products = array(); + for ($i = 0; $i < 3; $i++) { + $products[$i] = $this->createProduct(); + } + $kit_data = array( + 'type' => 'product_kit', + 'title' => $this->randomName(32), + 'products' => array( + $products[0]->nid, + $products[1]->nid, + $products[2]->nid, + ), + 'default_qty' => 1, + 'ordering' => 0, + ); + + // Test kits with no listing. + $kit_data['mutable'] = UC_PRODUCT_KIT_UNMUTABLE_NO_LIST; + $kit = $this->drupalCreateNode($kit_data); + $this->drupalGet('node/' . $kit->nid); + $this->assertText($kit->title, 'Product kit title found.'); + $this->assertNoText($products[0]->title, 'Product 1 title not shown.'); + $this->assertNoText($products[1]->title, 'Product 2 title not shown.'); + $this->assertNoText($products[2]->title, 'Product 3 title not shown.'); + + $this->drupalPost('node/' . $kit->nid, array(), 'Add to cart'); + $this->drupalGet('cart'); + $this->assertText($kit->title, 'Product kit title found.'); + $this->assertNoText($products[0]->title, 'Product 1 title not shown.'); + $this->assertNoText($products[1]->title, 'Product 2 title not shown.'); + $this->assertNoText($products[2]->title, 'Product 3 title not shown.'); + + $this->drupalPost(NULL, array(), 'Remove'); + $this->assertText('There are no products in your shopping cart.'); + + // Test kits with listing. + $kit_data['mutable'] = UC_PRODUCT_KIT_UNMUTABLE_WITH_LIST; + $kit = $this->drupalCreateNode($kit_data); + $this->drupalGet('node/' . $kit->nid); + $this->assertText($kit->title, 'Product kit title found.'); + $this->assertText($products[0]->title, 'Product 1 title shown.'); + $this->assertText($products[1]->title, 'Product 2 title shown.'); + $this->assertText($products[2]->title, 'Product 3 title shown.'); + + $this->drupalPost('node/' . $kit->nid, array(), 'Add to cart'); + $this->drupalGet('cart'); + $this->assertText($kit->title, 'Product kit title found.'); + $this->assertText($products[0]->title, 'Product 1 title shown.'); + $this->assertText($products[1]->title, 'Product 2 title shown.'); + $this->assertText($products[2]->title, 'Product 3 title shown.'); + + $this->drupalPost(NULL, array(), 'Remove'); + $this->assertText('There are no products in your shopping cart.'); + + // Test mutable kits. + $kit_data['mutable'] = UC_PRODUCT_KIT_MUTABLE; + $kit = $this->drupalCreateNode($kit_data); + $this->drupalGet('node/' . $kit->nid); + $this->assertText($kit->title, 'Product kit title found.'); + $this->assertText($products[0]->title, 'Product 1 title shown.'); + $this->assertText($products[1]->title, 'Product 2 title shown.'); + $this->assertText($products[2]->title, 'Product 3 title shown.'); + + $this->drupalPost('node/' . $kit->nid, array(), 'Add to cart'); + $this->drupalGet('cart'); + $this->assertNoText($kit->title, 'Product kit title not shown.'); + $this->assertText($products[0]->title, 'Product 1 title shown.'); + $this->assertText($products[1]->title, 'Product 2 title shown.'); + $this->assertText($products[2]->title, 'Product 3 title shown.'); + + $this->drupalPost(NULL, array(), 'Remove'); + $this->drupalPost(NULL, array(), 'Remove'); + $this->drupalPost(NULL, array(), 'Remove'); + $this->assertText('There are no products in your shopping cart.'); + } + +}