From e8e1083e2ec08176dc4413cc0c90495bbde027f9 Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Tue, 23 Aug 2011 12:31:10 +0000 Subject: [PATCH] test fixes and refactorings for search --- app/controllers/public/search_controller.rb | 13 ++++--------- app/helpers/application_helper.rb | 13 ------------- app/helpers/search_helper.rb | 47 +++++++++++++++++++++++++---------------------- app/models/article.rb | 8 ++++---- app/views/search/_event.rhtml | 6 ------ app/views/search/_results_header.rhtml | 8 ++++---- app/views/search/_search_form.rhtml | 1 - app/views/search/articles.rhtml | 5 ++--- app/views/search/category_index.rhtml | 23 ----------------------- app/views/search/category_index.rhtml | 1 + app/views/search/communities.rhtml | 5 ++--- app/views/search/enterprises.rhtml | 5 ++--- app/views/search/index.rhtml | 30 ++++++++++++++++++++++++------ app/views/search/people.rhtml | 5 ++--- app/views/search/products.rhtml | 5 ++--- public/stylesheets/search.css | 5 ++++- test/functional/search_controller_test.rb | 315 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- test/unit/category_test.rb | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 18 files changed, 196 insertions(+), 360 deletions(-) delete mode 100644 app/views/search/category_index.rhtml create mode 120000 app/views/search/category_index.rhtml diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index dd1e1aa..d6575cd 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -123,6 +123,8 @@ class SearchController < PublicController @results_only = true @enabled_searchs.select { |key,description| @searching[key] }.each do |key, description| + load_query + @asset = key send(key) @order << key @names[key] = getterm(description) @@ -204,8 +206,9 @@ class SearchController < PublicController @category = environment.categories.find_by_path(path) if @category.nil? render_not_found(path) + else + @category_id = @category.id end - @category_id = @category.id end end @@ -301,12 +304,4 @@ class SearchController < PublicController result end - def asset_class(asset) - asset.to_s.singularize.camelize.constantize - end - - def asset_table(asset) - asset_class(asset).table_name - end - end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index bb4bd61..ca3017b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -884,19 +884,6 @@ module ApplicationHelper result end - def search_page_title(title, category = nil) - title = "

" + title - title += '' + category.name + '' if category - title + "

" - end - - def search_page_link_to_all(options={}) - if options[:category] - title = "
" + _('In all categories') + "
" - link_to title, :action => 'assets', :asset => options[:asset], :category_path => [] - end - end - def template_stylesheet_path if profile.nil? "/designs/templates/#{environment.layout_template}/stylesheets/style.css" diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index c58218c..d226b64 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -3,10 +3,17 @@ module SearchHelper # FIXME remove it after search_controler refactored include EventsHelper - def relevance_for(hit) - n = (hit.ferret_score if hit.respond_to?(:ferret_score)) - n ||= 1.0 - (n * 100.0).round + def search_page_title(title, category = nil) + title = "

" + title + title += '' + category.name + '' if category + title + "

" + end + + def category_context(category, url) + content_tag('div', category.full_name + _(', ') + + link_to(_('search in all categories'), + url.merge(:category_path => [], :action => (params[:action] == 'category_index' ? 'index' : params[:action]) )), + :align => 'center', :class => 'search-category-context') if category end def display_results(use_map = false) @@ -24,7 +31,7 @@ module SearchHelper def display_map_list_button button(:search, params[:display] == 'map' ? _('Display in list') : _('Display in map'), params.merge(:display => (params[:display] == 'map' ? 'list' : 'map')), - :class => "map-toggle-button" ) + :class => "map-toggle-button" ) if GoogleMaps.enabled?(environment.default_hostname) end def city_with_state(city) @@ -115,14 +122,6 @@ module SearchHelper end.join end - def asset_class(asset) - asset.to_s.singularize.camelize.constantize - end - - def asset_table(asset) - asset_class(asset).table_name - end - def order_by(asset) options = { :products => [[_('Relevance'), ''], [_('Name'), 'name_or_category_sort asc'], [_('Lower price'), 'price_sort asc'], [_('Higher price'), 'price_sort desc']], @@ -134,9 +133,9 @@ module SearchHelper } content_tag('div', _('Sort results by ') + - select_tag(asset.to_s + '[order]', options_for_select(options[asset], params[:order_by]), - {:onchange => "window.location=jQuery.param.querystring(window.location.href, { 'order_by' : this.options[this.selectedIndex].value})"}), - :class => "search-ordering") + select_tag(asset.to_s + '[order]', options_for_select(options[asset], params[:order_by]), + {: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) @@ -148,12 +147,16 @@ module SearchHelper :enterprises => _("%s enterprises found"), :communities => _("%s communities found"), } - if labels[asset] - content_tag('span', labels[asset] % total_found, - :class => "total-pages-found") - else - '' - end + content_tag('span', labels[asset] % total_found, + :class => "total-pages-found") if labels[asset] + end + + def asset_class(asset) + asset.to_s.singularize.camelize.constantize + end + + def asset_table(asset) + asset_class(asset).table_name end end diff --git a/app/models/article.rb b/app/models/article.rb index 21d1246..db08d30 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -152,7 +152,7 @@ class Article < ActiveRecord::Base # Only includes articles where advertise == true def self.recent(limit = nil, extra_conditions = {}) # FIXME this method is a horrible hack - options = { :limit => limit, + options = { :page => 1, :per_page => limit, :conditions => [ "advertise = ? AND published = ? AND @@ -170,10 +170,10 @@ class Article < ActiveRecord::Base options.delete(:include) end if extra_conditions == {} - self.find(:all, options) + self.paginate(options) else with_scope :find => {:conditions => extra_conditions} do - self.find(:all, options) + self.paginate(options) end end end @@ -181,7 +181,7 @@ class Article < ActiveRecord::Base # retrives the most commented articles, sorted by the comment count (largest # first) def self.most_commented(limit) - find(:all, :order => 'comments_count DESC', :limit => limit) + paginate(:order => 'comments_count DESC', :page => 1, :per_page => limit) end # produces the HTML code that is to be displayed as this article's contents. diff --git a/app/views/search/_event.rhtml b/app/views/search/_event.rhtml index d40e7fb..de7d97a 100644 --- a/app/views/search/_event.rhtml +++ b/app/views/search/_event.rhtml @@ -4,12 +4,6 @@ <%= render :partial => 'image', :object => event %>
- <% if event.body %> - <% body_stripped = strip_tags(event.body.to_s) %> -
- <%= excerpt(body_stripped, body_stripped.first(3), 200) %> -
- <% end %> <% if event.start_date %>
<%= _('Start date') %> diff --git a/app/views/search/_results_header.rhtml b/app/views/search/_results_header.rhtml index 9c0663b..cb2402b 100644 --- a/app/views/search/_results_header.rhtml +++ b/app/views/search/_results_header.rhtml @@ -1,15 +1,15 @@
"> <% if !@empty_query %>
- <%= label_total_found(asset, results.total_entries) %> + <%= label_total_found(@asset, @results[@asset].total_entries) %> <% if params[:display] != 'map' %> - <%= _("Showing page %s of %s") % [results.current_page, results.total_pages] %> + <%= _("Showing page %s of %s") % [@results[@asset].current_page, @results[@asset].total_pages] %> <% end %>
- <%= facets_unselect_menu(asset) %> - <%= order_by(asset) if params[:display] != 'map' %> + <%= facets_unselect_menu(@asset) %> + <%= order_by(@asset) if params[:display] != 'map' %>
<% else %>
<%= @filter_title if @filter_title %>
diff --git a/app/views/search/_search_form.rhtml b/app/views/search/_search_form.rhtml index f6cfe85..e956dc1 100644 --- a/app/views/search/_search_form.rhtml +++ b/app/views/search/_search_form.rhtml @@ -1,5 +1,4 @@
-<% simple_search = false unless defined? simple_search %> <% form_tag( { :controller => 'search', :action => @asset ? @asset : 'index', :asset => nil, :category_path => ( @category ? @category.explode_path : [] ) }, :method => 'get', :class => 'search_form' ) do %> diff --git a/app/views/search/articles.rhtml b/app/views/search/articles.rhtml index 83f4138..a131214 100644 --- a/app/views/search/articles.rhtml +++ b/app/views/search/articles.rhtml @@ -7,9 +7,8 @@
- <%= render :partial => 'search_form', :locals => { :form_title => _('Search'), :simple_search => true, - :hint => _('Type the title, author or content desired') } %> - <%= render :partial => 'results_header', :locals => { :asset => :articles, :results => @results[:articles] } %> + <%= render :partial => 'search_form', :locals => { :hint => _('Type the title, author or content desired') } %> + <%= render :partial => 'results_header' %> <% if !@empty_query or @filter %> <%= display_results %> diff --git a/app/views/search/category_index.rhtml b/app/views/search/category_index.rhtml deleted file mode 100644 index 97e8a20..0000000 --- a/app/views/search/category_index.rhtml +++ /dev/null @@ -1,23 +0,0 @@ -
- -
<%= image_tag(@category.image.public_filename(:thumb), :id => 'category-image') if @category.image %>
-

<%= @category.name %>

- - <%= display_results %> - -
-

<%= _('Sub-categories') %>

- <% if @category.children.empty? %> - <%= _('No sub-categories') %> - <% else %> -
    - <% @category.children.each do |c| %> -
  • <%= link_to_category c, false %>
  • - <% end %> -
- <% end %> -
- -
-
- diff --git a/app/views/search/category_index.rhtml b/app/views/search/category_index.rhtml new file mode 120000 index 0000000..3dd5e55 --- /dev/null +++ b/app/views/search/category_index.rhtml @@ -0,0 +1 @@ +index.rhtml \ No newline at end of file diff --git a/app/views/search/communities.rhtml b/app/views/search/communities.rhtml index da735ad..f27d23a 100644 --- a/app/views/search/communities.rhtml +++ b/app/views/search/communities.rhtml @@ -14,9 +14,8 @@
- <%= render :partial => 'search_form', :locals => { :form_title => _('Search'), :simple_search => true, - :hint => _("Type words about the community you're looking for") } %> - <%= render :partial => 'results_header', :locals => { :asset => :communities, :results => @results[:communities] } %> + <%= render :partial => 'search_form', :locals => { :hint => _("Type words about the community you're looking for") } %> + <%= render :partial => 'results_header' %> <%= display_results %> <%= pagination_links @results.values.first %> diff --git a/app/views/search/enterprises.rhtml b/app/views/search/enterprises.rhtml index 6383514..5c0d57b 100644 --- a/app/views/search/enterprises.rhtml +++ b/app/views/search/enterprises.rhtml @@ -16,9 +16,8 @@
- <%= render :partial => 'search_form', :locals => { :form_title => _('Search'), :simple_search => true, - :hint => _("Type words about the enterprise you're looking for") } %> - <%= render :partial => 'results_header', :locals => { :asset => :enterprises, :results => @results[:enterprises] } %> + <%= render :partial => 'search_form', :locals => { :hint => _("Type words about the enterprise you're looking for") } %> + <%= render :partial => 'results_header' %> <%= display_results(true) %> <% if params[:display] != 'map' %> diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml index 0e3902d..c8544e5 100644 --- a/app/views/search/index.rhtml +++ b/app/views/search/index.rhtml @@ -1,10 +1,28 @@ -
+
"> -<%= search_page_title(_('Search Results'), @category) %> + <% if @category %> +
<%= image_tag(@category.image.public_filename(:thumb), :id => 'category-image') if @category.image %>
+ <% end %> -<%= render :partial => 'search_form', :locals => { :form_title => _("Refine your search"), :simple_search => true } %> + <%= search_page_title(_('Search Results'), @category) %> + <%= render :partial => 'search_form', :locals => { :hint => '' } %> + <%= category_context(@category, params) %> + <%= display_results %> + +
+ <% if @category %> +

<%= _('Sub-categories') %>

+ <% if @category.children.empty? %> + <%= _('No sub-categories') %> + <% else %> +
    + <% @category.children.each do |c| %> +
  • <%= link_to_category c, false %>
  • + <% end %> +
+ <% end %> + <% end %> +
-<%= render :partial => 'display_results' %> - -
+

diff --git a/app/views/search/people.rhtml b/app/views/search/people.rhtml index 250a221..50ed044 100644 --- a/app/views/search/people.rhtml +++ b/app/views/search/people.rhtml @@ -7,9 +7,8 @@
- <%= render :partial => 'search_form', :locals => { :form_title => _('Search'), :simple_search => true, - :hint => _("Type words about the person you're looking for") } %> - <%= render :partial => 'results_header', :locals => { :asset => :people, :results => @results[:people] } %> + <%= render :partial => 'search_form', :locals => { :hint => _("Type words about the person you're looking for") } %> + <%= render :partial => 'results_header' %> <%= display_results %> <% if params[:display] != 'map' %> diff --git a/app/views/search/products.rhtml b/app/views/search/products.rhtml index 4c3b42a..513b393 100644 --- a/app/views/search/products.rhtml +++ b/app/views/search/products.rhtml @@ -10,9 +10,8 @@
- <%= render :partial => 'search_form', :locals => { :form_title => _("Search"), :simple_search => true, - :hint => _('Type the product, service, city or qualifier desired') } %> - <%= render :partial => 'results_header', :locals => { :asset => :products, :results => @results[:products] } %> + <%= render :partial => 'search_form', :locals => { :hint => _('Type the product, service, city or qualifier desired') } %> + <%= render :partial => 'results_header' %> <% if !@empty_query %> <%= display_results(true) %> diff --git a/public/stylesheets/search.css b/public/stylesheets/search.css index 44da748..3dedbb1 100644 --- a/public/stylesheets/search.css +++ b/public/stylesheets/search.css @@ -1,3 +1,6 @@ +.search-category-context { + margin: 30px 0px; +} .controller-search #search-page { position: relative; /* to the text appear on MSIE 6 */ width: 100%; @@ -726,4 +729,4 @@ li.search-product-item hr { } .facet-menu a.facet-options-toggle.facet-less-options { background-position: 0 -32px; -} \ No newline at end of file +} diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index dfbce62..0b9a941 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -51,15 +51,10 @@ class SearchControllerTest < Test::Unit::TestCase end should 'search only in specified types of content' do - get :index, :query => 'something not important', :find_in => [ 'articles' ] + get :articles, :query => 'something not important' assert_equal [:articles], assigns(:results).keys end - should 'search in more than one specified types of content' do - get :index, :query => 'something not important', :find_in => [ 'articles', 'people' ] - assert_equivalent [:articles, :people ], assigns(:results).keys - end - should 'render success in search' do get :index, :query => 'something not important' assert_response :success @@ -94,7 +89,7 @@ class SearchControllerTest < Test::Unit::TestCase art1 = create_article_with_optional_category('one article', person, @category) art2 = create_article_with_optional_category('two article', person, @category) - get :assets, :asset => 'articles' + get :articles assert_includes assigns(:results)[:articles], art1 assert_includes assigns(:results)[:articles], art2 @@ -110,7 +105,7 @@ class SearchControllerTest < Test::Unit::TestCase # not in category art3 = create_article_with_optional_category('another article', person) - get :assets, :asset => 'articles', :category_path => ['my-category'] + get :articles, :category_path => ['my-category'] assert_includes assigns(:results)[:articles], art1 assert_includes assigns(:results)[:articles], art2 @@ -140,7 +135,7 @@ class SearchControllerTest < Test::Unit::TestCase ent1 = create_profile_with_optional_category(Enterprise, 'teste 1') ent2 = create_profile_with_optional_category(Enterprise, 'teste 2') - get :assets, :asset => 'enterprises' + get :enterprises assert_includes assigns(:results)[:enterprises], ent1 assert_includes assigns(:results)[:enterprises], ent2 end @@ -152,7 +147,7 @@ class SearchControllerTest < Test::Unit::TestCase # not in category ent2 = create_profile_with_optional_category(Enterprise, 'teste 2') - get :assets, :asset => 'enterprises', :category_path => [ 'my-category' ] + get :enterprises, :category_path => [ 'my-category' ] assert_includes assigns(:results)[:enterprises], ent1 assert_not_includes assigns(:results)[:enterprises], ent2 end @@ -178,7 +173,7 @@ class SearchControllerTest < Test::Unit::TestCase p1 = create_user('test1').person p2 = create_user('test2').person - get :assets, :asset => 'people' + get :people assert_equivalent [p2,p1], assigns(:results)[:people] end @@ -193,7 +188,7 @@ class SearchControllerTest < Test::Unit::TestCase # not in category p2 = create_user('test2').person - get :assets, :asset => 'people', :category_path => [ 'my-category' ] + get :people, :category_path => [ 'my-category' ] assert_equal [p1], assigns(:results)[:people] end @@ -216,7 +211,7 @@ class SearchControllerTest < Test::Unit::TestCase c1 = create_profile_with_optional_category(Community, 'a beautiful community') c2 = create_profile_with_optional_category(Community, 'another beautiful community') - get :assets, :asset => 'communities' + get :communities assert_equivalent [c2, c1], assigns(:results)[:communities] end @@ -232,7 +227,7 @@ class SearchControllerTest < Test::Unit::TestCase # in category c3 = create_profile_with_optional_category(Community, 'yet another beautiful community', @category) - get :assets, :asset => 'communities', :category_path => [ 'my-category' ] + get :communities, :category_path => [ 'my-category' ] assert_equivalent [c3, c1], assigns(:results)[:communities] end @@ -263,7 +258,7 @@ class SearchControllerTest < Test::Unit::TestCase 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' + get :products assert_equivalent [prod2, prod1], assigns(:results)[:products] end @@ -279,7 +274,7 @@ class SearchControllerTest < Test::Unit::TestCase ent2 = create_profile_with_optional_category(Enterprise, 'teste2') prod2 = ent2.products.create!(:name => 'another beautiful product', :product_category => @product_category) - get :assets, :asset => 'products', :category_path => [ 'my-category' ] + get :products, :category_path => [ 'my-category' ] assert_equal [prod1], assigns(:results)[:products] end @@ -328,7 +323,7 @@ class SearchControllerTest < Test::Unit::TestCase environment.enable_plugin(Plugin1.name) environment.enable_plugin(Plugin2.name) - get :assets, :asset => 'products' + get :products assert_tag :tag => 'li', :content => /Property1/, :child => {:tag => 'a', :attributes => {:href => '/plugin1'}, :content => product.name} assert_tag :tag => 'li', :content => /Property2/, :child => {:tag => 'a', :attributes => {:href => '/plugin2'}, :content => product.name} @@ -339,7 +334,7 @@ class SearchControllerTest < Test::Unit::TestCase ent1 = create_profile_with_optional_category(Enterprise, 'teste 1') ent2 = create_profile_with_optional_category(Enterprise, 'teste 2') - get :assets, :asset => 'enterprises', :page => '2' + get :enterprises, :page => '2' assert_equal 1, assigns(:results)[:enterprises].size end @@ -372,16 +367,6 @@ class SearchControllerTest < Test::Unit::TestCase assert_tag :tag => 'div', :attributes => { :class => /search-results-people/ }, :descendant => { :tag => 'h3', :content => /People/ } end - should 'search in whole site when told so' do - parent = Category.create!(:name => 'randomcat', :environment => Environment.default) - Category.create!(:name => 'randomchild', :environment => Environment.default, :parent => parent) - - get :index, :category_path => [ 'randomcat', 'randomchild' ], :query => 'some random query', :search_whole_site => 'yes' - - # search_whole_site must be removed to precent a infinite redirect loop - assert_redirected_to :action => 'index', :category_path => [], :query => 'some random query', :search_whole_site => nil - end - should 'display a given category' do get :category_index, :category_path => [ 'my-category' ] assert_equal @category, assigns(:category) @@ -392,68 +377,9 @@ class SearchControllerTest < Test::Unit::TestCase assert_same assigns(:category), @controller.category end - should 'list recent articles in the category' do - recent = [] - finger = CategoryFinder.new(@category) - finger.expects(:recent).with(any_parameters).at_least_once - finger.expects(:recent).with('text_articles', anything).returns(recent) - CategoryFinder.expects(:new).with(@category).returns(finger) - - get :category_index, :category_path => [ 'my-category' ] - assert_same recent, assigns(:results)[:articles] - end - - should 'list most commented articles in the category' do - most_commented = [] - finger = CategoryFinder.new(@category) - finger.expects(:most_commented_articles).returns(most_commented) - CategoryFinder.expects(:new).with(@category).returns(finger) - - get :category_index, :category_path => [ 'my-category' ] - assert_same most_commented, assigns(:results)[:most_commented_articles] - end - - should 'list recently registered people in the category' do - recent_people = [] - finger = CategoryFinder.new(@category) - finger.expects(:recent).with(any_parameters).at_least_once - finger.expects(:recent).with('people', kind_of(Integer)).returns(recent_people) - CategoryFinder.expects(:new).with(@category).returns(finger) - - get :category_index, :category_path => [ 'my-category' ] - assert_same recent_people, assigns(:results)[:people] - end - - should 'list recently registered communities in the category' do - recent_communities = [] - finger = CategoryFinder.new(@category) - finger.expects(:recent).with(any_parameters).at_least_once - finger.expects(:recent).with('communities', anything).returns(recent_communities) - CategoryFinder.expects(:new).with(@category).returns(finger) - - get :category_index, :category_path => [ 'my-category' ] - assert_same recent_communities, assigns(:results)[:communities] - end - - should 'list recently registered enterprises in the category' do - recent_enterptises = [] - finger = CategoryFinder.new(@category) - finger.expects(:recent).with(any_parameters).at_least_once - finger.expects(:recent).with('enterprises', anything).returns(recent_enterptises) - CategoryFinder.expects(:new).with(@category).returns(finger) - - get :category_index, :category_path => [ 'my-category' ] - assert_same recent_enterptises, assigns(:results)[:enterprises] - end - - should 'not list "Search for ..." in category_index' do - get :category_index, :category_path => [ 'my-category' ] - assert_no_tag :content => /Search for ".*" in the whole site/ - end - # SECURITY should 'not allow unrecognized assets' do - get :assets, :asset => 'unexisting_asset' + get :unexisting_asset assert_response 403 end @@ -478,11 +404,6 @@ class SearchControllerTest < Test::Unit::TestCase } end - should 'offer button search in the whole site' do - get :index, :category_path => [ 'my-category' ], :query => 'a sample search' - assert_tag :tag => 'input', :attributes => { :type => 'submit', :name => 'search_whole_site_yes' } - end - should 'display only category name in "search results for ..." title' do parent = Category.create!(:name => 'Parent Category', :environment => Environment.default) child = Category.create!(:name => "Child Category", :environment => Environment.default, :parent => parent) @@ -498,7 +419,7 @@ class SearchControllerTest < Test::Unit::TestCase p = create_profile_with_optional_category(Person, 'test_profile', child) - get :index, :category_path => ['parent-category'], :query => 'test_profile', :find_in => ['people'] + get :category_index, :category_path => ['parent-category'], :query => 'test_profile', :find_in => ['people'] assert_includes assigns(:results)[:people], p end @@ -525,22 +446,6 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns('results')[:enterprises], ent2 end - should 'find profiles by radius and region' do - city = City.create!(:name => 'r-test', :environment => Environment.default, :lat => 45.0, :lng => 45.0) - ent1 = create_profile_with_optional_category(Enterprise, 'test 1', nil, :lat => 45.0, :lng => 45.0) - p1 = create_profile_with_optional_category(Person, 'test 2', nil, :lat => 45.0, :lng => 45.0) - - ent2 = create_profile_with_optional_category(Enterprise, 'test 1', nil, :lat => 30.0, :lng => 30.0) - p2 = create_profile_with_optional_category(Person, 'test 2', nil, :lat => 30.0, :lng => 30.0) - - get :index, :city => city.id, :radius => 10, :query => 'test' - - assert_includes assigns('results')[:enterprises], ent1 - assert_not_includes assigns('results')[:enterprises], ent2 - assert_includes assigns('results')[:people], p1 - assert_not_includes assigns('results')[:people], p2 - end - should 'display category image while in directory' do parent = Category.create!(:name => 'category1', :environment => Environment.default) cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent => parent, @@ -552,60 +457,6 @@ class SearchControllerTest < Test::Unit::TestCase assert_tag :tag => 'img', :attributes => { :src => /rails_thumb\.png/ } end - should 'complete region name' do - r1 = Region.create!(:name => 'One region', :environment => Environment.default, :lat => 111.07, :lng => '88.9') - r2 = Region.create!(:name => 'Another region', :environment => Environment.default, :lat => 111.07, :lng => '88.9') - - get :complete_region, :region => { :name => 'one' } - assert_includes assigns(:regions), r1 - assert_tag :tag => 'ul', :descendant => { :tag => 'li', :content => 'One region' } - end - - should 'render completion results without layout' do - get :complete_region, :region => { :name => 'test' } - assert_no_tag :tag => 'body' - end - - should 'complete only georeferenced regions' do - r1 = Region.create!(:name => 'One region', :environment => Environment.default, :lat => 111.07, :lng => '88.9') - r2 = Region.create!(:name => 'Another region', :environment => Environment.default) - - get :complete_region, :region => { :name => 'region' } - assert_includes assigns(:regions), r1 - assert_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r1.name } - assert_not_includes assigns(:regions), r2 - assert_no_tag :tag => 'ul', :descendant => { :tag => 'li', :content => r2.name } - end - - should 'return options of cities by its state' do - state1 = State.create!(:name => 'State One', :environment => Environment.default) - state2 = State.create!(:name => 'State Two', :environment => Environment.default) - city1 = City.create!(:name => 'City One', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state1) - city2 = City.create!(:name => 'City Two', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state2) - - get :cities, :state_id => state1.id - assert_includes assigns(:cities), city1 - assert_tag :tag => 'option', :content => city1.name, :attributes => {:value => city1.id} - assert_not_includes assigns(:cities), city2 - assert_no_tag :tag => 'option', :content => city2.name, :attributes => {:value => city2.id} - end - - should 'render cities results without layout' do - get :cities, :state_id => 1 - assert_no_tag :tag => 'body' - end - - should 'list only georeferenced cities' do - state = State.create!(:name => 'State One', :environment => Environment.default) - city1 = City.create!(:name => 'City One', :environment => Environment.default, :lat => 111.07, :lng => '88.9', :parent => state) - city2 = City.create!(:name => 'City Two', :environment => Environment.default, :parent => state) - - get :cities, :state_id => state.id - - assert_includes assigns(:cities), city1 - assert_not_includes assigns(:cities), city2 - end - should 'search for events' do person = create_user('teste').person ev = create_event(person, :name => 'an event to be found') @@ -642,7 +493,7 @@ class SearchControllerTest < Test::Unit::TestCase ev2 = create_event(person2, :name => 'two event', :category_ids => [@category.id]) - get :assets, :asset => 'events' + get :events assert_includes assigns(:results)[:events], ev1 assert_includes assigns(:results)[:events], ev2 @@ -659,7 +510,7 @@ class SearchControllerTest < Test::Unit::TestCase # not in category ev3 = create_event(person, :name => 'another event') - get :assets, :asset => 'events', :category_path => ['my-category'] + get :events, :category_path => ['my-category'] assert_includes assigns(:results)[:events], ev1 assert_includes assigns(:results)[:events], ev2 @@ -672,7 +523,7 @@ class SearchControllerTest < Test::Unit::TestCase create_event(person, :name => 'upcoming event 1', :category_ids => [@category.id], :start_date => Date.new(2008, 1, 25)) create_event(person, :name => 'upcoming event 2', :category_ids => [@category.id], :start_date => Date.new(2008, 4, 27)) - get :assets, :asset => 'events', :year => '2008', :month => '1' + get :events, :year => '2008', :month => '1' assert_equal [ 'upcoming event 1' ], assigns(:results)[:events].map(&:name) end @@ -683,7 +534,7 @@ class SearchControllerTest < Test::Unit::TestCase create_event(person, :name => 'upcoming event 1', :category_ids => [@category.id], :start_date => Date.new(2008, 1, 25)) create_event(person, :name => 'upcoming event 2', :category_ids => [@category.id], :start_date => Date.new(2008, 4, 27)) - get :assets, :asset => 'events', :category_path => [ 'my-category' ], :year => '2008', :month => '1' + get :events, :category_path => [ 'my-category' ], :year => '2008', :month => '1' assert_equal [ 'upcoming event 1' ], assigns(:results)[:events].map(&:name) end @@ -866,27 +717,15 @@ class SearchControllerTest < Test::Unit::TestCase should 'display current year/month by default as caption of current month' do Date.expects(:today).returns(Date.new(2008, 8, 1)).at_least_once - get :assets, :asset => 'events' + get :events assert_tag :tag => 'table', :attributes => {:class => /current-month/}, :descendant => {:tag => 'caption', :content => /August 2008/} end should 'submit search form to /search when viewing asset' do - get :index, :asset => 'people' + get :people assert_tag :tag => "form", :attributes => { :class => 'search_form', :action => '/search' } end - - should 'treat blank input for the city id' do - get :index, :city => '' - - assert_equal nil, assigns(:region) - end - should 'treat non-numeric input for the city id' do - get :index, :city => 'bla' - - assert_equal nil, assigns(:region) - end - should 'found TextileArticle in articles' do person = create_user('teste').person art = TextileArticle.create!(:name => 'an text_article article to be found', :profile => person) @@ -910,32 +749,16 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes @controller.where_to_search.map(&:first), :products end - 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', :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', :product_category => @product_category) - - get :assets, :asset => 'products', :city => s.id, :radius => 15 - - assert_includes assigns(:results)[:products], p1 - assert_not_includes assigns(:results)[:products], p2 - end - - should 'show link to article asset in the see all foot link of the most_commented_articles block in the category page' do - art = create_user('teste').person.articles.create!(:name => 'an article to be found') - most_commented = [art] - finder = CategoryFinder.new(@category) - finder.expects(:most_commented_articles).returns(most_commented) - CategoryFinder.expects(:new).with(@category).returns(finder) + should 'show link to article asset in the see all foot link of the articles block in the category page' do + a = create_user('test1').person.articles.create!(:name => 'an article to be found') + a.add_category @category get :category_index, :category_path => [ 'my-category' ] - assert_tag :tag => 'div', :attributes => {:class => /search-results-most_commented_articles/} , :descendant => {:tag => 'a', :attributes => { :href => '/search/index/my-category?asset=articles'}} + assert_tag :tag => 'div', :attributes => {:class => /search-results-articles/} , :descendant => {:tag => 'a', :attributes => { :href => '/search/articles/my-category'}} end should 'display correct title on list communities' do - get :assets, :asset => 'communities' + get :communities assert_tag :tag => 'h1', :content => 'Communities' end @@ -952,14 +775,14 @@ class SearchControllerTest < Test::Unit::TestCase should 'add link to list in all categories when in a category' do ['people', 'enterprises', 'products', 'communities', 'articles'].each do |asset| - get :index, :asset => asset, :category_path => [ 'my-category' ] + get asset, :category_path => [ 'my-category' ] assert_tag :tag => 'div', :content => 'In all categories' end end should 'not add link to list in all categories when not in a category' do ['people', 'enterprises', 'products', 'communities', 'articles'].each do |asset| - get :index, :asset => asset + get asset assert_no_tag :tag => 'div', :content => 'In all categories' end end @@ -1019,7 +842,7 @@ class SearchControllerTest < Test::Unit::TestCase event_in_searched_category = create_event(person, :name => 'Maria Birthday', :start_date => Date.today, :category_ids => [searched_category.id]) event_in_non_searched_category = create_event(person, :name => 'Joao Birthday', :start_date => Date.today, :category_ids => [@category.id]) - get :assets, :asset => 'events', :category_path => ['category-with-events'] + get :events, :category_path => ['category-with-events'] assert_includes assigns(:events_of_the_day), event_in_searched_category assert_not_includes assigns(:events_of_the_day), event_in_non_searched_category @@ -1112,7 +935,7 @@ class SearchControllerTest < Test::Unit::TestCase get :people, :filter => 'more_active' - assert_equal Person.count/per_page, assigns(:results).total_pages + assert_equal Person.count/per_page, assigns(:results).total_entries end should 'list all people filter by more active' do @@ -1141,36 +964,52 @@ class SearchControllerTest < Test::Unit::TestCase assert_equal [p2,p1,p3] , assigns(:results) end - should 'the people filter be only the hardcoded one' do + should 'set filter be only the hardcoded one' do get :people, :filter => 'more_recent' assert_equal 'more_recent' , assigns(:filter) - get :people, :filter => 'more_active' assert_equal 'more_active' , assigns(:filter) - get :people, :filter => 'more_popular' assert_equal 'more_popular' , assigns(:filter) - get :people, :filter => 'more_anything' assert_equal 'more_recent' , assigns(:filter) + + get :communities, :filter => 'more_recent' + assert_equal 'more_recent' , assigns(:filter) + get :communities, :filter => 'more_active' + assert_equal 'more_active' , assigns(:filter) + get :communities, :filter => 'more_popular' + assert_equal 'more_popular' , assigns(:filter) + get :communities, :filter => 'more_anything' + assert_equal 'more_recent' , assigns(:filter) end should 'the people filter define the title' do get :people, :filter => 'more_recent' - assert_equal 'More recent people' , assigns(:title) - assert_tag :h1, :content => 'More recent people' - + assert_equal 'More recent people from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More recent people from network' get :people, :filter => 'more_active' - assert_equal 'More active people' , assigns(:title) - assert_tag :h1, :content => 'More active people' - + assert_equal 'More active people from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More active people from network' get :people, :filter => 'more_popular' - assert_equal 'More popular people' , assigns(:title) - assert_tag :h1, :content => 'More popular people' - + assert_equal 'More popular people from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More popular people from network' get :people, :filter => 'more_anything' - assert_equal 'More recent people' , assigns(:title) - assert_tag :h1, :content => 'More recent people' + assert_equal 'More recent people from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More recent people from network' + get :communities, :filter => 'more_recent' + + assert_equal 'More recent communities from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More recent communities from network' + get :communities, :filter => 'more_active' + assert_equal 'More active communities from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More active communities from network' + get :communities, :filter => 'more_popular' + assert_equal 'More popular communities from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More popular communities from network' + get :communities, :filter => 'more_anything' + assert_equal 'More recent communities from network' , assigns(:filter_title) + assert_tag :h1, :content => 'More recent communities from network' end should 'search for community' do @@ -1259,38 +1098,6 @@ class SearchControllerTest < Test::Unit::TestCase assert_equal [c2,c1] , assigns(:results) end - should 'the communities filter be only the hardcoded one' do - get :communities, :filter => 'more_recent' - assert_equal 'more_recent' , assigns(:filter) - - get :communities, :filter => 'more_active' - assert_equal 'more_active' , assigns(:filter) - - get :communities, :filter => 'more_popular' - assert_equal 'more_popular' , assigns(:filter) - - get :communities, :filter => 'more_anything' - assert_equal 'more_recent' , assigns(:filter) - end - - should 'the communities filter define the title' do - get :communities, :filter => 'more_recent' - assert_equal 'More recent communities' , assigns(:title) - assert_tag :h1, :content => 'More recent communities' - - get :communities, :filter => 'more_active' - assert_equal 'More active communities' , assigns(:title) - assert_tag :h1, :content => 'More active communities' - - get :communities, :filter => 'more_popular' - assert_equal 'More popular communities' , assigns(:title) - assert_tag :h1, :content => 'More popular communities' - - get :communities, :filter => 'more_anything' - assert_equal 'More recent communities' , assigns(:title) - assert_tag :h1, :content => 'More recent communities' - end - should "only include visible people in more_recent filter" do # assuming that all filters behave the same! p1 = fast_create(Person, :visible => false) diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index cacf305..2ef7b1f 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -219,6 +219,62 @@ class CategoryTest < Test::Unit::TestCase ################################################################ # category filter stuff ################################################################ + + should 'should paginate recent-like methods' do + c = @env.categories.build(:name => 'my category'); c.save! + assert c.recent_people.respond_to? 'total_entries' + assert c.recent_enterprises.respond_to? 'total_entries' + assert c.recent_communities.respond_to? 'total_entries' + assert c.recent_products.respond_to? 'total_entries' + assert c.recent_articles.respond_to? 'total_entries' + assert c.recent_comments.respond_to? 'total_entries' + assert c.most_commented_articles.respond_to? 'total_entries' + end + + should 'list recent people' do + c = @env.categories.build(:name => 'my category'); c.save! + p1 = create_user('testuser').person + p1.add_category c + p1.save! + + p2 = create_user('testuser2').person + p2.add_category c + p2.save! + + assert_equal [p2, p1], c.recent_people + end + + should 'list recent enterprises' do + 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 + + assert_equal [ent2, ent1], c.recent_enterprises + end + + should 'list recent communities' do + c = @env.categories.build(:name => 'my category'); c.save! + c1 = fast_create(Community, :name => 'testcommunity_1') + c1.add_category c + c2 = fast_create(Community, :name => 'testcommunity_2') + c2.add_category c + + assert_equal [c2, c1], c.recent_communities + end + + should 'list recent products' 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', :product_category => product_category) + prod2 = ent2.products.create!(:name => 'test_prod2', :product_category => product_category) + assert_equal [prod2, prod1], c.recent_products + end should 'list recent articles' do c = @env.categories.build(:name => 'my category'); c.save! @@ -232,7 +288,7 @@ class CategoryTest < Test::Unit::TestCase a2.add_category c a2.save! - assert_equivalent [a1, a2], c.recent_articles + assert_equal [a2, a1], c.recent_articles end should 'list recent comments' do @@ -249,7 +305,7 @@ class CategoryTest < Test::Unit::TestCase a2.save! c2 = a2.comments.build(:title => 'comm1', :body => 'khdkashd ', :author => person); c2.save! - assert_equivalent [c1, c2], c.recent_comments + assert_equal [c2, c1], c.recent_comments end should 'list most commented articles' do @@ -267,6 +323,7 @@ class CategoryTest < Test::Unit::TestCase assert_equal [a3, a2], c.most_commented_articles(2) end + should 'have comments' do c = @env.categories.build(:name => 'my category'); c.save! person = create_user('testuser').person -- libgit2 0.21.2