blacklight 5.9.4 → 5.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -1
  3. data/VERSION +1 -1
  4. data/app/assets/stylesheets/blacklight/_facets.scss +11 -10
  5. data/app/helpers/blacklight/blacklight_helper_behavior.rb +14 -14
  6. data/app/helpers/blacklight/configuration_helper_behavior.rb +16 -10
  7. data/app/helpers/blacklight/facets_helper_behavior.rb +15 -14
  8. data/app/helpers/blacklight/url_helper_behavior.rb +1 -1
  9. data/app/models/bookmark.rb +1 -4
  10. data/app/models/search.rb +3 -6
  11. data/app/views/catalog/_facet_layout.html.erb +2 -2
  12. data/app/views/catalog/_facet_limit.html.erb +5 -3
  13. data/app/views/catalog/_facet_pivot.html.erb +4 -4
  14. data/app/views/catalog/_home_text.html.erb +8 -48
  15. data/app/views/catalog/_index_default.html.erb +3 -3
  16. data/app/views/catalog/_show_default.html.erb +3 -3
  17. data/app/views/catalog/_sms_form.html.erb +1 -1
  18. data/app/views/catalog/facet.html.erb +1 -1
  19. data/blacklight.gemspec +1 -1
  20. data/config/jetty.yml +0 -3
  21. data/config/locales/blacklight.de.yml +2 -0
  22. data/config/locales/blacklight.en.yml +2 -0
  23. data/config/locales/blacklight.es.yml +2 -0
  24. data/config/locales/blacklight.fr.yml +2 -0
  25. data/config/locales/blacklight.pt-BR.yml +2 -0
  26. data/lib/blacklight.rb +70 -26
  27. data/lib/blacklight/abstract_repository.rb +29 -0
  28. data/lib/blacklight/base.rb +7 -7
  29. data/lib/blacklight/bookmarks.rb +5 -5
  30. data/lib/blacklight/catalog.rb +34 -19
  31. data/lib/blacklight/catalog/search_context.rb +1 -1
  32. data/lib/blacklight/configuration.rb +112 -46
  33. data/lib/blacklight/configuration/facet_field.rb +9 -7
  34. data/lib/blacklight/configuration/field.rb +27 -0
  35. data/lib/blacklight/configuration/fields.rb +25 -20
  36. data/lib/blacklight/configuration/search_field.rb +6 -8
  37. data/lib/blacklight/configuration/solr_field.rb +3 -18
  38. data/lib/blacklight/configuration/sort_field.rb +6 -7
  39. data/lib/blacklight/document.rb +156 -0
  40. data/lib/blacklight/document/dublin_core.rb +41 -0
  41. data/lib/blacklight/document/email.rb +16 -0
  42. data/lib/blacklight/document/export.rb +107 -0
  43. data/lib/blacklight/document/extensions.rb +56 -0
  44. data/lib/blacklight/document/schema_org.rb +7 -0
  45. data/lib/blacklight/document/semantic_fields.rb +51 -0
  46. data/lib/blacklight/document/sms.rb +14 -0
  47. data/lib/blacklight/document_presenter.rb +3 -3
  48. data/lib/blacklight/exceptions.rb +9 -2
  49. data/lib/blacklight/facet.rb +21 -16
  50. data/lib/blacklight/request_builders.rb +60 -284
  51. data/lib/blacklight/routes.rb +1 -1
  52. data/lib/blacklight/search_builder.rb +130 -0
  53. data/lib/blacklight/search_helper.rb +316 -0
  54. data/lib/blacklight/solr.rb +1 -0
  55. data/lib/blacklight/solr/document.rb +4 -187
  56. data/lib/blacklight/solr/document/dublin_core.rb +3 -37
  57. data/lib/blacklight/solr/document/email.rb +4 -13
  58. data/lib/blacklight/solr/document/export.rb +3 -103
  59. data/lib/blacklight/solr/document/extensions.rb +4 -52
  60. data/lib/blacklight/solr/document/more_like_this.rb +1 -1
  61. data/lib/blacklight/solr/document/schema_org.rb +4 -4
  62. data/lib/blacklight/solr/document/sms.rb +4 -11
  63. data/lib/blacklight/solr/facet_paginator.rb +2 -2
  64. data/lib/blacklight/solr/search_builder.rb +264 -0
  65. data/lib/blacklight/solr_helper.rb +6 -261
  66. data/lib/blacklight/solr_repository.rb +30 -24
  67. data/lib/blacklight/solr_response.rb +3 -3
  68. data/lib/blacklight/user.rb +1 -2
  69. data/lib/blacklight/utils.rb +0 -23
  70. data/lib/generators/blacklight/controller_generator.rb +38 -0
  71. data/lib/generators/blacklight/document_generator.rb +20 -0
  72. data/lib/generators/blacklight/install_generator.rb +38 -39
  73. data/lib/generators/blacklight/models_generator.rb +2 -62
  74. data/lib/generators/blacklight/templates/catalog_controller.rb +3 -4
  75. data/lib/generators/blacklight/templates/config/{solr.yml → blacklight.yml} +3 -0
  76. data/lib/generators/blacklight/templates/config/jetty.yml +0 -3
  77. data/lib/generators/blacklight/templates/solr_document.rb +6 -6
  78. data/lib/generators/blacklight/test_support_generator.rb +1 -6
  79. data/lib/generators/blacklight/user_generator.rb +59 -0
  80. data/lib/railties/blacklight.rake +16 -7
  81. data/spec/controllers/catalog_controller_spec.rb +9 -15
  82. data/spec/features/facets_spec.rb +8 -0
  83. data/spec/helpers/configuration_helper_spec.rb +6 -13
  84. data/spec/helpers/facets_helper_spec.rb +3 -2
  85. data/spec/lib/blacklight/configuration_spec.rb +11 -38
  86. data/spec/lib/blacklight/{solr/document → document}/dublin_core_spec.rb +4 -4
  87. data/spec/lib/blacklight/{solr/document → document}/email_spec.rb +2 -2
  88. data/spec/lib/blacklight/{solr/document → document}/sms_spec.rb +2 -2
  89. data/spec/lib/blacklight/search_builder_spec.rb +145 -0
  90. data/spec/lib/blacklight/search_helper_spec.rb +775 -0
  91. data/spec/lib/blacklight/solr/document/more_like_this_spec.rb +1 -1
  92. data/spec/lib/blacklight/solr/search_builder_spec.rb +561 -0
  93. data/spec/lib/blacklight/solr_helper_spec.rb +5 -1291
  94. data/spec/lib/blacklight/solr_repository_spec.rb +13 -13
  95. data/spec/models/record_mailer_spec.rb +2 -2
  96. data/spec/spec_helper.rb +1 -0
  97. data/spec/test_app_templates/lib/generators/test_app_generator.rb +1 -1
  98. data/spec/views/catalog/_constraints.html.erb_spec.rb +1 -1
  99. data/spec/views/catalog/_paginate_compact.html.erb_spec.rb +2 -2
  100. data/spec/views/catalog/index.atom.builder_spec.rb +1 -1
  101. data/tasks/blacklight.rake +1 -1
  102. data/template.demo.rb +1 -1
  103. metadata +33 -45
  104. data/doc/Adding-new-document-actions.md +0 -94
  105. data/doc/Atom-Responses.md +0 -90
  106. data/doc/Blacklight-Add-ons.md +0 -23
  107. data/doc/Blacklight-configuration.md +0 -411
  108. data/doc/Blacklight-on-Heroku.md +0 -100
  109. data/doc/Blacklight-out-of-the-box.md +0 -47
  110. data/doc/Bookmarks.md +0 -1
  111. data/doc/Code4Lib-2014.md +0 -94
  112. data/doc/Configuration---Facet-Fields.md +0 -130
  113. data/doc/Configuration---Results-View.md +0 -224
  114. data/doc/Configuration---Solr-fields.md +0 -106
  115. data/doc/Configuring-and-Customizing-Blacklight.md +0 -257
  116. data/doc/Configuring-rails-routes.md +0 -13
  117. data/doc/Contributing-to-Blacklight.md +0 -43
  118. data/doc/Examples.md +0 -120
  119. data/doc/Extending-or-Modifying-Blacklight-Search-Behavior.md +0 -141
  120. data/doc/Home.md +0 -100
  121. data/doc/How-to-release-a-version.md +0 -45
  122. data/doc/Indexing-your-data-into-solr.md +0 -36
  123. data/doc/Internationalization.md +0 -32
  124. data/doc/JSON-API.md +0 -83
  125. data/doc/Pagination.md +0 -52
  126. data/doc/Providing-your-own-view-templates.md +0 -69
  127. data/doc/Quickstart.md +0 -153
  128. data/doc/README_SOLR.md +0 -245
  129. data/doc/Saved-Searches.md +0 -5
  130. data/doc/Solr-Configuration.md +0 -154
  131. data/doc/Sunspot-for-indexing.md +0 -46
  132. data/doc/Support.md +0 -33
  133. data/doc/Theming.md +0 -62
  134. data/doc/Understanding-Rails-and-Blacklight.md +0 -75
  135. data/doc/User-Authentication.md +0 -60
  136. data/doc/_Sidebar.md +0 -9
  137. data/doc/testing.md +0 -58
@@ -11,7 +11,7 @@ describe Blacklight::Solr::Document::MoreLikeThis do
11
11
  mock_solr_response = double(:more_like => [{'id' => 'abc'}])
12
12
  result = @mock_class.new({:id => '123'}, mock_solr_response).more_like_this
13
13
  expect(result).to have(1).item
14
- expect(result.first).to be_a_kind_of(SolrDocument)
14
+ expect(result.first).to be_a_kind_of(@mock_class)
15
15
  expect(result.first.id).to eq 'abc'
16
16
  expect(result.first.solr_response).to eq mock_solr_response
17
17
  end
@@ -0,0 +1,561 @@
1
+ require 'spec_helper'
2
+
3
+ describe Blacklight::Solr::SearchBuilder do
4
+ let(:single_facet) { { format: 'Book' } }
5
+ let(:multi_facets) { { format: 'Book', language_facet: 'Tibetan' } }
6
+ let(:mult_word_query) { 'tibetan history' }
7
+ let(:subject_search_params) { { commit: "search", search_field: "subject", action: "index", controller: "catalog", rows: "10", q: "wome" } }
8
+
9
+ let(:blacklight_config) { CatalogController.blacklight_config.deep_copy }
10
+ let(:method_chain) { CatalogController.search_params_logic }
11
+ let(:user_params) { Hash.new }
12
+ let(:context) { CatalogController.new }
13
+
14
+ before { allow(context).to receive(:blacklight_config).and_return(blacklight_config) }
15
+
16
+ let(:search_builder) { described_class.new(method_chain, context) }
17
+
18
+ subject { search_builder.with(user_params) }
19
+
20
+ context "with a complex parameter environment" do
21
+ subject { search_builder.with(user_params).processed_parameters }
22
+
23
+ let(:user_params) do
24
+ {:search_field => "test_field", :q => "test query", "facet.field" => "extra_facet"}
25
+ end
26
+
27
+ let(:blacklight_config) do
28
+ Blacklight::Configuration.new.tap do |config|
29
+ config.add_search_field("test_field",
30
+ :display_label => "Test",
31
+ :key=>"test_field",
32
+ :solr_parameters => {
33
+ :qf => "fieldOne^2.3 fieldTwo fieldThree^0.4",
34
+ :pf => "",
35
+ :spellcheck => 'false',
36
+ :rows => "55",
37
+ :sort => "request_params_sort" }
38
+ )
39
+ end
40
+ end
41
+ it "should merge parameters from search_field definition" do
42
+ expect(subject[:qf]).to eq "fieldOne^2.3 fieldTwo fieldThree^0.4"
43
+ expect(subject[:spellcheck]).to eq 'false'
44
+ end
45
+ it "should merge empty string parameters from search_field definition" do
46
+ expect(subject[:pf]).to eq ""
47
+ end
48
+
49
+ describe "should respect proper precedence of settings, " do
50
+ it "should not put :search_field in produced params" do
51
+ expect(subject[:search_field]).to be_nil
52
+ end
53
+
54
+ it "should fall through to BL general defaults for qt not otherwise specified " do
55
+ expect(subject[:qt]).to eq blacklight_config[:default_solr_params][:qt]
56
+ end
57
+
58
+ it "should take rows from search field definition where specified" do
59
+ expect(subject[:rows]).to eq "55"
60
+ end
61
+
62
+ it "should take q from request params" do
63
+ expect(subject[:q]).to eq "test query"
64
+ end
65
+
66
+ it "should add in extra facet.field from params" do
67
+ expect(subject[:"facet.field"]).to include("extra_facet")
68
+ end
69
+ end
70
+ end
71
+
72
+ # SPECS for actual search parameter generation
73
+ describe "#processed_parameters" do
74
+ subject do
75
+ Deprecation.silence(Blacklight::SearchBuilder) do
76
+ search_builder.with(user_params).processed_parameters
77
+ end
78
+ end
79
+
80
+ context "when search_params_logic is customized" do
81
+ let(:method_chain) { [:add_foo_to_solr_params] }
82
+
83
+ it "allows customization of search_params_logic" do
84
+ # Normally you'd include a new module into (eg) your CatalogController
85
+ # but a sub-class defininig it directly is simpler for test.
86
+ allow(context).to receive(:add_foo_to_solr_params) do |solr_params, user_params|
87
+ solr_params[:wt] = "TESTING"
88
+ end
89
+
90
+ expect(subject[:wt]).to eq "TESTING"
91
+ end
92
+ end
93
+
94
+ it "should generate a facet limit" do
95
+ expect(subject[:"f.subject_topic_facet.facet.limit"]).to eq 21
96
+ end
97
+
98
+ it "should handle no facet_limits in config" do
99
+ blacklight_config.facet_fields = {}
100
+ expect(subject).not_to have_key(:"f.subject_topic_facet.facet.limit")
101
+ end
102
+
103
+ describe "with max per page enforced" do
104
+ let(:blacklight_config) do
105
+ Blacklight::Configuration.new.tap do |config|
106
+ config.max_per_page = 123
107
+ end
108
+ end
109
+
110
+ let(:user_params) { { per_page: 98765 } }
111
+ it "should enforce max_per_page against all parameters" do
112
+ expect(blacklight_config.max_per_page).to eq 123
113
+ expect(subject[:rows]).to eq 123
114
+ end
115
+ end
116
+
117
+ describe 'for an entirely empty search' do
118
+
119
+ it 'should not have a q param' do
120
+ expect(subject[:q]).to be_nil
121
+ expect(subject["spellcheck.q"]).to be_nil
122
+ end
123
+ it 'should have default rows' do
124
+ expect(subject[:rows]).to eq 10
125
+ end
126
+ it 'should have default facet fields' do
127
+ # remove local params from the facet.field
128
+ expect(subject[:"facet.field"].map { |x| x.gsub(/\{![^}]+\}/, '') }).to match_array ["format", "subject_topic_facet", "pub_date", "language_facet", "lc_1letter_facet", "subject_geo_facet", "subject_era_facet"]
129
+ end
130
+
131
+ it "should have default qt" do
132
+ expect(subject[:qt]).to eq "search"
133
+ end
134
+ it "should have no fq" do
135
+ expect(subject[:phrase_filters]).to be_blank
136
+ expect(subject[:fq]).to be_blank
137
+ end
138
+ end
139
+
140
+
141
+ describe "for an empty string search" do
142
+ let(:user_params) { { q: "" } }
143
+ it "should return empty string q in solr parameters" do
144
+ expect(subject[:q]).to eq ""
145
+ end
146
+ end
147
+
148
+ describe "for request params also passed in as argument" do
149
+ let(:user_params) { { q: "some query", 'q' => 'another value' } }
150
+ it "should only have one value for the key 'q' regardless if a symbol or string" do
151
+ expect(subject[:q]).to eq 'some query'
152
+ expect(subject['q']).to eq 'some query'
153
+ end
154
+ end
155
+
156
+
157
+ describe "for one facet, no query" do
158
+ let(:user_params) { { f: single_facet } }
159
+ it "should have proper solr parameters" do
160
+
161
+ expect(subject[:q]).to be_blank
162
+ expect(subject["spellcheck.q"]).to be_blank
163
+
164
+ single_facet.each_value do |value|
165
+ expect(subject[:fq]).to include("{!raw f=#{single_facet.keys[0]}}#{value}")
166
+ end
167
+ end
168
+ end
169
+
170
+ describe "for an empty facet limit param" do
171
+ let(:user_params) { { f: { "format" => [""] } } }
172
+ it "should not add any fq to solr" do
173
+ expect(subject[:fq]).to be_blank
174
+ end
175
+ end
176
+
177
+ describe "with Multi Facets, No Query" do
178
+ let(:user_params) { { f: multi_facets } }
179
+ it 'should have fq set properly' do
180
+ multi_facets.each_pair do |facet_field, value_list|
181
+ value_list ||= []
182
+ value_list = [value_list] unless value_list.respond_to? :each
183
+ value_list.each do |value|
184
+ expect(subject[:fq]).to include("{!raw f=#{facet_field}}#{value}" )
185
+ end
186
+ end
187
+
188
+ end
189
+ end
190
+
191
+ describe "with Multi Facets, Multi Word Query" do
192
+ let(:user_params) { { q: mult_word_query, f: multi_facets } }
193
+ it 'should have fq and q set properly' do
194
+ multi_facets.each_pair do |facet_field, value_list|
195
+ value_list ||= []
196
+ value_list = [value_list] unless value_list.respond_to? :each
197
+ value_list.each do |value|
198
+ expect(subject[:fq]).to include("{!raw f=#{facet_field}}#{value}" )
199
+ end
200
+ end
201
+ expect(subject[:q]).to eq mult_word_query
202
+ end
203
+ end
204
+
205
+
206
+ describe "solr parameters for a field search from config (subject)" do
207
+ let(:user_params) { subject_search_params }
208
+
209
+ it "should look up qt from field definition" do
210
+ expect(subject[:qt]).to eq "search"
211
+ end
212
+
213
+ it "should not include weird keys not in field definition" do
214
+ expect(subject[:phrase_filters]).to be_nil
215
+ expect(subject[:fq]).to eq []
216
+ expect(subject[:commit]).to be_nil
217
+ expect(subject[:action]).to be_nil
218
+ expect(subject[:controller]).to be_nil
219
+ end
220
+
221
+ it "should include proper 'q', possibly with LocalParams" do
222
+ expect(subject[:q]).to match(/(\{[^}]+\})?wome/)
223
+ end
224
+ it "should include proper 'q' when LocalParams are used" do
225
+ if subject[:q] =~ /\{[^}]+\}/
226
+ expect(subject[:q]).to match(/\{[^}]+\}wome/)
227
+ end
228
+ end
229
+ it "should include spellcheck.q, without LocalParams" do
230
+ expect(subject["spellcheck.q"]).to eq "wome"
231
+ end
232
+
233
+ it "should include spellcheck.dictionary from field def solr_parameters" do
234
+ expect(subject[:"spellcheck.dictionary"]).to eq "subject"
235
+ end
236
+ it "should add on :solr_local_parameters using Solr LocalParams style" do
237
+ #q == "{!pf=$subject_pf $qf=subject_qf} wome", make sure
238
+ #the LocalParams are really there
239
+ subject[:q] =~ /^\{!([^}]+)\}/
240
+ key_value_pairs = $1.split(" ")
241
+ expect(key_value_pairs).to include("pf=$subject_pf")
242
+ expect(key_value_pairs).to include("qf=$subject_qf")
243
+ end
244
+ end
245
+
246
+ describe "overriding of qt parameter" do
247
+ let(:user_params) do
248
+ { qt: 'overridden' }
249
+ end
250
+
251
+ it "should return the correct overriden parameter" do
252
+ expect(subject[:qt]).to eq "overridden"
253
+ end
254
+ end
255
+
256
+
257
+ describe "sorting" do
258
+ it "should send the default sort parameter to solr" do
259
+ expect(subject[:sort]).to eq 'score desc, pub_date_sort desc, title_sort asc'
260
+ end
261
+
262
+ it "should not send a sort parameter to solr if the sort value is blank" do
263
+ blacklight_config.sort_fields = {}
264
+ blacklight_config.add_sort_field('', :label => 'test')
265
+
266
+ expect(subject).not_to have_key(:sort)
267
+ end
268
+
269
+ context "when the user provides sort parmeters" do
270
+ let(:user_params) { { sort: 'solr_test_field desc' } }
271
+ it "passes them through" do
272
+ expect(subject[:sort]).to eq 'solr_test_field desc'
273
+ end
274
+ end
275
+ end
276
+
277
+ describe "for :solr_local_parameters config" do
278
+ let(:blacklight_config) do
279
+ config = Blacklight::Configuration.new
280
+ config.add_search_field(
281
+ "custom_author_key",
282
+ :display_label => "Author",
283
+ :qt => "author_qt",
284
+ :key => "custom_author_key",
285
+ :solr_local_parameters => {
286
+ :qf => "$author_qf",
287
+ :pf => "you'll have \" to escape this",
288
+ :pf2 => "$pf2_do_not_escape_or_quote"
289
+ },
290
+ :solr_parameters => {
291
+ :qf => "someField^1000",
292
+ :ps => "2"
293
+ }
294
+ )
295
+ return config
296
+ end
297
+
298
+ let(:user_params) { { search_field: "custom_author_key", q: "query" } }
299
+
300
+ it "should pass through ordinary params" do
301
+ expect(subject[:qt]).to eq "author_qt"
302
+ expect(subject[:ps]).to eq "2"
303
+ expect(subject[:qf]).to eq "someField^1000"
304
+ end
305
+
306
+ it "should include include local params with escaping" do
307
+ expect(subject[:q]).to include('qf=$author_qf')
308
+ expect(subject[:q]).to include('pf=\'you\\\'ll have \\" to escape this\'')
309
+ expect(subject[:q]).to include('pf2=$pf2_do_not_escape_or_quote')
310
+ end
311
+ end
312
+
313
+ describe "mapping facet.field" do
314
+ let(:blacklight_config) do
315
+ Blacklight::Configuration.new do |config|
316
+ config.add_facet_field 'some_field'
317
+ config.add_facet_fields_to_solr_request!
318
+ end
319
+ end
320
+
321
+ context "user provides a single facet.field" do
322
+ let(:user_params) { { "facet.field" => "additional_facet" } }
323
+ it "adds the field" do
324
+ expect(subject[:"facet.field"]).to include("additional_facet")
325
+ expect(subject[:"facet.field"]).to have(2).fields
326
+ end
327
+ end
328
+
329
+ context "user provides a multiple facet.field" do
330
+ let(:user_params) { { "facet.field" => ["add_facet1", "add_facet2"] } }
331
+ it "adds the fields" do
332
+ expect(subject[:"facet.field"]).to include("add_facet1")
333
+ expect(subject[:"facet.field"]).to include("add_facet2")
334
+ expect(subject[:"facet.field"]).to have(3).fields
335
+ end
336
+ end
337
+
338
+ context "user provides a multiple facets" do
339
+ let(:user_params) { { "facets" => ["add_facet1", "add_facet2"] } }
340
+ it "adds the fields" do
341
+ expect(subject[:"facet.field"]).to include("add_facet1")
342
+ expect(subject[:"facet.field"]).to include("add_facet2")
343
+ expect(subject[:"facet.field"]).to have(3).fields
344
+ end
345
+ end
346
+ end
347
+ end
348
+
349
+
350
+ describe "#facet_value_to_fq_string" do
351
+
352
+ it "should use the raw handler for strings" do
353
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name}my value"
354
+ end
355
+
356
+ it "should pass booleans through" do
357
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "facet_name:true"
358
+ end
359
+
360
+ it "should pass boolean-like strings through" do
361
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "true")).to eq "facet_name:true"
362
+ end
363
+
364
+ it "should pass integers through" do
365
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", 1)).to eq "facet_name:1"
366
+ end
367
+
368
+ it "should pass integer-like strings through" do
369
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "1")).to eq "facet_name:1"
370
+ end
371
+
372
+ it "should pass floats through" do
373
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", 1.11)).to eq "facet_name:1.11"
374
+ end
375
+
376
+ it "should pass floats through" do
377
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "1.11")).to eq "facet_name:1.11"
378
+ end
379
+
380
+ it "should escape negative integers" do
381
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", -1)).to eq "facet_name:\\-1"
382
+ end
383
+
384
+ it "should pass date-type fields through" do
385
+ allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil))
386
+
387
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "2012-01-01")).to eq "facet_name:2012\\-01\\-01"
388
+ end
389
+
390
+ it "should escape datetime-type fields" do
391
+ allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => true, :query => nil, :tag => nil))
392
+
393
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "2003-04-09T00:00:00Z")).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z"
394
+ end
395
+
396
+ it "should format Date objects correctly" do
397
+ allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:date => nil, :query => nil, :tag => nil))
398
+ d = DateTime.parse("2003-04-09T00:00:00")
399
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", d)).to eq "facet_name:2003\\-04\\-09T00\\:00\\:00Z"
400
+ end
401
+
402
+ it "should handle range requests" do
403
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", 1..5)).to eq "facet_name:[1 TO 5]"
404
+ end
405
+
406
+ it "should add tag local parameters" do
407
+ allow(blacklight_config.facet_fields).to receive(:[]).with('facet_name').and_return(double(:query => nil, :tag => 'asdf', :date => nil))
408
+
409
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", true)).to eq "{!tag=asdf}facet_name:true"
410
+ expect(subject.send(:facet_value_to_fq_string, "facet_name", "my value")).to eq "{!raw f=facet_name tag=asdf}my value"
411
+ end
412
+ end
413
+
414
+ describe "#add_facet_fq_to_solr" do
415
+ it "converts a String fq into an Array" do
416
+ solr_parameters = {:fq => 'a string' }
417
+
418
+ subject.add_facet_fq_to_solr(solr_parameters)
419
+
420
+ expect(solr_parameters[:fq]).to be_a_kind_of Array
421
+ end
422
+ end
423
+
424
+ describe "#add_solr_fields_to_query" do
425
+ let(:blacklight_config) do
426
+ config = Blacklight::Configuration.new do |config|
427
+
428
+ config.add_index_field 'an_index_field', solr_params: { 'hl.alternativeField' => 'field_x'}
429
+ config.add_show_field 'a_show_field', solr_params: { 'hl.alternativeField' => 'field_y'}
430
+ config.add_field_configuration_to_solr_request!
431
+ end
432
+ end
433
+
434
+ let(:solr_parameters) do
435
+ solr_parameters = Blacklight::Solr::Request.new
436
+
437
+ subject.add_solr_fields_to_query(solr_parameters)
438
+
439
+ solr_parameters
440
+ end
441
+
442
+ it "should add any extra solr parameters from index and show fields" do
443
+ expect(solr_parameters[:'f.an_index_field.hl.alternativeField']).to eq "field_x"
444
+ expect(solr_parameters[:'f.a_show_field.hl.alternativeField']).to eq "field_y"
445
+ end
446
+ end
447
+
448
+ describe "#add_facetting_to_solr" do
449
+
450
+ let(:blacklight_config) do
451
+ config = Blacklight::Configuration.new
452
+
453
+ config.add_facet_field 'test_field', :sort => 'count'
454
+ config.add_facet_field 'some-query', :query => {'x' => {:fq => 'some:query' }}, :ex => 'xyz'
455
+ config.add_facet_field 'some-pivot', :pivot => ['a','b'], :ex => 'xyz'
456
+ config.add_facet_field 'some-field', solr_params: { 'facet.mincount' => 15 }
457
+ config.add_facet_fields_to_solr_request!
458
+
459
+ config
460
+ end
461
+
462
+ let(:solr_parameters) do
463
+ solr_parameters = Blacklight::Solr::Request.new
464
+
465
+ subject.add_facetting_to_solr(solr_parameters)
466
+
467
+ solr_parameters
468
+ end
469
+
470
+ it "should add sort parameters" do
471
+ expect(solr_parameters[:facet]).to be true
472
+
473
+ expect(solr_parameters[:'facet.field']).to include('test_field')
474
+ expect(solr_parameters[:'f.test_field.facet.sort']).to eq 'count'
475
+ end
476
+
477
+ it "should add facet exclusions" do
478
+ expect(solr_parameters[:'facet.query']).to include('{!ex=xyz}some:query')
479
+ expect(solr_parameters[:'facet.pivot']).to include('{!ex=xyz}a,b')
480
+ end
481
+
482
+ it "should add any additional solr_params" do
483
+ expect(solr_parameters[:'f.some-field.facet.mincount']).to eq 15
484
+ end
485
+
486
+ describe ":include_in_request" do
487
+ let(:solr_parameters) do
488
+ solr_parameters = Blacklight::Solr::Request.new
489
+ subject.add_facetting_to_solr(solr_parameters)
490
+ solr_parameters
491
+ end
492
+
493
+ it "should respect the include_in_request parameter" do
494
+ blacklight_config.add_facet_field 'yes_facet', include_in_request: true
495
+ blacklight_config.add_facet_field 'no_facet', include_in_request: false
496
+
497
+ expect(solr_parameters[:'facet.field']).to include('yes_facet')
498
+ expect(solr_parameters[:'facet.field']).not_to include('no_facet')
499
+ end
500
+
501
+ it "should default to including facets if add_facet_fields_to_solr_request! was called" do
502
+ blacklight_config.add_facet_field 'yes_facet'
503
+ blacklight_config.add_facet_field 'no_facet', include_in_request: false
504
+ blacklight_config.add_facet_fields_to_solr_request!
505
+
506
+ expect(solr_parameters[:'facet.field']).to include('yes_facet')
507
+ expect(solr_parameters[:'facet.field']).not_to include('no_facet')
508
+ end
509
+ end
510
+
511
+ describe ":add_facet_fields_to_solr_request!" do
512
+
513
+ let(:blacklight_config) do
514
+ config = Blacklight::Configuration.new
515
+ config.add_facet_field 'yes_facet', include_in_request: true
516
+ config.add_facet_field 'no_facet', include_in_request: false
517
+ config.add_facet_field 'maybe_facet'
518
+ config.add_facet_field 'another_facet'
519
+ config
520
+ end
521
+
522
+ let(:solr_parameters) do
523
+ solr_parameters = Blacklight::Solr::Request.new
524
+ subject.add_facetting_to_solr(solr_parameters)
525
+ solr_parameters
526
+ end
527
+
528
+ it "should add facets to the solr request" do
529
+ blacklight_config.add_facet_fields_to_solr_request!
530
+ expect(solr_parameters[:'facet.field']).to match_array ['yes_facet', 'maybe_facet', 'another_facet']
531
+ end
532
+
533
+ it "should not override field-specific configuration by default" do
534
+ blacklight_config.add_facet_fields_to_solr_request!
535
+ expect(solr_parameters[:'facet.field']).to_not include 'no_facet'
536
+ end
537
+
538
+ it "should allow white-listing facets" do
539
+ blacklight_config.add_facet_fields_to_solr_request! 'maybe_facet'
540
+ expect(solr_parameters[:'facet.field']).to include 'maybe_facet'
541
+ expect(solr_parameters[:'facet.field']).not_to include 'another_facet'
542
+ end
543
+
544
+ it "should allow white-listed facets to override any field-specific include_in_request configuration" do
545
+ blacklight_config.add_facet_fields_to_solr_request! 'no_facet'
546
+ expect(solr_parameters[:'facet.field']).to include 'no_facet'
547
+ end
548
+ end
549
+ end
550
+
551
+ describe "#with_tag_ex" do
552
+ it "should add an !ex local parameter if the facet configuration requests it" do
553
+ expect(subject.with_ex_local_param("xyz", "some-value")).to eq "{!ex=xyz}some-value"
554
+ end
555
+
556
+ it "should not add an !ex local parameter if it isn't configured" do
557
+ mock_field = double()
558
+ expect(subject.with_ex_local_param(nil, "some-value")).to eq "some-value"
559
+ end
560
+ end
561
+ end