require 'spec_helper' describe Blacklight::DocumentPresenter do include Capybara::RSpecMatchers let(:request_context) { double } let(:config) { Blacklight::Configuration.new } subject { presenter } let(:presenter) { Blacklight::DocumentPresenter.new(document, request_context, config) } let(:document) do SolrDocument.new(id: 1, 'link_to_search_true' => 'x', 'link_to_search_named' => 'x', 'qwer' => 'document qwer value', 'mnbv' => 'document mnbv value') end before do allow(request_context).to receive(:search_state).and_return(Blacklight::SearchState.new({}, config)) end describe "link_rel_alternates" do before do class MockDocument include Blacklight::Solr::Document end module MockExtension def self.extended(document) document.will_export_as(:weird, "application/weird") document.will_export_as(:weirder, "application/weirder") document.will_export_as(:weird_dup, "application/weird") end def export_as_weird ; "weird" ; end def export_as_weirder ; "weirder" ; end def export_as_weird_dup ; "weird_dup" ; end end MockDocument.use_extension(MockExtension) def mock_document_app_helper_url *args solr_document_url(*args) end allow(request_context).to receive(:polymorphic_url) do |_, opts| "url.#{opts[:format]}" end end let(:document) { MockDocument.new(id: "MOCK_ID1") } context "with no arguments" do subject { presenter.link_rel_alternates } it "generates tags" do tmp_value = Capybara.ignore_hidden_elements Capybara.ignore_hidden_elements = false document.export_formats.each_pair do |format, spec| expect(subject).to have_selector("link[href$='.#{ format }']") do |matches| expect(matches).to have(1).match tag = matches[0] expect(tag.attributes["rel"].value).to eq "alternate" expect(tag.attributes["title"].value).to eq format.to_s expect(tag.attributes["href"].value).to eq mock_document_app_helper_url(document, format: format) end end Capybara.ignore_hidden_elements = tmp_value end it { is_expected.to be_html_safe } end context "with unique: true" do subject { presenter.link_rel_alternates(unique: true) } it "respects unique: true" do tmp_value = Capybara.ignore_hidden_elements Capybara.ignore_hidden_elements = false expect(subject).to have_selector("link[type='application/weird']", count: 1) Capybara.ignore_hidden_elements = tmp_value end end context "with exclude" do subject { presenter.link_rel_alternates(unique: true) } it "excludes formats from :exclude" do tmp_value = Capybara.ignore_hidden_elements Capybara.ignore_hidden_elements = false expect(subject).to_not have_selector("link[href$='.weird_dup']") Capybara.ignore_hidden_elements = tmp_value end end end describe "render_index_field_value" do let(:config) do Blacklight::Configuration.new.configure do |config| config.add_index_field 'qwer' config.add_index_field 'asdf', :helper_method => :render_asdf_index_field config.add_index_field 'link_to_search_true', :link_to_search => true config.add_index_field 'link_to_search_named', :link_to_search => :some_field config.add_index_field 'highlight', :highlight => true config.add_index_field 'solr_doc_accessor', :accessor => true config.add_index_field 'explicit_accessor', :accessor => :solr_doc_accessor config.add_index_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg config.add_index_field 'alias', field: 'qwer' config.add_index_field 'with_default', default: 'value' end end it "should check for an explicit value" do value = subject.render_index_field_value 'asdf', :value => 'asdf' expect(value).to eq 'asdf' end it "should check for a helper method to call" do allow(request_context).to receive(:render_asdf_index_field).and_return('custom asdf value') value = subject.render_index_field_value 'asdf' expect(value).to eq 'custom asdf value' end it "should check for a link_to_search" do allow(request_context).to receive(:search_action_path).with(:f => { :link_to_search_true => ['x'] }).and_return('/foo') allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar') value = subject.render_index_field_value 'link_to_search_true' expect(value).to eq 'bar' end it "should check for a link_to_search with a field name" do allow(request_context).to receive(:search_action_path).with(:f => { :some_field => ['x'] }).and_return('/foo') allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar') value = subject.render_index_field_value 'link_to_search_named' expect(value).to eq 'bar' end it "should gracefully handle when no highlight field is available" do allow(document).to receive(:has_highlight_field?).and_return(false) value = subject.render_index_field_value 'highlight' expect(value).to be_blank end it "should check for a highlighted field" do allow(document).to receive(:has_highlight_field?).and_return(true) allow(document).to receive(:highlight_field).with('highlight').and_return(['highlight'.html_safe]) value = subject.render_index_field_value 'highlight' expect(value).to eq 'highlight' end it "should check the document field value" do value = subject.render_index_field_value 'qwer' expect(value).to eq 'document qwer value' end it "should work with index fields that aren't explicitly defined" do value = subject.render_index_field_value 'mnbv' expect(value).to eq 'document mnbv value' end it "should call an accessor on the solr document" do allow(document).to receive_messages(solr_doc_accessor: "123") value = subject.render_index_field_value 'solr_doc_accessor' expect(value).to eq "123" end it "should call an explicit accessor on the solr document" do allow(document).to receive_messages(solr_doc_accessor: "123") value = subject.render_index_field_value 'explicit_accessor' expect(value).to eq "123" end it "should call an accessor on the solr document with the field as an argument" do allow(document).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123") value = subject.render_index_field_value 'explicit_accessor_with_arg' expect(value).to eq "123" end it "should support solr field configuration" do value = subject.render_index_field_value 'alias' expect(value).to eq "document qwer value" end it "should support default values in the field configuration" do value = subject.render_index_field_value 'with_default' expect(value).to eq "value" end end describe "render_document_show_field_value" do let(:config) do Blacklight::Configuration.new.configure do |config| config.add_show_field 'qwer' config.add_show_field 'asdf', :helper_method => :render_asdf_document_show_field config.add_show_field 'link_to_search_true', :link_to_search => true config.add_show_field 'link_to_search_named', :link_to_search => :some_field config.add_show_field 'highlight', :highlight => true config.add_show_field 'solr_doc_accessor', :accessor => true config.add_show_field 'explicit_accessor', :accessor => :solr_doc_accessor config.add_show_field 'explicit_array_accessor', :accessor => [:solr_doc_accessor, :some_method] config.add_show_field 'explicit_accessor_with_arg', :accessor => :solr_doc_accessor_with_arg end end it 'html-escapes values' do value = subject.render_document_show_field_value 'asdf', value: 'val1' expect(value).to eq '<b>val1</b>' end it 'joins multivalued valued fields' do value = subject.render_document_show_field_value 'asdf', value: [' 'val1' expect(value).to eq 'val1' end it "should check for a helper method to call" do allow(request_context).to receive(:render_asdf_document_show_field).and_return('custom asdf value') value = subject.render_document_show_field_value 'asdf' expect(value).to eq 'custom asdf value' end it "should check for a link_to_search" do allow(request_context).to receive(:search_action_path).and_return('/foo') allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar') value = subject.render_document_show_field_value 'link_to_search_true' expect(value).to eq 'bar' end it "should check for a link_to_search with a field name" do allow(request_context).to receive(:search_action_path).and_return('/foo') allow(request_context).to receive(:link_to).with("x", '/foo').and_return('bar') value = subject.render_document_show_field_value 'link_to_search_named' expect(value).to eq 'bar' end it "should gracefully handle when no highlight field is available" do allow(document).to receive(:has_highlight_field?).and_return(false) value = subject.render_document_show_field_value 'highlight' expect(value).to be_blank end it "should check for a highlighted field" do allow(document).to receive(:has_highlight_field?).and_return(true) allow(document).to receive(:highlight_field).with('highlight').and_return(['highlight'.html_safe]) value = subject.render_document_show_field_value 'highlight' expect(value).to eq 'highlight' end it 'respects the HTML-safeness of multivalued highlight fields' do allow(document).to receive(:has_highlight_field?).and_return(true) allow(document).to receive(:highlight_field).with('highlight').and_return(['highlight'.html_safe, 'other highlight'.html_safe]) value = subject.render_document_show_field_value 'highlight' expect(value).to eq 'highlight and other highlight' end it "should check the document field value" do value = subject.render_document_show_field_value 'qwer' expect(value).to eq 'document qwer value' end it "should work with show fields that aren't explicitly defined" do value = subject.render_document_show_field_value 'mnbv' expect(value).to eq 'document mnbv value' end it "should call an accessor on the solr document" do allow(document).to receive_messages(solr_doc_accessor: "123") value = subject.render_document_show_field_value 'solr_doc_accessor' expect(value).to eq "123" end it "should call an explicit accessor on the solr document" do allow(document).to receive_messages(solr_doc_accessor: "123") value = subject.render_document_show_field_value 'explicit_accessor' expect(value).to eq "123" end it "should call an explicit array-style accessor on the solr document" do allow(document).to receive_message_chain(:solr_doc_accessor, some_method: "123") value = subject.render_document_show_field_value 'explicit_array_accessor' expect(value).to eq "123" end it "should call an accessor on the solr document with the field as an argument" do allow(document).to receive(:solr_doc_accessor_with_arg).with('explicit_accessor_with_arg').and_return("123") value = subject.render_document_show_field_value 'explicit_accessor_with_arg' expect(value).to eq "123" end end describe "render_field_value" do it "should join and html-safe values" do expect(subject.render_field_value(['a', 'b'])).to eq "a and b" end it "should use the separator from the Blacklight field configuration by default" do Deprecation.silence(Blacklight::DocumentPresenter) do expect(subject.render_field_value(['c', 'd'], double(separator: '; ', itemprop: nil, separator_options: nil))).to eq "c; d" end end it "should use the field_config.separator_options from the Blacklight field configuration" do expect(subject.render_field_value(['c', 'd'], double(separator: nil, itemprop: nil, separator_options: { two_words_connector: '; '}))).to eq "c; d" end it "should include schema.org itemprop attributes" do expect(subject.render_field_value('a', double(separator: nil, itemprop: 'some-prop', separator_options: nil))).to have_selector("span[@itemprop='some-prop']", :text => "a") end end describe "#document_heading" do it "should fallback to an id" do allow(document).to receive(:id).and_return "xyz" expect(subject.document_heading).to eq document.id end it "should return the value of the field" do config.show.title_field = :x allow(document).to receive(:has?).with(:x).and_return(true) allow(document).to receive(:[]).with(:x).and_return("value") expect(subject.document_heading).to eq "value" end it "should return the first present value" do config.show.title_field = [:x, :y] allow(document).to receive(:has?).with(:x).and_return(false) allow(document).to receive(:has?).with(:y).and_return(true) allow(document).to receive(:[]).with(:y).and_return("value") expect(subject.document_heading).to eq "value" end end describe "#document_show_html_title" do it "should fallback to an id" do allow(document).to receive(:id).and_return "xyz" expect(subject.document_show_html_title).to eq document.id end it "should return the value of the field" do config.show.html_title_field = :x allow(document).to receive(:has?).with(:x).and_return(true) allow(document).to receive(:[]).with(:x).and_return("value") expect(subject.document_show_html_title).to eq "value" end it "should return the first present value" do config.show.html_title_field = [:x, :y] allow(document).to receive(:has?).with(:x).and_return(false) allow(document).to receive(:has?).with(:y).and_return(true) allow(document).to receive(:[]).with(:y).and_return("value") expect(subject.document_show_html_title).to eq "value" end end end