diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb index 8671569..d442d60 100644 --- a/app/controllers/my_profile/manage_products_controller.rb +++ b/app/controllers/my_profile/manage_products_controller.rb @@ -22,23 +22,36 @@ class ManageProductsController < ApplicationController @product = @profile.products.find(params[:id]) end + def categories_for_selection + @category = Category.find(params[:category_id]) if params[:category_id] + if @category + @categories = @category.children + @level = @category.leaf? ? @category.level : @categories.first.level + else + @categories = ProductCategory.top_level_for(environment) + @level = 0 + end + render :partial => 'categories_for_selection' + end + def new - @object = Product.new - @categories = @current_category.nil? ? ProductCategory.top_level_for(environment) : @current_category.children @product = @profile.products.build(params[:product]) + @category = @product.product_category + @categories = ProductCategory.top_level_for(environment) + @level = 0 if request.post? if @product.save flash[:notice] = _('Product succesfully created') - redirect_to :action => 'show', :id => @product + render :partial => 'shared/redirect_via_javascript', + :locals => { :url => url_for(:controller => 'manage_products', :action => 'show', :id => @product) } else - flash[:notice] = _('Could not create the product') + render :partial => 'shared/dialog_error_messages', :locals => { :object_name => 'product' } end end end def edit - @product = @profile.products.find(params[:id]) - @object = @profile.products.find(params[:id]) + @object = @product = @profile.products.find(params[:id]) @current_category = @product.product_category @categories = @current_category.nil? ? [] : @current_category.children if request.post? @@ -62,56 +75,4 @@ class ManageProductsController < ApplicationController end end - def update_categories - @object = params[:id] ? @profile.products.find(params[:id]) : Product.new - if params[:category_id] - @current_category = Category.find(params[:category_id]) - @categories = @current_category.children - else - @current_category = ProductCategory.top_level_for(environment).first - @categories = @current_category.nil? ? [] : @current_category.children - end - render :partial => 'shared/select_categories', :locals => {:object_name => 'product', :multiple => false}, :layout => false - end - - def update_subcategories - @current_category = ProductCategory.find(params[:id]) if params[:id] - @categories = @current_category ? @current_category.children : ProductCategory.top_level_for(environment) - render :partial => 'subcategories' - end - - def new_consumption - @consumption = @profile.consumptions.build(params[:consumption]) - if request.post? - if @consumption.save - flash[:notice] = _('Raw material succesfully created') - redirect_to :action => 'index' - else - flash[:notice] = _('Could not create the raw material') - end - end - end - - def destroy_consumption - @consumption = @profile.consumptions.find(params[:id]) - if @consumption.destroy - flash[:notice] = _('Raw material succesfully removed') - else - flash[:notice] = _('Could not remove the raw material') - end - redirect_back_or_default :action => 'index' - end - - def edit_consumption - @consumption = @profile.consumptions.find(params[:id]) - if request.post? - if @consumption.update_attributes(params[:consumption]) - flash[:notice] = _('Raw material succesfully updated') - redirect_back_or_default :action => 'index' - else - flash[:notice] = _('Could not update the raw material') - end - end - end - end diff --git a/app/helpers/manage_products_helper.rb b/app/helpers/manage_products_helper.rb index dad5dd1..3638ffb 100644 --- a/app/helpers/manage_products_helper.rb +++ b/app/helpers/manage_products_helper.rb @@ -1,2 +1,71 @@ module ManageProductsHelper + + def remote_function_to_update_categories_selection(container_id, options = {}) + remote_function({ + :update => container_id, + :url => { :action => "categories_for_selection" }, + :loading => "loading('hierarchy_navigation', '#{ _('loading…') }'); loading('#{container_id}', ' ')", + :complete => "loading_done('hierarchy_navigation'); loading_done('#{container_id}')" + }.merge(options)) + end + + def hierarchy_category_item(category, make_links, title = nil) + title ||= category.name + if make_links + link_to(title, '#', + :title => title, + :onclick => remote_function_to_update_categories_selection("categories_container_level#{ category.level + 1 }", + :with => "'category_id=#{ category.id }'" + ) + ) + else + title + end + end + + def hierarchy_category_navigation(current_category, options = {}) + hierarchy = [] + if current_category + hierarchy << current_category.name + count_chars = current_category.name.length + ancestors = current_category.ancestors + toplevel = ancestors.pop + if toplevel + count_chars += toplevel.name.length + end + ancestors.each do |category| + if count_chars > 60 + hierarchy << hierarchy_category_item(category, options[:make_links], '( … )') + break + else + hierarchy << hierarchy_category_item(category, options[:make_links]) + end + count_chars += category.name.length + end + if toplevel + hierarchy << hierarchy_category_item(toplevel, options[:make_links]) + end + end + hierarchy.reverse.join(options[:separator] || ' → ') + end + + def options_for_select_categories(categories) + categories.sort_by{|cat| cat.name.transliterate}.map do |category| + "" + end.join("\n") + end + + def select_for_categories(categories, level = 0) + if categories.empty? + content_tag('div', '', :id => 'no_subcategories') + else + select_tag('category_id', + options_for_select_categories(categories), + :size => 10, + :onchange => remote_function_to_update_categories_selection("categories_container_level#{ level + 1 }", :with => "'category_id=' + this.value") + ) + + content_tag('div', '', :class => 'categories_container', :id => "categories_container_level#{ level + 1 }") + end + end + end diff --git a/app/models/product.rb b/app/models/product.rb index 3b4e218..923ca66 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -3,8 +3,8 @@ class Product < ActiveRecord::Base belongs_to :product_category has_many :product_categorizations - validates_presence_of :name - validates_uniqueness_of :name, :scope => :enterprise_id + validates_uniqueness_of :name, :scope => :enterprise_id, :allow_nil => true + validates_presence_of :product_category validates_numericality_of :price, :allow_nil => true after_update :save_image diff --git a/app/views/manage_products/_categories_for_selection.rhtml b/app/views/manage_products/_categories_for_selection.rhtml new file mode 100644 index 0000000..08100e8 --- /dev/null +++ b/app/views/manage_products/_categories_for_selection.rhtml @@ -0,0 +1,8 @@ +<%= select_for_categories(@categories, @level) %> + +<% javascript_tag do %> + jQuery('#categories_container_wrapper').scrollTo('100%', 1000) + $('product_product_category_id').value = <%= @category && @category.id %> + $('hierarchy_navigation').update('<%= escape_javascript(hierarchy_category_navigation(@category, :make_links => true)) %>') + toggleDisabled(<%= @category && @category.accept_products? ? 'true' : 'false' %>, $('save_and_continue')) +<% end %> diff --git a/app/views/manage_products/_form.rhtml b/app/views/manage_products/_form.rhtml index 235e718..7d1147e 100644 --- a/app/views/manage_products/_form.rhtml +++ b/app/views/manage_products/_form.rhtml @@ -3,7 +3,7 @@ <% form_for :product, @product, :html => {:multipart => true }, :url => {:action => mode} do |f| %> <%= required_fields_message %> - <%= required display_form_field( _('Name:'), f.text_field(:name) ) %> + <%= display_form_field( _('Name:'), f.text_field(:name) ) %> <%= display_form_field( _('Price:'), f.text_field(:price) ) %> <%= display_form_field( _('Description:'), f.text_area(:description, :rows => 10) ) %> <%= labelled_form_field(f.check_box(:highlighted) + _('Highlight this product'),'') %> @@ -11,10 +11,6 @@ <%= file_field_or_thumbnail(_('Image:'), @product.image, i) %> <% end %> -
<%= _('Current category:') %>
- <%= hidden_field_tag 'product[product_category_id]', @current_category.id %> - <%= link_to_remote( _('Back'), - :update => "subcategories", - :url => { :action => 'update_subcategories', :id => nil, :loaded => visual_effect(:highlight, "subcategories") }, - :class => 'select-current-category-link') + ' → ' - %> - <%= ([@current_category] + @current_category.ancestors).reverse.map{|i| - link_to_remote(i.name, - :update => "subcategories", - :url => { :action => 'update_subcategories', :id => i, :loaded => visual_effect(:highlight, "subcategories") }, - :class => 'select-current-category-link')}.join(' → ') - %> -<% end %> - -<% if @current_category.nil? %> -<%= _('Select a category:') %>
-<% elsif !@categories.empty? %> -<%= _('Select a subcategory:') %>
-<% end %> - -<% for category in @categories do %> - <%= link_to_remote category.name, { - :update => "subcategories", - :url => { :action => "update_subcategories", :id => category.id }, - :loaded => visual_effect(:highlight, "subcategories") }, - :class => 'select-subcategory-link' - %> -<% end %> diff --git a/app/views/manage_products/edit_consumption.rhtml b/app/views/manage_products/edit_consumption.rhtml deleted file mode 100644 index b57b95a..0000000 --- a/app/views/manage_products/edit_consumption.rhtml +++ /dev/null @@ -1,3 +0,0 @@ -<%= _('Raw material') %> | -<%= _('Actions') %> | -|
---|---|---|
<%= _('(no raw material registered yet)') %> | -||
- <%= link_to_product_category(consumption.product_category) %>
- - <%= consumption.aditional_specifications %> - |
- - <%= button :edit, _('Edit'), :action => 'edit_consumption', :id => consumption %> - <%= button :delete, _('Remove'), { :action => 'destroy_consumption', :id => consumption }, :confirm => _('Are you sure, you want to remove this raw material from your list') %> - | -
<%= _('Description:') %> <%= txt2html @product.description if @product.description %>
<%= _('Category:') %> <%= link_to_product_category(@product.product_category) %>
-<%= button :back, _('back'), :action => 'index' %> +<%= button :back, _('Back'), :action => 'index' %> -<%= button :delete, _('destroy'), :action => 'destroy', :id => @product %> -<%= button :edit, _('edit'), :action => 'edit', :id => @product %> +<%= button :delete, _('Destroy'), :action => 'destroy', :id => @product %> +<%= button :edit, _('Edit'), :action => 'edit', :id => @product %> diff --git a/db/migrate/20100617195958_add_accept_products_to_categories.rb b/db/migrate/20100617195958_add_accept_products_to_categories.rb new file mode 100644 index 0000000..3e468de --- /dev/null +++ b/db/migrate/20100617195958_add_accept_products_to_categories.rb @@ -0,0 +1,10 @@ +class AddAcceptProductsToCategories < ActiveRecord::Migration + def self.up + add_column :categories, :accept_products, :boolean, :default => true + execute 'UPDATE categories SET accept_products = (1 > 0)' + end + + def self.down + remove_column :categories, :accept_products + end +end diff --git a/db/migrate/20100621235235_set_product_category_id_to_products.rb b/db/migrate/20100621235235_set_product_category_id_to_products.rb new file mode 100644 index 0000000..b271b0c --- /dev/null +++ b/db/migrate/20100621235235_set_product_category_id_to_products.rb @@ -0,0 +1,13 @@ +class SetProductCategoryIdToProducts < ActiveRecord::Migration + def self.up + Product.all(:conditions => { :product_category_id => nil }).each do |product| + next if product.enterprise.nil? + product.product_category = ProductCategory.top_level_for(product.enterprise.environment).first + product.save! + end + end + + def self.down + say "WARNING: cannot undo this migration" + end +end diff --git a/db/schema.rb b/db/schema.rb index 39aa2f2..b5d4643 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,8 +9,7 @@ # # It's strongly recommended to check this file into your version control system. - -ActiveRecord::Schema.define(:version => 20100619031945) do +ActiveRecord::Schema.define(:version => 20100621235235) do create_table "article_versions", :force => true do |t| t.integer "article_id" @@ -130,6 +129,7 @@ ActiveRecord::Schema.define(:version => 20100619031945) do t.float "lng" t.boolean "display_in_menu", :default => false t.integer "children_count", :default => 0 + t.boolean "accept_products", :default => true end create_table "categories_profiles", :id => false, :force => true do |t| diff --git a/features/manage_products.feature b/features/manage_products.feature new file mode 100644 index 0000000..80269b3 --- /dev/null +++ b/features/manage_products.feature @@ -0,0 +1,136 @@ +Feature: manage products + As an enterprise owner + I want to manage my products + + Background: + Given the following users + | login | name | + | joaosilva | Joao Silva | + And the following enterprises + | identifier | owner | name | enabled | + | redemoinho | joaosilva | Rede Moinho | true | + And feature "disable_products_for_enterprises" is disabled on environment + And I am logged in as "joaosilva" + And I am on Rede Moinho's control panel + And I follow "Manage Products and Services" + + Scenario: listing products and services + Then I should see "Listing products and services" + + Scenario: see toplevel categories + Given the following product_categories + | name | + | Products | + | Services | + When I follow "New product or service" + Then I should see "Products" + And I should see "Service" + + @selenium + Scenario: select a toplevel category and see subcategories + Given the following product_categories + | name | + | Products level0 | + And the following product_categories + | name | parent | + | Computers level1 | products-level0 | + | DVDs level1 | products-level0 | + When I follow "New product or service" + And I select "Products level0 »" + Then I should see "Computers level1" + And I should see "DVDs level1" + + @selenium + Scenario: hide subcategories when select other toplevel category + Given the following product_categories + | name | + | Products level0 | + | Services level0 | + And the following product_categories + | name | parent | + | Computers level1 | products-level0 | + | Software development level1 | services-level0 | + When I follow "New product or service" + And I select "Products level0 »" + And I select "Computers level1" + And I select "Services level0 »" + Then I should see "Software development level1" + And I should not see "Computers level1" + + @selenium + Scenario: show hierarchy of categories + Given the following product_categories + | name | + | Products | + And the following product_category + | name | parent | + | Computers | products | + When I follow "New product or service" + And I select "Products »" + And I select "Computers" + Then I should see "Products → Computers" + + @selenium + Scenario: show links in hierarchy of categories and not link current category + Given the following product_category + | name | + | Toplevel Product Categories | + Given the following product_category + | name | parent | + | Category Level 1 | toplevel-product-categories | + When I follow "New product or service" + And I select "Toplevel Product Categories »" + And I select "Category Level 1" + Then I should see "Toplevel Product Categories" link + And I should not see "Category Level 1" link + + @selenium + Scenario: save button come initialy disabled + Given the following product_category + | name | + | Only for test | + When I go to /myprofile/redemoinho/manage_products/new + Then the "#save_and_continue" button should not be enabled + + @selenium + Scenario: enable save button when select one category + Given the following product_category + | name | + | Browsers (accept categories) | + When I follow "New product or service" + And I select "Browsers (accept categories)" + Then the "Save and continue" button should be enabled + + @selenium + Scenario: dont enable save button when select category with not accept products + Given the following product_category + | name | accept_products | + | Browsers | false | + When I follow "New product or service" + And I select "Browsers" + Then the "#save_and_continue" button should not be enabled + + @selenium + Scenario: save product + Given the following product_category + | name | + | Bicycle | + When I follow "New product or service" + And I select "Bicycle" + And I press "Save and continue" + Then I should see "Category: Bicycle" + + @selenium + Scenario: stay on the same place after error on save + Given the following product_category + | name | + | Bicycle | + And I follow "New product or service" + And I select "Bicycle" + And I press "Save and continue" + When I follow "Back" + And I follow "New product or service" + And I select "Bicycle" + And I press "Save and continue" + Then I should be on Rede Moinho's new product page + And I should see "Bicycle" diff --git a/features/search.feature b/features/search.feature index f5d22df..b764d11 100644 --- a/features/search.feature +++ b/features/search.feature @@ -55,10 +55,13 @@ Feature: search Given the following enterprises | identifier | name | | colivre-ent | Colivre | + And the following product_categories + | name | + | Development | And the following products - | owner | name | - | colivre-ent | social networks consultancy | - | colivre-ent | wikis consultancy | + | owner | category | name | + | colivre-ent | development | social networks consultancy | + | colivre-ent | development | wikis consultancy | When I go to the search page And I fill in "query" with "wikis" And I press "Search" diff --git a/features/step_definitions/custom_webrat_steps.rb b/features/step_definitions/custom_webrat_steps.rb index a096b94..7035954 100644 --- a/features/step_definitions/custom_webrat_steps.rb +++ b/features/step_definitions/custom_webrat_steps.rb @@ -1,11 +1,20 @@ -When /^I should see "([^\"]+)" link$/ do |link| - response.should have_selector("a", :content => link) +When /^I should see "([^\"]+)" link$/ do |text| + response.should have_selector("a:contains('#{text}')") end -When /^I should not see "([^\"]+)" link$/ do |link| - response.should_not have_selector("a", :content => link) +When /^I should not see "([^\"]+)" link$/ do |text| + response.should_not have_selector("a:contains('#{text}')") end Then /^I should be exactly on (.+)$/ do |page_name| URI.parse(current_url).request_uri.should == path_to(page_name) end + +When /^I select "([^\"]*)"$/ do |value| + select(value) + # FIXME ugly hack to make selenium tests waiting to render page + # "select(value, :wait_for => :ajax)" did not effect + if selenium + selenium.wait_for_ajax + end +end diff --git a/features/step_definitions/noosfero_steps.rb b/features/step_definitions/noosfero_steps.rb index 6f65c64..344a45e 100644 --- a/features/step_definitions/noosfero_steps.rb +++ b/features/step_definitions/noosfero_steps.rb @@ -61,7 +61,8 @@ Given /^the following products$/ do |table| table.hashes.each do |item| data = item.dup owner = Enterprise[data.delete("owner")] - Product.create!(data.merge(:enterprise => owner)) + category = Category.find_by_slug(data.delete("category")) + Product.create!(data.merge(:enterprise => owner, :product_category => category)) end end @@ -84,6 +85,18 @@ Given /^the following validation info$/ do |table| end end +Given /^the following (product_categories|product_category|category|categories)$/ do |kind,table| + klass = kind.singularize.camelize.constantize + table.hashes.each do |row| + parent = row.delete("parent") + if parent + parent = Category.find_by_slug(parent) + row.merge!({:parent_id => parent.id}) + end + category = klass.create!({:environment_id => Environment.default.id}.merge(row)) + end +end + Given /^I am logged in as "(.+)"$/ do |username| visit('/account/logout') visit('/account/login') diff --git a/features/step_definitions/selenium_steps.rb b/features/step_definitions/selenium_steps.rb index 69dfa09..9297192 100644 --- a/features/step_definitions/selenium_steps.rb +++ b/features/step_definitions/selenium_steps.rb @@ -3,8 +3,12 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "pat def string_to_element_locator(selector) if selector.gsub!(/^\./, '') "css=[class='#{selector}']" + elsif selector.gsub!(/^value\./, '') + "xpath=//input[@value='#{selector}']" + elsif selector.gsub!(/^#/, '') + "css=[id='#{selector}']" else - raise "I can't find '#{selector}'!" + selector end end @@ -19,3 +23,11 @@ end When /^I click "([^\"]*)"$/ do |selector| selenium.click(string_to_element_locator(selector)) end + +Then /^the "([^\"]*)" button should not be enabled$/ do |text| + selenium.is_editable(string_to_element_locator(text)).should be_false +end + +Then /^the "([^\"]*)" button should be enabled$/ do |text| + selenium.is_editable(string_to_element_locator("value.#{text}")).should be_true +end diff --git a/features/support/paths.rb b/features/support/paths.rb index 8075c12..1bef6aa 100644 --- a/features/support/paths.rb +++ b/features/support/paths.rb @@ -51,6 +51,9 @@ module NavigationHelpers when /^(.+)'s members management/ '/myprofile/%s/profile_members' % Profile.find_by_name($1).identifier + when /^(.+)'s new product page/ + '/myprofile/%s/manage_products/new' % Profile.find_by_name($1).identifier + # Add more mappings here. # Here is a more fancy example: # diff --git a/public/images/ccc.gif b/public/images/ccc.gif new file mode 100644 index 0000000..6066bef Binary files /dev/null and b/public/images/ccc.gif differ diff --git a/public/images/fff.gif b/public/images/fff.gif new file mode 100644 index 0000000..81240ae Binary files /dev/null and b/public/images/fff.gif differ diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 7cd0893..27ba09e 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -79,6 +79,15 @@ function disable_button(button) { button.addClassName("disabled"); } +function toggleDisabled(enable, element) { + if (enable) { + enable_button(element); + } + else { + disable_button(element); + } +} + /* ICON SELECTOR - LinkListBlock */ function showIconSelector(main_div) { diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css index c8f0108..8220fc8 100644 --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -113,6 +113,15 @@ a img { border: none; } +.tooltip { + padding: 8px; + border: 1px solid #FF8000; + background-color: #FFFF66; + z-index: 100; + -moz-border-radius: 3px; + -webkit-border-radius: 3px; +} + /*********************************************************/ @@ -1332,6 +1341,12 @@ input.button.with-text:hover { border: none; } +body.noosfero a.button.with-text.icon-none, +body.noosfero input.button.with-text.icon-none { + padding-left: 2px; +} + +a.button.disabled, input.disabled { opacity: 0.5; filter-opacity: 50%; @@ -2989,6 +3004,70 @@ h1#agenda-title { /* ==> public/stylesheets/controller_manage_products.css <== */ +#new_product_title { + text-align: center; +} +#new_product_form { + border: 1px solid #AABB88; + -moz-border-radius: 5px; + -webkit-border-radius: 5px; + background: url(/images/ccc.gif); /* image ccc.gif from http://www.wannabegirl.org/translucent */ + padding: 10px 30px 20px 30px; +} +#categories_container_wrapper { + overflow-x: scroll; + margin: 10px 0 15px 0; + padding: 5px; + -moz-border-radius: 3px; + -webkit-border-radius: 3px; + background: url(/images/fff.gif); /* image fff.gif from http://www.wannabegirl.org/translucent */ + border: 1px solid #CCC; + border-bottom: 0; + position: relative; +} +.categories_container { + min-height: 176px; + min-width: 100px; + display: inline-block; + position: relative; +} +.categories_container .categories_container { + position: absolute; + top: 0; + left: 100%; +} +.msie .categories_container .categories_container { + left: auto; +} +.categories_container select { + margin-right: 10px; + height: 176px; +} +.controller-manage_products #categories_selection_actionbar input.button { + height: 23px !important; +} +#save_and_continue_wrapper { + position: relative; +} +#save_and_continue_disabled_tooltip { + display: none; + position: absolute; + top: 30px; + left: 0; + min-width: 250px; +} +.msie #save_and_continue_disabled_tooltip { + font-size: small; +} +#save_and_continue_wrapper #save_and_continue.disabled:hover + #save_and_continue_disabled_tooltip { + display: block; +} +#hierarchy_navigation { + min-height: 1em; +} +.msie #hierarchy_navigation { + font-size: small; +} /* ==> public/stylesheets/controller_memberships.css <== */ /* @import url(profile-list-block.css); ==> BROKEN REFERENCE, OH MY! */ diff --git a/script/sample-products b/script/sample-products new file mode 100755 index 0000000..3c9130f --- /dev/null +++ b/script/sample-products @@ -0,0 +1,25 @@ +#!/usr/bin/env ruby +require File.dirname(__FILE__) + '/../config/environment' +require File.dirname(__FILE__) + '/../test/test_helper' +include ActionController::TestProcess + +# tourn on autoflush +STDOUT.sync = true + +enterprises = Enterprise.all + +print "Creating products: " +THINGS = %w[ Car House Bicycle Book Pen Computer Webcam ] +COLORS = %w[ Black Red White Blue Green Purple ] +for thing in THINGS + for color in COLORS + name = [color, thing].join(' ') + rand(10).times do |i| + Product.create(:name => name, :enterprise_id => enterprises.rand.id, :price => (i * 13.7), :image_builder => { + :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') + }) + print '.' + end + end +end +puts ' done!' diff --git a/test/factories.rb b/test/factories.rb index c6cfe85..3e26bca 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -212,7 +212,7 @@ module Noosfero::Factory end ############################################### - # Article + # Article (and friends) ############################################### def defaults_for_article @@ -220,18 +220,12 @@ module Noosfero::Factory { :name => name, :slug => name.to_slug, :path => name.to_slug } end - alias :defaults_for_text_article :defaults_for_article - alias :defaults_for_textile_article :defaults_for_article - alias :defaults_for_tiny_mce_article :defaults_for_article - alias :defaults_for_rss_feed :defaults_for_article - - ############################################### - # Folder - ############################################### - - def defaults_for_folder - defaults_for_article - end + alias :defaults_for_text_article :defaults_for_article + alias :defaults_for_textile_article :defaults_for_article + alias :defaults_for_tiny_mce_article :defaults_for_article + alias :defaults_for_rss_feed :defaults_for_article + alias :defaults_for_textile_article :defaults_for_article + alias :defaults_for_folder :defaults_for_article ############################################### # Event @@ -300,6 +294,7 @@ module Noosfero::Factory end alias :defaults_for_region :defaults_for_category + alias :defaults_for_product_category :defaults_for_category ############################################### # Box @@ -327,4 +322,12 @@ module Noosfero::Factory alias :defaults_for_create_community :defaults_for_task alias :defaults_for_email_activation :defaults_for_task + ############################################### + # Product + ############################################### + + def defaults_for_product + { :name => 'Product ' + factory_num_seq.to_s } + end + end diff --git a/test/fixtures/roles.yml b/test/fixtures/roles.yml index 6da2688..4fcdeb9 100644 --- a/test/fixtures/roles.yml +++ b/test/fixtures/roles.yml @@ -49,6 +49,7 @@ profile_admin: - perform_task - post_content - view_private_content + - manage_products profile_member: id: 6 environment_id: 1 diff --git a/test/functional/catalog_controller_test.rb b/test/functional/catalog_controller_test.rb index 79aee27..ae9409e 100644 --- a/test/functional/catalog_controller_test.rb +++ b/test/functional/catalog_controller_test.rb @@ -10,13 +10,13 @@ class CatalogControllerTest < Test::Unit::TestCase @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new - @enterprise = Enterprise.create!(:name => 'My enterprise', :identifier => 'testent') + @enterprise = fast_create(Enterprise, :name => 'My enterprise', :identifier => 'testent') + @product_category = fast_create(ProductCategory) end attr_accessor :enterprise def test_local_files_reference - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - assert_local_files_reference :get, :index, :profile => ent.identifier + assert_local_files_reference :get, :index, :profile => @enterprise.identifier end def test_valid_xhtml @@ -35,22 +35,20 @@ class CatalogControllerTest < Test::Unit::TestCase end should 'list products of enterprise' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - get :index, :profile => ent.identifier + get :index, :profile => @enterprise.identifier assert_kind_of Array, assigns(:products) end should 'show product of enterprise' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - prod = ent.products.create!(:name => 'Product test') - get :show, :id => prod.id, :profile => ent.identifier + prod = @enterprise.products.create!(:name => 'Product test', :product_category => @product_category) + get :show, :id => prod.id, :profile => @enterprise.identifier assert_tag :tag => 'h1', :content => /#{prod.name}/ end should 'link back to index from product show' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - prod = ent.products.create!(:name => 'Product test') - get :show, :id => prod.id, :profile => ent.identifier + enterprise = Enterprise.create!(:name => 'test_enterprise_1', :identifier => 'test_enterprise_1', :environment => Environment.default) + prod = enterprise.products.create!(:name => 'Product test', :product_category => @product_category) + get :show, :id => prod.id, :profile => enterprise.identifier assert_tag({ :tag => 'div', :attributes => { @@ -59,7 +57,7 @@ class CatalogControllerTest < Test::Unit::TestCase :descendant => { :tag => 'a', :attributes => { - :href => '/catalog/test_enterprise1' + :href => "/catalog/#{enterprise.identifier}" } } }) @@ -77,43 +75,31 @@ class CatalogControllerTest < Test::Unit::TestCase end should 'not show product price when listing products if not informed' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - prod = ent.products.create!(:name => 'Product test') - get :index, :profile => ent.identifier + prod = @enterprise.products.create!(:name => 'Product test', :product_category => @product_category) + get :index, :profile => @enterprise.identifier assert_no_tag :tag => 'li', :attributes => { :class => 'product_price' }, :content => /Price:/ end should 'show product price when listing products if informed' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - prod = ent.products.create!(:name => 'Product test', :price => 50.00) - get :index, :profile => ent.identifier + prod = @enterprise.products.create!(:name => 'Product test', :price => 50.00, :product_category => @product_category) + get :index, :profile => @enterprise.identifier assert_tag :tag => 'li', :attributes => { :class => 'product_price' }, :content => /Price:/ end should 'not show product price when showing product if not informed' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - prod = ent.products.create!(:name => 'Product test') - get :show, :id => prod.id, :profile => ent.identifier + prod = @enterprise.products.create!(:name => 'Product test', :product_category => @product_category) + get :show, :id => prod.id, :profile => @enterprise.identifier assert_no_tag :tag => 'p', :attributes => { :class => 'product_price' }, :content => /Price:/ end should 'show product price when showing product if informed' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1') - prod = ent.products.create!(:name => 'Product test', :price => 50.00) - get :show, :id => prod.id, :profile => ent.identifier + prod = @enterprise.products.create!(:name => 'Product test', :price => 50.00, :product_category => @product_category) + get :show, :id => prod.id, :profile => @enterprise.identifier assert_tag :tag => 'p', :attributes => { :class => 'product_price' }, :content => /Price:/ end - should 'not crash on index when product has no product_category and enterprise not enabled' do - ent = Enterprise.create!(:identifier => 'test_enterprise1', :name => 'Test enteprise1', :enabled => false) - prod = ent.products.create!(:name => 'Product test', :price => 50.00, :product_category => nil) - assert_nothing_raised do - get :index, :profile => ent.identifier - end - end - should 'link to assets products wiht product category in the link to product category on index' do pc = ProductCategory.create!(:name => 'some product', :environment => enterprise.environment) prod = enterprise.products.create!(:name => 'Product test', :price => 50.00, :product_category => pc) diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb index b09c21b..f352090 100644 --- a/test/functional/manage_products_controller_test.rb +++ b/test/functional/manage_products_controller_test.rb @@ -13,6 +13,8 @@ class ManageProductsControllerTest < Test::Unit::TestCase @response = ActionController::TestResponse.new @enterprise = Enterprise.create(:name => 'teste', :identifier => 'test_ent') @user = create_user_with_permission('test_user', 'manage_products', @enterprise) + @environment = @enterprise.environment + @product_category = fast_create(ProductCategory) login_as :test_user end @@ -48,16 +50,15 @@ class ManageProductsControllerTest < Test::Unit::TestCase should "create new product" do assert_difference Product, :count do - post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'} - assert_response :redirect + post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product', :product_category_id => @product_category.id} assert assigns(:product) - assert ! assigns(:product).new_record? + assert !assigns(:product).new_record? end end should "not create invalid product" do assert_no_difference Product, :count do - post 'new', :profile => @enterprise.identifier, :product => {:price => 'test product'} + post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'} assert_response :success assert assigns(:product) assert assigns(:product).new_record? @@ -65,7 +66,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should "get edit form" do - p = @enterprise.products.create(:name => 'test product') + p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) get 'edit', :profile => @enterprise.identifier, :id => p.id assert_response :success assert assigns(:product) @@ -74,7 +75,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should "edit product" do - p = @enterprise.products.create(:name => 'test product') + p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) post 'edit', :profile => @enterprise.identifier, :product => {:name => 'new test product'}, :id => p.id assert_response :redirect assert assigns(:product) @@ -83,15 +84,15 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should "not edit to invalid parameters" do - p = @enterprise.products.create(:name => 'test product') - post 'edit', :profile => @enterprise.identifier, :product => {:name => ''}, :id => p.id + p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) + post 'edit', :profile => @enterprise.identifier, :product => {:product_category => nil}, :id => p.id assert_response :success assert assigns(:product) assert ! assigns(:product).valid? end should "destroy product" do - p = @enterprise.products.create(:name => 'test product') + p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) assert_difference Product, :count, -1 do post 'destroy', :profile => @enterprise.identifier, :id => p.id assert_response :redirect @@ -102,7 +103,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should "fail to destroy product" do - p = @enterprise.products.create(:name => 'test product') + p = @enterprise.products.create!(:name => 'test product', :product_category => @product_category) Product.any_instance.stubs(:destroy).returns(false) assert_no_difference Product, :count do post 'destroy', :profile => @enterprise.identifier, :id => p.id @@ -113,54 +114,17 @@ class ManageProductsControllerTest < Test::Unit::TestCase end end - should 'show categories list' do - environment = Environment.default - category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) - category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) - category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2) + should 'show categories selection' do + category1 = fast_create(ProductCategory, :name => 'Category 1') + category2 = fast_create(ProductCategory, :name => 'Category 2', :parent_id => category1.id) + category3 = fast_create(ProductCategory, :name => 'Category 3', :parent_id => category2.id) get :new, :profile => @enterprise.identifier - assert_tag :tag => 'p', :content => /Select a category:/ - assert_tag :tag => 'a', :content => /#{category1.name}/ - end - - should 'show current category' do - environment = Environment.default - category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) - category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) - category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2) - get 'update_categories', :profile => @enterprise.identifier, :category_id => category2.id - assert_tag :tag => 'h3', :content => /Current category:/, :sibling => { :tag => 'a', :content => /#{category3.name}/ } - end - - should 'show subcategories list' do - environment = Environment.default - category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) - category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) - category3 = ProductCategory.create!(:name => 'Category 3', :environment => environment, :parent => category2) - get 'update_categories', :profile => @enterprise.identifier, :category_id => category2.id - assert !assigns(:categories).empty? - assert_tag :tag => 'h3', :content => /Categories:/, :sibling => { :tag => 'a', :attributes => { :href => '#' }, :content => /#{category2.name}/ } - end - - should 'update subcategories' do - environment = Environment.default - category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) - category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) - get 'update_categories', :profile => @enterprise.identifier, :category_id => category1.id - assert_tag :tag => 'a', :attributes => { :href => '#' }, :content => /#{category2.name}/ - end - - should 'not show subcategories list when no subcategories' do - environment = Environment.default - category1 = @enterprise.products.create!(:name => 'Category 1') - get 'update_categories', :profile => @enterprise.identifier, :id => category1.id - assert_no_tag :tag => 'h3', :content => 'Categories:' + assert_tag :tag => 'select', :attributes => { :id => 'category_id' }, :descendant => { :tag => 'option', :content => category1.name } end should "create new product categorized" do - environment = Environment.default - category1 = ProductCategory.create!(:name => 'Category 1', :environment => environment) - category2 = ProductCategory.create!(:name => 'Category 2', :environment => environment, :parent => category1) + category1 = fast_create(ProductCategory, :name => 'Category 1') + category2 = fast_create(ProductCategory, :name => 'Category 2', :parent_id => category1) assert_difference Product, :count do post 'new', :profile => @enterprise.identifier, :product => { :name => 'test product', :product_category_id => category2.id } assert_equal category2, assigns(:product).product_category @@ -168,7 +132,7 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should 'show thumbnail image when edit product' do - p = @enterprise.products.create!(:name => 'test product1', :image_builder => { + p = @enterprise.products.create!(:name => 'test product1', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) get 'edit', :profile => @enterprise.identifier, :id => p.id @@ -176,85 +140,25 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should 'show change image link above thumbnail image' do - p = @enterprise.products.create!(:name => 'test product1', :image_builder => { + p = @enterprise.products.create!(:name => 'test product1', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) get 'edit', :profile => @enterprise.identifier, :id => p.id assert_tag :tag => 'a', :attributes => { :href => '#' }, :content => 'Change image' end - should 'show change image field when new product' do - get 'new', :profile => @enterprise.identifier - assert_tag :tag => 'input', :attributes => { :type => 'file', :name => 'product[image_builder][uploaded_data]' } - end - should 'filter html from name of product' do - category = ProductCategory.create!(:name => 'Category 1', :environment => Environment.default) + category = fast_create(ProductCategory, :name => 'Category 1') post 'new', :profile => @enterprise.identifier, :product => { :name => "name bold", :product_category_id => category.id } assert_sanitized assigns(:product).name end should 'filter html from description of product' do - category = ProductCategory.create!(:name => 'Category 1', :environment => Environment.default) + category = fast_create(ProductCategory, :name => 'Category 1') post 'new', :profile => @enterprise.identifier, :product => { :name => 'name', :description => "descr bold", :product_category_id => category.id } assert_sanitized assigns(:product).description end - - should 'display new consumption form' do - get :new_consumption, :profile => @enterprise.identifier - assert_tag :tag => 'h2', :content => 'Add raw material' - end - - should 'create consumption product' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - assert_difference Consumption, :count do - post :new_consumption, :profile => @enterprise.identifier, :consumption => { :product_category_id => product_category.id } - end - end - - should 'display list of consumption products' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - @enterprise.consumptions.create!(:product_category_id => product_category.id, :aditional_specifications => 'extra info') - get :index, :profile => @enterprise.identifier - assert_tag :tag => 'em', :content => 'extra info' - end - - should 'filter html from consumption specifications' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - post :new_consumption, :profile => @enterprise.identifier, - :consumption => { :product_category_id => product_category.id, :aditional_specifications => 'extra info' } - assert_sanitized assigns(:consumption).aditional_specifications - end - - should 'destroy consumption product' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - product = @enterprise.consumptions.create!(:product_category_id => product_category.id, :aditional_specifications => 'extra info') - assert_difference Consumption, :count, -1 do - post :destroy_consumption, :profile => @enterprise.identifier, :id => product.id - end - end - should 'display edit consumption form' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - product = @enterprise.consumptions.create!(:product_category_id => product_category.id, :aditional_specifications => 'extra info') - get :edit_consumption, :profile => @enterprise.identifier, :id => product - assert_tag :tag => 'h2', :content => 'Editing Food' - end - - should 'update consumption product' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - product = @enterprise.consumptions.create!(:product_category_id => product_category.id, :aditional_specifications => 'extra info') - post :edit_consumption, :profile => @enterprise.identifier, :id => product, :consumption => { :aditional_specifications => 'new extra info' } - assert_equal 'new extra info', @enterprise.consumptions.find(product.reload.id).aditional_specifications - end - - should 'not show product_category field on edit consumption form' do - product_category = ProductCategory.create!(:name => 'Food', :environment => Environment.default) - product = @enterprise.consumptions.create!(:product_category_id => product_category.id, :aditional_specifications => 'extra info') - get :edit_consumption, :profile => @enterprise.identifier, :id => product - assert_no_tag :tag => 'select', :attributes => { :name => 'consumption[product_category_id]' } - end - should 'not let users in if environment do not let' do env = Environment.default env.enable('disable_products_for_enterprises') @@ -267,30 +171,65 @@ class ManageProductsControllerTest < Test::Unit::TestCase end should 'show top level product categories for the user to choose' do - pc1 = ProductCategory.create!(:name => 'test_category1', :environment => Environment.default) - pc2 = ProductCategory.create!(:name => 'test_category2', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test_category1') + pc2 = fast_create(ProductCategory, :name => 'test_category2') get :new, :profile => @enterprise.identifier - assert_equivalent [pc1, pc2], assigns(:categories) + assert_equivalent ProductCategory.top_level_for(pc1.environment), assigns(:categories) end - should 'links to products asset for consumption link' do - pc = ProductCategory.create!(:name => 'test_category', :environment =>@enterprise.environment) - @enterprise.consumptions.create!(:product_category => pc) + should 'links to products asset for product category link when showing' do + pc = fast_create(ProductCategory, :name => 'test_category') + p = @enterprise.products.create!(:name => 'test product', :product_category => pc) - get :index, :profile => @enterprise.identifier + get :show, :profile => @enterprise.identifier, :id => p.id assert_tag :tag => 'a', :attributes => {:href => /assets\/products\?product_category=#{pc.id}/} end - should 'links to products asset for product category link when showing' do - pc = ProductCategory.create!(:name => 'test_category', :environment =>@enterprise.environment) - p = @enterprise.products.create!(:name => 'test product', :product_category => pc) + should 'increase level while navigate on hierarchy categories' do + category_level0 = fast_create(ProductCategory, :name => 'Products', :environment_id => @environment.id) + category_level1 = fast_create(ProductCategory, :parent_id => category_level0.id, :name => 'Shoes', :environment_id => @environment.id) + category_level2 = fast_create(ProductCategory, :parent_id => category_level1.id, :name => 'Athletic Shoes', :environment_id => @environment.id) - get :show, :profile => @enterprise.identifier, :id => p.id + get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category_level0.id + assert_equal 0, assigns(:level) - assert_tag :tag => 'a', :attributes => {:href => /assets\/products\?product_category=#{pc.id}/} + get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category_level1.id + assert_equal 1, assigns(:level) + + get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category_level2.id + assert_equal 2, assigns(:level) end - + + should 'remember the selected category' do + category0 = fast_create(ProductCategory, :name => 'Products', :environment_id => @environment.id) + category1 = fast_create(ProductCategory, :name => 'Shoes', :environment_id => @environment.id) + + get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category0.id + assert_equal category0, assigns(:category) + + get :categories_for_selection, :profile => @enterprise.identifier, :category_id => category1.id + assert_equal category1, assigns(:category) + end + + should 'list top level categories when has no category_id' do + get :categories_for_selection, :profile => @enterprise.identifier + + assert_equal ProductCategory.top_level_for(@environment), assigns(:categories) + end + + should 'render dialog_error_messages template for invalid product' do + post :new, :profile => @enterprise.identifier, :product => { :name => 'Invalid product' } + assert_template 'shared/_dialog_error_messages' + end + + should 'render redirect_via_javascript template after save' do + assert_difference Product, :count do + post :new, :profile => @enterprise.identifier, :product => { :name => 'Invalid product', :product_category_id => @product_category.id } + assert_template 'shared/_redirect_via_javascript' + end + end + end diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index ab721e9..98f44cf 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -72,6 +72,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase @controller.stubs(:boxes_holder).returns(holder) login_as 'designtestuser' + + @product_category = fast_create(ProductCategory) end attr_reader :profile @@ -217,8 +219,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase block = ProductsBlock.new enterprise = Enterprise.create!(:name => "test", :identifier => 'testenterprise') - p1 = enterprise.products.create!(:name => 'product one') - p2 = enterprise.products.create!(:name => 'product two') + p1 = enterprise.products.create!(:name => 'product one', :product_category => @product_category) + p2 = enterprise.products.create!(:name => 'product two', :product_category => @product_category) enterprise.boxes.first.blocks << block enterprise.add_admin(holder) @@ -237,8 +239,8 @@ class ProfileDesignControllerTest < Test::Unit::TestCase block = ProductsBlock.new enterprise = Enterprise.create!(:name => "test", :identifier => 'testenterprise') - p1 = enterprise.products.create!(:name => 'product one') - p2 = enterprise.products.create!(:name => 'product two') + p1 = enterprise.products.create!(:name => 'product one', :product_category => @product_category) + p2 = enterprise.products.create!(:name => 'product two', :product_category => @product_category) enterprise.boxes.first.blocks << block enterprise.add_admin(holder) diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 337eb43..832f671 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -15,6 +15,8 @@ class SearchControllerTest < Test::Unit::TestCase domain = Environment.default.domains.first domain.google_maps_key = 'ENVIRONMENT_KEY' domain.save! + + @product_category = fast_create(ProductCategory) end def create_article_with_optional_category(name, profile, category = nil) @@ -244,7 +246,7 @@ class SearchControllerTest < Test::Unit::TestCase should 'find products' do ent = create_profile_with_optional_category(Enterprise, 'teste') - prod = ent.products.create!(:name => 'a beautiful product') + prod = ent.products.create!(:name => 'a beautiful product', :product_category => @product_category) get 'index', :query => 'beautiful', :find_in => ['products'] assert_includes assigns(:results)[:products], prod end @@ -252,8 +254,8 @@ class SearchControllerTest < Test::Unit::TestCase should 'find products in a specific category' do ent1 = create_profile_with_optional_category(Enterprise, 'teste1', @category) ent2 = create_profile_with_optional_category(Enterprise, 'teste2') - prod1 = ent1.products.create!(:name => 'a beautiful product') - prod2 = ent2.products.create!(:name => 'another beautiful product') + prod1 = ent1.products.create!(:name => 'a beautiful product', :product_category => @product_category) + prod2 = ent2.products.create!(:name => 'another beautiful product', :product_category => @product_category) get :index, :category_path => @category.path.split('/'), :query => 'beautiful', :find_in => ['products'] assert_includes assigns(:results)[:products], prod1 assert_not_includes assigns(:results)[:products], prod2 @@ -265,8 +267,8 @@ class SearchControllerTest < Test::Unit::TestCase ent1 = create_profile_with_optional_category(Enterprise, 'teste1') ent2 = create_profile_with_optional_category(Enterprise, 'teste2') - prod1 = ent1.products.create!(:name => 'a beautiful product') - prod2 = ent2.products.create!(:name => 'another beautiful product') + prod1 = ent1.products.create!(:name => 'a beautiful product', :product_category => @product_category) + prod2 = ent2.products.create!(:name => 'another beautiful product', :product_category => @product_category) get :assets, :asset => 'products' assert_equivalent [prod2, prod1], assigns(:results)[:products] @@ -278,11 +280,11 @@ class SearchControllerTest < Test::Unit::TestCase # in category ent1 = create_profile_with_optional_category(Enterprise, 'teste1', @category) - prod1 = ent1.products.create!(:name => 'a beautiful product') + prod1 = ent1.products.create!(:name => 'a beautiful product', :product_category => @product_category) # not in category ent2 = create_profile_with_optional_category(Enterprise, 'teste2') - prod2 = ent2.products.create!(:name => 'another beautiful product') + prod2 = ent2.products.create!(:name => 'another beautiful product', :product_category => @product_category) get :assets, :asset => 'products', :category_path => [ 'my-category' ] @@ -301,7 +303,7 @@ class SearchControllerTest < Test::Unit::TestCase should 'display search results' do ent = create_profile_with_optional_category(Enterprise, 'display enterprise') - product = ent.products.create!(:name => 'display product') + product = ent.products.create!(:name => 'display product', :product_category => @product_category) person = create_user('displayperson').person; person.name = 'display person'; person.save! article = person.articles.create!(:name => 'display article') event = Event.new(:name => 'display event', :start_date => Date.today); event.profile = person; event.save! @@ -919,9 +921,9 @@ class SearchControllerTest < Test::Unit::TestCase should 'search for products by origin and radius correctly' do s = City.create!(:name => 'Salvador', :lat => -12.97, :lng => -38.51, :environment => Environment.default) e1 = create_profile_with_optional_category(Enterprise, 'test ent 1', nil, :lat => -12.97, :lng => -38.51) - p1 = e1.products.create!(:name => 'test_product1') + p1 = e1.products.create!(:name => 'test_product1', :product_category => @product_category) e2 = create_profile_with_optional_category(Enterprise, 'test ent 2', nil, :lat => -14.97, :lng => -40.51) - p2 = e2.products.create!(:name => 'test_product2') + p2 = e2.products.create!(:name => 'test_product2', :product_category => @product_category) get :assets, :asset => 'products', :city => s.id, :radius => 15 @@ -988,7 +990,7 @@ class SearchControllerTest < Test::Unit::TestCase should 'find products when enterprises has own hostname' do ent = create_profile_with_optional_category(Enterprise, 'teste') ent.domains << Domain.new(:name => 'testent.com'); ent.save! - prod = ent.products.create!(:name => 'a beautiful product') + prod = ent.products.create!(:name => 'a beautiful product', :product_category => @product_category) get 'index', :query => 'beautiful', :find_in => ['products'] assert_includes assigns(:results)[:products], prod end diff --git a/test/test_helper.rb b/test/test_helper.rb index 5c641c1..a8ecdb7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -177,6 +177,40 @@ class Test::Unit::TestCase end +module NoosferoTestHelper + def link_to(content, url, options = {}) + "#{content}" + end + + def content_tag(tag, content, options = {}) + "<#{tag}>#{content}#{tag}>" + end + + def submit_tag(content, options = {}) + content + end + + def remote_function(options = {}) + '' + end + + def tag(tag) + "<#{tag}/>" + end + + def options_from_collection_for_select(collection, value_method, content_method) + "" + end + + def select_tag(id, collection, options = {}) + "" + end + + def options_for_select(collection) + collection.map{|item| ""}.join("\n") + end +end + class ActionController::IntegrationTest def assert_can_login assert_tag :tag => 'a', :attributes => { :id => 'link_login' } diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 9667090..1fc64a7 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -796,7 +796,7 @@ class ArticleTest < Test::Unit::TestCase end should 'update slug from name' do - article = create(Article, :name => 'A test article', :profile_id => profile.id) + article = Article.create!(:name => 'A test article', :profile_id => profile.id) assert_equal 'a-test-article', article.slug article.name = 'Changed name' assert_equal 'changed-name', article.slug diff --git a/test/unit/category_finder_test.rb b/test/unit/category_finder_test.rb index 5752234..3c240b2 100644 --- a/test/unit/category_finder_test.rb +++ b/test/unit/category_finder_test.rb @@ -5,6 +5,7 @@ class CategoryFinderTest < ActiveSupport::TestCase def setup @category = Category.create!(:name => 'my category', :environment => Environment.default) @finder = CategoryFinder.new(@category) + @product_category = fast_create(ProductCategory, :name => 'Products') Profile.rebuild_index end @@ -48,7 +49,7 @@ class CategoryFinderTest < ActiveSupport::TestCase ent1 = Enterprise.create!(:name => 'beautiful enterprise 1', :identifier => 'test1', :category_ids => [@category.id]) # not in category - ent2 = Enterprise.create!(:name => 'beautiful enterprise 2', :identifier => 'test2') + ent2 = fast_create(Enterprise, :name => 'beautiful enterprise 2', :identifier => 'test2') list = @finder.find(:enterprises, 'beautiful') assert_includes list, ent1 @@ -65,8 +66,8 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'search for communities in a specific category' do - c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default) - c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default) + c1 = fast_create(Community, :name => 'a beautiful community', :identifier => 'bea_comm', :environment_id => Environment.default.id) + c2 = fast_create(Community, :name => 'another beautiful community', :identifier => 'an_bea_comm', :environment_id => Environment.default.id) c1.add_category(@category); c1.save! list = @finder.find(:communities, 'beautiful') @@ -75,10 +76,10 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'search for products in a specific category' do - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1'); ent1.add_category(@category) - ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') - prod1 = ent1.products.create!(:name => 'a beautiful product') - prod2 = ent2.products.create!(:name => 'another beautiful product') + ent1 = fast_create(Enterprise, :name => 'teste1', :identifier => 'teste1'); ent1.add_category(@category) + ent2 = fast_create(Enterprise, :name => 'teste2', :identifier => 'teste2') + prod1 = ent1.products.create!(:name => 'a beautiful product', :product_category => @product_category) + prod2 = ent2.products.create!(:name => 'another beautiful product', :product_category => @product_category) list = @finder.find(:products, 'beautiful') assert_includes list, prod1 @@ -86,8 +87,8 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'search people in category hierarchy' do - parent = Category.create!(:name => 'parent category', :environment => Environment.default) - child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent) + parent = fast_create(Category, :name => 'parent category', :environment_id => Environment.default.id) + child = fast_create(Category, :name => 'child category', :environment_id => Environment.default.id, :parent_id => parent.id) p1 = create_user('people_1').person p1.name = 'a beautiful person' p1.add_category(child) @@ -100,8 +101,8 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'search article in category hierarchy' do - parent = Category.create!(:name => 'parent category', :environment => Environment.default) - child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent) + parent = fast_create(Category, :name => 'parent category', :environment_id => Environment.default.id) + child = fast_create(Category, :name => 'child category', :environment_id => Environment.default.id, :parent_id => parent.id) p1 = create_user('people_1').person @@ -174,8 +175,8 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'not return the same result twice' do - parent = Category.create!(:name => 'parent category', :environment => Environment.default) - child = Category.create!(:name => 'child category', :environment => Environment.default, :parent => parent) + parent = fast_create(Category, :name => 'parent category', :environment_id => Environment.default.id) + child = fast_create(Category, :name => 'child category', :environment_id => Environment.default.id, :parent_id => parent.id) p1 = create_user('people_1').person p1.name = 'a beautiful person' p1.category_ids = [child.id, parent.id]; p1.save! @@ -205,7 +206,7 @@ class CategoryFinderTest < ActiveSupport::TestCase should 'find person and enterprise by radius and region' do finder = CategoryFinder.new(@category) - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) + region = fast_create(Region, :name => 'r-test', :environment_id => Environment.default.id, :lat => 45.0, :lng => 45.0) ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0, :category_ids => [@category.id]) p1 = create_user('test2').person p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.add_category(@category); p1.save! @@ -229,7 +230,7 @@ class CategoryFinderTest < ActiveSupport::TestCase e1 = Event.create!(:name => 'e1', :profile => person, :start_date => Date.new(2008,1,1), :category_ids => [@category.id]) # not in category - e2 = Event.create!(:name => 'e2', :profile => person, :start_date => Date.new(2008,1,15)) + e2 = fast_create(Event, :name => 'e2', :profile_id => person.id, :start_date => Date.new(2008,1,15)) events = finder.current_events(2008, 1) assert_includes events, e1 @@ -246,16 +247,16 @@ class CategoryFinderTest < ActiveSupport::TestCase # event 2 is created after, but must be listed before (since it happens before) upcoming_event_2 = Event.create!(:name => 'upcoming event 2', :profile => person, :start_date => Date.new(2008,1,25), :category_ids => [@category.id]) upcoming_event_1 = Event.create!(:name => 'upcoming event 1', :profile => person, :start_date => Date.new(2008,1,20), :category_ids => [@category.id]) - not_in_category = Event.create!(:name => 'e1', :profile => person, :start_date => Date.new(2008,1,20)) + not_in_category = fast_create(Event, :name => 'e1', :profile_id => person.id, :start_date => Date.new(2008,1,20)) assert_equal [upcoming_event_1, upcoming_event_2], @finder.upcoming_events end should 'find person and enterprise in category by radius and region even without query' do - cat = Category.create!(:name => 'test category', :environment => Environment.default) + cat = fast_create(Category, :name => 'test category', :environment_id => Environment.default.id) finder = CategoryFinder.new(cat) - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) + region = fast_create(Region, :name => 'r-test', :environment_id => Environment.default.id, :lat => 45.0, :lng => 45.0) ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0, :category_ids => [cat.id]) p1 = create_user('test2').person p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.add_category(cat); p1.save! @@ -273,13 +274,13 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'find products in category wihin product category' do - cat = Category.create!(:name => 'test category', :environment => Environment.default) + cat = fast_create(Category, :name => 'test category', :environment_id => Environment.default.id) finder = CategoryFinder.new(cat) - prod_cat = ProductCategory.create!(:name => 'test product category', :environment => Environment.default) + prod_cat = fast_create(ProductCategory, :name => 'test product category', :environment_id => Environment.default.id) ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :category_ids => [cat.id]) prod1 = ent.products.create!(:name => 'test product 1', :product_category => prod_cat) - prod2 = ent.products.create!(:name => 'test product 2') + prod2 = ent.products.create!(:name => 'test product 2', :product_category => @product_category) prods = finder.find(:products, nil, :product_category => prod_cat) @@ -288,8 +289,8 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'find enterprises by its products categories without query' do - pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default) - pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test_cat1', :environment_id => Environment.default.id) + pc2 = fast_create(ProductCategory, :name => 'test_cat2', :environment_id => Environment.default.id) ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) ent1.products.create!(:name => 'test product 1', :product_category => pc1) @@ -303,8 +304,8 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'find enterprises by its products categories with query' do - pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default) - pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test_cat1', :environment_id => Environment.default.id) + pc2 = fast_create(ProductCategory, :name => 'test_cat2', :environment_id => Environment.default.id) ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) ent1.products.create!(:name => 'test product 1', :product_category => pc1) @@ -318,10 +319,10 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'count product categories results by products' do - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) @@ -330,7 +331,7 @@ class CategoryFinderTest < ActiveSupport::TestCase p4 = ent.products.create!(:name => 'test product 4', :product_category => pc2) # not in the count p5 = ent.products.create!(:name => 'test product 5', :product_category => pc3) # not in the count - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) counts = @finder.product_categories_count(:products, [pc1.id, pc11.id, pc2.id], [p1.id, p2.id, p3.id, p5.id, p6.id] ) @@ -342,10 +343,10 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'count product categories results by all products' do - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) @@ -353,7 +354,7 @@ class CategoryFinderTest < ActiveSupport::TestCase p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) p4 = ent.products.create!(:name => 'test product 4', :product_category => pc3) # not in the count - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) @@ -366,10 +367,10 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'count product categories results by enterprises' do - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) ent1.products.create!(:name => 'test product 1', :product_category => pc1) @@ -384,7 +385,7 @@ class CategoryFinderTest < ActiveSupport::TestCase ent5.products.create!(:name => 'test product 5', :product_category => pc2) ent5.products.create!(:name => 'test product 6', :product_category => pc3) - ent6 = Enterprise.create!(:name => 'test enterprise 6', :identifier => 'test_ent6') + ent6 = fast_create(Enterprise, :name => 'test enterprise 6', :identifier => 'test_ent6') p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) counts = @finder.product_categories_count(:enterprises, [pc1.id, pc11.id, pc2.id], [ent1.id, ent2.id, ent3.id, ent4.id] ) @@ -396,10 +397,10 @@ class CategoryFinderTest < ActiveSupport::TestCase end should 'count product categories results by all enterprises' do - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1', :category_ids => [@category.id]) ent1.products.create!(:name => 'test product 1', :product_category => pc1) @@ -412,7 +413,7 @@ class CategoryFinderTest < ActiveSupport::TestCase ent4.products.create!(:name => 'test product 4', :product_category => pc2) ent4.products.create!(:name => 'test product 5', :product_category => pc3) - ent5 = Enterprise.create!(:name => 'test enterprise 5', :identifier => 'test_ent5') + ent5 = fast_create(Enterprise, :name => 'test enterprise 5', :identifier => 'test_ent5') p6 = ent2.products.create!(:name => 'test product 6', :product_category => pc1) counts = @finder.product_categories_count(:enterprises, [pc1.id, pc11.id, pc2.id] ) @@ -438,12 +439,12 @@ class CategoryFinderTest < ActiveSupport::TestCase person = create_user('teste').person # in category - art1 = TextileArticle.create!(:name => 'an article to be found', :profile => person) + art1 = fast_create(TextileArticle, :name => 'an article to be found', :profile_id => person.id) art1.add_category(@category) art1.save! # not in category - art2 = TextileArticle.create!(:name => 'another article to be found', :profile => person) + art2 = fast_create(TextileArticle, :name => 'another article to be found', :profile_id => person.id) list = @finder.find(:text_articles, 'found') assert_includes list, art1 diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 1127a40..4b29c5a 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -137,7 +137,7 @@ class CategoryTest < Test::Unit::TestCase end def test_should_refuse_to_duplicate_slug_under_the_same_parent - c1 = create(Category, :name => 'test category', :environment_id => @env.id) + c1 = Category.create!(:name => 'test category', :environment_id => @env.id) c2 = Category.new(:name => 'Test: Category', :environment_id => @env.id) assert !c2.valid? @@ -157,9 +157,9 @@ class CategoryTest < Test::Unit::TestCase end def test_renaming_a_category_should_change_path_of_children - c1 = create(Category, :name => 'parent', :environment_id => @env.id) - c2 = create(Category, :name => 'child', :environment_id => @env.id, :parent_id => c1.id) - c3 = create(Category, :name => 'grandchild', :environment_id => @env.id, :parent_id => c2.id) + c1 = Category.create!(:name => 'parent', :environment_id => @env.id) + c2 = Category.create!(:name => 'child', :environment_id => @env.id, :parent_id => c1.id) + c3 = Category.create!(:name => 'grandchild', :environment_id => @env.id, :parent_id => c2.id) c1.name = 'parent new name' c1.save! @@ -311,13 +311,14 @@ class CategoryTest < Test::Unit::TestCase end should 'have products through enteprises' do + product_category = fast_create(ProductCategory, :name => 'Products', :environment_id => Environment.default.id) c = @env.categories.build(:name => 'my category'); c.save! ent1 = fast_create(Enterprise, :identifier => 'enterprise_1', :name => 'Enterprise one') ent1.add_category c ent2 = fast_create(Enterprise, :identifier => 'enterprise_2', :name => 'Enterprise one') ent2.add_category c - prod1 = ent1.products.create!(:name => 'test_prod1') - prod2 = ent2.products.create!(:name => 'test_prod2') + prod1 = ent1.products.create!(:name => 'test_prod1', :product_category => product_category) + prod2 = ent2.products.create!(:name => 'test_prod2', :product_category => product_category) assert_includes c.products, prod1 assert_includes c.products, prod2 end @@ -384,4 +385,8 @@ class CategoryTest < Test::Unit::TestCase assert_equal 2, c.children.size end + should 'accept_products is true by default' do + assert Category.new.accept_products? + end + end diff --git a/test/unit/enterprise_homepage_test.rb b/test/unit/enterprise_homepage_test.rb index fd7fb32..4104fd0 100644 --- a/test/unit/enterprise_homepage_test.rb +++ b/test/unit/enterprise_homepage_test.rb @@ -4,6 +4,7 @@ class EnterpriseHomepageTest < Test::Unit::TestCase def setup @profile = create_user('testing').person + @product_category = fast_create(ProductCategory, :name => 'Products') end attr_reader :profile @@ -25,8 +26,8 @@ class EnterpriseHomepageTest < Test::Unit::TestCase end should 'display products list' do - ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enteprise') - prod = ent.products.create!(:name => 'Product test') + ent = fast_create(Enterprise, :identifier => 'test_enterprise', :name => 'Test enteprise') + prod = ent.products.create!(:name => 'Product test', :product_category => @product_category) a = EnterpriseHomepage.new(:name => 'article homepage') ent.articles << a result = a.to_html @@ -37,8 +38,8 @@ class EnterpriseHomepageTest < Test::Unit::TestCase e = Environment.default e.enable('disable_products_for_enterprises') e.save! - ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enteprise', :environment => e) - prod = ent.products.create!(:name => 'Product test') + ent = fast_create(Enterprise, :identifier => 'test_enterprise', :name => 'Test enteprise', :environment_id => e.id) + prod = ent.products.create!(:name => 'Product test', :product_category => @product_category) a = EnterpriseHomepage.new(:name => 'article homepage') ent.articles << a result = a.to_html @@ -46,8 +47,8 @@ class EnterpriseHomepageTest < Test::Unit::TestCase end should 'display link to product' do - ent = Enterprise.create!(:identifier => 'test_enterprise', :name => 'Test enteprise') - prod = ent.products.create!(:name => 'Product test') + ent = fast_create(Enterprise, :identifier => 'test_enterprise', :name => 'Test enteprise') + prod = ent.products.create!(:name => 'Product test', :product_category => @product_category) a = EnterpriseHomepage.new(:name => 'article homepage') ent.articles << a result = a.to_html diff --git a/test/unit/enterprise_test.rb b/test/unit/enterprise_test.rb index 589924e..c4c0720 100644 --- a/test/unit/enterprise_test.rb +++ b/test/unit/enterprise_test.rb @@ -3,6 +3,10 @@ require File.dirname(__FILE__) + '/../test_helper' class EnterpriseTest < Test::Unit::TestCase fixtures :profiles, :environments, :users + def setup + @product_category = fast_create(ProductCategory, :name => 'Products') + end + def test_identifier_validation p = Enterprise.new p.valid? @@ -46,9 +50,9 @@ class EnterpriseTest < Test::Unit::TestCase end should 'remove products when removing enterprise' do - e = fast_create(Enterprise) - e.products.build(:name => 'One product').save! - e.products.build(:name => 'Another product').save! + e = fast_create(Enterprise, :name => "My enterprise", :identifier => 'myenterprise') + e.products.create!(:name => 'One product', :product_category => @product_category) + e.products.create!(:name => 'Another product', :product_category => @product_category) assert_difference Product, :count, -2 do e.destroy @@ -63,7 +67,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'create default set of blocks' do - e = Enterprise.create!(:name => 'my new community', :identifier => 'mynewcommunity') + e = Enterprise.create(:name => 'my new community', :identifier => 'mynewcommunity') assert e.boxes[0].blocks.map(&:class).include?(MainBlock), 'enterprise must have a MainBlock upon creation' @@ -77,11 +81,11 @@ class EnterpriseTest < Test::Unit::TestCase end should 'be found in search for its product categories' do - ent1 = fast_create(Enterprise) - prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default) + ent1 = fast_create(Enterprise, :name => 'test1', :identifier => 'test1') + prod_cat = fast_create(ProductCategory, :name => 'pctest', :environment_id => Environment.default.id) prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2') + ent2 = fast_create(Enterprise, :name => 'test2', :identifier => 'test2') result = Enterprise.find_by_contents(prod_cat.name) @@ -90,12 +94,12 @@ class EnterpriseTest < Test::Unit::TestCase end should 'be found in search for its product categories hierarchy' do - ent1 = fast_create(Enterprise) - prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default) - prod_child = ProductCategory.create!(:name => 'pchild', :environment => Environment.default, :parent => prod_cat) + ent1 = fast_create(Enterprise, :name => 'test1', :identifier => 'test1') + prod_cat = fast_create(ProductCategory, :name => 'pctest', :environment_id => Environment.default.id) + prod_child = fast_create(ProductCategory, :name => 'pchild', :environment_id => Environment.default.id, :parent_id => prod_cat.id) prod = ent1.products.create!(:name => 'teste', :product_category => prod_child) - ent2 = Enterprise.create!(:name => 'test2', :identifier => 'test2') + ent2 = fast_create(Enterprise, :name => 'test2', :identifier => 'test2') result = Enterprise.find_by_contents(prod_cat.name) @@ -104,7 +108,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'not allow to add new members' do - o = fast_create(Enterprise) + o = fast_create(Enterprise, :name => 'my test profile', :identifier => 'mytestprofile') p = create_user('mytestuser').person o.add_member(p) @@ -114,7 +118,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'allow to remove members' do - c = fast_create(Enterprise) + c = fast_create(Enterprise, :name => 'my other test profile', :identifier => 'myothertestprofile') c.expects(:closed?).returns(false) p = create_user('myothertestuser').person @@ -126,7 +130,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'have foudation_year' do - ent = fast_create(Enterprise) + ent = fast_create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent') assert_respond_to ent, 'foundation_year' assert_respond_to ent, 'foundation_year=' @@ -140,13 +144,13 @@ class EnterpriseTest < Test::Unit::TestCase end should 'block' do - ent = fast_create(Enterprise) + ent = fast_create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent') ent.block assert Enterprise.find(ent.id).blocked? end should 'unblock' do - ent = fast_create(Enterprise) + ent = fast_create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent') ent.data[:blocked] = true ent.save ent.unblock @@ -154,7 +158,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'enable and make user admin' do - ent = fast_create(Enterprise, :enabled => false) + ent = fast_create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false) p = create_user('test_user').person assert ent.enable(p) @@ -164,7 +168,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'replace template if environment allows' do - template = Enterprise.create!(:name => 'template enteprise', :identifier => 'template_enterprise', :enabled => false) + template = fast_create(Enterprise, :name => 'template enteprise', :identifier => 'template_enterprise', :enabled => false) template.boxes.destroy_all template.boxes << Box.new template.boxes[0].blocks << Block.new @@ -175,7 +179,7 @@ class EnterpriseTest < Test::Unit::TestCase e.enterprise_template = template e.save! - ent = Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => false) + ent = fast_create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => false) p = create_user('test_user').person ent.enable(p) @@ -185,7 +189,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'not replace template if environment doesnt allow' do - inactive_template = Enterprise.create!(:name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template') + inactive_template = fast_create(Enterprise, :name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template') inactive_template.boxes.destroy_all inactive_template.boxes << Box.new inactive_template.save! @@ -220,14 +224,14 @@ class EnterpriseTest < Test::Unit::TestCase should 'not create activation task when enabled = true' do assert_no_difference EnterpriseActivation, :count do - Enterprise.create!(:name => 'test enteprise', :identifier => 'test_ent', :enabled => true) + fast_create(Enterprise, :name => 'test enteprise', :identifier => 'test_ent', :enabled => true) end end should 'be able to enable even if there are mandatory fields blank' do # enterprise is created, waiting for being enabled - environment = Environment.create!(:name => 'my test environment') - enterprise = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent', :enabled => false, :environment => environment) + environment = fast_create(Environment, :name => 'my test environment') + enterprise = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false, :environment_id => environment.id) # administrator decides now that the 'city' field is mandatory environment.custom_enterprise_fields = { 'city' => { 'active' => 'true', 'required' => 'true' } } @@ -241,22 +245,11 @@ class EnterpriseTest < Test::Unit::TestCase end should 'list product categories full name' do - full_name = mock - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - p = ent.products.create!(:name => 'test prod') - p.expects(:category_full_name).returns(full_name) + subcategory = fast_create(ProductCategory, :name => 'Products subcategory', :parent_id => @product_category.id) + ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') + p = ent.products.create!(:name => 'test prod', :product_category => subcategory) - assert_equal [full_name], ent.product_categories - end - - should 'not return nil values when have uncategorized products' do - full_name = mock - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - p1 = ent.products.create!(:name => 'test prod 1') - p1.expects(:category_full_name).returns(full_name) - p2 = ent.products.create!(:name => 'test prod 2') - - assert_equal [full_name], ent.product_categories + assert_equal [p.category_full_name], ent.product_categories end should 'default home page is a EnterpriseHomepage' do @@ -268,18 +261,18 @@ class EnterpriseTest < Test::Unit::TestCase env = Environment.default env.enable('disable_products_for_enterprises') env.save! - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') assert_not_includes ent.blocks.map(&:class), ProductsBlock end should 'have a enterprise template' do env = Environment.create!(:name => 'test env') - p = Enterprise.create!(:name => 'test_com', :identifier => 'test_com', :environment => env) + p = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => env.id) assert_kind_of Enterprise, p.template end should 'contact us enabled by default' do - e = Enterprise.create!(:name => 'test_com', :identifier => 'test_com', :environment => Environment.default) + e = fast_create(Enterprise, :name => 'test_com', :identifier => 'test_com', :environment_id => Environment.default.id) assert e.enable_contact_us end @@ -349,7 +342,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'have inactive_template when creating enterprise and feature is enabled' do - inactive_template = Enterprise.create!(:name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template') + inactive_template = fast_create(Enterprise, :name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template') inactive_template.boxes.destroy_all inactive_template.boxes << Box.new inactive_template.save! @@ -364,7 +357,7 @@ class EnterpriseTest < Test::Unit::TestCase end should 'have active_template when creating enterprise and feature is disabled' do - inactive_template = Enterprise.create!(:name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template') + inactive_template = fast_create(Enterprise, :name => 'inactive enteprise template', :identifier => 'inactive_enterprise_template') inactive_template.boxes.destroy_all inactive_template.boxes << Box.new inactive_template.save! @@ -381,15 +374,16 @@ class EnterpriseTest < Test::Unit::TestCase should 'collect the highlighted products with image' do env = Environment.default e1 = fast_create(Enterprise) - p1 = e1.products.create!(:name => 'test_prod1') + p1 = e1.products.create!(:name => 'test_prod1', :product_category_id => @product_category.id) products = [] 3.times {|n| - products.push(Product.create!(:name => "product #{n}", :enterprise_id => e1.id, :highlighted => true, :image_builder => { - :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') - })) + products.push(Product.create!(:name => "product #{n}", :enterprise_id => e1.id, + :highlighted => true, :product_category_id => @product_category.id, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } + )) } - Product.create!(:name => "product 4", :enterprise_id => e1.id, :highlighted => true) - Product.create!(:name => "product 5", :enterprise_id => e1.id, :image_builder => { + Product.create!(:name => "product 4", :enterprise_id => e1.id, :product_category_id => @product_category.id, :highlighted => true) + Product.create!(:name => "product 5", :enterprise_id => e1.id, :product_category_id => @product_category.id, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) assert_equal products, e1.highlighted_products_with_image diff --git a/test/unit/environment_finder_test.rb b/test/unit/environment_finder_test.rb index 01023d7..d30ed7e 100644 --- a/test/unit/environment_finder_test.rb +++ b/test/unit/environment_finder_test.rb @@ -2,6 +2,10 @@ require File.dirname(__FILE__) + '/../test_helper' class EnvironmentFinderTest < ActiveSupport::TestCase + def setup + @product_category = fast_create(ProductCategory, :name => 'Products') + end + should 'find articles' do person = create_user('teste').person art = person.articles.build(:name => 'an article to be found'); art.save! @@ -23,8 +27,8 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find products' do finder = EnvironmentFinder.new(Environment.default) - ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') - prod = ent.products.create!(:name => 'a beautiful product') + ent = fast_create(Enterprise, :name => 'teste', :identifier => 'teste') + prod = ent.products.create!(:name => 'a beautiful product', :product_category => @product_category) assert_includes finder.find(:products, 'beautiful'), prod end @@ -36,14 +40,14 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'list recent enterprises' do finder = EnvironmentFinder.new(Environment.default) - ent = Enterprise.create!(:name => 'teste', :identifier => 'teste') + ent = fast_create(Enterprise, :name => 'teste', :identifier => 'teste') assert_includes finder.recent('enterprises'), ent end should 'not list more enterprises than limit' do finder = EnvironmentFinder.new(Environment.default) - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') - ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') + ent1 = fast_create(Enterprise, :name => 'teste1', :identifier => 'teste1') + ent2 = fast_create(Enterprise, :name => 'teste2', :identifier => 'teste2') recent = finder.recent('enterprises', 1) assert_equal 1, recent.size @@ -51,8 +55,8 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'paginate the list of more enterprises than limit' do finder = EnvironmentFinder.new(Environment.default) - ent1 = Enterprise.create!(:name => 'teste1', :identifier => 'teste1') - ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2') + ent1 = fast_create(Enterprise, :name => 'teste1', :identifier => 'teste1') + ent2 = fast_create(Enterprise, :name => 'teste2', :identifier => 'teste2') assert_equal 1, finder.find('enterprises', nil, :per_page => 1, :page => 1).size end @@ -72,11 +76,11 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find person and enterprise by radius and region' do finder = EnvironmentFinder.new(Environment.default) - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) - ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) + region = fast_create(Region, :name => 'r-test', :environment_id => Environment.default.id, :lat => 45.0, :lng => 45.0) + ent1 = fast_create(Enterprise, :name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) p1 = create_user('test2').person p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save! - ent2 = Enterprise.create!(:name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0) + ent2 = fast_create(Enterprise, :name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0) p2 = create_user('test4').person p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save! @@ -92,11 +96,11 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find person and enterprise by radius and region even without query' do finder = EnvironmentFinder.new(Environment.default) - region = Region.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) - ent1 = Enterprise.create!(:name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) + region = fast_create(Region, :name => 'r-test', :environment_id => Environment.default.id, :lat => 45.0, :lng => 45.0) + ent1 = fast_create(Enterprise, :name => 'test 1', :identifier => 'test1', :lat => 45.0, :lng => 45.0) p1 = create_user('test2').person p1.name = 'test 2'; p1.lat = 45.0; p1.lng = 45.0; p1.save! - ent2 = Enterprise.create!(:name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0) + ent2 = fast_create(Enterprise, :name => 'test 3', :identifier => 'test3', :lat => 30.0, :lng => 30.0) p2 = create_user('test4').person p2.name = 'test 4'; p2.lat = 30.0; p2.lng = 30.0; p2.save! @@ -111,10 +115,10 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find products wihin product category' do finder = EnvironmentFinder.new(Environment.default) - cat = ProductCategory.create!(:name => 'test category', :environment => Environment.default) - ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') + cat = fast_create(ProductCategory, :name => 'test category', :environment_id => Environment.default.id) + ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent') prod1 = ent.products.create!(:name => 'test product 1', :product_category => cat) - prod2 = ent.products.create!(:name => 'test product 2') + prod2 = ent.products.create!(:name => 'test product 2', :product_category => @product_category) prods = finder.find(:products, nil, :product_category => cat) @@ -124,12 +128,12 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find products wihin product category with query' do finder = EnvironmentFinder.new(Environment.default) - cat = ProductCategory.create!(:name => 'test category', :environment => Environment.default) - ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_ent') + cat = fast_create(ProductCategory, :name => 'test category', :environment_id => Environment.default.id) + ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent') prod1 = ent.products.create!(:name => 'test product a_word 1', :product_category => cat) prod2 = ent.products.create!(:name => 'test product b_word 1', :product_category => cat) - prod3 = ent.products.create!(:name => 'test product a_word 2') - prod4 = ent.products.create!(:name => 'test product b_word 2') + prod3 = ent.products.create!(:name => 'test product a_word 2', :product_category => @product_category) + prod4 = ent.products.create!(:name => 'test product b_word 2', :product_category => @product_category) prods = finder.find(:products, 'a_word', :product_category => cat) @@ -142,9 +146,9 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find enterprises in alphabetical order of name' do finder = EnvironmentFinder.new(Environment.default) - ent1 = Enterprise.create!(:name => 'test enterprise B', :identifier => 'test_ent_b') - ent2 = Enterprise.create!(:name => 'test enterprise A', :identifier => 'test_ent_a') - ent3 = Enterprise.create!(:name => 'test enterprise C', :identifier => 'test_ent_c') + ent1 = fast_create(Enterprise, :name => 'test enterprise B', :identifier => 'test_ent_b') + ent2 = fast_create(Enterprise, :name => 'test enterprise A', :identifier => 'test_ent_a') + ent3 = fast_create(Enterprise, :name => 'test enterprise C', :identifier => 'test_ent_c') ents = finder.find(:enterprises, nil) @@ -155,12 +159,12 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find enterprises by its products categories' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default) - pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test_cat1', :environment_id => Environment.default.id) + pc2 = fast_create(ProductCategory, :name => 'test_cat2', :environment_id => Environment.default.id) - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') ent1.products.create!(:name => 'test product 1', :product_category => pc1) - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') ent2.products.create!(:name => 'test product 2', :product_category => pc2) ents = finder.find(:enterprises, nil, :product_category => pc1) @@ -172,12 +176,12 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find enterprises by its products categories with query' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test_cat1', :environment => Environment.default) - pc2 = ProductCategory.create!(:name => 'test_cat2', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test_cat1', :environment_id => Environment.default.id) + pc2 = fast_create(ProductCategory, :name => 'test_cat2', :environment_id => Environment.default.id) - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') ent1.products.create!(:name => 'test product 1', :product_category => pc1) - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') ent2.products.create!(:name => 'test product 2', :product_category => pc2) ents = finder.find(:enterprises, 'test', :product_category => pc1) @@ -189,12 +193,12 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'find enterprises by a product category with name with spaces' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') ent1.products.create!(:name => 'test product 1', :product_category => pc1) - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') ent2.products.create!(:name => 'test product 2', :product_category => pc2) ents = finder.find(:enterprises, 'test', :product_category => pc1) @@ -206,13 +210,13 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'count product categories results by products' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11= fast_create(ProductCategory, :name => 'test cat11',:environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) - ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) p2 = ent.products.create!(:name => 'test product 2', :product_category => pc11) p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) @@ -230,13 +234,13 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'count product categories results by all products' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) - ent = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') p1 = ent.products.create!(:name => 'test product 1', :product_category => pc1) p2 = ent.products.create!(:name => 'test product 2', :product_category => pc11) p3 = ent.products.create!(:name => 'test product 3', :product_category => pc2) @@ -253,21 +257,21 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'count product categories results by enterprises' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') ent1.products.create!(:name => 'test product 1', :product_category => pc1) ent1.products.create!(:name => 'test product 2', :product_category => pc1) - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') ent2.products.create!(:name => 'test product 2', :product_category => pc11) - ent3 = Enterprise.create!(:name => 'test enterprise 3', :identifier => 'test_ent3') + ent3 = fast_create(Enterprise, :name => 'test enterprise 3', :identifier => 'test_ent3') ent3.products.create!(:name => 'test product 3', :product_category => pc2) - ent4 = Enterprise.create!(:name => 'test enterprise 4', :identifier => 'test_ent4') + ent4 = fast_create(Enterprise, :name => 'test enterprise 4', :identifier => 'test_ent4') ent4.products.create!(:name => 'test product 4', :product_category => pc2) - ent5 = Enterprise.create!(:name => 'test enterprise 5', :identifier => 'test_ent5') # not in the count + ent5 = fast_create(Enterprise, :name => 'test enterprise 5', :identifier => 'test_ent5') # not in the count ent5.products.create!(:name => 'test product 5', :product_category => pc2) ent5.products.create!(:name => 'test product 6', :product_category => pc3) @@ -282,19 +286,19 @@ class EnvironmentFinderTest < ActiveSupport::TestCase should 'count product categories results by all enterprises' do finder = EnvironmentFinder.new(Environment.default) - pc1 = ProductCategory.create!(:name => 'test cat1', :environment => Environment.default) - pc11 = ProductCategory.create!(:name => 'test cat11', :environment => Environment.default, :parent => pc1) - pc2 = ProductCategory.create!(:name => 'test cat2', :environment => Environment.default) - pc3 = ProductCategory.create!(:name => 'test cat3', :environment => Environment.default) + pc1 = fast_create(ProductCategory, :name => 'test cat1', :environment_id => Environment.default.id) + pc11 = fast_create(ProductCategory, :name => 'test cat11', :environment_id => Environment.default.id, :parent_id => pc1.id) + pc2 = fast_create(ProductCategory, :name => 'test cat2', :environment_id => Environment.default.id) + pc3 = fast_create(ProductCategory, :name => 'test cat3', :environment_id => Environment.default.id) - ent1 = Enterprise.create!(:name => 'test enterprise 1', :identifier => 'test_ent1') + ent1 = fast_create(Enterprise, :name => 'test enterprise 1', :identifier => 'test_ent1') ent1.products.create!(:name => 'test product 1', :product_category => pc1) ent1.products.create!(:name => 'test product 2', :product_category => pc1) - ent2 = Enterprise.create!(:name => 'test enterprise 2', :identifier => 'test_ent2') + ent2 = fast_create(Enterprise, :name => 'test enterprise 2', :identifier => 'test_ent2') ent2.products.create!(:name => 'test product 2', :product_category => pc11) - ent3 = Enterprise.create!(:name => 'test enterprise 3', :identifier => 'test_ent3') + ent3 = fast_create(Enterprise, :name => 'test enterprise 3', :identifier => 'test_ent3') ent3.products.create!(:name => 'test product 3', :product_category => pc2) - ent4 = Enterprise.create!(:name => 'test enterprise 4', :identifier => 'test_ent4') + ent4 = fast_create(Enterprise, :name => 'test enterprise 4', :identifier => 'test_ent4') ent4.products.create!(:name => 'test product 4', :product_category => pc2) ent4.products.create!(:name => 'test product 5', :product_category => pc3) @@ -323,8 +327,8 @@ class EnvironmentFinderTest < ActiveSupport::TestCase date_range = Date.new(2009, 11, 28)..Date.new(2009, 12, 3) - event_in_range = Event.create!(:name => 'Event in range', :profile => person, :start_date => Date.new(2009, 11, 27), :end_date => date_range.last) - event_out_of_range = Event.create!(:name => 'Event out of range', :profile => person, :start_date => Date.new(2009, 12, 4)) + event_in_range = fast_create(Event, :name => 'Event in range', :profile_id => person.id, :start_date => Date.new(2009, 11, 27), :end_date => date_range.last) + event_out_of_range = fast_create(Event, :name => 'Event out of range', :profile_id => person.id, :start_date => Date.new(2009, 12, 4)) events_found = finder.find(:events, '', :date_range => date_range) diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index bec3af6..a44effc 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -367,9 +367,10 @@ class EnvironmentTest < Test::Unit::TestCase end should 'have products through enterprises' do + product_category = fast_create(ProductCategory, :name => 'Products', :environment_id => Environment.default.id) env = Environment.default e1 = fast_create(Enterprise) - p1 = e1.products.create!(:name => 'test_prod1') + p1 = e1.products.create!(:name => 'test_prod1', :product_category => product_category) assert_includes env.products, p1 end @@ -377,15 +378,17 @@ class EnvironmentTest < Test::Unit::TestCase should 'collect the highlighted products with image through enterprises' do env = Environment.default e1 = fast_create(Enterprise) - p1 = e1.products.create!(:name => 'test_prod1') + category = fast_create(ProductCategory) + p1 = e1.products.create!(:name => 'test_prod1', :product_category_id => category.id) products = [] 3.times {|n| - products.push(Product.create!(:name => "product #{n}", :enterprise_id => e1.id, :highlighted => true, :image_builder => { - :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') - })) + products.push(Product.create!(:name => "product #{n}", :enterprise_id => e1.id, + :product_category_id => category.id, :highlighted => true, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } + )) } - Product.create!(:name => "product 4", :enterprise_id => e1.id, :highlighted => true) - Product.create!(:name => "product 5", :enterprise_id => e1.id, :image_builder => { + Product.create!(:name => "product 4", :enterprise_id => e1.id, :product_category_id => category.id, :highlighted => true) + Product.create!(:name => "product 5", :enterprise_id => e1.id, :product_category_id => category.id, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) assert_equal products, env.highlighted_products_with_image diff --git a/test/unit/featured_products_block_test.rb b/test/unit/featured_products_block_test.rb index c48101b..0f67c4f 100644 --- a/test/unit/featured_products_block_test.rb +++ b/test/unit/featured_products_block_test.rb @@ -11,7 +11,8 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase should 'refer to products' do products = [] - 3.times {|n| products.push(Product.create!(:name => "product #{n}", :enterprise_id => profile.id)) } + category = fast_create(ProductCategory) + 3.times {|n| products.push(Product.create!(:name => "product #{n}", :enterprise_id => profile.id, :product_category_id => category.id)) } featured_products_block = FeaturedProductsBlock.create!(:product_ids => products.map(&:id)) assert_equal products, featured_products_block.products end @@ -62,10 +63,12 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase block = FeaturedProductsBlock.new() block.product_ids = [] enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'myenterprise', :environment => @environment) + category = fast_create(ProductCategory) 3.times {|n| - Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, :highlighted => true, :image_builder => { - :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') - }) + Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, + :highlighted => true, :product_category_id => category.id, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } + ) } @environment.boxes.first.blocks<< block @@ -76,8 +79,9 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase block = FeaturedProductsBlock.new() block.product_ids = [] enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'myenterprise', :environment => @environment) + category = fast_create(ProductCategory) 3.times {|n| - Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, :highlighted => true) + Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, :highlighted => true, :product_category_id => category.id) } @environment.boxes.first.blocks<< block @@ -88,8 +92,9 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase block = FeaturedProductsBlock.new() block.product_ids = [] enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'myenterprise', :environment => @environment) + category = fast_create(ProductCategory) 3.times {|n| - Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, :image_builder => { + Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, :product_category_id => category.id, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) } @@ -109,14 +114,16 @@ class FeaturedProductsBlockTest < ActiveSupport::TestCase block = FeaturedProductsBlock.new() block.product_ids = [] enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'myenterprise', :environment => @environment) + category = fast_create(ProductCategory) products = [] 3.times {|n| - products.push(Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, :highlighted => true, :image_builder => { - :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') - })) + products.push(Product.create!(:name => "product #{n}", :enterprise_id => enterprise.id, + :highlighted => true, :product_category_id => category.id, + :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } + )) } - Product.create!(:name => "product 4", :enterprise_id => enterprise.id, :highlighted => true) - Product.create!(:name => "product 5", :enterprise_id => enterprise.id, :image_builder => { + Product.create!(:name => "product 4", :enterprise_id => enterprise.id, :product_category_id => category.id, :highlighted => true) + Product.create!(:name => "product 5", :enterprise_id => enterprise.id, :product_category_id => category.id, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) @environment.boxes.first.blocks<< block diff --git a/test/unit/manage_products_helper_test.rb b/test/unit/manage_products_helper_test.rb new file mode 100644 index 0000000..99dd90c --- /dev/null +++ b/test/unit/manage_products_helper_test.rb @@ -0,0 +1,44 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ManageProductsHelperTest < Test::Unit::TestCase + + include ManageProductsHelper + include ContentViewerHelper + include ActionView::Helpers::AssetTagHelper + include ApplicationHelper + + def setup + stubs(:show_date).returns('') + @environment = Environment.default + @profile = create_user('blog_helper_test').person + end + + should 'omit second category when lenght of all names is over 60 chars' do + category_1 = fast_create(ProductCategory, :name => ('Category 1' * 5), :environment_id => @environment.id) + category_2 = fast_create(ProductCategory, :name => ('Category 2' * 5), :environment_id => @environment.id, :parent_id => category_1.id) + category_3 = fast_create(ProductCategory, :name => ('Category 3' * 5), :environment_id => @environment.id, :parent_id => category_2.id) + + assert_match /Category 1/, hierarchy_category_navigation(category_3) + assert_no_match /Category 2/, hierarchy_category_navigation(category_3) + end + + should 'show dots when lenght of all names is over 60 chars' do + category_1 = fast_create(ProductCategory, :name => ('Category 1' * 5), :environment_id => @environment.id) + category_2 = fast_create(ProductCategory, :name => ('Category 2' * 5), :environment_id => @environment.id, :parent_id => category_1.id) + category_3 = fast_create(ProductCategory, :name => ('Category 3' * 5), :environment_id => @environment.id, :parent_id => category_2.id) + + assert_match /…/, hierarchy_category_navigation(category_3) + end + + should 'display select for categories' do + category_1 = fast_create(ProductCategory, :name => 'Category 1', :environment_id => @environment.id) + fast_create(ProductCategory, :name => 'Category 2.1', :environment_id => @environment.id, :parent_id => category_1.id) + fast_create(ProductCategory, :name => 'Category 2.2', :environment_id => @environment.id, :parent_id => category_1.id) + + assert_tag_in_string select_for_categories(category_1.children(true), 1), :tag => 'select', :attributes => {:id => 'category_id'} + end + + protected + include NoosferoTestHelper + +end diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb index fe71ec0..01ec01c 100644 --- a/test/unit/product_test.rb +++ b/test/unit/product_test.rb @@ -2,53 +2,49 @@ require File.dirname(__FILE__) + '/../test_helper' class ProductTest < Test::Unit::TestCase + def setup + @product_category = fast_create(ProductCategory, :name => 'Products') + end + should 'create product' do assert_difference Product, :count do - p = Product.new(:name => 'test product1') + p = Product.new(:name => 'test product1', :product_category => @product_category) assert p.save end end should 'destroy product' do - p = Product.create(:name => 'test product2') + p = fast_create(Product, :name => 'test product2', :product_category_id => @product_category.id) assert_difference Product, :count, -1 do p.destroy end end - should 'name be unique' do - Product.create(:name => 'test product3') - assert_no_difference Product, :count do - p = Product.new(:name => 'test product3') - assert !p.save - end - end - should 'list recent products' do - enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'my-enterprise') + enterprise = fast_create(Enterprise, :name => "My enterprise", :identifier => 'my-enterprise') Product.delete_all - p1 = enterprise.products.create!(:name => 'product 1') - p2 = enterprise.products.create!(:name => 'product 2') - p3 = enterprise.products.create!(:name => 'product 3') + p1 = enterprise.products.create!(:name => 'product 1', :product_category => @product_category) + p2 = enterprise.products.create!(:name => 'product 2', :product_category => @product_category) + p3 = enterprise.products.create!(:name => 'product 3', :product_category => @product_category) assert_equal [p3, p2, p1], Product.recent end should 'list recent products with limit' do - enterprise = Enterprise.create!(:name => "My enterprise", :identifier => 'my-enterprise') + enterprise = fast_create(Enterprise, :name => "My enterprise", :identifier => 'my-enterprise') Product.delete_all - p1 = enterprise.products.create!(:name => 'product 1') - p2 = enterprise.products.create!(:name => 'product 2') - p3 = enterprise.products.create!(:name => 'product 3') + p1 = enterprise.products.create!(:name => 'product 1', :product_category => @product_category) + p2 = enterprise.products.create!(:name => 'product 2', :product_category => @product_category) + p3 = enterprise.products.create!(:name => 'product 3', :product_category => @product_category) assert_equal [p3, p2], Product.recent(2) end should 'save image on create product' do assert_difference Product, :count do - p = Product.create!(:name => 'test product1', :image_builder => { + p = Product.create!(:name => 'test product1', :product_category => @product_category, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') }) assert_equal p.image(true).filename, 'rails.png' @@ -71,7 +67,7 @@ class ProductTest < Test::Unit::TestCase end should 'be indexed by category full name' do - p = Product.new(:name => 'a test product') + p = Product.new(:name => 'a test product', :product_category => @product_category) p.expects(:category_full_name).returns('interesting category') p.save! @@ -79,8 +75,8 @@ class ProductTest < Test::Unit::TestCase end should 'have same lat and lng of its enterprise' do - ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0 ) - prod = ent.products.create!(:name => 'test product') + ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0) + prod = ent.products.create!(:name => 'test product', :product_category => @product_category) prod = Product.find(prod.id) assert_equal ent.lat, prod.lat @@ -88,8 +84,8 @@ class ProductTest < Test::Unit::TestCase end should 'update lat and lng of product afer update enterprise' do - ent = Enterprise.create!(:name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0 ) - prod = ent.products.create!(:name => 'test product') + ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_enterprise', :lat => 30.0, :lng => 30.0) + prod = ent.products.create!(:name => 'test product', :product_category => @product_category) ent.lat = 45.0; ent.lng = 45.0; ent.save! @@ -100,8 +96,8 @@ class ProductTest < Test::Unit::TestCase end should 'be searched by radius and distance' do - prod1 = Product.create!(:name => 'prod test 1', :lat => 30.0, :lng => 30.0) - prod2 = Product.create!(:name => 'prod test 2', :lat => 45.0, :lng => 45.0) + prod1 = fast_create(Product, :name => 'prod test 1', :lat => 30.0, :lng => 30.0, :product_category_id => @product_category.id) + prod2 = fast_create(Product, :name => 'prod test 2', :lat => 45.0, :lng => 45.0, :product_category_id => @product_category.id) prods = Product.find(:all, :within => 10, :origin => [30.0, 30.0]) @@ -121,9 +117,9 @@ class ProductTest < Test::Unit::TestCase end should 'categorize also with product categorization' do - cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - p = ent.products.create!(:name => 'test product') + cat = fast_create(ProductCategory, :name => 'test cat', :environment_id => Environment.default.id) + ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') + p = ent.products.new(:name => 'test product') p.product_category = cat p.save! @@ -131,10 +127,10 @@ class ProductTest < Test::Unit::TestCase end should 'categorize parent cateogries with product categorization' do - parent_cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default) - child_cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default, :parent => parent_cat) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - p = ent.products.create!(:name => 'test product') + parent_cat = fast_create(ProductCategory, :name => 'test cat', :environment_id => Environment.default.id) + child_cat = fast_create(ProductCategory, :name => 'test cat', :environment_id => Environment.default.id, :parent_id => parent_cat.id) + ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') + p = ent.products.new(:name => 'test product') p.product_category = child_cat p.save! @@ -143,9 +139,9 @@ class ProductTest < Test::Unit::TestCase end should 'change product categorization when product category changes' do - cat1 = ProductCategory.create(:name => 'test cat 1', :environment => Environment.default) - cat2 = ProductCategory.create(:name => 'test cat 2', :environment => Environment.default) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') + cat1 = fast_create(ProductCategory, :name => 'test cat 1', :environment_id => Environment.default.id) + cat2 = fast_create(ProductCategory, :name => 'test cat 2', :environment_id => Environment.default.id) + ent = fast_create(Enterprise, :name => 'test ent', :identifier => 'test_ent') p = ent.products.create!(:name => 'test product', :product_category => cat1) p.product_category = cat2 @@ -155,25 +151,14 @@ class ProductTest < Test::Unit::TestCase assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat1}), 'must exclude the old category' end - should 'remove categorization when product category is removed' do - cat = ProductCategory.create(:name => 'test cat', :environment => Environment.default) - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent') - p = ent.products.create!(:name => 'test product', :product_category => cat) - - p.product_category = nil - p.save! - - assert !ProductCategorization.find(:first, :conditions => {:product_id => p, :category_id => cat}) - end - should 'respond to public? as its enterprise public?' do - e1 = Enterprise.create!(:name => 'test ent 1', :identifier => 'test_ent1') - p1 = Product.create!(:name => 'test product 1', :enterprise => e1) + e1 = fast_create(Enterprise, :name => 'test ent 1', :identifier => 'test_ent1') + p1 = fast_create(Product, :name => 'test product 1', :enterprise_id => e1.id, :product_category_id => @product_category.id) assert p1.public? e1.public_profile = false - e1.save! + e1.save!; p1.reload; assert !p1.public? end @@ -193,16 +178,16 @@ class ProductTest < Test::Unit::TestCase end end - should 'sanitize name before validation' do - product = Product.new + should 'strip name with malformed HTML when sanitize' do + product = Product.new(:product_category => @product_category) product.name = "