Commit ee116023e6fee6b2b3ac0863c48391abdc7fcb3a
1 parent
78b068d9
Exists in
master
and in
28 other branches
Added some functional tests and small fixes for SearchController
Showing
2 changed files
with
86 additions
and
9 deletions
Show diff stats
app/controllers/public/search_controller.rb
| ... | ... | @@ -114,7 +114,7 @@ class SearchController < PublicController |
| 114 | 114 | @names = {} |
| 115 | 115 | @results_only = true |
| 116 | 116 | |
| 117 | - @enabled_searchs.select { |key,description| @searching[key] }.each do |key, description| | |
| 117 | + @enabled_searches.select { |key,description| @searching[key] }.each do |key, description| | |
| 118 | 118 | load_query |
| 119 | 119 | @asset = key |
| 120 | 120 | send(key) |
| ... | ... | @@ -149,7 +149,7 @@ class SearchController < PublicController |
| 149 | 149 | ].each do |asset, name, filter| |
| 150 | 150 | @order << asset |
| 151 | 151 | @results[asset] = @category.send(filter, limit) |
| 152 | - raise "nao total #{asset}" unless @results[asset].respond_to?(:total_entries) | |
| 152 | + raise "No total_entries for: #{asset}" unless @results[asset].respond_to?(:total_entries) | |
| 153 | 153 | @names[asset] = name |
| 154 | 154 | end |
| 155 | 155 | end |
| ... | ... | @@ -229,7 +229,7 @@ class SearchController < PublicController |
| 229 | 229 | end |
| 230 | 230 | |
| 231 | 231 | def load_search_assets |
| 232 | - @enabled_searchs = [ | |
| 232 | + @enabled_searches = [ | |
| 233 | 233 | [ :articles, _('Contents') ], |
| 234 | 234 | [ :enterprises, _('Enterprises') ], |
| 235 | 235 | [ :people, _('People') ], |
| ... | ... | @@ -240,10 +240,11 @@ class SearchController < PublicController |
| 240 | 240 | |
| 241 | 241 | @searching = {} |
| 242 | 242 | @titles = {} |
| 243 | - @enabled_searchs.each do |key, name| | |
| 243 | + @enabled_searches.each do |key, name| | |
| 244 | 244 | @titles[key] = name |
| 245 | 245 | @searching[key] = params[:action] == 'index' || params[:action] == key.to_s |
| 246 | 246 | end |
| 247 | + @names = @titles if @names.nil? | |
| 247 | 248 | end |
| 248 | 249 | |
| 249 | 250 | def limit | ... | ... |
test/functional/search_controller_test.rb
| ... | ... | @@ -74,6 +74,15 @@ class SearchControllerTest < ActionController::TestCase |
| 74 | 74 | assert_includes assigns(:results)[:articles], art |
| 75 | 75 | end |
| 76 | 76 | |
| 77 | + should 'redirect contents to articles' do | |
| 78 | + person = fast_create(Person) | |
| 79 | + art = create_article_with_optional_category('an article to be found', person) | |
| 80 | + | |
| 81 | + get 'contents', :query => 'article found' | |
| 82 | + # full description to avoid deprecation warning | |
| 83 | + assert_redirected_to :controller => :search, :action => :articles, :query => 'article found' | |
| 84 | + end | |
| 85 | + | |
| 77 | 86 | # 'assets' outside any category |
| 78 | 87 | should 'list articles in general' do |
| 79 | 88 | person = fast_create(Person) |
| ... | ... | @@ -89,7 +98,7 @@ class SearchControllerTest < ActionController::TestCase |
| 89 | 98 | |
| 90 | 99 | should 'find enterprises' do |
| 91 | 100 | ent = create_profile_with_optional_category(Enterprise, 'teste') |
| 92 | - get 'enterprises', :query => 'teste' | |
| 101 | + get :enterprises, :query => 'teste' | |
| 93 | 102 | assert_includes assigns(:results)[:enterprises], ent |
| 94 | 103 | end |
| 95 | 104 | |
| ... | ... | @@ -143,8 +152,9 @@ class SearchControllerTest < ActionController::TestCase |
| 143 | 152 | end |
| 144 | 153 | |
| 145 | 154 | # 'assets' menu outside any category |
| 146 | - should 'list products in general' do | |
| 155 | + should 'list products in general without geosearch' do | |
| 147 | 156 | Profile.delete_all |
| 157 | + SearchController.stubs(:logged_in?).returns(false) | |
| 148 | 158 | |
| 149 | 159 | ent1 = create_profile_with_optional_category(Enterprise, 'teste1') |
| 150 | 160 | ent2 = create_profile_with_optional_category(Enterprise, 'teste2') |
| ... | ... | @@ -161,13 +171,13 @@ class SearchControllerTest < ActionController::TestCase |
| 161 | 171 | lambda {"<span id='plugin1'>This is Plugin1 speaking!</span>"} |
| 162 | 172 | end |
| 163 | 173 | end |
| 164 | - | |
| 174 | + | |
| 165 | 175 | class Plugin2 < Noosfero::Plugin |
| 166 | 176 | def asset_product_extras(product, enterprise) |
| 167 | 177 | lambda {"<span id='plugin2'>This is Plugin2 speaking!</span>"} |
| 168 | 178 | end |
| 169 | 179 | end |
| 170 | - | |
| 180 | + | |
| 171 | 181 | enterprise = fast_create(Enterprise) |
| 172 | 182 | prod_cat = fast_create(ProductCategory) |
| 173 | 183 | product = fast_create(Product, {:enterprise_id => enterprise.id, :name => "produto1", :product_category_id => prod_cat.id}, :search => true) |
| ... | ... | @@ -272,6 +282,38 @@ class SearchControllerTest < ActionController::TestCase |
| 272 | 282 | assert_not_includes assigns('results')[:enterprises], ent2 |
| 273 | 283 | end |
| 274 | 284 | |
| 285 | + should 'show only results in general search' do | |
| 286 | + ent1 = create_profile_with_optional_category(Enterprise, 'test1') | |
| 287 | + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default) | |
| 288 | + prod = ent1.products.create!(:name => 'teste', :product_category => prod_cat) | |
| 289 | + | |
| 290 | + ent2 = create_profile_with_optional_category(Enterprise, 'test2') | |
| 291 | + | |
| 292 | + get :index, :query => prod_cat.name | |
| 293 | + | |
| 294 | + assert assigns(:facets).empty? | |
| 295 | + assert_nil assigns(:results)[:enterprises].facets | |
| 296 | + assert_nil assigns(:results)[:products].facets | |
| 297 | + end | |
| 298 | + | |
| 299 | + should 'search all enabled assets in general search' do | |
| 300 | + ent1 = create_profile_with_optional_category(Enterprise, 'test enterprise') | |
| 301 | + prod_cat = ProductCategory.create!(:name => 'pctest', :environment => Environment.default) | |
| 302 | + prod = ent1.products.create!(:name => 'test product', :product_category => prod_cat) | |
| 303 | + art = Article.create!(:name => 'test article', :profile_id => fast_create(Person).id) | |
| 304 | + per = Person.create!(:name => 'test person', :identifier => 'test-person', :user_id => fast_create(User).id) | |
| 305 | + com = Community.create!(:name => 'test community') | |
| 306 | + eve = Event.create!(:name => 'test event', :profile_id => fast_create(Person).id) | |
| 307 | + | |
| 308 | + get :index, :query => 'test' | |
| 309 | + | |
| 310 | + [:articles, :enterprises, :people, :communities, :products, :events].select do |key, name| | |
| 311 | + !@controller.environment.enabled?('disable_asset_' + key.to_s) | |
| 312 | + end.each do |asset| | |
| 313 | + assert !assigns(:results)[asset].docs.empty? | |
| 314 | + end | |
| 315 | + end | |
| 316 | + | |
| 275 | 317 | should 'display category image while in directory' do |
| 276 | 318 | parent = Category.create!(:name => 'category1', :environment => Environment.default) |
| 277 | 319 | cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent => parent, |
| ... | ... | @@ -371,7 +413,7 @@ class SearchControllerTest < ActionController::TestCase |
| 371 | 413 | assert_tag :tag => 'h1', :content => 'Communities' |
| 372 | 414 | end |
| 373 | 415 | |
| 374 | - should 'indicate more than page for total_entries' do | |
| 416 | + should 'indicate more than the page limit for total_entries' do | |
| 375 | 417 | Enterprise.destroy_all |
| 376 | 418 | ('1'..'20').each do |n| |
| 377 | 419 | create_profile_with_optional_category(Enterprise, 'test ' + n) |
| ... | ... | @@ -504,6 +546,40 @@ class SearchControllerTest < ActionController::TestCase |
| 504 | 546 | assert_not_includes assigns(:results), p1 |
| 505 | 547 | end |
| 506 | 548 | |
| 549 | + should 'browse facets when query is not empty' do | |
| 550 | + get :articles, :query => 'something' | |
| 551 | + get :facets_browse, :asset => 'articles', :facet_id => 'f_type' | |
| 552 | + assert_equal assigns(:facet)[:id], 'f_type' | |
| 553 | + get :products, :query => 'something' | |
| 554 | + get :facets_browse, :asset => 'products', :facet_id => 'f_category' | |
| 555 | + assert_equal assigns(:facet)[:id], 'f_category' | |
| 556 | + get :people, :query => 'something' | |
| 557 | + get :facets_browse, :asset => 'people', :facet_id => 'f_region' | |
| 558 | + assert_equal assigns(:facet)[:id], 'f_region' | |
| 559 | + end | |
| 560 | + | |
| 561 | + should 'raise exception when facet is invalid' do | |
| 562 | + get :articles, :query => 'something' | |
| 563 | + assert_raise RuntimeError do | |
| 564 | + get :facets_browse, :asset => 'articles', :facet_id => 'f_whatever' | |
| 565 | + end | |
| 566 | + end | |
| 567 | + | |
| 568 | + should 'keep old urls working' do | |
| 569 | + get :assets, :asset => 'articles' | |
| 570 | + assert_redirected_to :controller => :search, :action => :articles | |
| 571 | + get :assets, :asset => 'people' | |
| 572 | + assert_redirected_to :controller => :search, :action => :people | |
| 573 | + get :assets, :asset => 'communities' | |
| 574 | + assert_redirected_to :controller => :search, :action => :communities | |
| 575 | + get :assets, :asset => 'products' | |
| 576 | + assert_redirected_to :controller => :search, :action => :products | |
| 577 | + get :assets, :asset => 'enterprises' | |
| 578 | + assert_redirected_to :controller => :search, :action => :enterprises | |
| 579 | + get :assets, :asset => 'events' | |
| 580 | + assert_redirected_to :controller => :search, :action => :events | |
| 581 | + end | |
| 582 | + | |
| 507 | 583 | ################################################################## |
| 508 | 584 | ################################################################## |
| 509 | 585 | ... | ... |