From 5cac53de4a005bcd007d47dd557bcb009acadc3e Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Tue, 12 Mar 2013 20:05:52 +0000 Subject: [PATCH] Fixing plugin tests on search controller --- plugins/solr/lib/ext/facets_browse.rb | 2 +- plugins/solr/lib/solr_plugin.rb | 4 ++++ plugins/solr/lib/solr_plugin/results_helper.rb | 122 -------------------------------------------------------------------------------------------------------------------------- plugins/solr/lib/solr_plugin/search_helper.rb | 239 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------- plugins/solr/test/functional/search_controller_test.rb | 361 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/solr/test/unit/search_helper_test.rb | 2 +- plugins/solr/views/search/facets_browse.html.erb | 2 +- plugins/solr/views/search/search_page.html.erb | 2 +- test/functional/search_controller_test.rb | 3 +++ test/test_helper.rb | 1 - 10 files changed, 552 insertions(+), 186 deletions(-) delete mode 100644 plugins/solr/lib/solr_plugin/results_helper.rb create mode 100644 plugins/solr/test/functional/search_controller_test.rb diff --git a/plugins/solr/lib/ext/facets_browse.rb b/plugins/solr/lib/ext/facets_browse.rb index f5de95e..0ab3bb9 100644 --- a/plugins/solr/lib/ext/facets_browse.rb +++ b/plugins/solr/lib/ext/facets_browse.rb @@ -3,7 +3,7 @@ require_dependency 'search_controller' module SolrPlugin::FacetsBrowse def self.included(base) base.send :include, InstanceMethods - base.send :include, SolrPlugin::ResultsHelper + base.send :include, SolrPlugin::SearchHelper end module InstanceMethods diff --git a/plugins/solr/lib/solr_plugin.rb b/plugins/solr/lib/solr_plugin.rb index e7cd6dd..904ce2a 100644 --- a/plugins/solr/lib/solr_plugin.rb +++ b/plugins/solr/lib/solr_plugin.rb @@ -1,9 +1,13 @@ +class SolrPlugin < Noosfero::Plugin; end; + require_dependency 'solr_plugin/search_helper' class SolrPlugin < Noosfero::Plugin include SolrPlugin::SearchHelper + delegate :params, :current_user, :to => :context + def self.plugin_name "Solr" end diff --git a/plugins/solr/lib/solr_plugin/results_helper.rb b/plugins/solr/lib/solr_plugin/results_helper.rb deleted file mode 100644 index 5555236..0000000 --- a/plugins/solr/lib/solr_plugin/results_helper.rb +++ /dev/null @@ -1,122 +0,0 @@ -class SolrPlugin < Noosfero::Plugin - module ResultsHelper - def asset_class(asset) - asset.to_s.singularize.camelize.constantize - end - - def set_facets_variables - @facets = @searches[@asset][:facets] - @all_facets = @searches[@asset][:all_facets] - end - - def order_by(asset) - options = SolrPlugin::SortOptions[asset].map do |name, options| - next if options[:if] && !instance_eval(&options[:if]) - [_(options[:label]), name.to_s] - end.compact - - content_tag('div', _('Sort results by ') + - select_tag(asset.to_s + '[order]', options_for_select(options, params[:order_by] || 'none'), - {:onchange => "window.location = jQuery.param.querystring(window.location.href, { 'order_by' : this.options[this.selectedIndex].value})"} - ), - :class => "search-ordering" - ) - end - - def label_total_found(asset, total_found) - labels = { - :products => _("%s products offers found"), - :articles => _("%s articles found"), - :events => _("%s events found"), - :people => _("%s people found"), - :enterprises => _("%s enterprises found"), - :communities => _("%s communities found"), - } - content_tag('span', labels[asset] % total_found, - :class => "total-pages-found") if labels[asset] - end - - def facets_menu(asset, _facets) - @asset_class = asset_class(asset) - @facets = _facets - render(:partial => 'facets_menu') - end - - def facets_unselect_menu(asset) - @asset_class = asset_class(asset) - render(:partial => 'facets_unselect_menu') - end - - def facet_selecteds_html_for(environment, klass, params) - def name_with_extra(klass, facet, value) - name = klass.facet_result_name(facet, value) - name = name[0] + name[1] if name.kind_of?(Array) - name - end - - ret = [] - params = params.dup - params[:facet].each do |id, value| - facet = klass.facet_by_id(id.to_sym) - next unless facet - if value.kind_of?(Hash) - label_hash = facet[:label].call(environment) - value.each do |label_id, value| - facet[:label_id] = label_id - facet[:label] = label_hash[label_id] - value.to_a.each do |value| - ret << [facet[:label], name_with_extra(klass, facet, value), - params.merge(:facet => params[:facet].merge(id => params[:facet][id].merge(label_id => params[:facet][id][label_id].to_a.reject{ |v| v == value })))] - end - end - else - ret << [klass.facet_label(facet), name_with_extra(klass, facet, value), - params.merge(:facet => params[:facet].reject{ |k,v| k == id })] - end - end - - ret.map do |label, name, url| - content_tag('div', content_tag('span', label, :class => 'facet-selected-label') + - content_tag('span', name, :class => 'facet-selected-name') + - link_to('', url, :class => 'facet-selected-remove', :title => 'remove facet'), :class => 'facet-selected') - end.join - end - - def facet_link_html(facet, params, value, label, count) - params = params ? params.dup : {} - has_extra = label.kind_of?(Array) - link_label = has_extra ? label[0] : label - id = facet[:solr_field].to_s - params[:facet] ||= {} - params[:facet][id] ||= {} - params[:page] = {} if params[:page] - - selected = facet[:label_id].nil? ? params[:facet][id] == value : params[:facet][id][facet[:label_id]].to_a.include?(value) - - if count > 0 - url = params.merge(:facet => params[:facet].merge( - id => facet[:label_id].nil? ? value : params[:facet][id].merge( facet[:label_id] => params[:facet][id][facet[:label_id]].to_a | [value] ) - )) - else - # preserve others filters and change this filter - url = params.merge(:facet => params[:facet].merge( - id => facet[:label_id].nil? ? value : { facet[:label_id] => value } - )) - end - - content_tag 'div', link_to(link_label, url, :class => 'facet-result-link-label') + - content_tag('span', (has_extra ? label[1] : ''), :class => 'facet-result-extra-label') + - (count > 0 ? content_tag('span', " (#{count})", :class => 'facet-result-count') : ''), - :class => 'facet-menu-item' + (selected ? ' facet-result-link-selected' : '') - end - - def facet_javascript(input_id, facet, array) - array = [] if array.nil? - hintText = _('Type in an option') - text_field_tag('facet['+input_id+']', '', :id => input_id) + - javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, - {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true, - #{jquery_token_input_messages_json(hintText)}});") - end - end -end diff --git a/plugins/solr/lib/solr_plugin/search_helper.rb b/plugins/solr/lib/solr_plugin/search_helper.rb index 734ee8e..3d7626a 100644 --- a/plugins/solr/lib/solr_plugin/search_helper.rb +++ b/plugins/solr/lib/solr_plugin/search_helper.rb @@ -1,4 +1,8 @@ -class SolrPlugin < Noosfero::Plugin +require_dependency 'search_helper' + +module SolrPlugin::SearchHelper + + include SearchHelper LIST_SEARCH_LIMIT = 20 DistFilt = 200 @@ -30,81 +34,198 @@ class SolrPlugin < Noosfero::Plugin ], } - module SearchHelper - def asset_class(asset) - asset.to_s.singularize.camelize.constantize - end +# def asset_class(asset) +# asset.to_s.singularize.camelize.constantize +# end - def class_asset(klass) - klass.name.underscore.pluralize.to_sym - end - - def asset_table(asset) - asset_class(asset).table_name - end + def class_asset(klass) + klass.name.underscore.pluralize.to_sym + end - def multiple_search? - ['index', 'category_index'].include?(context.params[:action]) + def asset_table(asset) + asset_class(asset).table_name + end +# +# def multiple_search? +# ['index', 'category_index'].include?(context.params[:action]) +# end + + def filters(asset) + case asset + when :products + ['solr_plugin_public:true'] + when :events + [] + else + ['solr_plugin_public:true'] end + end - def filters(asset) - case asset - when :products - ['solr_plugin_public:true'] - when :events - [] - else - ['solr_plugin_public:true'] + def results_only? + context.params[:action] == 'index' + end + + def empty_query?(query, category) + category.nil? && query.blank? + end + + def solr_options(asset, category) + asset_class = asset_class(asset) + solr_options = {} + if !multiple_search? + if !results_only? and asset_class.respond_to? :facets + solr_options.merge! asset_class.facets_find_options(context.params[:facet]) + solr_options[:all_facets] = true + end + solr_options[:filter_queries] ||= [] + solr_options[:filter_queries] += filters(asset) + solr_options[:filter_queries] << "environment_id:#{context.environment.id}" + solr_options[:filter_queries] << asset_class.facet_category_query.call(category) if category + + solr_options[:boost_functions] ||= [] + context.params[:order_by] = nil if context.params[:order_by] == 'none' + if context.params[:order_by] + order = SortOptions[asset][context.params[:order_by].to_sym] + raise "Unknown order by" if order.nil? + order[:solr_opts].each do |opt, value| + solr_options[opt] = value.is_a?(Proc) ? instance_eval(&value) : value + end end end + solr_options + end + + def products_options(person) + geosearch = person && person.lat && person.lng - def results_only? - context.params[:action] == 'index' + extra_limit = LIST_SEARCH_LIMIT*5 + sql_options = {:limit => LIST_SEARCH_LIMIT, :order => 'random()'} + if geosearch + {:sql_options => sql_options, :extra_limit => extra_limit, + :alternate_query => "{!boost b=recip(geodist(),#{"%e" % (1.to_f/DistBoost)},1,1)}", + :radius => DistFilt, :latitude => person.lat, :longitude => person.lng} + else + { :sql_options => sql_options, :extra_limit => extra_limit, + :boost_functions => ['recip(ms(NOW/HOUR,updated_at),1.3e-10,1,1)']} end + end + + def asset_class(asset) + asset.to_s.singularize.camelize.constantize + end - def empty_query?(query, category) - category.nil? && query.blank? + def set_facets_variables + @facets = @searches[@asset][:facets] + @all_facets = @searches[@asset][:all_facets] + end + + def order_by(asset) + options = SortOptions[asset].map do |name, options| + next if options[:if] && !instance_eval(&options[:if]) + [_(options[:label]), name.to_s] + end.compact + + content_tag('div', _('Sort results by ') + + select_tag(asset.to_s + '[order]', options_for_select(options, params[:order_by] || 'none'), + {:onchange => "window.location = jQuery.param.querystring(window.location.href, { 'order_by' : this.options[this.selectedIndex].value})"} + ), + :class => "search-ordering" + ) + end + + def label_total_found(asset, total_found) + labels = { + :products => _("%s products offers found"), + :articles => _("%s articles found"), + :events => _("%s events found"), + :people => _("%s people found"), + :enterprises => _("%s enterprises found"), + :communities => _("%s communities found"), + } + content_tag('span', labels[asset] % total_found, + :class => "total-pages-found") if labels[asset] + end + + def facets_menu(asset, _facets) + @asset_class = asset_class(asset) + @facets = _facets + render(:partial => 'facets_menu') + end + + def facets_unselect_menu(asset) + @asset_class = asset_class(asset) + render(:partial => 'facets_unselect_menu') + end + + def facet_selecteds_html_for(environment, klass, params) + def name_with_extra(klass, facet, value) + name = klass.facet_result_name(facet, value) + name = name[0] + name[1] if name.kind_of?(Array) + name end - def solr_options(asset, category) - asset_class = asset_class(asset) - solr_options = {} - if !multiple_search? - if !results_only? and asset_class.respond_to? :facets - solr_options.merge! asset_class.facets_find_options(context.params[:facet]) - solr_options[:all_facets] = true - end - solr_options[:filter_queries] ||= [] - solr_options[:filter_queries] += filters(asset) - solr_options[:filter_queries] << "environment_id:#{context.environment.id}" - solr_options[:filter_queries] << asset_class.facet_category_query.call(category) if category - - solr_options[:boost_functions] ||= [] - context.params[:order_by] = nil if context.params[:order_by] == 'none' - if context.params[:order_by] - order = SolrPlugin::SortOptions[asset][context.params[:order_by].to_sym] - raise "Unknown order by" if order.nil? - order[:solr_opts].each do |opt, value| - solr_options[opt] = value.is_a?(Proc) ? instance_eval(&value) : value + ret = [] + params = params.dup + params[:facet].each do |id, value| + facet = klass.facet_by_id(id.to_sym) + next unless facet + if value.kind_of?(Hash) + label_hash = facet[:label].call(environment) + value.each do |label_id, value| + facet[:label_id] = label_id + facet[:label] = label_hash[label_id] + value.to_a.each do |value| + ret << [facet[:label], name_with_extra(klass, facet, value), + params.merge(:facet => params[:facet].merge(id => params[:facet][id].merge(label_id => params[:facet][id][label_id].to_a.reject{ |v| v == value })))] end end + else + ret << [klass.facet_label(facet), name_with_extra(klass, facet, value), + params.merge(:facet => params[:facet].reject{ |k,v| k == id })] end - solr_options end - def products_options(person) - geosearch = person && person.lat && person.lng + ret.map do |label, name, url| + content_tag('div', content_tag('span', label, :class => 'facet-selected-label') + + content_tag('span', name, :class => 'facet-selected-name') + + link_to('', url, :class => 'facet-selected-remove', :title => 'remove facet'), :class => 'facet-selected') + end.join + end - extra_limit = LIST_SEARCH_LIMIT*5 - sql_options = {:limit => LIST_SEARCH_LIMIT, :order => 'random()'} - if geosearch - {:sql_options => sql_options, :extra_limit => extra_limit, - :alternate_query => "{!boost b=recip(geodist(),#{"%e" % (1.to_f/DistBoost)},1,1)}", - :radius => DistFilt, :latitude => person.lat, :longitude => person.lng} - else - { :sql_options => sql_options, :extra_limit => extra_limit, - :boost_functions => ['recip(ms(NOW/HOUR,updated_at),1.3e-10,1,1)']} - end + def facet_link_html(facet, params, value, label, count) + params = params ? params.dup : {} + has_extra = label.kind_of?(Array) + link_label = has_extra ? label[0] : label + id = facet[:solr_field].to_s + params[:facet] ||= {} + params[:facet][id] ||= {} + params[:page] = {} if params[:page] + + selected = facet[:label_id].nil? ? params[:facet][id] == value : params[:facet][id][facet[:label_id]].to_a.include?(value) + + if count > 0 + url = params.merge(:facet => params[:facet].merge( + id => facet[:label_id].nil? ? value : params[:facet][id].merge( facet[:label_id] => params[:facet][id][facet[:label_id]].to_a | [value] ) + )) + else + # preserve others filters and change this filter + url = params.merge(:facet => params[:facet].merge( + id => facet[:label_id].nil? ? value : { facet[:label_id] => value } + )) end + + content_tag 'div', link_to(link_label, url, :class => 'facet-result-link-label') + + content_tag('span', (has_extra ? label[1] : ''), :class => 'facet-result-extra-label') + + (count > 0 ? content_tag('span', " (#{count})", :class => 'facet-result-count') : ''), + :class => 'facet-menu-item' + (selected ? ' facet-result-link-selected' : '') + end + + def facet_javascript(input_id, facet, array) + array = [] if array.nil? + hintText = _('Type in an option') + text_field_tag('facet['+input_id+']', '', :id => input_id) + + javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, + {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true, + #{jquery_token_input_messages_json(hintText)}});") end end diff --git a/plugins/solr/test/functional/search_controller_test.rb b/plugins/solr/test/functional/search_controller_test.rb new file mode 100644 index 0000000..790fab7 --- /dev/null +++ b/plugins/solr/test/functional/search_controller_test.rb @@ -0,0 +1,361 @@ +require 'test_helper' +require 'test_solr_helper' +require File.dirname(__FILE__) + '/../../lib/ext/facets_browse' + +# Re-raise errors caught by the controller. +class SearchController; def rescue_action(e) raise e end; end + +class SearchControllerTest < ActionController::TestCase + + def setup + TestSolr.enable + p1 = File.join(RAILS_ROOT, 'app', 'views') + p2 = File.join(File.dirname(__FILE__) + '/../../views') + SearchController.append_view_path([p1,p2]) + @controller = SearchController.new + @request = ActionController::TestRequest.new + @request.stubs(:ssl?).returns(false) + @response = ActionController::TestResponse.new + + @category = Category.create!(:name => 'my category', :environment => Environment.default) + + env = Environment.default + domain = env.domains.first + if !domain + domain = Domain.create!(:name => "127.0.0.1") + env.domains = [domain] + env.save! + end + domain.google_maps_key = 'ENVIRONMENT_KEY' + domain.save! + + @product_category = fast_create(ProductCategory) + + # By pass user validation on person creation + user = mock() + user.stubs(:id).returns(1) + user.stubs(:valid?).returns(true) + user.stubs(:email).returns('some@test.com') + user.stubs(:save!).returns(true) + Person.any_instance.stubs(:user).returns(user) + + env.enable_plugin(SolrPlugin) + end + + should 'get facets with articles search results' do + cat1 = fast_create(Category, :name => 'cat1') + cat2 = fast_create(Category, :name => 'cat2') + person = fast_create(Person) + art = create_article_with_optional_category('an article to be found', person) + art.add_category cat1, false + art.add_category cat2, true + art.save! + + get 'articles', :query => 'article found' + assert !assigns(:searches)[:articles][:facets].blank? + assert assigns(:searches)[:articles][:facets]['facet_fields']['solr_plugin_f_type_facet'][0][0] == 'Article' + assert assigns(:searches)[:articles][:facets]['facet_fields']['solr_plugin_f_profile_type_facet'][0][0] == 'Person' + assert assigns(:searches)[:articles][:facets]['facet_fields']['solr_plugin_f_category_facet'][0][0] == 'cat1' + assert assigns(:searches)[:articles][:facets]['facet_fields']['solr_plugin_f_category_facet'][1][0] == 'cat2' + end + + should 'get facets with people search results' do + state = fast_create(State, :name => 'Acre', :acronym => 'AC') + city = fast_create(City, :name => 'Rio Branco', :parent_id => state.id) + person = Person.create!(:name => 'Hildebrando', :identifier => 'hild', :user_id => fast_create(User).id, :region_id => city.id) + cat1 = fast_create(Category, :name => 'cat1') + cat2 = fast_create(Category, :name => 'cat2') + person.add_category cat1 + person.add_category cat2 + + get 'people', :query => 'Hildebrando' + + assert !assigns(:searches)[:people][:facets].blank? + assert assigns(:searches)[:people][:facets]['facet_fields']['solr_plugin_f_region_facet'][0][0] == city.id.to_s + + categories_facet = assigns(:searches)[:people][:facets]['facet_fields']['solr_plugin_f_categories_facet'] + assert_equal 2, categories_facet.count + assert_equivalent [cat1.id.to_s, cat2.id.to_s], [categories_facet[0][0], categories_facet[1][0]] + end + + should 'get facets with products search results' do + state = fast_create(State, :name => 'Acre', :acronym => 'AC') + city = fast_create(City, :name => 'Rio Branco', :parent_id => state.id) + ent = fast_create(Enterprise, :region_id => city.id) + prod = Product.create!(:name => 'Sound System', :enterprise_id => ent.id, :product_category_id => @product_category.id) + qualifier1 = fast_create(Qualifier) + qualifier2 = fast_create(Qualifier) + prod.qualifiers_list = [[qualifier1.id, 0], [qualifier2.id, 0]] + prod.qualifiers.reload + prod.save! + + get 'products', :query => 'Sound' + assert !assigns(:searches)[:products][:facets].blank? + assert assigns(:searches)[:products][:facets]['facet_fields']['solr_plugin_f_category_facet'][0][0] == @product_category.name + assert assigns(:searches)[:products][:facets]['facet_fields']['solr_plugin_f_region_facet'][0][0] == city.id.to_s + assert assigns(:searches)[:products][:facets]['facet_fields']['solr_plugin_f_qualifier_facet'][0][0] == "#{qualifier1.id} 0" + assert assigns(:searches)[:products][:facets]['facet_fields']['solr_plugin_f_qualifier_facet'][1][0] == "#{qualifier2.id} 0" + end + + # 'assets' menu outside any category + should 'list products in general without geosearch' do + Profile.delete_all + SearchController.stubs(:logged_in?).returns(false) + + ent1 = create_profile_with_optional_category(Enterprise, 'teste1') + ent2 = create_profile_with_optional_category(Enterprise, '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) + + get :products + assert_equivalent [prod2, prod1], assigns(:searches)[:products][:results] + assert_match 'Highlights', @response.body + end + + should 'offer text box to enter a new search in general context' do + get :index, :query => 'a sample search' + assert_tag :tag => 'form', :attributes => { :action => '/search' }, :descendant => { + :tag => 'input', + :attributes => { :name => 'query', :value => 'a sample search' } + } + end + + should 'offer text box to enter a new seach in specific context' do + get :index, :category_path => [ 'my-category'], :query => 'a sample search' + assert_tag :tag => 'form', :attributes => { :action => '/search/index/my-category' }, :descendant => { + :tag => 'input', + :attributes => { :name => 'query', :value => 'a sample search' } + } + end + + should 'find enterprise by product category' do + ent1 = create_profile_with_optional_category(Enterprise, 'test1') + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default) + prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) + + ent2 = create_profile_with_optional_category(Enterprise, 'test2') + + get :index, :query => prod_cat.name + + assert_includes assigns(:searches)[:enterprises][:results], ent1 + assert_not_includes assigns(:searches)[:enterprises][:results], ent2 + end + + should 'show only results in general search' do + ent1 = create_profile_with_optional_category(Enterprise, 'test1') + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default) + prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) + + ent2 = create_profile_with_optional_category(Enterprise, 'test2') + + get :index, :query => prod_cat.name + + assert assigns(:facets).blank? + assert assigns(:searches)[:enterprises][:facets].blank? + assert assigns(:searches)[:products][:facets].blank? + end + + # Testing random sequences always have a small chance of failing +# should 'randomize product display in empty search' do +# prod_cat = ProductCategory.create!(:name => 'prod cat test', :environment => Environment.default) +# ent = create_profile_with_optional_category(Enterprise, 'test enterprise') +# (1..SearchController::LIST_SEARCH_LIMIT+5).each do |n| +# fast_create(Product, {:name => "produto #{n}", :enterprise_id => ent.id, :product_category_id => prod_cat.id}, :search => true) +# end +# +# get :products +# result1 = assigns(:searches)[:products][:results].map(&:id) +# +# (1..10).each do |n| +# get :products +# end +# result2 = assigns(:searches)[:products][:results].map(&:id) +# +# assert_not_equal result1, result2 +# end +# +# should 'remove far products by geolocalization empty logged search' do +# user = create_user('a_logged_user') +# # trigger geosearch +# user.person.lat = '1.0' +# user.person.lng = '1.0' +# SearchController.any_instance.stubs(:logged_in?).returns(true) +# SearchController.any_instance.stubs(:current_user).returns(user) +# +# cat = fast_create(ProductCategory) +# ent1 = Enterprise.create!(:name => 'ent1', :identifier => 'ent1', :lat => '1.3', :lng => '1.3') +# prod1 = Product.create!(:name => 'produto 1', :enterprise_id => ent1.id, :product_category_id => cat.id) +# ent2 = Enterprise.create!(:name => 'ent2', :identifier => 'ent2', :lat => '2.0', :lng => '2.0') +# prod2 = Product.create!(:name => 'produto 2', :enterprise_id => ent2.id, :product_category_id => cat.id) +# ent3 = Enterprise.create!(:name => 'ent3', :identifier => 'ent3', :lat => '1.6', :lng => '1.6') +# prod3 = Product.create!(:name => 'produto 3', :enterprise_id => ent3.id, :product_category_id => cat.id) +# ent4 = Enterprise.create!(:name => 'ent4', :identifier => 'ent4', :lat => '10', :lng => '10') +# prod4 = Product.create!(:name => 'produto 4', :enterprise_id => ent4.id, :product_category_id => cat.id) +# +# get :products +# assert_equivalent [prod1, prod3, prod2], assigns(:searches)[:products][:results] +# end + + should 'browse facets when query is not empty' do + get :articles, :query => 'something' + get :facets_browse, :asset_key => 'articles', :facet_id => 'solr_plugin_f_type' + assert_equal assigns(:facet)[:id], 'solr_plugin_f_type' + get :products, :query => 'something' + get :facets_browse, :asset_key => 'products', :facet_id => 'solr_plugin_f_category' + assert_equal assigns(:facet)[:id], 'solr_plugin_f_category' + get :people, :query => 'something' + get :facets_browse, :asset_key => 'people', :facet_id => 'solr_plugin_f_region' + assert_equal assigns(:facet)[:id], 'solr_plugin_f_region' + end + + should 'raise exception when facet is invalid' do + get :articles, :query => 'something' + assert_raise RuntimeError do + get :facets_browse, :asset_key => 'articles', :facet_id => 'solr_plugin_fwhatever' + end + end + + should 'order product results by more recent when requested' do + ent = fast_create(Enterprise) + prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent.id, :product_category_id => @product_category.id) + prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent.id, :product_category_id => @product_category.id) + prod3 = Product.create!(:name => 'product 3', :enterprise_id => ent.id, :product_category_id => @product_category.id) + + # change others attrs will make updated_at = Time.now for all + Product.record_timestamps = false + prod3.update_attribute :updated_at, Time.now-2.days + prod1.update_attribute :updated_at, Time.now-1.days + prod2.update_attribute :updated_at, Time.now + Product.record_timestamps = true + + get :products, :query => 'product', :order_by => :more_recent + + assert_equal [prod2, prod1, prod3], assigns(:searches)[:products][:results].docs + end + + should 'only list products from enabled enterprises' do + ent1 = fast_create(Enterprise, :enabled => true) + ent2 = fast_create(Enterprise, :enabled => false) + prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent1.id, :product_category_id => @product_category.id) + prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent2.id, :product_category_id => @product_category.id) + + get :products, :query => 'product' + + assert_includes assigns(:searches)[:products][:results], prod1 + assert_not_includes assigns(:searches)[:products][:results], prod2 + end + + should 'order product results by name when requested' do + ent = fast_create(Enterprise) + prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent.id, :product_category_id => @product_category.id) + prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent.id, :product_category_id => @product_category.id) + prod3 = Product.create!(:name => 'product 3', :enterprise_id => ent.id, :product_category_id => @product_category.id) + + prod3.update_attribute :name, 'product A' + prod2.update_attribute :name, 'product B' + prod1.update_attribute :name, 'product C' + + get :products, :query => 'product', :order_by => :name + + assert_equal [prod3, prod2, prod1], assigns(:searches)[:products][:results].docs + end + + should 'order product results by closest when requested' do + user = create_user('a_logged_user') + user.person.lat = '1.0' + user.person.lng = '1.0' + # trigger geosearch + SearchController.any_instance.stubs(:logged_in?).returns(true) + SearchController.any_instance.stubs(:current_user).returns(user) + + cat = fast_create(ProductCategory) + ent1 = Enterprise.create!(:name => 'ent1', :identifier => 'ent1', :lat => '-5.0', :lng => '-5.0') + prod1 = Product.create!(:name => 'product 1', :enterprise_id => ent1.id, :product_category_id => cat.id) + ent2 = Enterprise.create!(:name => 'ent2', :identifier => 'ent2', :lat => '2.0', :lng => '2.0') + prod2 = Product.create!(:name => 'product 2', :enterprise_id => ent2.id, :product_category_id => cat.id) + ent3 = Enterprise.create!(:name => 'ent3', :identifier => 'ent3', :lat => '10.0', :lng => '10.0') + prod3 = Product.create!(:name => 'product 3', :enterprise_id => ent3.id, :product_category_id => cat.id) + + get :products, :query => 'product', :order_by => :closest + assert_equal [prod2, prod1, prod3], assigns(:searches)[:products][:results].docs + end + + + should 'order events by name when requested' do + person = create_user('someone').person + ev1 = create_event(person, :name => 'party B', :category_ids => [@category.id], :start_date => Date.today - 1.day) + ev2 = create_event(person, :name => 'party A', :category_ids => [@category.id], :start_date => Date.today - 2.days) + + get :events, :query => 'party', :order_by => :name + + assert_equal [ev2, ev1], assigns(:searches)[:events][:results].docs + end + + should 'order articles by name when requested' do + art1 = Article.create!(:name => 'review C', :profile_id => fast_create(Person).id) + art2 = Article.create!(:name => 'review A', :profile_id => fast_create(Person).id) + art3 = Article.create!(:name => 'review B', :profile_id => fast_create(Person).id) + + get :articles, :query => 'review', :order_by => :name + + assert_equal [art2, art3, art1], assigns(:searches)[:articles][:results].docs + end + + should 'order enterprise results by name when requested' do + ent1 = Enterprise.create!(:name => 'Company B', :identifier => 'com_b') + ent2 = Enterprise.create!(:name => 'Company A', :identifier => 'com_a') + ent3 = Enterprise.create!(:name => 'Company C', :identifier => 'com_c') + + get :enterprises, :query => 'Company', :order_by => :name + + assert_equal [ent2, ent1, ent3], assigns(:searches)[:enterprises][:results].docs + end + + should 'order people results by name when requested' do + person1 = Person.create!(:name => 'Deodárbio Silva', :identifier => 'deod', :user_id => fast_create(User).id) + person2 = Person.create!(:name => 'Glislange Silva', :identifier => 'glis', :user_id => fast_create(User).id) + person3 = Person.create!(:name => 'Ausêncio Silva', :identifier => 'ause', :user_id => fast_create(User).id) + + get :people, :query => 'Silva', :order_by => :name + + assert_equal [person3, person1, person2], assigns(:searches)[:people][:results].docs + end + + should 'order community results by name when requested' do + com1 = Community.create!(:name => 'Yellow Group') + com2 = Community.create!(:name => 'Red Group') + com3 = Community.create!(:name => 'Green Group') + + get :communities, :query => 'Group', :order_by => :name + + assert_equal [com3, com2, com1], assigns(:searches)[:communities][:results].docs + end + + should 'raise error when requested to order by unknown filter' do + com1 = Community.create!(:name => 'Yellow Group') + com2 = Community.create!(:name => 'Red Group') + com3 = Community.create!(:name => 'Green Group') + + assert_raise RuntimeError do + get :communities, :query => 'Group', :order_by => :something + end + end + + protected + + def create_article_with_optional_category(name, profile, category = nil) + fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category) + end + + def create_profile_with_optional_category(klass, name, category = nil, data = {}) + fast_create(klass, { :name => name }.merge(data), :search => true, :category => category) + end + + def create_event(profile, options) + ev = Event.new({ :name => 'some event', :start_date => Date.new(2008,1,1) }.merge(options)) + ev.profile = profile + ev.save! + ev + end + +end diff --git a/plugins/solr/test/unit/search_helper_test.rb b/plugins/solr/test/unit/search_helper_test.rb index 77b796e..886daa8 100644 --- a/plugins/solr/test/unit/search_helper_test.rb +++ b/plugins/solr/test/unit/search_helper_test.rb @@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../test_helper' class SearchHelperTest < ActiveSupport::TestCase - include SearchHelper + include SolrPlugin::SearchHelper should 'display facets menu' do expects(:asset_class).with('asset') diff --git a/plugins/solr/views/search/facets_browse.html.erb b/plugins/solr/views/search/facets_browse.html.erb index 54c55c2..bfc7566 100644 --- a/plugins/solr/views/search/facets_browse.html.erb +++ b/plugins/solr/views/search/facets_browse.html.erb @@ -1,4 +1,4 @@ -<% extend SolrPlugin::ResultsHelper %> +<% extend SolrPlugin::SearchHelper %> <% results = @asset_class.map_facet_results(@facet, params[:facet], @facets, @all_facets) %> diff --git a/plugins/solr/views/search/search_page.html.erb b/plugins/solr/views/search/search_page.html.erb index a2a8539..a3a0196 100644 --- a/plugins/solr/views/search/search_page.html.erb +++ b/plugins/solr/views/search/search_page.html.erb @@ -1,4 +1,4 @@ -<% extend SolrPlugin::ResultsHelper %> +<% extend SolrPlugin::SearchHelper %> <% set_facets_variables %> <%= search_page_title( @titles[@asset], @category ) %> diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index ddf562f..5a093de 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -7,6 +7,7 @@ class SearchController; def rescue_action(e) raise e end; end class SearchControllerTest < ActionController::TestCase def setup + TestSolr.enable @controller = SearchController.new @request = ActionController::TestRequest.new @request.stubs(:ssl?).returns(false) @@ -33,6 +34,8 @@ class SearchControllerTest < ActionController::TestCase user.stubs(:email).returns('some@test.com') user.stubs(:save!).returns(true) Person.any_instance.stubs(:user).returns(user) + + env.enable_plugin(SolrPlugin) end def create_article_with_optional_category(name, profile, category = nil) diff --git a/test/test_helper.rb b/test/test_helper.rb index 36935d4..88a12f7 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,7 +17,6 @@ require 'noosfero/test' require File.dirname(__FILE__) + '/factories' require File.dirname(__FILE__) + '/noosfero_doc_test' require File.dirname(__FILE__) + '/action_tracker_test_helper' -require File.expand_path(File.dirname(__FILE__) + "/test_solr_helper.rb") FileUtils.rm_rf(File.join(RAILS_ROOT, 'index', 'test')) -- libgit2 0.21.2