Commit ee116023e6fee6b2b3ac0863c48391abdc7fcb3a

Authored by Rafael Martins
1 parent 78b068d9

Added some functional tests and small fixes for SearchController

app/controllers/public/search_controller.rb
@@ -114,7 +114,7 @@ class SearchController < PublicController @@ -114,7 +114,7 @@ class SearchController < PublicController
114 @names = {} 114 @names = {}
115 @results_only = true 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 load_query 118 load_query
119 @asset = key 119 @asset = key
120 send(key) 120 send(key)
@@ -149,7 +149,7 @@ class SearchController < PublicController @@ -149,7 +149,7 @@ class SearchController < PublicController
149 ].each do |asset, name, filter| 149 ].each do |asset, name, filter|
150 @order << asset 150 @order << asset
151 @results[asset] = @category.send(filter, limit) 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 @names[asset] = name 153 @names[asset] = name
154 end 154 end
155 end 155 end
@@ -229,7 +229,7 @@ class SearchController &lt; PublicController @@ -229,7 +229,7 @@ class SearchController &lt; PublicController
229 end 229 end
230 230
231 def load_search_assets 231 def load_search_assets
232 - @enabled_searchs = [ 232 + @enabled_searches = [
233 [ :articles, _('Contents') ], 233 [ :articles, _('Contents') ],
234 [ :enterprises, _('Enterprises') ], 234 [ :enterprises, _('Enterprises') ],
235 [ :people, _('People') ], 235 [ :people, _('People') ],
@@ -240,10 +240,11 @@ class SearchController &lt; PublicController @@ -240,10 +240,11 @@ class SearchController &lt; PublicController
240 240
241 @searching = {} 241 @searching = {}
242 @titles = {} 242 @titles = {}
243 - @enabled_searchs.each do |key, name| 243 + @enabled_searches.each do |key, name|
244 @titles[key] = name 244 @titles[key] = name
245 @searching[key] = params[:action] == 'index' || params[:action] == key.to_s 245 @searching[key] = params[:action] == 'index' || params[:action] == key.to_s
246 end 246 end
  247 + @names = @titles if @names.nil?
247 end 248 end
248 249
249 def limit 250 def limit
test/functional/search_controller_test.rb
@@ -74,6 +74,15 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -74,6 +74,15 @@ class SearchControllerTest &lt; ActionController::TestCase
74 assert_includes assigns(:results)[:articles], art 74 assert_includes assigns(:results)[:articles], art
75 end 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 # 'assets' outside any category 86 # 'assets' outside any category
78 should 'list articles in general' do 87 should 'list articles in general' do
79 person = fast_create(Person) 88 person = fast_create(Person)
@@ -89,7 +98,7 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -89,7 +98,7 @@ class SearchControllerTest &lt; ActionController::TestCase
89 98
90 should 'find enterprises' do 99 should 'find enterprises' do
91 ent = create_profile_with_optional_category(Enterprise, 'teste') 100 ent = create_profile_with_optional_category(Enterprise, 'teste')
92 - get 'enterprises', :query => 'teste' 101 + get :enterprises, :query => 'teste'
93 assert_includes assigns(:results)[:enterprises], ent 102 assert_includes assigns(:results)[:enterprises], ent
94 end 103 end
95 104
@@ -143,8 +152,9 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -143,8 +152,9 @@ class SearchControllerTest &lt; ActionController::TestCase
143 end 152 end
144 153
145 # 'assets' menu outside any category 154 # 'assets' menu outside any category
146 - should 'list products in general' do 155 + should 'list products in general without geosearch' do
147 Profile.delete_all 156 Profile.delete_all
  157 + SearchController.stubs(:logged_in?).returns(false)
148 158
149 ent1 = create_profile_with_optional_category(Enterprise, 'teste1') 159 ent1 = create_profile_with_optional_category(Enterprise, 'teste1')
150 ent2 = create_profile_with_optional_category(Enterprise, 'teste2') 160 ent2 = create_profile_with_optional_category(Enterprise, 'teste2')
@@ -161,13 +171,13 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -161,13 +171,13 @@ class SearchControllerTest &lt; ActionController::TestCase
161 lambda {"<span id='plugin1'>This is Plugin1 speaking!</span>"} 171 lambda {"<span id='plugin1'>This is Plugin1 speaking!</span>"}
162 end 172 end
163 end 173 end
164 - 174 +
165 class Plugin2 < Noosfero::Plugin 175 class Plugin2 < Noosfero::Plugin
166 def asset_product_extras(product, enterprise) 176 def asset_product_extras(product, enterprise)
167 lambda {"<span id='plugin2'>This is Plugin2 speaking!</span>"} 177 lambda {"<span id='plugin2'>This is Plugin2 speaking!</span>"}
168 end 178 end
169 end 179 end
170 - 180 +
171 enterprise = fast_create(Enterprise) 181 enterprise = fast_create(Enterprise)
172 prod_cat = fast_create(ProductCategory) 182 prod_cat = fast_create(ProductCategory)
173 product = fast_create(Product, {:enterprise_id => enterprise.id, :name => "produto1", :product_category_id => prod_cat.id}, :search => true) 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 &lt; ActionController::TestCase @@ -272,6 +282,38 @@ class SearchControllerTest &lt; ActionController::TestCase
272 assert_not_includes assigns('results')[:enterprises], ent2 282 assert_not_includes assigns('results')[:enterprises], ent2
273 end 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 should 'display category image while in directory' do 317 should 'display category image while in directory' do
276 parent = Category.create!(:name => 'category1', :environment => Environment.default) 318 parent = Category.create!(:name => 'category1', :environment => Environment.default)
277 cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent => parent, 319 cat = Category.create!(:name => 'category2', :environment => Environment.default, :parent => parent,
@@ -371,7 +413,7 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -371,7 +413,7 @@ class SearchControllerTest &lt; ActionController::TestCase
371 assert_tag :tag => 'h1', :content => 'Communities' 413 assert_tag :tag => 'h1', :content => 'Communities'
372 end 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 Enterprise.destroy_all 417 Enterprise.destroy_all
376 ('1'..'20').each do |n| 418 ('1'..'20').each do |n|
377 create_profile_with_optional_category(Enterprise, 'test ' + n) 419 create_profile_with_optional_category(Enterprise, 'test ' + n)
@@ -504,6 +546,40 @@ class SearchControllerTest &lt; ActionController::TestCase @@ -504,6 +546,40 @@ class SearchControllerTest &lt; ActionController::TestCase
504 assert_not_includes assigns(:results), p1 546 assert_not_includes assigns(:results), p1
505 end 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