bento_search 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module BentoSearch
2
- VERSION = "1.5.0"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -1,10 +1,5 @@
1
- require 'rubygems'
2
- gemfile = File.expand_path('../../../../Gemfile', __FILE__)
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
3
3
 
4
- if File.exist?(gemfile)
5
- ENV['BUNDLE_GEMFILE'] = gemfile
6
- require 'bundler'
7
- Bundler.setup
8
- end
9
-
10
- $:.unshift File.expand_path('../../../../lib', __FILE__)
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,15 @@
1
+ # This file is auto-generated from the current state of the database. Instead
2
+ # of editing this file, please use the migrations feature of Active Record to
3
+ # incrementally modify your database, and then regenerate this schema definition.
4
+ #
5
+ # Note that this schema.rb definition is the authoritative source for your
6
+ # database schema. If you need to create the application database on another
7
+ # system, you should be using db:schema:load, not running all the migrations
8
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(version: 0) do
14
+
15
+ end
@@ -7,23 +7,23 @@ module BentoSearch
7
7
  config.engine = "MockEngine"
8
8
  config.allow_routable_results = true
9
9
  end
10
-
10
+
11
11
  BentoSearch.register_engine("failed_response") do |config|
12
12
  config.engine = "MockEngine"
13
13
  config.allow_routable_results = true
14
14
  config.error = {:message => "faked error"}
15
15
  end
16
-
16
+
17
17
  BentoSearch.register_engine("not_routable") do |config|
18
18
  config.engine = "MockEngine"
19
19
  # no allow_routable_results
20
20
  end
21
-
21
+
22
22
  BentoSearch.register_engine("with_layout_config") do |config|
23
23
  config.engine = "MockEngine"
24
-
24
+
25
25
  config.allow_routable_results = true
26
-
26
+
27
27
  config.for_display do |display|
28
28
  display.ajax do |ajax|
29
29
  ajax.wrapper_template = "bento_search/wrap_with_count"
@@ -31,119 +31,125 @@ module BentoSearch
31
31
  end
32
32
  end
33
33
  end
34
-
35
- def teardown
34
+
35
+ def teardown
36
36
  BentoSearch.reset_engine_registrations!
37
37
  end
38
-
39
-
38
+
39
+
40
40
  test "search" do
41
- get :search, {:engine_id => "mock", :query => "my search"}
41
+ get_search_with_params :engine_id => "mock", :query => "my search"
42
42
  assert_response :success
43
43
  assert_not_nil assigns(:results)
44
-
44
+
45
45
  assert_template "bento_search/search/search"
46
-
46
+
47
47
  # meta tag with count
48
48
  meta_tag = assert_select("meta[itemprop=total_items][content]", :count => 1 )
49
- assert_match /^\d+$/, meta_tag.attribute("content").text
49
+ assert_match(/^\d+$/, meta_tag.attribute("content").text)
50
50
  end
51
-
51
+
52
52
  test "failed search" do
53
- get :search, {:engine_id => "failed_response", :query => "my search"}
54
-
53
+ get_search_with_params :engine_id => "failed_response", :query => "my search"
54
+
55
55
  # should this really be a success? Yes, I think so, we don't
56
56
  # want to stop ajax from getting it, it'll just have an error
57
57
  # message in the HTML. Should it maybe have an html5 meta microdata
58
58
  # warning?
59
59
  assert_response :success
60
-
60
+
61
61
  assert_template "bento_search/search/search"
62
62
  assert_template "bento_search/_search_error"
63
-
63
+
64
64
  assert_select("meta[itemprop=total_items]", :count => 0)
65
65
  end
66
-
67
-
68
-
69
-
70
-
66
+
67
+
68
+
69
+
70
+
71
71
  test "custom layout config" do
72
- get :search, {:engine_id => "with_layout_config", :query => "my search"}
73
-
72
+ get_search_with_params :engine_id => "with_layout_config", :query => "my search"
73
+
74
74
  assert_response :success
75
-
75
+
76
76
  assert_not_nil assigns(:partial_wrapper)
77
-
77
+
78
78
  assert_template "bento_search/_wrap_with_count"
79
79
  assert_template "bento_search/search/search"
80
80
  end
81
-
81
+
82
82
  test "non-routable engine" do
83
- get :search, {:engine_id => "not_routable", :query => "my search"}
84
-
83
+ get_search_with_params :engine_id => "not_routable", :query => "my search"
84
+
85
85
  assert_response 403
86
86
  end
87
-
87
+
88
88
  test "non-existent engine" do
89
- get :search, {:engine_id => "not_existing", :query => "my search"}
90
-
89
+ get_search_with_params :engine_id => "not_existing", :query => "my search"
90
+
91
91
  assert_response 404
92
92
  end
93
-
94
-
93
+
94
+
95
95
  test "respects public_settable_search_args" do
96
- get :search, {:engine_id => "mock",
97
- 'query' => "query", 'sort' => "sort", 'per_page' => "15",
98
- 'page' => "6", 'search_field' => "title", 'not_allowed' => "not allowed"}
99
-
100
-
96
+ get_search_with_params :engine_id => "mock",
97
+ 'query' => "query", 'sort' => "sort", 'per_page' => "15",
98
+ 'page' => "6", 'search_field' => "title", 'not_allowed' => "not allowed"
99
+
100
+
101
101
  search_args = assigns[:engine].last_args
102
-
102
+
103
103
  [:query, :sort, :per_page, :page, :search_field].each do |allowed_key|
104
104
  assert search_args.has_key?(allowed_key)
105
105
  end
106
106
  assert ! search_args.has_key?(:not_allowed)
107
107
  assert ! search_args.has_key?("not_allowed")
108
108
  end
109
-
109
+
110
110
  test "custom before filter" do
111
111
  # Okay, we're going to do a weird thing with a custom controller subclass
112
- # we can add a custom before filter like a local app might.
112
+ # we can add a custom before filter like a local app might.
113
113
  #
114
- # SUPER HACKY, but I dunno what else to do.
115
-
114
+ # SUPER HACKY, but I dunno what else to do.
115
+
116
116
  class CustomSearchController < BentoSearch::SearchController
117
117
  before_filter :deny_everyone
118
-
118
+
119
119
  def deny_everyone
120
120
  raise BentoSearch::SearchController::AccessDenied
121
121
  end
122
122
  end
123
-
124
123
 
125
-
124
+
125
+
126
126
  orig_controller = @controller
127
-
127
+
128
128
  begin
129
129
  Rails.application.routes.draw do
130
130
  get "/custom_search" => "bento_search/search_controller_test/custom_search#search"
131
131
  end
132
132
  @controller = CustomSearchController.new
133
-
134
- get :search, {:engine_id => "mock", :query => "my search"}
135
-
133
+
134
+ get_search_with_params :engine_id => "mock", :query => "my search"
135
+
136
136
  assert_response 403
137
137
  ensure
138
138
  @controller = orig_controller
139
139
  Rails.application.reload_routes!
140
140
  end
141
-
141
+
142
+ end
143
+
144
+
145
+ # Do rails4 or rails5
146
+ def get_search_with_params(params)
147
+ if Gem::Version.new(Rails.version) >= Gem::Version.new(5.0)
148
+ get :search, params: params
149
+ else
150
+ get :search, params
151
+ end
142
152
  end
143
-
144
153
 
145
-
146
-
147
-
148
154
  end
149
155
  end
@@ -1,246 +1,246 @@
1
1
  # encoding: UTF-8
2
+ require 'nokogiri'
2
3
 
3
4
  require 'test_helper'
4
5
 
5
6
  class BentoSearchHelperTest < ActionView::TestCase
6
7
  include BentoSearchHelper
7
-
8
+
8
9
  MockEngine = BentoSearch::MockEngine
9
-
10
+
10
11
  def teardown
11
12
  BentoSearch.reset_engine_registrations!
12
13
  end
13
-
14
+
14
15
  def setup
15
16
  # Make routing work
16
- @routes = Rails.application.routes
17
-
17
+ @routes = Rails.application.routes
18
+
18
19
  end
19
-
20
+
20
21
  def test_with_results_arg
21
22
  results = MockEngine.new.search(:query => "foo")
22
- bento_search(results)
23
-
23
+ bento_search(results)
24
+
24
25
  assert_select("div.bento_item", 10)
25
26
  end
26
-
27
+
27
28
  def test_custom_partial
28
29
  results = MockEngine.new(:for_display => {:item_partial => "test_custom_item_partial"})
29
-
30
+
30
31
  bento_search(results)
31
-
32
+
32
33
  assert_select("p.custom_item_partial")
33
34
  end
34
-
35
+
35
36
  def test_with_failed_search
36
37
  results = BentoSearch::Results.new
37
38
  results.error = {:error => true}
38
-
39
+
39
40
  assert results.failed?
40
-
41
- response = HTML::Document.new(bento_search(results))
42
-
43
- assert (no_results_div = response.find(:attributes => {:class => "bento_search_error alert alert-error"})), "has search_error div"
44
-
45
- assert no_results_div.match(Regexp.new I18n.translate("bento_search.search_error")), "has error message"
46
41
 
47
- assert_nil response.find(:attributes => {:class => "bento_item"})
42
+ response = Nokogiri::HTML(bento_search(results))
43
+
44
+ assert (no_results_div = response.at_css(".bento_search_error.alert.alert-error")), "has search_error div"
45
+
46
+ assert no_results_div.text =~ Regexp.new(I18n.translate("bento_search.search_error")), "has error message"
47
+
48
+ assert_nil response.at_css(".bento_item")
48
49
  end
49
-
50
+
50
51
  def test_with_empty_results
51
52
  results = MockEngine.new(:num_results => 0).search(:query => "foo")
52
-
53
- response = HTML::Document.new(bento_search(results))
54
-
55
- assert (no_results_div = response.find(:attributes => {:class => "bento_search_no_results"})), "has no_results div"
56
- assert no_results_div.match(Regexp.new(I18n.translate("bento_search.no_results")))
57
53
 
58
-
59
- assert_nil response.find(:attributes => {:class => "bento_item"}), "has no results message"
54
+ response = Nokogiri::HTML(bento_search(results))
55
+
56
+ assert (no_results_div = response.at_css(".bento_search_no_results")), "has no_results div"
57
+ assert no_results_div.text =~ (Regexp.new(I18n.translate("bento_search.no_results")))
58
+
59
+ assert_nil response.at_css(".bento_item"), "has no results message"
60
60
  end
61
-
61
+
62
62
  def test_with_engine_arg
63
63
  engine = MockEngine.new
64
64
  bento_search(engine, :query => "QUERY")
65
-
65
+
66
66
  assert_select("div.bento_item", 10).each_with_index do |node, i|
67
67
  assert_select node, "*", /QUERY/
68
68
  assert_select node, "*", /#{i +1 }/
69
- end
69
+ end
70
70
  end
71
-
71
+
72
72
  def test_with_registered_id
73
73
  BentoSearch.register_engine("test_engine") do |conf|
74
74
  conf.engine = "MockEngine"
75
75
  end
76
-
76
+
77
77
  bento_search("test_engine", :query => "QUERY")
78
-
78
+
79
79
  assert_select("div.bento_item", 10).each_with_index do |node, i|
80
80
  assert_select node, "*", /QUERY/
81
- assert_select node, "*", /#{i +1 }/
82
- end
81
+ assert_select node, "*", /#{i +1 }/
82
+ end
83
83
  end
84
-
84
+
85
85
  def test_ajax_load_without_registration
86
86
  assert_raises(ArgumentError) { bento_search(MockEngine.new, :load => :ajax_auto) }
87
87
  end
88
-
89
88
 
90
-
91
- def test_ajax_load
89
+
90
+
91
+ def test_ajax_load
92
92
  BentoSearch.register_engine("test_engine") do |conf|
93
93
  conf.engine = "MockEngine"
94
94
  end
95
-
95
+
96
96
  results = bento_search("test_engine", :query => "QUERY", :load => :ajax_auto)
97
- results = HTML::Document.new(results)
98
-
99
-
100
- div = results.find(:attributes => {:class => "bento_search_ajax_wait"})
97
+ results = Nokogiri::HTML(results)
98
+
99
+
100
+ div = results.at_css(".bento_search_ajax_wait")
101
101
  assert div, "produces div.bento_search_ajax_wait"
102
+
102
103
  assert_equal "ajax_auto", div["data-bento-search-load"], "has data-bento-search-load attribute"
103
104
 
104
-
105
- assert_present div.attributes["data-bento-ajax-url"]
106
- url = URI.parse(div.attributes["data-bento-ajax-url"])
105
+ assert_present div["data-bento-ajax-url"]
106
+ url = URI.parse(div["data-bento-ajax-url"])
107
107
  assert_equal "/bento/test_engine", url.path
108
-
108
+
109
109
  query = CGI.parse(url.query.gsub("&amp;", "&")) # gsub weirdness of HTML::Tag
110
110
  assert_equal ["QUERY"], query["query"]
111
111
  assert_empty query["load"]
112
-
112
+
113
113
  # hidden loading msg
114
- loading_msg = div.find(:attributes => {:class => "bento_search_ajax_loading"})
114
+ loading_msg = div.at_css(".bento_search_ajax_loading")
115
115
  assert_present loading_msg, "bento_search_ajax_loading present"
116
116
  assert_match /display\:none/, loading_msg["style"], "loading has CSS style hidden"
117
-
118
- assert div.find(:tag => "noscript"), "has <noscript> tag"
119
-
120
- assert (img = loading_msg.find(:tag => "img")), "Has spinner gif"
121
- assert_equal I18n.translate("bento_search.ajax_loading"), img.attributes["alt"]
117
+
118
+ assert div.at_css("noscript"), "has <noscript> tag"
119
+
120
+ assert (img = loading_msg.at_css("img")), "Has spinner gif"
121
+ assert_equal I18n.translate("bento_search.ajax_loading"), img["alt"]
122
122
  end
123
-
123
+
124
124
  def test_ajax_triggered_load
125
125
  BentoSearch.register_engine("test_engine") do |conf|
126
126
  conf.engine = "MockEngine"
127
127
  end
128
-
128
+
129
129
  results = bento_search("test_engine", :query => "QUERY", :load => :ajax_triggered)
130
- results = HTML::Document.new(results)
130
+ results = Nokogiri::HTML(results)
131
131
 
132
- div = results.find(:attributes => {:class => "bento_search_ajax_wait"})
132
+ div = results.at_css(".bento_search_ajax_wait")
133
133
  assert div, "produces div.bento_search_ajax_wait"
134
134
  assert_equal "ajax_triggered", div["data-bento-search-load"], "has data-bento-search-load attribute"
135
135
 
136
136
  # hidden loading msg
137
- loading_msg = div.find(:attributes => {:class => "bento_search_ajax_loading"})
137
+ loading_msg = div.at_css(".bento_search_ajax_loading")
138
138
  assert_present loading_msg, "bento_search_ajax_loading present"
139
139
  assert_match /display\:none/, loading_msg["style"], "loading has CSS style hidden"
140
140
  end
141
-
142
-
141
+
142
+
143
143
  def test_sort_hash_for
144
144
  tested_keys = %w{title_asc date_desc relevance author_asc}
145
-
145
+
146
146
  sort_definitions = {}
147
147
  tested_keys.each {|k| sort_definitions[k] = {}}
148
-
148
+
149
149
  engine = MockEngine.new(:sort_definitions => sort_definitions)
150
150
 
151
151
  hash = bento_sort_hash_for(engine)
152
-
152
+
153
153
  assert_present hash
154
-
154
+
155
155
  tested_keys.each do |key|
156
156
  assert_equal key, hash[ I18n.translate(key, :scope => "bento_search.sort_keys") ]
157
- end
157
+ end
158
158
  end
159
-
159
+
160
160
  def test_sort_hash_for_no_i18n
161
- # If there's no 18n key available, use reasonable humanized approximation
162
-
161
+ # If there's no 18n key available, use reasonable humanized approximation
162
+
163
163
  engine = MockEngine.new(:sort_definitions => {"no_key_test" => {}})
164
-
164
+
165
165
  hash = bento_sort_hash_for(engine)
166
-
166
+
167
167
  assert_present hash
168
-
168
+
169
169
  key = hash.key("no_key_test")
170
-
170
+
171
171
  assert_no_match /translation missing/, key
172
-
172
+
173
173
  assert_equal "No Key Test", key
174
174
  end
175
-
175
+
176
176
  def test_field_hash_for
177
177
  # generic
178
178
  hash = bento_field_hash_for(nil)
179
-
179
+
180
180
  assert_equal I18n.t("bento_search.search_fields").invert, hash
181
-
181
+
182
182
  # specific engine
183
183
  engine = MockEngine.new(:search_field_definitions => {
184
184
  :mytitle => {:semantic => :title},
185
185
  :myauthor => {:semantic => :author},
186
186
  :myissn => {:semantic => :issn},
187
- :mycustom => {}
188
- })
187
+ :mycustom => {}
188
+ })
189
189
  hash = bento_field_hash_for(engine)
190
190
  expected = { I18n.t("bento_search.search_fields.title") => 'title',
191
191
  I18n.t("bento_search.search_fields.author") => 'author',
192
- I18n.t("bento_search.search_fields.issn") => 'issn',
192
+ I18n.t("bento_search.search_fields.issn") => 'issn',
193
193
  }
194
194
  assert_equal expected, hash
195
-
195
+
196
196
  # only
197
197
  hash = bento_field_hash_for(engine, :only => :author)
198
198
  assert_equal( {"Author" => "author"}, hash )
199
199
  hash = bento_field_hash_for(engine, :only => ["author", "title"])
200
200
  assert_equal( {"Title" => "title", "Author" => "author"}, hash )
201
-
201
+
202
202
  # except
203
-
204
-
203
+
204
+
205
205
  end
206
-
206
+
207
207
  def test_bento_decorate
208
208
  item = BentoSearch::ResultItem.new(:title => "foo")
209
-
209
+
210
210
  decorated = bento_decorate(item)
211
-
211
+
212
212
  assert_kind_of BentoSearch::StandardDecorator, decorated
213
-
213
+
214
214
  assert_equal "foo", decorated.title
215
-
216
- assert decorated.send("_h").respond_to?(:url_for), "has ActionView helpers available internally"
215
+
216
+ assert decorated.send("_h").respond_to?(:url_for), "has ActionView helpers available internally"
217
217
  end
218
-
218
+
219
219
  def test_bento_decorate_with_yield
220
220
  item = BentoSearch::ResultItem.new(:title => "foo")
221
-
221
+
222
222
  got_here = false
223
-
223
+
224
224
  bento_decorate(item) do |decorated|
225
225
  got_here = true
226
226
  assert_equal "foo", decorated.title
227
227
  end
228
-
228
+
229
229
  assert got_here, "Yielded block is called"
230
-
230
+
231
231
  end
232
-
232
+
233
233
  class SomeDecorator < BentoSearch::StandardDecorator ; end
234
-
234
+
235
235
  def test_bento_decorate_with_custom_decorator
236
236
  item = BentoSearch::ResultItem.new(:title => "foo", :decorator => "BentoSearchHelperTest::SomeDecorator")
237
-
237
+
238
238
  decorated = bento_decorate(item)
239
-
239
+
240
240
  assert_kind_of BentoSearchHelperTest::SomeDecorator, decorated
241
241
  end
242
242
 
243
-
244
243
 
245
-
244
+
245
+
246
246
  end