Commit c57ced6ac61e7ae8e26a372010d701926ac26e82
1 parent
debc04c9
Exists in
master
and in
28 other branches
SearchHelper fixes and unit tests
Showing
2 changed files
with
265 additions
and
8 deletions
Show diff stats
app/helpers/search_helper.rb
@@ -17,7 +17,7 @@ module SearchHelper | @@ -17,7 +17,7 @@ module SearchHelper | ||
17 | end | 17 | end |
18 | 18 | ||
19 | def display_results(use_map = false) | 19 | def display_results(use_map = false) |
20 | - if params[:display] == 'map' && use_map && GoogleMaps.enabled?(environment.default_hostname) | 20 | + if params[:display] == 'map' && use_map |
21 | partial = 'google_maps' | 21 | partial = 'google_maps' |
22 | klass = 'map' | 22 | klass = 'map' |
23 | else | 23 | else |
@@ -31,15 +31,19 @@ module SearchHelper | @@ -31,15 +31,19 @@ module SearchHelper | ||
31 | def display_map_list_button | 31 | def display_map_list_button |
32 | button(:search, params[:display] == 'map' ? _('Display in list') : _('Display in map'), | 32 | button(:search, params[:display] == 'map' ? _('Display in list') : _('Display in map'), |
33 | params.merge(:display => (params[:display] == 'map' ? 'list' : 'map')), | 33 | params.merge(:display => (params[:display] == 'map' ? 'list' : 'map')), |
34 | - :class => "map-toggle-button" ) if GoogleMaps.enabled?(environment.default_hostname) | 34 | + :class => "map-toggle-button" ) |
35 | end | 35 | end |
36 | 36 | ||
37 | def city_with_state(city) | 37 | def city_with_state(city) |
38 | - s = city.parent | ||
39 | - if city and city.kind_of?(City) and s and s.kind_of?(State) and s.acronym | ||
40 | - city.name + ', ' + s.acronym | 38 | + if city and city.kind_of?(City) |
39 | + s = city.parent | ||
40 | + if s and s.kind_of?(State) and s.acronym | ||
41 | + city.name + ', ' + s.acronym | ||
42 | + else | ||
43 | + city.name | ||
44 | + end | ||
41 | else | 45 | else |
42 | - city.name | 46 | + nil |
43 | end | 47 | end |
44 | end | 48 | end |
45 | 49 | ||
@@ -55,6 +59,7 @@ module SearchHelper | @@ -55,6 +59,7 @@ module SearchHelper | ||
55 | end | 59 | end |
56 | 60 | ||
57 | def facet_javascript(input_id, facet, array) | 61 | def facet_javascript(input_id, facet, array) |
62 | + array = [] if array.nil? | ||
58 | hintText = _('Type in an option') | 63 | hintText = _('Type in an option') |
59 | text_field_tag('facet['+input_id+']', '', :id => input_id) + | 64 | text_field_tag('facet['+input_id+']', '', :id => input_id) + |
60 | javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, | 65 | javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, |
@@ -63,13 +68,13 @@ module SearchHelper | @@ -63,13 +68,13 @@ module SearchHelper | ||
63 | end | 68 | end |
64 | 69 | ||
65 | def facet_link_html(facet, params, value, label, count) | 70 | def facet_link_html(facet, params, value, label, count) |
66 | - params = params.dup | 71 | + params = params ? params.dup : {} |
67 | has_extra = label.kind_of?(Array) | 72 | has_extra = label.kind_of?(Array) |
68 | link_label = has_extra ? label[0] : label | 73 | link_label = has_extra ? label[0] : label |
69 | id = facet[:solr_field].to_s | 74 | id = facet[:solr_field].to_s |
70 | params[:facet] ||= {} | 75 | params[:facet] ||= {} |
71 | params[:facet][id] ||= {} | 76 | params[:facet][id] ||= {} |
72 | - params[:page] = {} | 77 | + params[:page] = {} if params[:page] |
73 | 78 | ||
74 | selected = facet[:label_id].nil? ? params[:facet][id] == value : params[:facet][id][facet[:label_id]].to_a.include?(value) | 79 | selected = facet[:label_id].nil? ? params[:facet][id] == value : params[:facet][id][facet[:label_id]].to_a.include?(value) |
75 | 80 |
@@ -0,0 +1,252 @@ | @@ -0,0 +1,252 @@ | ||
1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
2 | + | ||
3 | +class SearchHelperTest < ActiveSupport::TestCase | ||
4 | + | ||
5 | + include SearchHelper | ||
6 | + | ||
7 | + | ||
8 | + | ||
9 | + should 'display search page title' do | ||
10 | + title = 'page_title' | ||
11 | + assert_equal search_page_title(title), '<h1>page_title</h1>' | ||
12 | + end | ||
13 | + | ||
14 | + should 'display search page title with category name' do | ||
15 | + title = 'page_title' | ||
16 | + category = mock | ||
17 | + category.stubs(:name).returns('category_name') | ||
18 | + assert_equal '<h1>page_title<small>category_name</small></h1>', | ||
19 | + search_page_title(title, category) | ||
20 | + end | ||
21 | + | ||
22 | + should 'display category context' do | ||
23 | + stubs(:params).returns({:action => 'action'}) | ||
24 | + category = mock | ||
25 | + category.stubs(:full_name).returns('category_full_name') | ||
26 | + expects('link_to').returns('link_to_result').once | ||
27 | + expects('content_tag').with('div', 'category_full_name, link_to_result', anything) | ||
28 | + category_context(category, {}) | ||
29 | + end | ||
30 | + | ||
31 | + should 'display results without map' do | ||
32 | + stubs(:params).returns({:display => ''}) | ||
33 | + expects('render').with({:partial => 'display_results'}).returns('render_return') | ||
34 | + expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results list') | ||
35 | + display_results | ||
36 | + end | ||
37 | + | ||
38 | + should 'display results with map' do | ||
39 | + stubs(:params).returns({:display => 'map'}) | ||
40 | + expects('render').with({:partial => 'google_maps'}).returns('render_return') | ||
41 | + expects('content_tag').with('div', 'render_return', :class => 'map-or-list-search-results map') | ||
42 | + display_results true | ||
43 | + end | ||
44 | + | ||
45 | + should 'show display_list button when in map view' do | ||
46 | + stubs(:params).returns({:display => 'map'}) | ||
47 | + expects(:button).with(:search, 'Display in list', {:display => 'list'}, anything) | ||
48 | + display_map_list_button | ||
49 | + end | ||
50 | + | ||
51 | + should 'show display_map button when in list view' do | ||
52 | + stubs(:params).returns({:display => ''}) | ||
53 | + expects(:button).with(:search, 'Display in map', {:display => 'map'}, anything) | ||
54 | + display_map_list_button | ||
55 | + end | ||
56 | + | ||
57 | + should 'return full city name with state' do | ||
58 | + state = mock | ||
59 | + state.stubs(:kind_of?).with(State).returns(true) | ||
60 | + state.stubs(:acronym).returns('CE') | ||
61 | + city = mock | ||
62 | + city.stubs(:parent).returns(state) | ||
63 | + city.stubs(:kind_of?).with(City).returns(true) | ||
64 | + city.stubs(:name).returns('Jijoca de Jericoacoara') | ||
65 | + assert_equal 'Jijoca de Jericoacoara, CE', city_with_state(city) | ||
66 | + end | ||
67 | + | ||
68 | + should 'not return city_with_state when city is nil' do | ||
69 | + assert_nil city_with_state nil | ||
70 | + end | ||
71 | + | ||
72 | + should 'not return city_with_state when parameter is not a City' do | ||
73 | + city = mock | ||
74 | + city.stubs(:kind_of?).with(City).returns(false) | ||
75 | + assert_nil city_with_state city | ||
76 | + end | ||
77 | + | ||
78 | + should 'return city name when parent is not defined' do | ||
79 | + city = mock | ||
80 | + city.stubs(:kind_of?).with(City).returns(true) | ||
81 | + city.stubs(:parent).returns(nil) | ||
82 | + city.stubs(:name).returns('Feliz Deserto') | ||
83 | + assert_equal 'Feliz Deserto', city_with_state(city) | ||
84 | + end | ||
85 | + | ||
86 | + should 'return city name when parent is not a State' do | ||
87 | + state = mock | ||
88 | + state.stubs(:kind_of?).with(State).returns(false) | ||
89 | + city = mock | ||
90 | + city.stubs(:kind_of?).with(City).returns(true) | ||
91 | + city.stubs(:parent).returns(state) | ||
92 | + city.stubs(:name).returns('Feliz Deserto') | ||
93 | + assert_equal 'Feliz Deserto', city_with_state(city) | ||
94 | + end | ||
95 | + | ||
96 | + should 'return city name when parent has no acronym' do | ||
97 | + state = mock | ||
98 | + state.stubs(:kind_of?).with(State).returns(true) | ||
99 | + state.stubs(:acronym).returns(nil) | ||
100 | + city = mock | ||
101 | + city.stubs(:kind_of?).with(City).returns(true) | ||
102 | + city.stubs(:parent).returns(state) | ||
103 | + city.stubs(:name).returns('Feliz Deserto') | ||
104 | + assert_equal 'Feliz Deserto', city_with_state(city) | ||
105 | + end | ||
106 | + | ||
107 | + should 'display facets menu' do | ||
108 | + expects(:asset_class).with('asset') | ||
109 | + expects(:render).with(:partial => 'facets_menu') | ||
110 | + facets_menu 'asset', nil | ||
111 | + end | ||
112 | + | ||
113 | + should 'display facets_unselect menu' do | ||
114 | + expects(:asset_class).with('asset') | ||
115 | + expects(:render).with(:partial => 'facets_unselect_menu') | ||
116 | + facets_unselect_menu 'asset' | ||
117 | + end | ||
118 | + | ||
119 | + should 'display facets javascript' do | ||
120 | + expects(:text_field_tag).returns('<text_field_tag_return>') | ||
121 | + expects(:javascript_tag).with(regexp_matches(/id.*[\'array_item\'].*json_message/m)).returns( | ||
122 | + '<javascript_tag_return>') | ||
123 | + stubs(:jquery_token_input_messages_json).returns('json_message') | ||
124 | + assert_equal '<text_field_tag_return><javascript_tag_return>', | ||
125 | + facet_javascript('id', '', ['array_item']) | ||
126 | + end | ||
127 | + | ||
128 | + should 'display empty array in facets javascript if array is nil' do | ||
129 | + expects(:text_field_tag).returns('<text_field_tag_return>') | ||
130 | + expects(:javascript_tag).with(regexp_matches(/id.*\[\].*json_message/m)).returns( | ||
131 | + '<javascript_tag_return>') | ||
132 | + stubs(:jquery_token_input_messages_json).returns('json_message') | ||
133 | + assert_equal '<text_field_tag_return><javascript_tag_return>', | ||
134 | + facet_javascript('id', '', []) | ||
135 | + end | ||
136 | + | ||
137 | + should 'return html code for facet link' do | ||
138 | + facet = { | ||
139 | + :solr_field => 'facet_solr_field', | ||
140 | + :label_id => 'facet_label_id' | ||
141 | + } | ||
142 | + params = {} | ||
143 | + value = 'facet_value' | ||
144 | + label = 'facet_label' | ||
145 | + count = 1 | ||
146 | + | ||
147 | + expected_url = {:facet => {'facet_solr_field' => { 'facet_label_id' => ['facet_value']}}} | ||
148 | + | ||
149 | + expects(:link_to).with('facet_label', expected_url, anything).returns('<link_to_result>') | ||
150 | + stubs(:content_tag).with(anything, '', anything).returns('<content_tag_extra>') | ||
151 | + stubs(:content_tag).with(anything, ' (1)', anything).returns('<content_tag_count>') | ||
152 | + stubs(:content_tag).with(anything, '<link_to_result><content_tag_extra><content_tag_count>', anything).returns('<content_tag_final_result>') | ||
153 | + | ||
154 | + assert_equal '<content_tag_final_result>', | ||
155 | + facet_link_html(facet, params, value, label, count) | ||
156 | + end | ||
157 | + | ||
158 | + should 'return html code for facet link with extra label' do | ||
159 | + facet = { | ||
160 | + :solr_field => 'facet_solr_field', | ||
161 | + :label_id => 'facet_label_id' | ||
162 | + } | ||
163 | + params = {} | ||
164 | + value = 'facet_value' | ||
165 | + label = ['facet_label', 'facet_extra'] | ||
166 | + count = 1 | ||
167 | + | ||
168 | + expected_url = {:facet => {'facet_solr_field' => { 'facet_label_id' => ['facet_value']}}} | ||
169 | + | ||
170 | + expects(:link_to).with('facet_label', expected_url, anything).returns('<link_to_result>') | ||
171 | + stubs(:content_tag).with(anything, 'facet_extra', anything).returns('<content_tag_extra>') | ||
172 | + stubs(:content_tag).with(anything, ' (1)', anything).returns('<content_tag_count>') | ||
173 | + stubs(:content_tag).with(anything, '<link_to_result><content_tag_extra><content_tag_count>', anything).returns('<content_tag_final_result>') | ||
174 | + | ||
175 | + assert_equal '<content_tag_final_result>', | ||
176 | + facet_link_html(facet, params, value, label, count) | ||
177 | + end | ||
178 | + | ||
179 | + should 'return html code for selected facet link' do | ||
180 | + facet = { | ||
181 | + :solr_field => 'facet_solr_field' | ||
182 | + } | ||
183 | + params = {:facet => {'facet_solr_field' => 'facet_value'}} | ||
184 | + value = 'facet_value' | ||
185 | + label = 'facet_label' | ||
186 | + count = 1 | ||
187 | + | ||
188 | + expected_url = {:facet => {'facet_solr_field' => 'facet_value'}} | ||
189 | + | ||
190 | + expects(:link_to).with('facet_label', expected_url, anything).returns('<link_to_result>') | ||
191 | + stubs(:content_tag).with(anything, '', anything).returns('<content_tag_extra>') | ||
192 | + stubs(:content_tag).with(anything, ' (1)', anything).returns('<content_tag_count>') | ||
193 | + stubs(:content_tag).with(anything, '<link_to_result><content_tag_extra><content_tag_count>', {:class => 'facet-menu-item facet-result-link-selected'}).returns('<content_tag_final_result>') | ||
194 | + | ||
195 | + assert_equal '<content_tag_final_result>', | ||
196 | + facet_link_html(facet, params, value, label, count) | ||
197 | + end | ||
198 | + | ||
199 | + should 'show html for non-hash selected facets' do | ||
200 | + klass = mock | ||
201 | + klass.stubs(:facet_by_id).with(:facet_id).returns('klass_facet_by_id') | ||
202 | + klass.stubs(:facet_label).with('klass_facet_by_id').returns('klass_facet_label') | ||
203 | + klass.stubs(:facet_result_name).with('klass_facet_by_id', 'facet_value').returns('klass_facet_result_name') | ||
204 | + params = {:facet => {:facet_id => 'facet_value'}} | ||
205 | + | ||
206 | + expects(:content_tag).with(anything, 'klass_facet_label', anything).returns('<content_tag_label>') | ||
207 | + expects(:content_tag).with(anything, 'klass_facet_result_name', anything).returns('<content_tag_name>') | ||
208 | + expects(:link_to).with(anything, {:facet => {}}, anything).returns('<link_to_url>') | ||
209 | + expects(:content_tag).with(anything, '<content_tag_label><content_tag_name><link_to_url>', anything).returns('<final_content>') | ||
210 | + | ||
211 | + environment = mock | ||
212 | + assert_match '<final_content>', facet_selecteds_html_for(environment, klass, params) | ||
213 | + end | ||
214 | + | ||
215 | + should 'show select tag for order_by' do | ||
216 | + [:products, :events, :articles, :enterprises, :people, :communities].each do |asset| | ||
217 | + params = {:order_by => 'Relevance'} | ||
218 | + | ||
219 | + stubs(:params).returns(params) | ||
220 | + stubs(:options_for_select).with(instance_of(Array), params[:order_by]).returns('<options_for_select>') | ||
221 | + stubs(:select_tag).with(regexp_matches(/#{asset}/), '<options_for_select>', anything).returns('<select_tag>') | ||
222 | + expects(:content_tag).with(anything, regexp_matches(/<select_tag>/), anything).returns('<final_content>') | ||
223 | + | ||
224 | + assert_equal '<final_content>', order_by(asset) | ||
225 | + end | ||
226 | + end | ||
227 | + | ||
228 | + should 'show total of assets found' do | ||
229 | + [:products, :events, :articles, :enterprises, :people, :communities].each do |asset| | ||
230 | + expects(:content_tag).with(anything, regexp_matches(/10.*#{asset}.*found/), anything).returns('<final_content>') | ||
231 | + assert_equal '<final_content>', label_total_found(asset, 10) | ||
232 | + end | ||
233 | + end | ||
234 | + | ||
235 | + | ||
236 | + should 'return asset class from string' do | ||
237 | + asset_names = ['products', 'events', 'articles', 'enterprises', 'people', 'communities'] | ||
238 | + asset_classes = [Product, Event, Article, Enterprise, Person, Community] | ||
239 | + asset_names.each_index do |i| | ||
240 | + assert_equal asset_classes[i], asset_class(asset_names[i]) | ||
241 | + end | ||
242 | + end | ||
243 | + | ||
244 | + should 'return asset table from string' do | ||
245 | + asset_classes = [Product, Event, Article, Enterprise, Person, Community] | ||
246 | + asset_tables = ['products', 'articles', 'articles', 'profiles', 'profiles', 'profiles'] | ||
247 | + asset_classes.each_index do |i| | ||
248 | + assert_equal asset_tables[i], asset_table(asset_classes[i]) | ||
249 | + end | ||
250 | + end | ||
251 | + | ||
252 | +end |