-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_controller_test.rb
More file actions
611 lines (523 loc) · 20.9 KB
/
Copy pathsearch_controller_test.rb
File metadata and controls
611 lines (523 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
require 'test_helper'
class SearchControllerTest < ActionDispatch::IntegrationTest
def setup
@test_strategy = Flipflop::FeatureSet.current.test!
@test_strategy.switch!(:gdt, false)
end
test 'index shows basic search form by default' do
get '/'
assert_response :success
assert_select 'form#basic-search', { count: 1 }
details_div = assert_select('details#advanced-search-panel')
assert_nil details_div.attribute('open')
end
test 'index shows advanced search form with URL parameter' do
get '/?advanced=true'
assert_response :success
details_div = assert_select('details#advanced-search-panel')
assert details_div.attribute('open')
end
test 'advanced search form appears on results page with URL parameter' do
VCR.use_cassette('advanced',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?advanced=true'
assert_response :success
details_div = assert_select('details#advanced-search-panel')
assert details_div.attribute('open')
end
end
test 'search form includes a number of fields' do
get '/'
# Please note that this test confirms fields in the DOM - but not whether
# they are visible. Fields in a hidden details panel are still in the DOM,
# but not visible or reachable via keyboard interaction.
assert_select 'input#basic-search-main', { count: 1 }
assert_select 'input#advanced-citation', { count: 1 }
assert_select 'input#advanced-contributors', { count: 1 }
assert_select 'input#advanced-fundingInformation', { count: 1 }
assert_select 'input#advanced-identifiers', { count: 1 }
assert_select 'input#advanced-locations', { count: 1 }
assert_select 'input#advanced-subjects', { count: 1 }
assert_select 'input#advanced-title', { count: 1 }
assert_select 'input.source', { minimum: 3 }
end
test 'advanced search source checkboxes can be controlled by env' do
get '/'
assert_select 'input.source', { minimum: 3 }
ClimateControl.modify TIMDEX_SOURCES: 'HIGHLANDER' do
get '/'
assert_select 'input.source', { count: 1 }
end
ClimateControl.modify TIMDEX_SOURCES: 'SITH,LORDS' do
get '/'
assert_select 'input.source', { count: 2 }
end
end
test 'results with no query redirects with info' do
get '/results'
assert_response :redirect
assert_equal 'A search term is required.', flash[:error]
end
test 'results with blank query redirects with info' do
get '/results?q='
assert_response :redirect
assert_equal 'A search term is required.', flash[:error]
end
test 'results with blank-ish query redirects with info' do
get '/results?q=%20'
assert_response :redirect
assert_equal 'A search term is required.', flash[:error]
end
test 'results with valid query displays the query' do
VCR.use_cassette('timdex hallo',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=hallo'
assert_response :success
assert_nil flash[:error]
assert_select 'input[value=?]', 'hallo'
end
end
test 'results with valid query shows search form' do
VCR.use_cassette('timdex hallo',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=hallo'
assert_response :success
assert_select 'form#basic-search', { count: 1 }
end
end
test 'results with valid query populates search form with query' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '#basic-search-main[value=data]'
end
end
test 'results with valid query has div for hints if any fact panels are enabled' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: 'fakepanel') do
get '/results?q=data'
end
assert_response :success
assert_select '#hint'
end
end
test 'results with valid query has no div for hints if no fact panels are enabled' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: '') do
get '/results?q=data'
end
assert_response :success
assert_select('#hint', 0)
end
end
test 'results with valid query has div for filters which is populated' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '#filters'
assert_select '#filters .filter-category .filter-label', { minimum: 1 }
end
end
test 'a filter category lists available filters with names and values' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '.filter-options .category-terms .name', { minimum: 1 }
assert_select '.filter-options .category-terms .count', { minimum: 1 }
end
end
test 'results with valid query has div for pagination' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '#pagination'
end
end
test 'results with valid query has div for results which is populated' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '#results'
assert_select '#results .record-title', { minimum: 1 }
end
end
test 'results with valid query include links' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_select '#results .record-title a' do |value|
refute_nil(value.xpath('./@href').text)
end
end
end
test 'results with valid query have query highlights' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '#results .result-highlights ul li', { minimum: 1 }
end
end
test 'highlights partial is not rendered for results with no relevant highlights' do
VCR.use_cassette('advanced title data',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?title=data&advanced=true'
assert_response :success
# We shouldn't see any highlighted terms because all of the matches will be on title, which is included in
# SearchHelper#displayed_fields
assert_select '#results .result-highlights ul li', { count: 0 }
end
end
test 'searches with zero results are handled gracefully' do
VCR.use_cassette('timdex no results',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=asdfiouwenlasd'
assert_response :success
# Result list contents state "no results"
assert_select '#results'
assert_select '#results', { count: 1 }
assert_select '#results p', 'No results found for your search'
# Filter sidebar is not shown
assert_select '#filters', { count: 0 }
# Filters are not shown
assert_select '#filters .filter-category h3', { count: 0 }
# Pagination is not shown
assert_select '#pagination', { count: 0 }
end
end
test 'searches with ISSN display issn fact card when enabled' do
VCR.use_cassette('timdex 1234-5678',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: 'issn') do
get '/results?q=1234-5678'
end
assert_response :success
actual_div = assert_select('div[data-content-loader-url-value]')
assert_equal '/issn?issn=1234-5678',
actual_div.attribute('data-content-loader-url-value').value
end
end
test 'searches with ISSN do not display issn fact card when not enabled' do
VCR.use_cassette('timdex 1234-5678',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: '') do
get '/results?q=1234-5678'
end
assert_response :success
assert_select('div[data-content-loader-url-value].fact-container', 0)
end
end
test 'searches with ISBN insert isbn dom element when enabled' do
VCR.use_cassette('timdex 9781857988536',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: 'isbn') do
get '/results?q=9781857988536'
end
assert_response :success
actual_div = assert_select('div[data-content-loader-url-value]')
assert_equal '/isbn?isbn=9781857988536',
actual_div.attribute('data-content-loader-url-value').value
end
end
test 'searches with ISBN do not insert isbn dom element when not enabled' do
VCR.use_cassette('timdex 9781857988536',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: '') do
get '/results?q=9781857988536'
end
assert_response :success
assert_select('div[data-content-loader-url-value].fact-container', 0)
end
end
test 'searches with DOI insert doi dom element when enabled' do
VCR.use_cassette('timdex 10.1038.nphys1170 ',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: 'doi') do
get '/results?q=10.1038/nphys1170 '
end
assert_response :success
actual_div = assert_select('div[data-content-loader-url-value]')
assert_equal '/doi?doi=10.1038%2Fnphys1170',
actual_div.attribute('data-content-loader-url-value').value
end
end
test 'searches with DOI do not insert doi dom element when not enabled' do
VCR.use_cassette('timdex 10.1038.nphys1170 ',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: '') do
get '/results?q=10.1038/nphys1170 '
end
assert_response :success
assert_select('div[data-content-loader-url-value].fact-container', 0)
end
end
test 'searches with pmid insert pmid dom element when enabled' do
VCR.use_cassette('timdex PMID 35649707',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: 'pmid') do
get '/results?q=PMID: 35649707'
end
assert_response :success
actual_div = assert_select('div[data-content-loader-url-value]')
assert_equal '/pmid?pmid=PMID%3A+35649707',
actual_div.attribute('data-content-loader-url-value').value
end
end
test 'searches with pmid do not insert pmid dom element when not enabled' do
VCR.use_cassette('timdex PMID 35649707',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
ClimateControl.modify(FACT_PANELS_ENABLED: '') do
get '/results?q=PMID: 35649707'
end
assert_response :success
assert_select('div[data-content-loader-url-value].fact-container', 0)
end
end
test 'TACOS intervention is inserted when TACOS enabled' do
VCR.use_cassette('tacos',
allow_playback_repeats: true) do
get '/results?q=tacos'
assert_response :success
tacos_div = assert_select('div[data-content-loader-url-value].tacos-container')
assert_equal '/analyze?q=tacos', tacos_div.attribute('data-content-loader-url-value').value
end
end
test 'TACOS intervention not inserted when TACOS not enabled' do
VCR.use_cassette('tacos',
allow_playback_repeats: true) do
ClimateControl.modify(TACOS_URL: '') do
get '/results?q=tacos'
end
assert_response :success
assert_select('div[data-content-loader-url-value].tacos-container', 0)
end
end
# Advanced search behavior
test 'advanced search by keyword' do
VCR.use_cassette('advanced keyword asdf',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=asdf&advanced=true'
assert_response :success
assert_nil flash[:error]
assert_select 'li', 'Keyword anywhere: asdf'
end
end
test 'can search an advanced field without a keyword search' do
# note, this confirms we only validate param[:q] is present for basic searches
VCR.use_cassette('advanced citation asdf',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?citation=asdf&advanced=true'
assert_response :success
assert_nil flash[:error]
end
end
test 'advanced search can accept values from all fields' do
VCR.use_cassette('advanced all',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
q: 'data',
citation: 'citation',
contributors: 'contribs',
fundingInformation: 'fund',
identifiers: 'ids',
locations: 'locs',
subjects: 'subs',
title: 'title',
source: 'sauce',
advanced: 'true'
}.to_query
get "/results?#{query}"
assert_response :success
assert_nil flash[:error]
assert_select 'li', 'Keyword anywhere: data'
assert_select 'li', 'Citation: citation'
assert_select 'li', 'Contributors: contribs'
assert_select 'li', 'Funding information: fund'
assert_select 'li', 'Identifiers: ids'
assert_select 'li', 'Locations: locs'
assert_select 'li', 'Subjects: subs'
assert_select 'li', 'Title: title'
end
end
test 'advanced search form retains values with spaces' do
VCR.use_cassette('advanced all spaces',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
q: 'some data',
citation: 'a citation',
contributors: 'some contribs',
fundingInformation: 'a fund',
identifiers: 'some ids',
locations: 'some locs',
subjects: 'some subs',
title: 'a title',
advanced: 'true'
}.to_query
get "/results?#{query}"
assert_response :success
assert_nil flash[:error]
assert_select 'input#basic-search-main', value: 'some data'
assert_select 'input#advanced-citation', value: 'a citation'
assert_select 'input#advanced-contributors', value: 'some contribs'
assert_select 'input#advanced-fundingInformation', value: 'a fund'
assert_select 'input#advanced-identifiers', value: 'some ids'
assert_select 'input#advanced-locations', value: 'some locs'
assert_select 'input#advanced-subjects', value: 'some subs'
assert_select 'input#advanced-title', value: 'a title'
end
end
def source_filter_count(controller)
controller.view_context.assigns['filters'][:sourceFilter].count
end
test 'advanced search can limit to a single source' do
VCR.use_cassette('advanced source limit to one source',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
q: 'data',
advanced: 'true',
sourceFilter: ['Woods Hole Open Access Server']
}.to_query
get "/results?#{query}"
assert_response :success
assert_nil flash[:error]
assert(source_filter_count(@controller) == 1)
end
end
test 'advanced search defaults to all sources' do
VCR.use_cassette('advanced source defaults to all',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
q: 'data',
advanced: 'true'
}.to_query
get "/results?#{query}"
assert_response :success
assert_nil flash[:error]
# Assumption is we'll have at least 2 RDI sources for the time being
assert(source_filter_count(@controller) > 2)
end
end
test 'advanced search can limit to multiple sources' do
VCR.use_cassette('advanced source limit to two sources',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
# NOTE: when regenerating cassettes, if the source data does not have these
# sources you may need to change the source array below to two sources that are
# valid in the current available data.
query = {
q: 'data',
advanced: 'true',
sourceFilter: ['Abdul Latif Jameel Poverty Action Lab Dataverse', 'Woods Hole Open Access Server']
}.to_query
get "/results?#{query}"
assert_response :success
assert_nil flash[:error]
assert(source_filter_count(@controller) == 2)
end
end
test 'applications can customize the displayed filters via ENV' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
# Our standard test ENV does not define ACTIVE_FILTERS, but this confirms
# the behavior when it is not defined.
ClimateControl.modify ACTIVE_FILTERS: '' do
get '/results?q=data'
assert_response :success
assert_select '#filters .filter-category .filter-label', { minimum: 1 }
end
# Ask for a single filter, get that filter.
ClimateControl.modify ACTIVE_FILTERS: 'subjects' do
get '/results?q=data'
assert_response :success
assert_select '#filters .filter-category .filter-label', { count: 1 }
assert_select '#filters .filter-category:first-of-type .filter-label', 'Subject'
end
# The order of the terms matter, so now Format should be first.
ClimateControl.modify ACTIVE_FILTERS: 'format, contentType, source' do
get '/results?q=data'
assert_response :success
assert_select '#filters .filter-category .filter-label', { count: 3 }
assert_select '#filters .filter-category:first-of-type .filter-label', 'Format'
end
# Including extra values does not affect anything - "nonsense" is extraneous.
ClimateControl.modify ACTIVE_FILTERS: 'contentType, nonsense, source' do
get '/results?q=data'
assert_response :success
assert_select '#filters .filter-category .filter-label', { count: 2 }
assert_select '#filters .filter-category:first-of-type .filter-label', 'Content type'
end
end
end
test 'clear all filters button does not appear with zero filters in query' do
VCR.use_cassette('data basic controller',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
get '/results?q=data'
assert_response :success
assert_select '.clear-filters', count: 0
end
end
test 'clear all filters button does not appear with one filter in query' do
VCR.use_cassette('filter one',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
q: 'data',
sourceFilter: ['Woods Hole Open Access Server']
}.to_query
get "/results?#{query}"
assert_response :success
assert_select '.clear-filters', count: 0
end
end
test 'clear all filters button appears with more than filter in query' do
VCR.use_cassette('filter multiple',
allow_playback_repeats: true,
match_requests_on: %i[method uri body]) do
query = {
q: 'data',
contentTypeFilter: ['dataset'],
contributorsFilter: ['Woods Hole Open Access Server']
}.to_query
get "/results?#{query}"
assert_response :success
assert_select '.clear-filters', count: 1
end
end
end