blacklight 5.0.0.pre3 → 5.0.0.pre4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +9 -16
  3. data/Gemfile +4 -0
  4. data/README.md +15 -1
  5. data/VERSION +1 -1
  6. data/app/assets/stylesheets/blacklight/_bookmark.css.scss +1 -1
  7. data/app/assets/stylesheets/blacklight/_catalog.css.scss +2 -12
  8. data/app/assets/stylesheets/blacklight/_facets.css.scss +1 -1
  9. data/app/assets/stylesheets/blacklight/_header.css.scss +1 -0
  10. data/app/assets/stylesheets/blacklight/_modal.css.scss +4 -2
  11. data/app/assets/stylesheets/blacklight/blacklight_defaults.css.scss +0 -5
  12. data/app/controllers/bookmarks_controller.rb +4 -0
  13. data/app/helpers/blacklight/blacklight_helper_behavior.rb +26 -187
  14. data/app/helpers/blacklight/catalog_helper_behavior.rb +20 -8
  15. data/app/helpers/blacklight/facets_helper_behavior.rb +0 -83
  16. data/app/helpers/blacklight/hash_as_hidden_fields_helper_behavior.rb +1 -1
  17. data/app/helpers/blacklight/url_helper_behavior.rb +201 -0
  18. data/app/helpers/blacklight_url_helper.rb +3 -0
  19. data/app/views/bookmarks/_tools.html.erb +4 -7
  20. data/app/views/catalog/_citation.html.erb +1 -1
  21. data/app/views/catalog/_did_you_mean.html.erb +1 -1
  22. data/app/views/catalog/_document.html.erb +1 -1
  23. data/app/views/catalog/_search_form.html.erb +1 -1
  24. data/app/views/catalog/_show_more_like_this.html.erb +1 -1
  25. data/app/views/catalog/_view_type_group.html.erb +4 -4
  26. data/app/views/catalog/email.html.erb +2 -2
  27. data/app/views/catalog/facet.html.erb +1 -1
  28. data/app/views/catalog/show.html.erb +1 -1
  29. data/app/views/catalog/sms.html.erb +1 -1
  30. data/blacklight.gemspec +2 -2
  31. data/config/jetty.yml +4 -1
  32. data/config/locales/blacklight.en.yml +3 -0
  33. data/config/locales/blacklight.fr.yml +3 -0
  34. data/{lib/generators/blacklight/templates/migrations/create_searches.rb → db/migrate/20140202020201_create_searches.rb} +2 -0
  35. data/{lib/generators/blacklight/templates/migrations/create_bookmarks.rb → db/migrate/20140202020202_create_bookmarks.rb} +1 -2
  36. data/gemfiles/rails3.gemfile +2 -0
  37. data/gemfiles/rails4.gemfile +2 -0
  38. data/lib/blacklight.rb +1 -40
  39. data/lib/blacklight/base.rb +3 -9
  40. data/lib/blacklight/catalog.rb +3 -16
  41. data/lib/blacklight/catalog/search_context.rb +8 -1
  42. data/lib/blacklight/configurable.rb +1 -2
  43. data/lib/blacklight/configuration.rb +13 -3
  44. data/lib/blacklight/configuration/view_config.rb +71 -0
  45. data/lib/blacklight/engine.rb +1 -0
  46. data/lib/blacklight/routes.rb +0 -1
  47. data/lib/blacklight/solr/document.rb +0 -4
  48. data/lib/blacklight/solr_helper.rb +8 -2
  49. data/lib/blacklight/user.rb +1 -16
  50. data/lib/blacklight/utils.rb +72 -1
  51. data/lib/generators/blacklight/install_generator.rb +92 -0
  52. data/lib/generators/blacklight/models_generator.rb +1 -25
  53. data/lib/generators/blacklight/templates/catalog_controller.rb +28 -32
  54. data/spec/helpers/blacklight_helper_spec.rb +24 -417
  55. data/spec/helpers/catalog_helper_spec.rb +13 -13
  56. data/spec/helpers/facets_helper_spec.rb +0 -127
  57. data/spec/helpers/hash_as_hidden_fields_spec.rb +1 -1
  58. data/spec/helpers/url_helper_spec.rb +360 -0
  59. data/spec/lib/blacklight/configuration_spec.rb +2 -3
  60. data/spec/lib/blacklight/solr_helper_spec.rb +11 -17
  61. data/spec/lib/blacklight/user_spec.rb +0 -41
  62. data/spec/lib/blacklight_spec.rb +0 -22
  63. data/spec/lib/utils_spec.rb +35 -4
  64. data/spec/spec_helper.rb +4 -3
  65. data/spec/test_app_templates/lib/generators/test_app_generator.rb +2 -2
  66. data/spec/views/catalog/_constraints.html.erb_spec.rb +4 -2
  67. data/spec/views/catalog/_index_default.erb_spec.rb +1 -1
  68. data/spec/views/catalog/_paginate_compact.html.erb_spec.rb +1 -5
  69. data/spec/views/catalog/_show_default.erb_spec.rb +1 -1
  70. data/spec/views/catalog/_view_type_group.html.erb_spec.rb +11 -3
  71. data/tasks/blacklight.rake +1 -5
  72. metadata +14 -16
  73. data/.gitmodules +0 -0
  74. data/app/views/catalog/endnote.endnote.erb +0 -1
  75. data/app/views/catalog/show.endnote.erb +0 -1
  76. data/lib/generators/blacklight/blacklight_generator.rb +0 -97
  77. data/lib/generators/blacklight/templates/migrations/add_user_types_to_bookmarks_searches.rb +0 -16
  78. data/lib/generators/blacklight/templates/migrations/remove_editable_fields_from_bookmarks.rb +0 -12
  79. data/lib/railties/all_tests.rake +0 -39
  80. data/lib/railties/blacklight_rspec.rake +0 -128
@@ -153,32 +153,32 @@ describe CatalogHelper do
153
153
 
154
154
  describe "has_thumbnail?" do
155
155
  it "should have a thumbnail if a thumbnail_method is configured" do
156
- helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new(:thumbnail_method => :xyz) ))
156
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz) ))
157
157
  document = double()
158
158
  expect(helper.has_thumbnail? document).to be_true
159
159
  end
160
160
 
161
161
  it "should have a thumbnail if a thumbnail_field is configured and it exists in the document" do
162
- helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new(:thumbnail_field => :xyz) ))
162
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
163
163
  document = double(:has? => true)
164
164
  expect(helper.has_thumbnail? document).to be_true
165
165
  end
166
166
 
167
167
  it "should not have a thumbnail if the thumbnail_field is missing from the document" do
168
- helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new(:thumbnail_field => :xyz) ))
168
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
169
169
  document = double(:has? => false)
170
170
  expect(helper.has_thumbnail? document).to be_false
171
171
  end
172
172
 
173
173
  it "should not have a thumbnail if none of the fields are configured" do
174
- helper.stub(:blacklight_config => OpenStruct.new(:index => OpenStruct.new()))
174
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new() ))
175
175
  expect(helper.has_thumbnail? double()).to be_false
176
176
  end
177
177
  end
178
178
 
179
179
  describe "render_thumbnail_tag" do
180
180
  it "should call the provided thumbnail method" do
181
- helper.stub(:blacklight_config => double(:index => double(:thumbnail_method => :xyz)))
181
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz) ))
182
182
  document = double()
183
183
  helper.stub(:xyz => "some-thumbnail")
184
184
 
@@ -187,7 +187,7 @@ describe CatalogHelper do
187
187
  end
188
188
 
189
189
  it "should create an image tag from the given field" do
190
- helper.stub(:blacklight_config => double(:index => OpenStruct.new(:thumbnail_field => :xyz)))
190
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
191
191
  document = double()
192
192
 
193
193
  document.stub(:has?).with(:xyz).and_return(true)
@@ -198,14 +198,14 @@ describe CatalogHelper do
198
198
  end
199
199
 
200
200
  it "should return nil if no thumbnail is available" do
201
- helper.stub(:blacklight_config => double(:index => OpenStruct.new()))
201
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new() ))
202
202
 
203
203
  document = double()
204
204
  expect(helper.render_thumbnail_tag document).to be_nil
205
205
  end
206
206
 
207
207
  it "should return nil if no thumbnail is returned from the thumbnail method" do
208
- helper.stub(:blacklight_config => double(:index => OpenStruct.new(:thumbnail_method => :xyz)))
208
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_method => :xyz) ))
209
209
  helper.stub(:xyz => nil)
210
210
  document = double()
211
211
 
@@ -215,17 +215,17 @@ describe CatalogHelper do
215
215
 
216
216
  describe "thumbnail_url" do
217
217
  it "should pull the configured thumbnail field out of the document" do
218
- helper.stub(:blacklight_config => double(:index => double(:thumbnail_field => "xyz")))
218
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
219
219
  document = double()
220
- document.stub(:has?).with("xyz").and_return(true)
221
- document.stub(:first).with("xyz").and_return("asdf")
220
+ document.stub(:has?).with(:xyz).and_return(true)
221
+ document.stub(:first).with(:xyz).and_return("asdf")
222
222
  expect(helper.thumbnail_url document).to eq("asdf")
223
223
  end
224
224
 
225
225
  it "should return nil if the thumbnail field doesn't exist" do
226
- helper.stub(:blacklight_config => double(:index => double(:thumbnail_field => "xyz")))
226
+ helper.stub(:blacklight_config => Blacklight::Configuration.new(:index => Blacklight::OpenStructWithHashAccess.new(:thumbnail_field => :xyz) ))
227
227
  document = double()
228
- document.stub(:has?).with("xyz").and_return(false)
228
+ document.stub(:has?).with(:xyz).and_return(false)
229
229
  expect(helper.thumbnail_url document).to be_nil
230
230
  end
231
231
  end
@@ -281,133 +281,6 @@ describe FacetsHelper do
281
281
  end
282
282
  end
283
283
 
284
- describe "add_facet_params" do
285
- before do
286
- @params_no_existing_facet = {:q => "query", :search_field => "search_field", :per_page => "50"}
287
- @params_existing_facets = {:q => "query", :search_field => "search_field", :per_page => "50", :f => {"facet_field_1" => ["value1"], "facet_field_2" => ["value2", "value2a"]}}
288
- end
289
-
290
- it "should add facet value for no pre-existing facets" do
291
- helper.stub(:params).and_return(@params_no_existing_facet)
292
-
293
- result_params = helper.add_facet_params("facet_field", "facet_value")
294
- expect(result_params[:f]).to be_a_kind_of(Hash)
295
- expect(result_params[:f]["facet_field"]).to be_a_kind_of(Array)
296
- expect(result_params[:f]["facet_field"]).to eq ["facet_value"]
297
- end
298
-
299
- it "should add a facet param to existing facet constraints" do
300
- helper.stub(:params).and_return(@params_existing_facets)
301
-
302
- result_params = helper.add_facet_params("facet_field_2", "new_facet_value")
303
-
304
- expect(result_params[:f]).to be_a_kind_of(Hash)
305
-
306
- @params_existing_facets[:f].each_pair do |facet_field, value_list|
307
- expect(result_params[:f][facet_field]).to be_a_kind_of(Array)
308
-
309
- if facet_field == 'facet_field_2'
310
- expect(result_params[:f][facet_field]).to eq (@params_existing_facets[:f][facet_field] | ["new_facet_value"])
311
- else
312
- expect(result_params[:f][facet_field]).to eq @params_existing_facets[:f][facet_field]
313
- end
314
- end
315
- end
316
- it "should leave non-facet params alone" do
317
- [@params_existing_facets, @params_no_existing_facet].each do |params|
318
- helper.stub(:params).and_return(params)
319
-
320
- result_params = helper.add_facet_params("facet_field_2", "new_facet_value")
321
-
322
- params.each_pair do |key, value|
323
- next if key == :f
324
- expect(result_params[key]).to eq params[key]
325
- end
326
- end
327
- end
328
-
329
- it "should replace facets for facets configured as single" do
330
- helper.should_receive(:facet_configuration_for_field).with('single_value_facet_field').and_return(double(:single => true))
331
- params = { :f => { 'single_value_facet_field' => 'other_value'}}
332
- helper.stub(:params).and_return params
333
-
334
- result_params = helper.add_facet_params('single_value_facet_field', 'my_value')
335
-
336
-
337
- expect(result_params[:f]['single_value_facet_field']).to have(1).item
338
- expect(result_params[:f]['single_value_facet_field'].first).to eq 'my_value'
339
- end
340
-
341
- it "should accept a FacetItem instead of a plain facet value" do
342
-
343
- result_params = helper.add_facet_params('facet_field_1', double(:value => 123))
344
-
345
- expect(result_params[:f]['facet_field_1']).to include(123)
346
- end
347
-
348
- it "should defer to the field set on a FacetItem" do
349
-
350
- result_params = helper.add_facet_params('facet_field_1', double(:field => 'facet_field_2', :value => 123))
351
-
352
- expect(result_params[:f]['facet_field_1']).to be_blank
353
- expect(result_params[:f]['facet_field_2']).to include(123)
354
- end
355
-
356
- it "should add any extra fq parameters from the FacetItem" do
357
-
358
- result_params = helper.add_facet_params('facet_field_1', double(:value => 123, :fq => {'facet_field_2' => 'abc'}))
359
-
360
- expect(result_params[:f]['facet_field_1']).to include(123)
361
- expect(result_params[:f]['facet_field_2']).to include('abc')
362
- end
363
- end
364
-
365
- describe "add_facet_params_and_redirect" do
366
- before do
367
- catalog_facet_params = {:q => "query",
368
- :search_field => "search_field",
369
- :per_page => "50",
370
- :page => "5",
371
- :f => {"facet_field_1" => ["value1"], "facet_field_2" => ["value2", "value2a"]},
372
- Blacklight::Solr::FacetPaginator.request_keys[:offset] => "100",
373
- Blacklight::Solr::FacetPaginator.request_keys[:sort] => "index",
374
- :id => 'facet_field_name'
375
- }
376
- helper.stub(:params).and_return(catalog_facet_params)
377
- end
378
- it "should redirect to 'index' action" do
379
- params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
380
-
381
- expect(params[:action]).to eq "index"
382
- end
383
- it "should not include request parameters used by the facet paginator" do
384
- params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
385
-
386
- bad_keys = Blacklight::Solr::FacetPaginator.request_keys.values + [:id]
387
- bad_keys.each do |paginator_key|
388
- expect(params.keys).to_not include(paginator_key)
389
- end
390
- end
391
- it 'should remove :page request key' do
392
- params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
393
-
394
- expect(params.keys).to_not include(:page)
395
- end
396
- it "should otherwise do the same thing as add_facet_params" do
397
- added_facet_params = helper.add_facet_params("facet_field_2", "facet_value")
398
- added_facet_params_from_facet_action = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
399
-
400
- added_facet_params_from_facet_action.each_pair do |key, value|
401
- next if key == :action
402
- expect(value).to eq added_facet_params[key]
403
- end
404
- end
405
- end
406
-
407
- describe "remove_facet_params" do
408
-
409
- end
410
-
411
284
  describe "facet_field_in_params?" do
412
285
  it "should check if the facet field is selected in the user params" do
413
286
  helper.stub(:params => { :f => { "some-field" => ["x"]}})
@@ -8,7 +8,7 @@ describe HashAsHiddenFieldsHelper do
8
8
 
9
9
  it "should convert a hash with nested complex data to Rails-style hidden form fields" do
10
10
 
11
- generated = hash_as_hidden_fields(@hash)
11
+ generated = render_hash_as_hidden_fields(@hash)
12
12
 
13
13
  expect(generated).to have_selector("input[type='hidden'][name='q'][value='query']")
14
14
  expect(generated).to have_selector("input[type='hidden'][name='per_page'][value='10']")
@@ -0,0 +1,360 @@
1
+ require 'spec_helper'
2
+
3
+ describe BlacklightHelper do
4
+
5
+ let(:blacklight_config) do
6
+ @config ||= Blacklight::Configuration.new.configure do |config|
7
+ config.index.title_field = 'title_display'
8
+ config.index.display_type_field = 'format'
9
+ end
10
+ end
11
+
12
+ before(:each) do
13
+ helper.stub(:search_action_url) do |*args|
14
+ catalog_index_url *args
15
+ end
16
+
17
+ helper.stub(:blacklight_config).and_return blacklight_config
18
+ end
19
+
20
+ def current_search_session
21
+
22
+ end
23
+
24
+ describe "link_back_to_catalog" do
25
+ let(:query_params) {{:q => "query", :f => "facets", :per_page => "10", :page => "2", :controller=>'catalog'}}
26
+ let(:bookmarks_query_params) {{ :page => "2", :controller=>'bookmarks'}}
27
+
28
+ it "should build a link tag to catalog using session[:search] for query params" do
29
+ helper.stub(:current_search_session).and_return double(:query_params => query_params)
30
+ tag = helper.link_back_to_catalog
31
+ expect(tag).to match /q=query/
32
+ expect(tag).to match /f=facets/
33
+ expect(tag).to match /per_page=10/
34
+ expect(tag).to match /page=2/
35
+ end
36
+
37
+ it "should build a link tag to bookmarks using session[:search] for query params" do
38
+ helper.stub(:current_search_session).and_return double(:query_params => bookmarks_query_params)
39
+ tag = helper.link_back_to_catalog
40
+ expect(tag).to match /Back to Bookmarks/
41
+ expect(tag).to match /\/bookmarks/
42
+ expect(tag).to match /page=2/
43
+ end
44
+
45
+ describe "when an alternate scope is passed in" do
46
+ let(:my_engine) { double("Engine") }
47
+
48
+ it "should call url_for on the engine scope" do
49
+ helper.stub(:current_search_session).and_return double(:query_params => query_params)
50
+ expect(my_engine).to receive(:url_for).and_return(url_for(query_params))
51
+ tag = helper.link_back_to_catalog(route_set: my_engine)
52
+ expect(tag).to match /Back to Search/
53
+ expect(tag).to match /q=query/
54
+ expect(tag).to match /f=facets/
55
+ expect(tag).to match /per_page=10/
56
+ expect(tag).to match /page=2/
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "link_to_query" do
62
+ it "should build a link tag to catalog using query string (no other params)" do
63
+ query = "brilliant"
64
+ self.should_receive(:params).and_return({})
65
+ tag = link_to_query(query)
66
+ expect(tag).to match /q=#{query}/
67
+ expect(tag).to match />#{query}<\/a>/
68
+ end
69
+ it "should build a link tag to catalog using query string and other existing params" do
70
+ query = "wonderful"
71
+ self.should_receive(:params).and_return({:qt => "title_search", :per_page => "50"})
72
+ tag = link_to_query(query)
73
+ expect(tag).to match /qt=title_search/
74
+ expect(tag).to match /per_page=50/
75
+ end
76
+ it "should ignore existing :page param" do
77
+ query = "yes"
78
+ self.should_receive(:params).and_return({:page => "2", :qt => "author_search"})
79
+ tag = link_to_query(query)
80
+ expect(tag).to match /qt=author_search/
81
+ expect(tag).to_not match /page/
82
+ end
83
+ it "should be html_safe" do
84
+ query = "brilliant"
85
+ self.should_receive(:params).and_return({:page => "2", :qt => "author_search"})
86
+ tag = link_to_query(query)
87
+ expect(tag).to be_html_safe
88
+ end
89
+ end
90
+
91
+ describe "sanitize_search_params" do
92
+ it "should strip nil values" do
93
+ result = sanitize_search_params(:a => nil, :b => 1)
94
+ expect(result).to_not have_key(:a)
95
+ expect(result[:b]).to eq 1
96
+ end
97
+
98
+ it "should remove blacklisted keys" do
99
+ result = sanitize_search_params(:action => true, :controller => true, :id => true, :commit => true, :utf8 => true)
100
+ expect(result).to be_empty
101
+ end
102
+ end
103
+
104
+ describe "params_for_search" do
105
+ def params
106
+ { 'default' => 'params'}
107
+ end
108
+
109
+ it "should default to using the controller's params" do
110
+ result = params_for_search
111
+ expect(result).to eq params
112
+ expect(params.object_id).to_not eq result.object_id
113
+ end
114
+
115
+ it "should let you pass in params to merge into the controller's params" do
116
+ source_params = { :q => 'query'}
117
+ result = params_for_search( source_params )
118
+ expect(result).to include(:q => 'query', 'default' => 'params')
119
+ end
120
+
121
+ it "should not return blacklisted elements" do
122
+ source_params = { :action => 'action', :controller => 'controller', :id => "id", :commit => 'commit'}
123
+ result = params_for_search(source_params)
124
+ expect(result.keys).to_not include(:action, :controller, :commit, :id)
125
+ end
126
+
127
+ it "should adjust the current page if the per_page changes" do
128
+ source_params = { :per_page => 20, :page => 5}
129
+ result = params_for_search(source_params, :per_page => 100)
130
+ expect(result[:page]).to eq 1
131
+ end
132
+
133
+ it "should not adjust the current page if the per_page is the same as it always was" do
134
+ source_params = { :per_page => 20, :page => 5}
135
+ result = params_for_search(source_params, :per_page => 20)
136
+ expect(result[:page]).to eq 5
137
+ end
138
+
139
+ it "should adjust the current page if the sort changes" do
140
+ source_params = { :sort => 'field_a', :page => 5}
141
+ result = params_for_search(source_params, :sort => 'field_b')
142
+ expect(result[:page]).to eq 1
143
+ end
144
+
145
+ it "should not adjust the current page if the sort is the same as it always was" do
146
+ source_params = { :sort => 'field_a', :page => 5}
147
+ result = params_for_search(source_params, :sort => 'field_a')
148
+ expect(result[:page]).to eq 5
149
+ end
150
+
151
+ describe "params_for_search with a block" do
152
+ it "should evalute the block and allow it to add or remove keys" do
153
+ result = params_for_search({:a => 1, :b => 2}, :c => 3) do |params|
154
+ params.except! :a, :b
155
+ params[:d] = 'd'
156
+ end
157
+
158
+ result.keys.should_not include(:a, :b)
159
+ expect(result[:c]).to eq 3
160
+ expect(result[:d]).to eq 'd'
161
+ end
162
+
163
+ end
164
+
165
+ end
166
+
167
+
168
+ describe "start_over_path" do
169
+ it 'should be the catalog path with the current view type' do
170
+ blacklight_config.stub(:view) { { list: nil, abc: nil} }
171
+ helper.stub(:blacklight_config => blacklight_config)
172
+ expect(helper.start_over_path(:view => 'abc')).to eq catalog_index_url(:view => 'abc')
173
+ end
174
+
175
+ it 'should not include the current view type if it is the default' do
176
+ blacklight_config.stub(:view) { { list: nil, asdf: nil} }
177
+ helper.stub(:blacklight_config => blacklight_config)
178
+ expect(helper.start_over_path(:view => 'list')).to eq catalog_index_url
179
+ end
180
+ end
181
+
182
+ describe "link_to_document" do
183
+ it "should consist of the document title wrapped in a <a>" do
184
+ data = {'id'=>'123456','title_display'=>['654321'] }
185
+ @document = SolrDocument.new(data)
186
+ expect(link_to_document(@document, { :label => :title_display })).to have_selector("a", :text => '654321', :count => 1)
187
+ end
188
+
189
+ it "should accept and return a string label" do
190
+ data = {'id'=>'123456','title_display'=>['654321'] }
191
+ @document = SolrDocument.new(data)
192
+ expect(link_to_document(@document, { :label => "title_display" })).to have_selector("a", :text => 'title_display', :count => 1)
193
+ end
194
+
195
+ it "should accept and return a Proc" do
196
+ data = {'id'=>'123456','title_display'=>['654321'] }
197
+ @document = SolrDocument.new(data)
198
+ expect(link_to_document(@document, { :label => Proc.new { |doc, opts| doc.get(:id) + ": " + doc.get(:title_display) } })).to have_selector("a", :text => '123456: 654321', :count => 1)
199
+ end
200
+
201
+ it "should return id when label is missing" do
202
+ data = {'id'=>'123456'}
203
+ @document = SolrDocument.new(data)
204
+ expect(link_to_document(@document, { :label => :title_display })).to have_selector("a", :text => '123456', :count => 1)
205
+ end
206
+
207
+ it "should be html safe" do
208
+ data = {'id'=>'123456'}
209
+ @document = SolrDocument.new(data)
210
+ expect(link_to_document(@document, { :label => :title_display })).to be_html_safe
211
+ end
212
+
213
+ it "should convert the counter parameter into a data- attribute" do
214
+ data = {'id'=>'123456','title_display'=>['654321']}
215
+ @document = SolrDocument.new(data)
216
+ expect(link_to_document(@document, { :label => :title_display, :counter => 5 })).to match /data-counter="5"/
217
+ end
218
+
219
+ it "passes on the title attribute to the link_to_with_data method" do
220
+ @document = SolrDocument.new('id'=>'123456')
221
+ expect(link_to_document(@document,:label=>"Some crazy long label...",:title=>"Some crazy longer label")).to match(/title=\"Some crazy longer label\"/)
222
+ end
223
+
224
+ it "doesn't add an erroneous title attribute if one isn't provided" do
225
+ @document = SolrDocument.new('id'=>'123456')
226
+ expect(link_to_document(@document,:label=>"Some crazy long label...")).to_not match(/title=/)
227
+ end
228
+ end
229
+
230
+ describe "link_to_previous_search" do
231
+ it "should link to the given search parameters" do
232
+ params = {}
233
+ helper.should_receive(:render_search_to_s).with(params).and_return "link text"
234
+ expect(helper.link_to_previous_search({})).to eq helper.link_to("link text", catalog_index_path)
235
+ end
236
+ end
237
+
238
+ describe "add_facet_params" do
239
+ before do
240
+ @params_no_existing_facet = {:q => "query", :search_field => "search_field", :per_page => "50"}
241
+ @params_existing_facets = {:q => "query", :search_field => "search_field", :per_page => "50", :f => {"facet_field_1" => ["value1"], "facet_field_2" => ["value2", "value2a"]}}
242
+ end
243
+
244
+ it "should add facet value for no pre-existing facets" do
245
+ helper.stub(:params).and_return(@params_no_existing_facet)
246
+
247
+ result_params = helper.add_facet_params("facet_field", "facet_value")
248
+ expect(result_params[:f]).to be_a_kind_of(Hash)
249
+ expect(result_params[:f]["facet_field"]).to be_a_kind_of(Array)
250
+ expect(result_params[:f]["facet_field"]).to eq ["facet_value"]
251
+ end
252
+
253
+ it "should add a facet param to existing facet constraints" do
254
+ helper.stub(:params).and_return(@params_existing_facets)
255
+
256
+ result_params = helper.add_facet_params("facet_field_2", "new_facet_value")
257
+
258
+ expect(result_params[:f]).to be_a_kind_of(Hash)
259
+
260
+ @params_existing_facets[:f].each_pair do |facet_field, value_list|
261
+ expect(result_params[:f][facet_field]).to be_a_kind_of(Array)
262
+
263
+ if facet_field == 'facet_field_2'
264
+ expect(result_params[:f][facet_field]).to eq (@params_existing_facets[:f][facet_field] | ["new_facet_value"])
265
+ else
266
+ expect(result_params[:f][facet_field]).to eq @params_existing_facets[:f][facet_field]
267
+ end
268
+ end
269
+ end
270
+ it "should leave non-facet params alone" do
271
+ [@params_existing_facets, @params_no_existing_facet].each do |params|
272
+ helper.stub(:params).and_return(params)
273
+
274
+ result_params = helper.add_facet_params("facet_field_2", "new_facet_value")
275
+
276
+ params.each_pair do |key, value|
277
+ next if key == :f
278
+ expect(result_params[key]).to eq params[key]
279
+ end
280
+ end
281
+ end
282
+
283
+ it "should replace facets for facets configured as single" do
284
+ helper.should_receive(:facet_configuration_for_field).with('single_value_facet_field').and_return(double(:single => true))
285
+ params = { :f => { 'single_value_facet_field' => 'other_value'}}
286
+ helper.stub(:params).and_return params
287
+
288
+ result_params = helper.add_facet_params('single_value_facet_field', 'my_value')
289
+
290
+
291
+ expect(result_params[:f]['single_value_facet_field']).to have(1).item
292
+ expect(result_params[:f]['single_value_facet_field'].first).to eq 'my_value'
293
+ end
294
+
295
+ it "should accept a FacetItem instead of a plain facet value" do
296
+
297
+ result_params = helper.add_facet_params('facet_field_1', double(:value => 123))
298
+
299
+ expect(result_params[:f]['facet_field_1']).to include(123)
300
+ end
301
+
302
+ it "should defer to the field set on a FacetItem" do
303
+
304
+ result_params = helper.add_facet_params('facet_field_1', double(:field => 'facet_field_2', :value => 123))
305
+
306
+ expect(result_params[:f]['facet_field_1']).to be_blank
307
+ expect(result_params[:f]['facet_field_2']).to include(123)
308
+ end
309
+
310
+ it "should add any extra fq parameters from the FacetItem" do
311
+
312
+ result_params = helper.add_facet_params('facet_field_1', double(:value => 123, :fq => {'facet_field_2' => 'abc'}))
313
+
314
+ expect(result_params[:f]['facet_field_1']).to include(123)
315
+ expect(result_params[:f]['facet_field_2']).to include('abc')
316
+ end
317
+ end
318
+
319
+ describe "add_facet_params_and_redirect" do
320
+ before do
321
+ catalog_facet_params = {:q => "query",
322
+ :search_field => "search_field",
323
+ :per_page => "50",
324
+ :page => "5",
325
+ :f => {"facet_field_1" => ["value1"], "facet_field_2" => ["value2", "value2a"]},
326
+ Blacklight::Solr::FacetPaginator.request_keys[:offset] => "100",
327
+ Blacklight::Solr::FacetPaginator.request_keys[:sort] => "index",
328
+ :id => 'facet_field_name'
329
+ }
330
+ helper.stub(:params).and_return(catalog_facet_params)
331
+ end
332
+ it "should redirect to 'index' action" do
333
+ params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
334
+
335
+ expect(params[:action]).to eq "index"
336
+ end
337
+ it "should not include request parameters used by the facet paginator" do
338
+ params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
339
+
340
+ bad_keys = Blacklight::Solr::FacetPaginator.request_keys.values + [:id]
341
+ bad_keys.each do |paginator_key|
342
+ expect(params.keys).to_not include(paginator_key)
343
+ end
344
+ end
345
+ it 'should remove :page request key' do
346
+ params = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
347
+
348
+ expect(params.keys).to_not include(:page)
349
+ end
350
+ it "should otherwise do the same thing as add_facet_params" do
351
+ added_facet_params = helper.add_facet_params("facet_field_2", "facet_value")
352
+ added_facet_params_from_facet_action = helper.add_facet_params_and_redirect("facet_field_2", "facet_value")
353
+
354
+ added_facet_params_from_facet_action.each_pair do |key, value|
355
+ next if key == :action
356
+ expect(value).to eq added_facet_params[key]
357
+ end
358
+ end
359
+ end
360
+ end