diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb index dbf701a..a77b652 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb @@ -278,7 +278,7 @@ class ShoppingCartPluginController < PublicController after_filter :save_cookie def save_cookie - if @cart.nil? && cookies[cookie_key] + if @cart.nil? cookies.delete(cookie_key) else cookies[cookie_key] = { diff --git a/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb b/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb index 905bb6c..b367653 100644 --- a/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb +++ b/plugins/shopping_cart/test/functional/shopping_cart_plugin_controller_test.rb @@ -16,23 +16,28 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase attr_reader :enterprise attr_reader :product + should 'return no cookie for an empty cart' do + get :get + assert_nil @response.cookies[:_noosfero_plugin_shopping_cart] + end + should 'add a new product to cart' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert product_in_cart?(product) assert_equal 1, product_quantity(product) end should 'grow quantity through add' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert_equal 1, product_quantity(product) - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert_equal 2, product_quantity(product) end should 'not add product to cart if it does not exists' do - assert_nothing_raised { get :add, :profile => enterprise.identifier, :id => 9999 } + assert_nothing_raised { get :add, :id => 9999 } assert !product_in_cart?(product) assert !response_ok? @@ -40,10 +45,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase end should 'remove cart if the product being removed is the last one' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert cart? - get :remove, :profile => enterprise.identifier, :id => product.id + get :remove, :id => product.id assert !cart? end @@ -51,25 +56,25 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase instantiate_cart assert !cart? - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } + assert_nothing_raised { get :remove, :id => 9999 } assert !response_ok? assert_equal 2, reponse_error_code end should 'just remove product if there are other products on cart' do another_product = fast_create(Product, :enterprise_id => enterprise.id) - get :add, :profile => enterprise.identifier, :id => product.id - get :add, :profile => enterprise.identifier, :id => another_product.id + get :add, :id => product.id + get :add, :id => another_product.id - get :remove, :profile => enterprise.identifier, :id => product.id + get :remove, :id => product.id assert cart? assert !product_in_cart?(product) end should 'not try to remove a product that is not in the cart' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert cart? - assert_nothing_raised { get :remove, :profile => enterprise.identifier, :id => 9999 } + assert_nothing_raised { get :remove, :id => 9999 } assert !response_ok? assert_equal 4, reponse_error_code @@ -79,23 +84,23 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase instantiate_cart assert !cart? - assert_nothing_raised { get :list, :profile => enterprise.identifier } + assert_nothing_raised { get :list } assert !response_ok? assert_equal 2, reponse_error_code end should 'list products without errors' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id - assert_nothing_raised { get :list, :profile => enterprise.identifier } + assert_nothing_raised { get :list } assert response_ok? end should 'update the quantity of a product' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert 1, product_quantity(product) - get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 3 + get :update_quantity, :id => product.id, :quantity => 3 assert 3, product_quantity(product) end @@ -103,55 +108,54 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase instantiate_cart assert !cart? - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } + assert_nothing_raised { get :update_quantity, :id => 9999, :quantity => 3 } assert !response_ok? assert_equal 2, reponse_error_code end should 'not try to update the quantity of a product that is not in the cart' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id assert cart? - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => 9999, :quantity => 3 } + assert_nothing_raised { get :update_quantity, :id => 9999, :quantity => 3 } assert !response_ok? assert_equal 4, reponse_error_code end should 'not update the quantity of a product with a invalid value' do - get :add, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => -1} + assert_nothing_raised { get :update_quantity, :id => product.id, :quantity => -1} assert !response_ok? assert_equal 5, reponse_error_code - assert_nothing_raised { get :update_quantity, :profile => enterprise.identifier, :id => product.id, :quantity => 'asdf'} + assert_nothing_raised { get :update_quantity, :id => product.id, :quantity => 'asdf'} assert !response_ok? assert_equal 5, reponse_error_code end should 'clean the cart' do another_product = fast_create(Product, :enterprise_id => enterprise.id) - get :add, :profile => enterprise.identifier, :id => product.id - get :add, :profile => enterprise.identifier, :id => another_product.id + get :add, :id => product.id + get :add, :id => another_product.id - assert_nothing_raised { get :clean, :profile => enterprise.identifier } + assert_nothing_raised { get :clean } assert !cart? end should 'not crash if there is no cart' do instantiate_cart assert !cart? - assert_nothing_raised { get :clean, :profile => enterprise.identifier } + assert_nothing_raised { get :clean } end should 'register order on send request' do product1 = fast_create(Product, :enterprise_id => enterprise.id, :price => 1.99) product2 = fast_create(Product, :enterprise_id => enterprise.id, :price => 2.23) - @controller.stubs(:cart).returns({:items => {product1.id => 1, product2.id => 2}}) + @controller.stubs(:cart).returns({ :enterprise_id => enterprise.id, :items => {product1.id => 1, product2.id => 2}}) assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do post :send_request, - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, - :profile => enterprise.identifier + :customer => {:name => "Manuel", :email => "manuel@ceu.com"} end order = ShoppingCartPlugin::PurchaseOrder.last @@ -165,11 +169,10 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase should 'register order on send request and not crash if product is not defined' do product1 = fast_create(Product, :enterprise_id => enterprise.id) - @controller.stubs(:cart).returns({:items => {product1.id => 1}}) + @controller.stubs(:cart).returns({ :enterprise_id => enterprise.id, :items => {product1.id => 1}}) assert_difference ShoppingCartPlugin::PurchaseOrder, :count, 1 do post :send_request, - :customer => {:name => "Manuel", :email => "manuel@ceu.com"}, - :profile => enterprise.identifier + :customer => {:name => "Manuel", :email => "manuel@ceu.com"} end order = ShoppingCartPlugin::PurchaseOrder.last @@ -184,11 +187,13 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase end def cart? - @controller.send(:cart).nil? + !@controller.send(:cart).nil? end def product_in_cart?(product) - @controller.send(:cart)[:items].has_key?(product.id) + @controller.send(:cart) && + @controller.send(:cart)[:items] && + @controller.send(:cart)[:items].has_key?(product.id) end def product_quantity(product) @@ -206,8 +211,8 @@ class ShoppingCartPluginControllerTest < ActionController::TestCase # temporary hack...if I don't do this the session stays as an Array instead # of a TestSession def instantiate_cart - get :add, :profile => enterprise.identifier, :id => product.id - get :remove, :profile => enterprise.identifier, :id => product.id + get :add, :id => product.id + get :remove, :id => product.id end end -- libgit2 0.21.2