-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathhyrax_helper_spec.rb
More file actions
697 lines (581 loc) · 27.3 KB
/
Copy pathhyrax_helper_spec.rb
File metadata and controls
697 lines (581 loc) · 27.3 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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
# frozen_string_literal: true
def new_state
Blacklight::SearchState.new({}, CatalogController.blacklight_config)
end
RSpec.describe HyraxHelper, type: :helper do
describe "show_transfer_request_title" do
let(:sender) { FactoryBot.create(:user) }
let(:user) { FactoryBot.create(:user) }
context "when work is canceled" do
let(:request) do
instance_double(ProxyDepositRequest,
deleted_work?: false,
canceled?: true,
to_s: 'Test work')
end
subject { helper.show_transfer_request_title request }
it { expect(subject).to eq 'Test work' }
end
end
describe '#render_notifications' do
let(:mailbox) { double('mailbox', unread_count: unread_count, label: 'Foobar') }
subject { helper.render_notifications(user: nil) }
before do
allow(UserMailbox).to receive(:new).and_return(mailbox)
end
context 'with unread messages' do
let(:unread_count) { 10 }
it 'renders with badge-danger and is visible' do
expect(subject).to eq '<a aria-description="Foobar" class="notify-number nav-link" href="/notifications">' \
'<span class="fa fa-bell" aria-label="Notifications"></span>' \
"\n" \
'<span class="count badge badge-danger">10</span></a>'
end
end
context 'with no unread messages' do
let(:unread_count) { 0 }
it 'renders with badge-secondary and is invisible' do
expect(subject).to eq '<a aria-description="Foobar" class="notify-number nav-link" href="/notifications">' \
'<span class="fa fa-bell" aria-label="Notifications"></span>' \
"\n" \
'<span class="count badge invisible badge-secondary">0</span></a>'
end
end
end
describe '#available_translations' do
subject { helper.available_translations }
it do
is_expected.to eq('de' => 'Deutsch',
'en' => 'English',
'es' => 'Español',
'fr' => 'Français',
'it' => 'Italiano',
'pt-BR' => 'Português do Brasil',
'zh' => '中文')
end
end
describe '#available_admin_sets_for_creating_works' do
let(:ability) { instance_double(Ability) }
let(:query_service) { instance_double('query_service') }
before do
allow(Flipflop).to receive(:assign_admin_set?).and_return(true)
allow(Hyrax).to receive(:query_service).and_return(query_service)
end
context 'when depositable admin sets are available' do
let(:source_ids) { ['z-admin-set', Hyrax::AdminSetCreateService::DEFAULT_ID, 'a-admin-set'] }
let(:solr_query_service) { instance_double(Hyrax::SolrQueryService) }
let(:solr_documents) do
[
{ 'id' => 'z-admin-set', 'title_tesim' => ['Zeta Admin Set'] },
{ 'id' => Hyrax::AdminSetCreateService::DEFAULT_ID, 'title_tesim' => ['Default Admin Set'] },
{ 'id' => 'a-admin-set', 'title_tesim' => ['Alpha Admin Set'] }
]
end
before do
allow(Hyrax::Collections::PermissionsService)
.to receive(:source_ids_for_deposit)
.with(ability:, source_type: 'admin_set')
.and_return(source_ids)
allow(Hyrax::AdminSetCreateService)
.to receive(:default_admin_set?) do |id:|
id == Hyrax::AdminSetCreateService::DEFAULT_ID
end
allow(Hyrax::SolrQueryService).to receive(:new).and_return(solr_query_service)
allow(solr_query_service).to receive(:with_ids).with(ids: source_ids).and_return(solr_query_service)
allow(solr_query_service)
.to receive(:with_field_pairs)
.with(
field_pairs: {
has_model_ssim: Hyrax::ModelRegistry.admin_set_rdf_representations.join(',')
},
type: 'terms'
)
.and_return(solr_query_service)
allow(solr_query_service)
.to receive(:solr_documents)
.with(rows: source_ids.length, fl: 'id,title_tesim')
.and_return(solr_documents)
end
it 'builds admin set options from Solr without loading repository objects by id' do
expect(query_service).not_to receive(:find_many_by_ids)
expect(helper.available_admin_sets_for_creating_works(ability:)).to eq(
[
['Default Admin Set', Hyrax::AdminSetCreateService::DEFAULT_ID],
['Alpha Admin Set', 'a-admin-set'],
['Zeta Admin Set', 'z-admin-set']
]
)
end
end
context 'when there are no depositable admin sets' do
before do
allow(Hyrax::Collections::PermissionsService)
.to receive(:source_ids_for_deposit)
.with(ability:, source_type: 'admin_set')
.and_return([])
end
it 'returns an empty list without querying Solr' do
expect(Hyrax::SolrQueryService).not_to receive(:new)
expect(query_service).not_to receive(:find_many_by_ids)
expect(helper.available_admin_sets_for_creating_works(ability:)).to eq([])
end
end
context 'when some depositable admin sets are missing from Solr' do
let(:source_ids) { [Hyrax::AdminSetCreateService::DEFAULT_ID, 'missing-admin-set'] }
let(:solr_query_service) { instance_double(Hyrax::SolrQueryService) }
let(:missing_admin_set) { instance_double('resource', title: ['Set B'], id: 'missing-admin-set') }
let(:solr_documents) do
[
{ 'id' => Hyrax::AdminSetCreateService::DEFAULT_ID, 'title_tesim' => ['Default Admin Set'] }
]
end
before do
allow(Hyrax::Collections::PermissionsService)
.to receive(:source_ids_for_deposit)
.with(ability:, source_type: 'admin_set')
.and_return(source_ids)
allow(Hyrax::AdminSetCreateService)
.to receive(:default_admin_set?) do |id:|
id == Hyrax::AdminSetCreateService::DEFAULT_ID
end
allow(Hyrax::SolrQueryService).to receive(:new).and_return(solr_query_service)
allow(solr_query_service).to receive(:with_ids).with(ids: source_ids).and_return(solr_query_service)
allow(solr_query_service)
.to receive(:with_field_pairs)
.with(
field_pairs: {
has_model_ssim: Hyrax::ModelRegistry.admin_set_rdf_representations.join(',')
},
type: 'terms'
)
.and_return(solr_query_service)
allow(solr_query_service)
.to receive(:solr_documents)
.with(rows: source_ids.length, fl: 'id,title_tesim')
.and_return(solr_documents)
allow(query_service)
.to receive(:find_many_by_ids)
.with(ids: ['missing-admin-set'])
.and_return([missing_admin_set])
end
it 'falls back to repository objects for admin sets that are not indexed yet' do
expect(helper.available_admin_sets_for_creating_works(ability:)).to eq(
[
['Default Admin Set', Hyrax::AdminSetCreateService::DEFAULT_ID],
['Set B', 'missing-admin-set']
]
)
end
end
end
context 'link helpers' do
before do
allow(helper).to receive(:search_action_path) do |*args|
search_catalog_path(*args)
end
allow(helper).to receive(:search_state).and_return(new_state)
end
describe "#link_to_facet_list" do
context "with values" do
subject { helper.link_to_facet_list(['car', 'truck'], 'vehicle_type') }
it "joins the values" do
expect(helper).to receive(:search_state).exactly(2).times
car_link = search_catalog_path(f: { 'vehicle_type_sim' => ['car'] })
truck_link = search_catalog_path(f: { 'vehicle_type_sim' => ['truck'] })
expect(subject).to eq "<a href=\"#{car_link}\">car</a>, <a href=\"#{truck_link}\">truck</a>"
expect(subject).to be_html_safe
end
end
context "without values" do
subject { helper.link_to_facet_list([], 'vehicle_type') }
it "shows the default text" do
expect(subject).to eq "No value entered"
end
end
end
describe '#index_field_link' do
let(:args) do
{
config: { field_name: 'contributor' },
value: ['Fritz Lang', 'Mel Brooks']
}
end
subject { helper.index_field_link(args) }
it 'returns a link' do
expect(subject).to be_html_safe
expect(subject).to eq '<a href="/catalog?q=%22Fritz+Lang%22&search_field=contributor">Fritz Lang</a>, ' \
+ '<a href="/catalog?q=%22Mel+Brooks%22&search_field=contributor">Mel Brooks</a>'
end
end
end
describe "#link_to_each_facet_field" do
subject { helper.link_to_each_facet_field(options) }
context "with helper_facet and default separator" do
let(:options) { { config: { helper_facet: "document_types_sim".to_sym }, value: ["Imaging > Object Photography"] } }
it do
is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">" \
"Imaging</a> > " \
"<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>")
end
end
context "with helper_facet and optional separator" do
let(:options) { { config: { helper_facet: "document_types_sim".to_sym, separator: " : " }, value: ["Imaging : Object Photography"] } }
it do
is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">" \
"Imaging</a> : " \
"<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>")
end
end
context "with :output_separator" do
let(:options) do
{ config: { helper_facet: "document_types_sim".to_sym, output_separator: ' ~ ', separator: ":" }, value: ["Imaging : Object Photography"] }
end
it do
is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">" \
"Imaging</a> ~ " \
"<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>")
end
end
context "with :no_spaces_around_separator" do
let(:options) do
{ config: { helper_facet: "document_types_sim".to_sym, output_separator: '~', separator: ":" }, value: ["Imaging : Object Photography"] }
end
it do
is_expected.to eq("<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Imaging\">" \
"Imaging</a>~" \
"<a href=\"/catalog?f%5Bdocument_types_sim%5D%5B%5D=Object+Photography\">Object Photography</a>")
end
end
end
describe "#collection_thumbnail" do
let(:document) { SolrDocument.new(has_model_ssim: ['Collection']) }
subject { helper.collection_thumbnail(document) }
it { is_expected.to eq '<span class="fa fa-cubes collection-icon-search"></span>' }
end
describe "#link_to_telephone" do
subject { helper.link_to_telephone(user) }
context "when user is set" do
let(:user) { mock_model(User, telephone: '867-5309') }
it { is_expected.to eq "<a href=\"wtai://wp/mc;867-5309\">867-5309</a>" }
end
context "when user is not set" do
let(:user) { nil }
it { is_expected.to be_nil }
end
end
describe "#current_search_parameters" do
context "when the user is not in the dashboard" do
it "is whatever q is" do
allow(helper).to receive(:params).and_return(controller: "catalog", q: "foo")
expect(helper.current_search_parameters).to eq("foo")
end
end
context "when the user is on any dashboard page" do
it "is ignored on dashboard" do
allow(helper).to receive(:params).and_return(controller: "hyrax/dashboard", q: "foo")
expect(helper.current_search_parameters).to be_nil
end
it "is ignored on dashboard works, collections, highlights and shares" do
allow(helper).to receive(:params).and_return(controller: "hyrax/my/works", q: "foo")
expect(helper.current_search_parameters).to be_nil
allow(helper).to receive(:params).and_return(controller: "hyrax/my/collections", q: "foo")
expect(helper.current_search_parameters).to be_nil
allow(helper).to receive(:params).and_return(controller: "hyrax/my/highlights", q: "foo")
expect(helper.current_search_parameters).to be_nil
allow(helper).to receive(:params).and_return(controller: "hyrax/my/shares", q: "foo")
expect(helper.current_search_parameters).to be_nil
end
end
end
describe "#search_form_action" do
context "when the user is not in the dashboard" do
it "returns the catalog index path" do
allow(helper).to receive(:params).and_return(controller: "foo")
expect(helper.search_form_action).to eq(search_catalog_path)
end
end
context "when the user is on the dashboard page" do
it "defaults to My Works" do
allow(helper).to receive(:params).and_return(controller: "hyrax/dashboard")
expect(helper.search_form_action).to eq(hyrax.my_works_path)
end
end
context "when the user is on the 'all works' tab on the dashboard page" do
it "returns the dashboard works path" do
allow(helper).to receive(:params).and_return(controller: "hyrax/dashboard/works")
expect(helper.search_form_action).to eq(hyrax.dashboard_works_path)
end
end
context "when the user is on the my works page" do
it "returns the my dashboard works path" do
allow(helper).to receive(:params).and_return(controller: "hyrax/my/works")
expect(helper.search_form_action).to eq(hyrax.my_works_path)
end
end
context "when the user is on the my collections page" do
it "returns the my dashboard collections path" do
allow(helper).to receive(:params).and_return(controller: "hyrax/my/collections")
expect(helper.search_form_action).to eq(hyrax.my_collections_path)
end
end
context "when the user is on the all collections page" do
it "returns the all dashboard collections path" do
allow(helper).to receive(:params).and_return(controller: "hyrax/dashboard/collections")
expect(helper.search_form_action).to eq(hyrax.dashboard_collections_path)
end
end
context "when the user is on the my highlights page" do
it "returns the my dashboard highlights path" do
allow(helper).to receive(:params).and_return(controller: "hyrax/my/highlights")
expect(helper.search_form_action).to eq(hyrax.dashboard_highlights_path)
end
end
context "when the user is on the my shares page" do
it "returns the my dashboard shares path" do
allow(helper).to receive(:params).and_return(controller: "hyrax/my/shares")
expect(helper.search_form_action).to eq(hyrax.dashboard_shares_path)
end
end
end
describe '#zotero_label' do
subject { helper }
it { is_expected.to respond_to(:zotero_label) }
end
describe "#iconify_auto_link" do
let(:text) { 'Foo < http://www.example.com. & More text' }
let(:linked_text) { 'Foo < <a href="http://www.example.com"><span class="fa fa-external-link"></span> http://www.example.com</a>. & More text' }
let(:document) { SolrDocument.new(has_model_ssim: ['GenericWork'], id: 512, title_tesim: text, description_tesim: text) }
let(:blacklight_config) { CatalogController.blacklight_config }
before do
allow(controller).to receive(:action_name).and_return('index')
allow(helper).to receive(:blacklight_config).and_return(blacklight_config)
end
it "boring String argument" do
expect(helper.iconify_auto_link('no escapes or links necessary')).to eq 'no escapes or links necessary'
expect(helper.iconify_auto_link('no escapes or links necessary', false)).to eq 'no escapes or links necessary'
end
context "interesting String argument" do
subject { helper.iconify_auto_link(text) }
it "escapes input" do
expect(subject).to start_with('Foo <').and end_with('. & More text')
end
it "adds links" do
expect(subject).to include('<a href="http://www.example.com">')
end
it "adds icons" do
expect(subject).to include('class="fa fa-external-link"')
end
end
context "when using a hash argument" do
subject { helper.iconify_auto_link(arg) }
describe "auto-linking in the title" do
let(:arg) { { document: document, value: [text], config: blacklight_config.index_fields['title_tesim'], field: 'title_tesim' } }
it { is_expected.to eq(linked_text) }
end
describe "auto-linking in the description" do
let(:arg) { { document: document, value: [text], config: blacklight_config.index_fields['description_tesim'], field: 'description_tesim' } }
it { is_expected.to eq(linked_text) }
end
end
end
describe "#render_html_index_value" do
it "strips tags and renders a plain-text snippet" do
html = '<h3>Title</h3><p>Body <strong>bold</strong> text.</p>'
expect(helper.render_html_index_value(html)).to eq('Title Body bold text.')
end
it "decodes HTML entities" do
expect(helper.render_html_index_value('<p>Bruce McLean’s work</p>')).to eq('Bruce McLean’s work')
end
it "does not glue words across block boundaries" do
expect(helper.render_html_index_value('<li>one</li><li>two</li>')).to eq('one two')
end
it "truncates long values" do
result = helper.render_html_index_value("<p>#{'word ' * 100}</p>")
expect(result.length).to be <= 230
expect(result).to end_with('...')
end
it "honors a per-field truncation length from the field config" do
arg = { value: ["<p>#{'word ' * 100}</p>"], config: double(search_results_truncate: 20) }
expect(helper.render_html_index_value(arg).length).to be <= 20
end
it "skips truncation when search_results_truncate is false" do
arg = { value: ["<p>#{'word ' * 100}</p>"], config: double(search_results_truncate: false) }
result = helper.render_html_index_value(arg)
expect(result.length).to be > 230
expect(result).not_to end_with('...')
end
it "strips markup that arrives as escaped entities" do
expect(helper.render_html_index_value('<em>hi</em> there')).to eq('hi there')
end
it "falls back to the default length for a non-integer truncation value" do
arg = { value: ["<p>#{'word ' * 100}</p>"], config: double(search_results_truncate: 'oops') }
result = helper.render_html_index_value(arg)
expect(result.length).to be <= 230
expect(result).to end_with('...')
end
it "falls back to the default length for a non-positive truncation value" do
arg = { value: ["<p>#{'word ' * 100}</p>"], config: double(search_results_truncate: 0) }
result = helper.render_html_index_value(arg)
expect(result.length).to be <= 230
expect(result).to end_with('...')
end
it "coerces a numeric string truncation value" do
arg = { value: ["<p>#{'word ' * 100}</p>"], config: double(search_results_truncate: '20') }
expect(helper.render_html_index_value(arg).length).to be <= 20
end
it "accepts Blacklight's hash form with a :value array" do
arg = { value: ['<p>Hello <em>world</em></p>'], field: 'narrative_tesim' }
expect(helper.render_html_index_value(arg)).to eq('Hello world')
end
it "returns an empty string for blank values" do
expect(helper.render_html_index_value(nil)).to eq('')
expect(helper.render_html_index_value(value: [])).to eq('')
end
end
describe "#license_links" do
it "maps the url to a link with a label" do
expect(helper.license_links(
value: ["http://creativecommons.org/publicdomain/zero/1.0/"]
)).to eq("<a href=\"http://creativecommons.org/publicdomain/zero/1.0/\">Creative Commons CC0 1.0 Universal</a>")
end
it "converts multiple licenses to a sentence" do
expect(helper.license_links(
value: ["http://creativecommons.org/publicdomain/zero/1.0/",
"http://creativecommons.org/publicdomain/mark/1.0/",
"http://www.europeana.eu/portal/rights/rr-r.html"]
)).to eq("<a href=\"http://creativecommons.org/publicdomain/zero/1.0/\">Creative Commons CC0 1.0 Universal</a>, " \
"<a href=\"http://creativecommons.org/publicdomain/mark/1.0/\">Creative Commons Public Domain Mark 1.0</a>, " \
"and <a href=\"http://www.europeana.eu/portal/rights/rr-r.html\">All rights reserved</a>")
end
it "renders an off-authority free-text value as plain text" do
expect(helper.license_links(value: ["All rights reserved"]))
.to eq("All rights reserved")
end
it "renders a non-URI single token as plain text rather than a relative link" do
# `URI.parse("moomin")` does not raise — it returns a URI::Generic with
# no scheme. We must not turn that into `<a href="moomin">`.
expect(helper.license_links(value: ["moomin"])).to eq("moomin")
end
it "renders a value with an unsafe scheme as plain text rather than a link" do
expect(helper.license_links(value: ["javascript:alert(1)"]))
.not_to include("href=")
end
it "renders an off-authority URI value as a link with the value as its own label" do
expect(helper.license_links(value: ["http://example.com/unknown"]))
.to eq("<a href=\"http://example.com/unknown\">http://example.com/unknown</a>")
end
it "escapes HTML in an off-authority free-text value" do
output = helper.license_links(value: ["<script>alert(1)</script>"])
expect(output).not_to include("<script>")
expect(output).to include("<script>")
end
end
describe "#rights_statment_links" do
it "maps the url to a link with a label" do
expect(helper.rights_statement_links(
value: ["http://rightsstatements.org/vocab/InC/1.0/"]
)).to eq("<a href=\"http://rightsstatements.org/vocab/InC/1.0/\">In Copyright</a>")
end
it "renders an off-authority free-text value as plain text" do
expect(helper.rights_statement_links(value: ["All rights reserved"]))
.to eq("All rights reserved")
end
it "renders a non-URI single token as plain text rather than a relative link" do
expect(helper.rights_statement_links(value: ["moomin"])).to eq("moomin")
end
it "renders a value with an unsafe scheme as plain text rather than a link" do
expect(helper.rights_statement_links(value: ["javascript:alert(1)"]))
.not_to include("href=")
end
it "renders an off-authority URI value as a link with the value as its own label" do
expect(helper.rights_statement_links(value: ["http://example.com/unknown"]))
.to eq("<a href=\"http://example.com/unknown\">http://example.com/unknown</a>")
end
it "escapes HTML in an off-authority free-text value" do
output = helper.rights_statement_links(value: ["<script>alert(1)</script>"])
expect(output).not_to include("<script>")
expect(output).to include("<script>")
end
end
describe "#human_readable_date" do
it "ensures that the display of the date is human-readable" do
expect(helper.human_readable_date(value: ["2016-08-15T00:00:00Z"])).to eq("08/15/2016")
end
end
describe "#banner_image" do
it "returns the configured Hyrax banner image" do
expect(helper.banner_image).to eq(Hyrax.config.banner_image)
end
end
describe "#collection_title_by_id" do
let(:solr_doc) { double(id: "abcd12345") }
let(:bad_solr_doc) { double(id: "efgh67890") }
let(:solr_response) { double(docs: [solr_doc]) }
let(:bad_solr_response) { double(docs: [bad_solr_doc]) }
let(:empty_solr_response) { double(docs: []) }
let(:repository) { double }
before do
allow(controller).to receive(:blacklight_config).and_return(CatalogController.blacklight_config)
allow(controller.blacklight_config).to receive(:repository).and_return(repository)
allow(solr_doc).to receive(:[]).with("title_tesim").and_return(["Collection of Awesomeness"])
allow(bad_solr_doc).to receive(:[]).with("title_tesim").and_return(nil)
allow(repository).to receive(:find).with("abcd12345").and_return(solr_response)
allow(repository).to receive(:find).with("efgh67890").and_return(bad_solr_response)
allow(repository).to receive(:find).with("bad-id").and_return(empty_solr_response)
allow(repository).to receive(:find).with("error-id").and_raise(Blacklight::Exceptions::RecordNotFound)
end
it "returns the first title of the collection" do
expect(helper.collection_title_by_id("abcd12345")).to eq "Collection of Awesomeness"
end
it "returns nil if collection doesn't have title_tesim field" do
expect(helper.collection_title_by_id("efgh67890")).to eq nil
end
it "returns nil if collection not found" do
expect(helper.collection_title_by_id("bad-id")).to eq nil
end
it "returns nil if RecordNotFound is raised" do
expect(helper.collection_title_by_id("error-id")).to eq nil
end
end
describe "#thumbnail_label_for" do
it "gives a string even if no thumbnail label can be found" do
expect(helper.thumbnail_label_for(object: Object.new)).to be_a String
end
it 'interoperates with display objects with #thumbnail_title' do
model_or_presenter_or_form = double(thumbnail_title: 'my display thumbnail title')
expect(helper.thumbnail_label_for(object: model_or_presenter_or_form))
.to eq 'my display thumbnail title'
end
it 'interoperates with CollectionForm', :active_fedora do
collection = ::Collection.new
collection.thumbnail = ::FileSet.create(title: ["thumbnail"])
form = Hyrax::Forms::CollectionForm.new(collection,
:FAKE_ABILITY,
:FAKE_BLACKLIGHT_REPOSITORY)
expect(helper.thumbnail_label_for(object: form)).to eq 'thumbnail'
end
it 'interoperates with AdminSetForm', :active_fedora do
admin_set = AdminSet.new
admin_set.thumbnail = ::FileSet.create(title: ["thumbnail"])
form = Hyrax::Forms::AdminSetForm.new(admin_set,
:FAKE_ABILITY,
:FAKE_BLACKLIGHT_REPOSITORY)
expect(helper.thumbnail_label_for(object: form)).to eq 'thumbnail'
end
end
describe "#cast_to_date_time_format" do
it "casts well date-like strings to the specified format" do
expect(helper.cast_to_date_time_format("2022-08-29 09:41:00 -0400", format: "%Y-%m-%d")).to eq("2022-08-29")
end
it "casts well date-like strings to the specified format" do
now = Time.zone.now
expect(helper.cast_to_date_time_format(now, format: "%Y-%m-%d")).to eq(now.strftime("%Y-%m-%d"))
end
it "falls back to the given string" do
expect(helper.cast_to_date_time_format("Hyrax Life", format: "%Y-%m-%d")).to eq("Hyrax Life")
end
it "casts nil to empty string" do
expect(helper.cast_to_date_time_format(nil, format: "%Y-%m-%d")).to eq("")
end
end
end