Commit 7265a237786f45127244b93cb1ee55e9dd76ef3e

Authored by Daniela Feitosa
1 parent 808df810

Fixed creation of order to avoid crashing when price is not defined

(ActionItem2362)
plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb
@@ -229,7 +229,8 @@ class ShoppingCartPluginProfileController < ProfileController @@ -229,7 +229,8 @@ class ShoppingCartPluginProfileController < ProfileController
229 new_items = {} 229 new_items = {}
230 items.each do |id, quantity| 230 items.each do |id, quantity|
231 product = Product.find(id) 231 product = Product.find(id)
232 - new_items[id] = {:quantity => quantity, :price => product.price, :name => product.name} 232 + price = product.price || 0
  233 + new_items[id] = {:quantity => quantity, :price => price, :name => product.name}
233 end 234 end
234 ShoppingCartPlugin::PurchaseOrder.create!( 235 ShoppingCartPlugin::PurchaseOrder.create!(
235 :seller => profile, 236 :seller => profile,
plugins/shopping_cart/test/functional/shopping_cart_plugin_myprofile_controller_test.rb
@@ -79,7 +79,7 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase @@ -79,7 +79,7 @@ class ShoppingCartPluginMyprofileControllerTest < ActionController::TestCase
79 should 'group filtered orders products and quantities' do 79 should 'group filtered orders products and quantities' do
80 p1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1, :name => 'p1') 80 p1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1, :name => 'p1')
81 p2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2, :name => 'p2') 81 p2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2, :name => 'p2')
82 - p3 = fast_create(Product, :enterprise_id => enterprise.id, :price => 3, :name => 'p3') 82 + p3 = fast_create(Product, :enterprise_id => enterprise.id, :price => 3)
83 po1_products = {p1.id => {:quantity => 1, :price => p1.price, :name => p1.name}, p2.id => {:quantity => 2, :price => p2.price, :name => p2.name }} 83 po1_products = {p1.id => {:quantity => 1, :price => p1.price, :name => p1.name}, p2.id => {:quantity => 2, :price => p2.price, :name => p2.name }}
84 po2_products = {p2.id => {:quantity => 1, :price => p2.price, :name => p2.name }, p3.id => {:quantity => 2, :price => p3.price, :name => p3.name}} 84 po2_products = {p2.id => {:quantity => 1, :price => p2.price, :name => p2.name }, p3.id => {:quantity => 2, :price => p3.price, :name => p3.name}}
85 po1 = ShoppingCartPlugin::PurchaseOrder.create!(:seller => enterprise, :products_list => po1_products, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED) 85 po1 = ShoppingCartPlugin::PurchaseOrder.create!(:seller => enterprise, :products_list => po1_products, :status => ShoppingCartPlugin::PurchaseOrder::Status::OPENED)
plugins/shopping_cart/test/functional/shopping_cart_plugin_profile_controller_test.rb
@@ -163,6 +163,20 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase @@ -163,6 +163,20 @@ class ShoppingCartPluginProfileControllerTest < ActionController::TestCase
163 assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status 163 assert_equal ShoppingCartPlugin::PurchaseOrder::Status::OPENED, order.status
164 end 164 end
165 165
  166 + should 'register order on send request and not crash if product is not defined' do
  167 + product1 = fast_create(Product, :enterprise_id => enterprise.id)
  168 + @controller.stubs(:session).returns({:cart => {:items => {product1.id => 1}}})
  169 + assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do
  170 + post :send_request,
  171 + :customer => {:name => "Manuel", :email => "manuel@ceu.com"},
  172 + :profile => enterprise.identifier
  173 + end
  174 +
  175 + order = ShoppingCartPlugin::PurchaseOrder.last
  176 +
  177 + assert_equal 0, order.products_list[product1.id][:price]
  178 + end
  179 +
166 private 180 private
167 181
168 def json_response 182 def json_response