From a380dcb9f307d22b4e798071f6f19751023bd10a Mon Sep 17 00:00:00 2001 From: Renee LeBeau Date: Mon, 23 Jun 2025 17:19:25 -0400 Subject: [PATCH] [IUS-2613] DataCORE presenter unit tests --- .../hyrax/characterization_behavior_spec.rb | 238 ++++++++ .../hyrax/collection_presenter_spec.rb | 554 ++++++++++++++++++ .../hyrax/data_set_presenter_spec.rb | 415 ++++++++++++- .../hyrax/deep_blue_presenter_spec.rb | 46 ++ .../hyrax/ds_file_set_presenter_spec.rb | 201 +++++++ .../hyrax/generic_work_presenter_spec.rb | 21 +- .../hyrax/homepage_presenter_spec.rb | 155 +++++ .../hyrax/member_presenter_factory_spec.rb | 116 ++++ .../hyrax/work_show_presenter_spec.rb | 99 ++++ .../provenance_log_presenter_spec.rb | 108 ++++ .../dataset/date_coverage_service_spec.rb | 107 ++++ 11 files changed, 2036 insertions(+), 24 deletions(-) create mode 100644 spec/presenters/hyrax/characterization_behavior_spec.rb create mode 100644 spec/presenters/hyrax/collection_presenter_spec.rb create mode 100644 spec/presenters/hyrax/deep_blue_presenter_spec.rb create mode 100644 spec/presenters/hyrax/ds_file_set_presenter_spec.rb create mode 100644 spec/presenters/hyrax/homepage_presenter_spec.rb create mode 100644 spec/presenters/hyrax/member_presenter_factory_spec.rb create mode 100644 spec/presenters/hyrax/work_show_presenter_spec.rb create mode 100644 spec/presenters/provenance_log_presenter_spec.rb create mode 100644 spec/services/dataset/date_coverage_service_spec.rb diff --git a/spec/presenters/hyrax/characterization_behavior_spec.rb b/spec/presenters/hyrax/characterization_behavior_spec.rb new file mode 100644 index 00000000..7404ff28 --- /dev/null +++ b/spec/presenters/hyrax/characterization_behavior_spec.rb @@ -0,0 +1,238 @@ +require 'rails_helper' + +class CharacterMock + include Hyrax::CharacterizationBehavior + + def solr_document + attributes = {} + SolrDocument.new(attributes) + end +end + +RSpec.describe Hyrax::CharacterizationBehavior do + + subject { CharacterMock.new } + + def char_terms + [ + :byte_order, + :compression, + :height, + :width, + :height, + :color_space, + :profile_name, + :profile_version, + :orientation, + :color_map, + :image_producer, + :capture_device, + :scanning_software, + :gps_timestamp, + :latitude, + :longitude, + :file_format, + :file_title, + :page_count, + :duration, + :sample_rate, + :format_label, + :file_size_human_readable, + :filename, + :well_formed, + :last_modified, + :original_checksum, + :mime_type + ] + end + def char_terms_admin_only + %i[ + virus_scan_service + virus_scan_status + virus_scan_status_date + ] + end + + describe "#characterization_terms" do + it do + expect( CharacterMock.characterization_terms).to eq char_terms + end + end + + describe "#characterization_terms_admin_only" do + it do + expect( CharacterMock.characterization_terms_admin_only).to eq char_terms_admin_only + end + end + + + pending "#characterized?" + + + describe "#characterization_metadata" do + context "when instance variable is set" do + before { + subject.instance_variable_set(:@characterization_metadata, "characterization metadata") + } + it "returns value of instance variable" do + expect(subject.characterization_metadata).to eq "characterization metadata" + end + end + + context "when instance variable is not set" do + before { + allow(subject).to receive(:build_characterization_metadata).and_return "built" + } + it "returns value of instance variable" do + expect(subject.characterization_metadata).to eq "built" + end + end + end + + + describe "#characterization_metadata_admin_only" do + context "when instance variable is set" do + before { + subject.instance_variable_set(:@characterization_metadata_admin_only, "characterization metadata admin only") + } + it "returns value of instance variable" do + expect(subject.characterization_metadata_admin_only).to eq "characterization metadata admin only" + end + end + + context "when instance variable is not set" do + before { + allow(subject).to receive(:build_characterization_metadata_admin_only).and_return "built in a day" + } + it "returns result of build_characterization_metadata_admin_only" do + expect(subject).to receive(:build_characterization_metadata_admin_only).and_return "built in a day" + + expect(subject.characterization_metadata_admin_only).to eq "built in a day" + end + end + end + + + describe "#additional_characterization_metadata" do + context "when instance variable is set" do + before { + subject.instance_variable_set(:@additional_characterization_metadata, "additional characterization") + } + it "returns value of instance variable" do + expect(subject.additional_characterization_metadata).to eq "additional characterization" + end + end + + context "when instance variable is not set" do + it "returns empty hash" do + expect(subject.additional_characterization_metadata).to be_blank + end + end + end + + + describe "#additional_characterization_metadata_admin_only" do + context "when instance variable is set" do + before { + subject.instance_variable_set(:@additional_characterization_metadata_admin_only, "additional characterization admin only") + } + it "returns value of instance variable" do + expect(subject.additional_characterization_metadata_admin_only).to eq "additional characterization admin only" + end + end + + context "when instance variable is not set" do + it "returns empty hash" do + expect(subject.additional_characterization_metadata_admin_only).to be_blank + end + end + end + + + describe "#label_for_term" do + context "when I18n has translation data" do + before { + allow(MsgHelper).to receive(:t).with("show.file_set.label.1001", raise: true ).and_return "labelmaker" + } + it "returns term label" do + expect(subject.label_for_term "1001").to eq "labelmaker" + end + end + + context "when MissingTranslationData error occurs" do + it "returns term capitalized" do + skip "Add a test" + end + end + end + + + describe "#primary_characterization_values" do + context "when characterization_metadata returns results for term" do + before { + allow(Hyrax.config).to receive(:fits_message_length).and_return 5 + allow(subject).to receive(:characterization_metadata).and_return [ ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel leo quis sapien placerat fringilla. Fusce mattis metus orci, sit amet efficitur lectus blandit et. Vivamus rhoncus turpis eget maximus porttitor. Nunc sagittis consequat eros luctus semper.", "Lion", "Tortoise", "Tiger", "Leopard", "Flamingo"] ] + } + it "returns truncated values" do + expect(subject.primary_characterization_values 0).to eq ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel leo quis sapien placerat fringilla. Fusce mattis metus orci, sit amet efficitur lectus blandit et. Vivamus rhoncus turpis eget maximus porttitor. Nunc sagittis consequat eros luc...", "Lion", "Tortoise", "Tiger", "Leopard"] + end + end + end + + + describe "#primary_characterization_values_admin_only" do + context "when characterization_metadata_admin_only returns results for term" do + before { + allow(Hyrax.config).to receive(:fits_message_length).and_return 5 + allow(subject).to receive(:characterization_metadata_admin_only).and_return [ ["Maecenas sapien tortor, laoreet eu ipsum vitae, rutrum rhoncus magna. Maecenas suscipit vitae augue eu auctor. Nullam sed suscipit sapien. Pellentesque tincidunt dapibus nisi, a sagittis velit ultrices ac. Integer ut elit ut justo consequat vulputate sed vel sapien.", "Eagle", "Hippopotamus", "Falcon", "Wolf", "Gazelle"] ] + } + it "returns truncated values" do + expect(subject.primary_characterization_values_admin_only 0).to eq ["Maecenas sapien tortor, laoreet eu ipsum vitae, rutrum rhoncus magna. Maecenas suscipit vitae augue eu auctor. Nullam sed suscipit sapien. Pellentesque tincidunt dapibus nisi, a sagittis velit ultrices ac. Integer ut elit ut justo consequat vulput...", "Eagle", "Hippopotamus", "Falcon", "Wolf"] + end + end + end + + + describe "#secondary_characterization_values" do + context "when characterization_metadata returns results for term" do + before { + allow(Hyrax.config).to receive(:fits_message_length).and_return 5 + allow(subject).to receive(:characterization_metadata).and_return [ ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer vel leo quis sapien placerat fringilla. Fusce mattis metus orci, sit amet efficitur lectus blandit et. Vivamus rhoncus turpis eget maximus porttitor. Nunc sagittis consequat eros luctus semper.", "Lion", "Tortoise", "Tiger", "Leopard", "Flamingo"] ] + } + it "returns truncated values" do + expect(subject.secondary_characterization_values 0).to eq ["Flamingo"] + end + end + + context "when characterization_metadata does not return results" do + before { + allow(Hyrax.config).to receive(:fits_message_length).and_return 5 + } + it "returns empty array" do + expect(subject.secondary_characterization_values 0).to be_empty + end + end + end + + + describe "#secondary_characterization_values_admin_only" do + context "when characterization_metadata_admin_only returns results for term" do + before { + allow(Hyrax.config).to receive(:fits_message_length).and_return 5 + allow(subject).to receive(:characterization_metadata_admin_only).and_return [ ["Maecenas sapien tortor, laoreet eu ipsum vitae, rutrum rhoncus magna. Maecenas suscipit vitae augue eu auctor. Nullam sed suscipit sapien. Pellentesque tincidunt dapibus nisi, a sagittis velit ultrices ac. Integer ut elit ut justo consequat vulputate sed vel sapien.", "Eagle", "Hippopotamus", "Falcon", "Wolf", "Gazelle"] ] + } + it "returns truncated values" do + expect(subject.secondary_characterization_values_admin_only 0).to eq ["Gazelle"] + end + end + + context "when characterization_metadata_admin_only does not return results" do + before { + allow(Hyrax.config).to receive(:fits_message_length).and_return 5 + } + it "returns empty array" do + expect(subject.secondary_characterization_values_admin_only 0).to be_empty + end + end + end +end diff --git a/spec/presenters/hyrax/collection_presenter_spec.rb b/spec/presenters/hyrax/collection_presenter_spec.rb new file mode 100644 index 00000000..60bdc7c1 --- /dev/null +++ b/spec/presenters/hyrax/collection_presenter_spec.rb @@ -0,0 +1,554 @@ +require 'rails_helper' + +class ViewableItemMock + def initialize(items) + @items = items + end + def accessible_by(current_ability) + @items + end +end + +class ItemMock + def initialize(can) + @can = can + end + def can?(*args) + @can + end +end + +RSpec.describe Hyrax::CollectionPresenter do + let(:user) { FactoryBot.create :user } + let(:solr_document) { SolrDocument.new } + let(:current_ability) { instance_double(Ability, current_user: user ) } + let(:request) { double(host: 'example.org', base_url: 'http://example.org') } + + subject{ described_class.new(solr_document, current_ability, request) } + + describe '#create_work_presenter_class' do + it do + expect(Hyrax::HomepagePresenter.create_work_presenter_class).instance_of?(Hyrax::SelectTypeListPresenter) + end + end + + describe "delegates methods to solr_document:" do + [:stringify_keys, :human_readable_type, :collection?, :representative_id, :to_s, :title, :description, :creator, :contributor, + :subject, :publisher, :keyword, :language, :embargo_release_date, :lease_expiration_date, :license, :date_created, :resource_type, + :based_near, :related_url, :identifier, :thumbnail_path, :title_or_label, :collection_type_gid, :create_date, :modified_date, + :visibility, :edit_groups, :edit_people].each do + |method| + it "#{method}" do + expect(subject).to delegate_method(method).to(:solr_document) + end + end + end + + describe "#initialize" do + it "sets instance variables" do + subject.instance_variable_get(:@solr_document) == "solr" + subject.instance_variable_get(:@current_ability) == "current" + subject.instance_variable_get(:@request) == "request" + subject.instance_variable_get(:@subcollection_count) == 0 + end + end + + + pending "#delegate collection_type_settings_methods" + + + describe "#collection_type" do + before { + allow(Hyrax::CollectionType).to receive(:find_by_gid!).with("collection type gid").and_return "collection type" + } + + context "@collection_type has a value" do + before { + subject.instance_variable_set(:@collection_type, "assemblage") + } + it "returns @collection_type" do + expect(subject.collection_type).to eq "assemblage" + + expect(Hyrax::CollectionType).not_to have_received(:find_by_gid!) + end + end + + context "@collection_type has no value" do + before { + subject.instance_variable_set(:@collection_type, nil) + allow(subject).to receive(:collection_type_gid).and_return "collection type gid" + } + it "returns Hyrax::CollectionType.find_by_gid!" do + expect(Hyrax::CollectionType).to receive(:find_by_gid!).with("collection type gid") + + expect(subject.collection_type).to eq "collection type" + end + end + end + + + pending "#self.terms" + pending "#terms_with_values" + + + describe "#[]" do + context "when key is :size" do + before { + allow(subject).to receive(:size).and_return "sizing" + } + it "returns result of size" do + expect(subject[:size]).to eq "sizing" + end + end + + context "when key is :total_items" do + before { + allow(subject).to receive(:total_items).and_return "totality" + } + it "result of total_items" do + expect(subject[:total_items]).to eq "totality" + end + end + + context "when key is not :size or :total_items" do + before { + allow(subject.solr_document).to receive(:send).with(:other).and_return "sending" + } + it "calls solr_document.send" do + expect(subject[:other]).to eq "sending" + end + end + end + + describe "#display_provenance_log_enabled?" do + it "returns true" do + expect(subject.display_provenance_log_enabled?).to eq true + end + end + + # NOTE: provenance_log_entries? function exactly the same in ds_file_set_presenter + describe "#provenance_log_entries?" do + context "calls Deepblue::ProvenancePath.path_for_reference" do + before { + allow(subject).to receive(:id).and_return 1000 + allow(Deepblue::ProvenancePath).to receive(:path_for_reference).with(1000).and_return "file_path" + allow(File).to receive(:exist?).with("file_path").and_return true + } + it "returns whether file path exists" do + expect(Deepblue::ProvenancePath).to receive(:path_for_reference) + expect(File).to receive(:exist?).with("file_path") + + expect(subject.provenance_log_entries?).to eq true + end + end + end + + # NOTE: relative_url_root function exactly the same in ds_file_set_presenter, work_show_presenter + describe "#relative_url_root" do + context "when DeepBlueDocs::Application.config.relative_url_root has value" do + before { + allow(DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return "site root" + } + it "returns value" do + expect(DeepBlueDocs::Application.config).to receive(:relative_url_root) + expect(subject.relative_url_root).to eq "site root" + end + end + + context "when DeepBlueDocs::Application.config.relative_url_root is nil or false" do + before { + allow(DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return false + } + it "returns empty string" do + expect(DeepBlueDocs::Application.config).to receive(:relative_url_root) + expect(subject.relative_url_root).to be_blank + end + end + end + + describe "#size" do + before { + subject.instance_variable_set(:@solr_document, {'bytes_lts' => 123}) + allow(subject).to receive(:number_to_human_size).with(123).and_return "123 KB" + } + it "calls number_to_human_size" do + expect(subject).to receive(:number_to_human_size) + expect(subject.size).to eq "123 KB" + end + end + + describe "#total_items" do + before { + allow(subject).to receive(:id).and_return "XY-101" + allow(ActiveFedora::Base).to receive(:where).with( "member_of_collection_ids_ssim:XY-101" ).and_return ["mallard", "wood duck", "Canada goose"] + } + it "calls ActiveFedora::Base.where" do + expect(ActiveFedora::Base).to receive(:where) + expect(subject.total_items).to eq 3 + end + end + + describe "#total_viewable_items" do + before { + allow(subject).to receive(:id).and_return "XY-102" + allow(subject).to receive(:current_ability).and_return "current ability" + allow(ActiveFedora::Base).to receive(:where).with( "member_of_collection_ids_ssim:XY-102" ) + .and_return ViewableItemMock.new ['watermelon', 'honeydew', 'cantaloupe', 'muskmelon'] + } + it "calls ActiveFedora::Base.where" do + expect(ActiveFedora::Base).to receive(:where) + expect(subject.total_viewable_items).to eq 4 + end + end + + describe "#total_viewable_works" do + before { + allow(subject).to receive(:id).and_return "XY-103" + allow(subject).to receive(:current_ability).and_return "current ability" + + allow(Deepblue::LoggingHelper).to receive(:here).and_return "here" + allow(Deepblue::LoggingHelper).to receive(:called_from).and_return "from" + allow(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", + "from", + "id=XY-103", + "current_ability=current ability", + ""] + allow(ActiveFedora::Base).to receive(:where).with( "member_of_collection_ids_ssim:XY-103 AND generic_type_sim:Work" ) + .and_return ViewableItemMock.new ['a', 'b', 'c', 'd', 'e', 'f'] + } + it "calls Deepblue::LoggingHelper.bold_debug and ActiveFedora::Base.where" do + expect(Deepblue::LoggingHelper).to receive(:bold_debug).with ["here", + "from", + "id=XY-103", + "current_ability=current ability", + ""] + expect(ActiveFedora::Base).to receive(:where) + + expect(subject.total_viewable_works).to eq 6 + end + end + + describe "#total_viewable_collections" do + before { + allow(subject).to receive(:id).and_return "XY-104" + allow(subject).to receive(:current_ability).and_return "current ability" + + allow(ActiveFedora::Base).to receive(:where).with( "member_of_collection_ids_ssim:XY-104 AND generic_type_sim:Collection" ) + .and_return ViewableItemMock.new ['latte', 'mocha', 'affogatto'] + } + it "calls ActiveFedora::Base.where" do + expect(ActiveFedora::Base).to receive(:where).with( "member_of_collection_ids_ssim:XY-104 AND generic_type_sim:Collection" ) + + expect(subject.total_viewable_collections).to eq 3 + end + end + + describe "#collection_type_badge" do + before { + allow(subject).to receive(:collection_type).and_return( double(title: 'Example of Grandiosity') ) + } + it "returns collection_type.title" do + expect(subject.collection_type_badge).to eq 'Example of Grandiosity' + end + end + + describe "#total_parent_collections" do + context "when parent_collections is nil" do + before { + allow(subject).to receive(:parent_collections).and_return( nil ) + } + it "returns 0" do + expect(subject.total_parent_collections).to eq 0 + end + end + + context "when parent_collections exists" do + before { + allow(subject).to receive(:parent_collections).and_return( double( response: {"numFound" => 7} ) ) + } + it "returns parent_collections response numFound" do + expect(subject.total_parent_collections).to eq 7 + end + end + end + + describe "#parent_collection_count" do + context "when parent_collections is nil" do + before { + allow(subject).to receive(:parent_collections).and_return( nil ) + } + it "returns 0" do + expect(subject.parent_collection_count).to eq 0 + end + end + + context "when parent_collections exists" do + before { + allow(subject).to receive(:parent_collections).and_return( double(documents: ['1', '2', '3', '4', '5']) ) + } + it "returns parent_collections documents size" do + expect(subject.parent_collection_count).to eq 5 + end + end + end + + describe "#user_can_nest_collection?" do + before { + allow(subject).to receive(:solr_document).and_return( "solr" ) + allow(subject).to receive(:current_ability).and_return ItemMock.new true + } + it "calls current_ability.can?" do + expect(subject.user_can_nest_collection?).to eq true + end + end + + describe "#user_can_create_new_nest_collection?" do + before { + allow(subject).to receive(:collection_type).and_return( "color" ) + allow(subject).to receive(:current_ability).and_return ItemMock.new true + } + it "calls current_ability.can?" do + expect(subject.user_can_create_new_nest_collection?).to eq true + end + end + + describe "#show_path" do + before { + allow(subject).to receive(:id).and_return( "Z-50" ) + # Could not stub Hyrax::Engine.routes.url_helpers.dashboard_collection_path -- actually calling it + } + it "calls Hyrax::Engine.routes.url_helpers.dashboard_collection_path" do + expect(subject.show_path).to eq "/dashboard/collections/Z-50" + end + end + + describe "#banner_file" do + context "when id has a value" do + before { + allow(subject).to receive(:id).and_return( 5 ) + allow(subject).to receive(:branding_banner_file).with(id: 5).and_return( "method result" ) + } + it "returns branding_banner_file(id)" do + expect(subject.banner_file).to eq "method result" + end + end + end + + describe "#logo_record" do + context "when id has a value" do + before { + allow(subject).to receive(:id).and_return( 12 ) + allow(subject).to receive(:branding_logo_record).with(id: 12).and_return( "logo record" ) + } + it "returns branding_logo_record(id)" do + expect(subject.logo_record).to eq "logo record" + end + end + end + + describe "#create_work_presenter" do + + context "when @create_work_presenter has a value" do + before { + subject.instance_variable_set(:@create_work_presenter, "instance variable") + } + it "returns value" do + expect(subject.create_work_presenter_class).not_to receive(:new) + expect(subject.create_work_presenter).to eq "instance variable" + end + end + + context "when @create_work_presenter has no value" do + before { + allow(subject.create_work_presenter_class).to receive(:new).with(user).and_return "new method" + } + it "calls create_work_presenter_class.new and sets instance variable" do + expect(subject.create_work_presenter_class).to receive(:new) + expect(subject.create_work_presenter).to eq "new method" + + subject.instance_variable_get(:@create_work_presenter) == "new method" + end + end + end + + describe "#create_many_work_types?" do + context "when Flipflop.only_use_data_set_work_type? returns true" do + before { + allow(Flipflop).to receive(:only_use_data_set_work_type?).and_return true + } + it "returns false" do + expect(Flipflop).to receive(:only_use_data_set_work_type?) + expect(subject.create_work_presenter).not_to receive(:many?) + + expect(subject.create_many_work_types?).to eq false + end + end + + context "when Flipflop.only_use_data_set_work_type? returns false" do + before { + allow(Flipflop).to receive(:only_use_data_set_work_type?).and_return false + allow(subject.create_work_presenter).to receive(:many?).and_return true + } + it "returns create_work_presenter.many?" do + expect(Flipflop).to receive(:only_use_data_set_work_type?) + expect(subject.create_work_presenter).to receive(:many?) + + expect(subject.create_many_work_types?).to eq true + end + end + end + + describe "#draw_select_work_modal?" do + before { + allow(subject).to receive(:create_many_work_types?).and_return(true) + } + it "calls create_many_work_types?" do + expect(subject).to receive(:create_many_work_types?) + + expect(subject.draw_select_work_modal?).to eq true + end + end + + describe "#first_work_type" do + context "when create_work_presenter calls first_model" do + before { + allow(subject.create_work_presenter).to receive(:first_model).and_return "Alpha" + } + it "returns first_model" do + expect(subject.create_work_presenter).to receive(:first_model) + + expect(subject.first_work_type).to eq "Alpha" + end + end + end + + describe "#available_parent_collections" do + context "when @available_parents present" do + before { + subject.instance_variable_set(:@available_parents, "presentation") + } + it "returns @available_parents" do + expect(subject.available_parent_collections scope: "").to eq "presentation" + end + end + + context "when @available_parents not present" do + before { + allow(subject).to receive(:id).and_return 707 + allow(Collection).to receive(:find).with(707).and_return "la collection" + allow(Hyrax::Collections::NestedCollectionQueryService).to receive(:available_parent_collections).with(child: "la collection", scope: "scope", limit_to_id: nil). + and_return [OpenStruct.new(id: 1, title: ["pancake", "flapjack"]), OpenStruct.new(id: 2, title: ["crepe", "tortilla"])] + } + it "sets @available_parents and return as json" do + expect(Collection).to receive(:find) + expect(Hyrax::Collections::NestedCollectionQueryService).to receive(:available_parent_collections).with(child: "la collection", scope: "scope", limit_to_id: nil) + + expect(subject.available_parent_collections scope: "scope").to eq "[{\"id\":1,\"title_first\":\"pancake\"},{\"id\":2,\"title_first\":\"crepe\"}]" + subject.instance_variable_get(:@available_parents) == [{"id" => 1, "title_first" => "pancake"}, {"id" => 2, "title_first" => "crepe"}] + end + end + end + + describe "#subcollection_count=" do + context "when total is nil" do + it "returns nil" do + expect(subject.subcollection_count = nil).to be_nil + + subject.instance_variable_get(:@subcollection_count) == 0 + end + end + + context "when total has a value" do + it "returns total value and sets instance variable" do + expect(subject.subcollection_count = 99).to eq 99 + + subject.instance_variable_get(:@subcollection_count) == 99 + end + end + end + + describe "#managed_access" do + before { + allow(I18n).to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.manage').and_return "managed" + allow(I18n).to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.deposit').and_return "deposited" + allow(I18n).to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.view').and_return "viewed" + } + + context "when can edit" do + before { + allow(subject.current_ability).to receive(:can?).with(:edit, solr_document).and_return true + } + it "return manage access" do + expect(I18n).to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.manage') + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.deposit') + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.view') + + expect(subject.managed_access).to eq "managed" + end + end + + context "when can deposit but not edit" do + before { + allow(subject.current_ability).to receive(:can?).with(:edit, solr_document).and_return false + allow(subject.current_ability).to receive(:can?).with(:deposit, solr_document).and_return true + } + it "return deposit access" do + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.manage') + expect(I18n).to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.deposit') + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.view') + + expect(subject.managed_access).to eq "deposited" + end + end + + context "when can read but not edit or deposit" do + before { + allow(subject.current_ability).to receive(:can?).with(:edit, solr_document).and_return false + allow(subject.current_ability).to receive(:can?).with(:deposit, solr_document).and_return false + allow(subject.current_ability).to receive(:can?).with(:read, solr_document).and_return true + } + it "return view access" do + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.manage') + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.deposit') + expect(I18n).to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.view') + + expect(subject.managed_access).to eq "viewed" + end + end + + context "when no access" do + before { + allow(subject.current_ability).to receive(:can?).with(:edit, solr_document).and_return false + allow(subject.current_ability).to receive(:can?).with(:deposit, solr_document).and_return false + allow(subject.current_ability).to receive(:can?).with(:read, solr_document).and_return false + } + it "return empty string" do + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.manage') + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.deposit') + expect(I18n).not_to receive(:t).with('hyrax.dashboard.my.collection_list.managed_access.view') + + expect(subject.managed_access).to be_blank + end + end + end + + describe "#allow_batch?" do + context "when current_ability.can? returns true" do + before { + allow(subject).to receive(:current_ability).and_return ItemMock.new true + } + it "return true" do + expect(subject.allow_batch?).to eq true + end + end + + context "when current_ability.can? returns false" do + before { + allow(subject).to receive(:current_ability).and_return ItemMock.new false + } + it "return false" do + expect(subject.allow_batch?).to eq false + end + end + end +end diff --git a/spec/presenters/hyrax/data_set_presenter_spec.rb b/spec/presenters/hyrax/data_set_presenter_spec.rb index a8b89711..9649d5d3 100644 --- a/spec/presenters/hyrax/data_set_presenter_spec.rb +++ b/spec/presenters/hyrax/data_set_presenter_spec.rb @@ -19,26 +19,401 @@ let(:ability) { double Ability } let(:presenter) { described_class.new(solr_document, ability, request) } - it { is_expected.to delegate_method(:to_s).to(:solr_document) } -# it { is_expected.to delegate_method(:human_readable_type).to(:solr_document) } - it { is_expected.to delegate_method(:curation_notes_admin).to(:solr_document) } - it { is_expected.to delegate_method(:curation_notes_user).to(:solr_document) } - it { is_expected.to delegate_method(:date_created).to(:solr_document) } - it { is_expected.to delegate_method(:date_modified).to(:solr_document) } - it { is_expected.to delegate_method(:date_published).to(:solr_document) } - it { is_expected.to delegate_method(:date_uploaded).to(:solr_document) } - it { is_expected.to delegate_method(:fundedby).to(:solr_document) } - it { is_expected.to delegate_method(:fundedby_other).to(:solr_document) } - it { is_expected.to delegate_method(:rights_license).to(:solr_document) } - it { is_expected.to delegate_method(:rights_license_other).to(:solr_document) } - - it { is_expected.to delegate_method(:based_near_label).to(:solr_document) } - it { is_expected.to delegate_method(:related_url).to(:solr_document) } - it { is_expected.to delegate_method(:depositor).to(:solr_document) } - it { is_expected.to delegate_method(:identifier).to(:solr_document) } - it { is_expected.to delegate_method(:resource_type).to(:solr_document) } - it { is_expected.to delegate_method(:keyword).to(:solr_document) } - it { is_expected.to delegate_method(:itemtype).to(:solr_document) } + describe "delegates methods to solr_document:" do + [:authoremail, + :based_near_label, + :curation_notes_admin, + :curation_notes_user, + :date_created, :date_modified, + :date_published, :date_published2, + :date_uploaded, + :depositor, + :doi, :doi_the_correct_one, + :doi_minted?, + :doi_minting_enabled?, + :doi_pending?, + :fundedby, + :fundedby_other, + :grantnumber, + :methodology, + :prior_identifier, + :referenced_by, + :related_url, + :rights_license, + :rights_license_other, + :subject_discipline, + :access_deepblue, + :geo_location_place, + :geo_location_box, + :license_other, + :academic_affiliation, + :alt_title, + :bibliographic_citation, + :contributor_affiliationumcampus, + :date_attribute, + :date_issued, + :description_abstract, + :description_mapping, + :description_sponsorship, + :external_link, + :identifier, + :identifier_orcid, + :identifier_source, + :itemtype, + :keyword, + :language_none, + :linked, + :other_affiliation, + :peerreviewed, + :relation_ispartofseries, + :resource_type, + :to_s, + :type_none].each do + |method| + it "#{method}" do + expect(subject).to delegate_method(method).to(:solr_document) + end + end + end + + pending "delegates date_coverage and total_file_size to solr_document" + + describe "#box_enabled?" do + before { + allow(DeepBlueDocs::Application.config).to receive(:box_integration_enabled).and_return "box integration enabled" + } + it "returns result of DeepBlueDocs::Application.config.box_integration_enabled" do + expect(subject.box_enabled?).to eq "box integration enabled" + end + end + + describe "#box_link" do + context "when box_enabled? is false" do + before { + allow(subject).to receive(:box_enabled?).and_return false + } + it "returns nil" do + expect(subject.box_link).to be_blank + end + end + + context "when box_enabled? is true" do + it "returns result of BoxHelper.box_link" do + skip "Add a test" + end + end + end + + + describe "#box_link_display_for_work?" do + context "when box_enabled? is false" do + before { + allow(subject).to receive(:box_enabled?).and_return false + } + it "returns false" do + expect(subject.box_link_display_for_work? "current user").to eq false + end + end + + context "when box_enabled? is true" do + it "returns result of BoxHelper.box_link_display_for_work?" do + skip "Add a test" + end + end + end + + + describe "#date_coverage" do + context "when @solr_document.date_coverage is blank" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(date_coverage: [])) + } + it "returns nil" do + expect(subject.date_coverage).to be_blank + end + end + + context "when @solr_document.date_coverage contains '/open'" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(date_coverage: "/open/hello world")) + } + it "returns substring" do + expect(subject.date_coverage).to eq "/hello world" + end + end + + context "when @solr_document.date_coverage does not contain '/open'" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(date_coverage: "/closed/hello world")) + } + it "returns substring" do + expect(subject.date_coverage).to eq " to closed/hello world" + end + end + end + + + describe "#display_provenance_log_enabled?" do + it "returns true" do + expect(subject.display_provenance_log_enabled?).to eq true + end + end + + + describe "#provenance_log_entries?" do + before { + allow(subject).to receive(:id).and_return 1234 + allow(Deepblue::ProvenancePath).to receive(:path_for_reference).with(1234).and_return "file path" + } + + context "when path_for_reference exists" do + before { + allow(File).to receive(:exist?).with("file path").and_return true + } + it "returns true" do + expect(subject.provenance_log_entries?).to eq true + end + end + + context "when path_for_reference does not exist" do + before { + allow(File).to receive(:exist?).with("file path").and_return false + } + it "returns false" do + expect(subject.provenance_log_entries?).to eq false + end + end + end + + + describe "#globus_download_enabled?" do + before { + allow(DeepBlueDocs::Application.config).to receive(:globus_enabled).and_return true + } + it "returns config globus_enabled" do + expect(subject.globus_download_enabled?).to eq true + end + end + + + describe "#globus_enabled?" do + before { + allow(DeepBlueDocs::Application.config).to receive(:globus_enabled).and_return true + } + it "returns config globus_enabled" do + expect(subject.globus_download_enabled?).to eq true + end + end + + + describe "#globus_external_url" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(id: 999)) + allow(::GlobusJob).to receive(:external_url).with(999).and_return "external url" + } + it "returns ::GlobusJob.external_url" do + expect(subject.globus_external_url).to eq "external url" + end + end + + + describe "#globus_files_available?" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(id: 999)) + allow(::GlobusJob).to receive(:files_available?).with(999).and_return true + } + it "returns ::GlobusJob.files_available?" do + expect(subject.globus_files_available?).to eq true + end + end + + + describe "#globus_files_prepping?" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(id: 999)) + allow(::GlobusJob).to receive(:files_prepping?).with(999).and_return true + } + it "returns ::GlobusJob.files_prepping?" do + expect(subject.globus_files_prepping?).to eq true + end + end + + + describe "#globus_last_error_msg" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(id: 999)) + allow(::GlobusJob).to receive(:error_file_contents).with(999).and_return "last message" + } + it "returns ::GlobusJob.error_file_contents?" do + expect(subject.globus_last_error_msg).to eq "last message" + end + end + + + describe "#hdl" do + it "returns nil" do + expect(subject.hdl).to be_blank + end + end + + + describe "#human_readable" do + before { + allow(ActiveSupport::NumberHelper::NumberToHumanSizeConverter).to receive(:convert).with("value", precision: 3).and_return "human readable value" + } + it "returns human readable value" do + expect(subject.human_readable "value").to eq "human readable value" + end + end + + + describe "#label_with_total_file_size" do + context "when total_file_size is zero" do + before { + allow(subject).to receive(:total_file_size).and_return 0 + } + it "returns the value passed in" do + expect(subject.label_with_total_file_size "label passed in").to eq "label passed in" + end + end + + context "when total_file_size is one" do + before { + allow(subject).to receive(:total_file_size).and_return 100 + allow(subject).to receive(:total_file_count).and_return 1 + allow(subject).to receive(:total_file_size_human_readable).and_return "lots of kilobytes" + } + it "returns the value passed in" do + expect(subject.label_with_total_file_size "This").to eq "This (lots of kilobytes in 1 file)" + end + end + + context "when total_file_size is more than one" do + before { + allow(subject).to receive(:total_file_size).and_return 1000 + allow(subject).to receive(:total_file_count).and_return 10 + allow(subject).to receive(:total_file_size_human_readable).and_return "lots of MB" + } + it "returns the value passed in" do + expect(subject.label_with_total_file_size "That").to eq "That (lots of MB in 10 files)" + end + end + end + + + describe "#tombstone" do + context "when @solr_document is blank" do + before { + subject.instance_variable_set(:@solr_document, nil) + } + it "returns nil" do + expect(subject.tombstone).to be_blank + end + end + + context "when @solr_document is not blank" do + before { + allow(Solrizer).to receive(:solr_name).with("tombstone", :symbol).and_return 0 + } + context "when tombstone is not blank" do + before { + subject.instance_variable_set(:@solr_document, [["mausoleum", "crypt"], "monument", "epitaph"]) + } + it "returns tombstone" do + expect(subject.tombstone).to eq "mausoleum" + end + end + + context "when tombstone is blank" do + before { + subject.instance_variable_set(:@solr_document, [" ", "pyramid"]) + } + it "returns nil" do + expect(subject.tombstone).to be_blank + end + end + end + end + + + describe "#tombstone_enabled?" do + it "returns true" do + expect(subject.tombstone_enabled?).to eq true + end + end + + + describe "#total_file_count" do + before { + allow(Solrizer).to receive(:solr_name).with("file_set_ids", :symbol).and_return 1 + } + + context "when file set ids are blank" do + before { + subject.instance_variable_set(:@solr_document, ["a", "", "c"]) + } + it "returns 0" do + expect(subject.total_file_count).to eq 0 + end + end + + context "when file set ids are not blank" do + before { + subject.instance_variable_set(:@solr_document, ["a", OpenStruct.new(size: 99), "c"]) + } + it "returns size parameter" do + expect(subject.total_file_count).to eq 99 + end + end + end + + + describe "#total_file_size" do + before { + allow(Solrizer).to receive(:solr_name).with("total_file_size", Hyrax::FileSetIndexer::STORED_LONG).and_return 2 + } + + context "when total file size is blank" do + before { + subject.instance_variable_set(:@solr_document, ["a", "b", ""]) + } + it "returns 0" do + expect(subject.total_file_size).to eq 0 + end + end + + context "when total file size is not blank" do + before { + subject.instance_variable_set(:@solr_document, ["a", "b", "total file size"]) + } + it "returns size parameter" do + expect(subject.total_file_size).to eq "total file size" + end + end + end + + + describe "#total_file_size_human_readable" do + before { + allow(subject).to receive(:total_file_size).and_return "total file size" + allow(subject).to receive(:human_readable).with("total file size").and_return "total file size human readable" + } + it "passes total_file_size to human_readable" do + expect(subject.total_file_size_human_readable).to eq "total file size human readable" + end + end + + + describe "#zip_download_enabled?" do + before { + allow(Settings).to receive(:zip_download_enabled).and_return true + } + it "returns zip_download_enabled from Settings" do + expect(subject.zip_download_enabled?).to eq true + end + end + + describe "#relative_url_root" do subject { presenter.relative_url_root } diff --git a/spec/presenters/hyrax/deep_blue_presenter_spec.rb b/spec/presenters/hyrax/deep_blue_presenter_spec.rb new file mode 100644 index 00000000..8ebe1797 --- /dev/null +++ b/spec/presenters/hyrax/deep_blue_presenter_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' + +RSpec.describe Hyrax::DeepbluePresenter do + let(:user) { FactoryBot.create :user } + let(:attributes) do {} end + let(:solr_document) { SolrDocument.new(attributes) } + let(:current_ability) { instance_double(Ability, current_user: user ) } + + subject { described_class.new(solr_document, current_ability) } + + describe '#box_enabled?' do + it 'returns false' do + expect(subject.box_enabled?).to eq false + end + end + + describe '#display_provenance_log_enabled?' do + it 'returns false' do + expect(subject.display_provenance_log_enabled?).to eq false + end + end + + describe '#doi_minting_enabled?' do + it 'returns false' do + expect(subject.doi_minting_enabled?).to eq false + end + end + + describe '#globus_download_enabled?' do + it 'returns false' do + expect(subject.globus_download_enabled?).to eq false + end + end + + describe '#human_readable_type' do + it 'returns Work text' do + expect(subject.human_readable_type).to eq 'Work' + end + end + + describe '#zip_download_enabled?' do + it 'returns false' do + expect(subject.zip_download_enabled?).to eq false + end + end +end diff --git a/spec/presenters/hyrax/ds_file_set_presenter_spec.rb b/spec/presenters/hyrax/ds_file_set_presenter_spec.rb new file mode 100644 index 00000000..36987f7a --- /dev/null +++ b/spec/presenters/hyrax/ds_file_set_presenter_spec.rb @@ -0,0 +1,201 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Hyrax::DsFileSetPresenter do + subject { described_class.new(double, double) } + let(:solr_document) { SolrDocument.new(attributes) } + let(:request) { double(host: 'example.org', base_url: 'http://example.org') } + + let(:ability) { double Ability } + let(:presenter) { described_class.new(solr_document, ability, request) } + + describe "delegates methods to solr_document:" do + [:doi, :doi_the_correct_one, :doi_minted?, :doi_minting_enabled?, :doi_pending?, :file_size, :file_size_human_readable, + :original_checksum, :mime_type, :title, :virus_scan_service, :virus_scan_status, :virus_scan_status_date].each do + |method| + it "#{method}" do + expect(subject).to delegate_method(method).to(:solr_document) + end + end + end + + # NOTE: relative_url_root function exactly the same in collection_presenter, work_show_presenter + describe "#relative_url_root" do + context "when DeepBlueDocs::Application.config.relative_url_root has value" do + before { + allow(DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return "site root" + } + it "returns value" do + expect(DeepBlueDocs::Application.config).to receive(:relative_url_root) + expect(subject.relative_url_root).to eq "site root" + end + end + + context "when DeepBlueDocs::Application.config.relative_url_root is nil or false" do + before { + allow(DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return false + } + it "returns empty string" do + expect(DeepBlueDocs::Application.config).to receive(:relative_url_root) + expect(subject.relative_url_root).to be_blank + end + end + end + + describe "#parent_doi_minted?" do + context "calls DataSet.find on parent.id" do + before { + allow(subject).to receive(:parent).and_return OpenStruct.new(id: 222) + allow(DataSet).to receive(:find).with(222).and_return OpenStruct.new(doi_minted?: true) + } + it "returns value of doi_minted?" do + expect(DataSet).to receive(:find).with(222) + expect(subject.parent_doi_minted?).to eq true + end + end + end + + describe '#display_provenance_log_enabled?' do + it 'returns true' do + expect(subject.display_provenance_log_enabled?).to eq true + end + end + + # NOTE: provenance_log_entries? function exactly the same in collection_presenter + describe "#provenance_log_entries?" do + context "calls Deepblue::ProvenancePath.path_for_reference" do + before { + allow(subject).to receive(:id).and_return 1000 + allow(Deepblue::ProvenancePath).to receive(:path_for_reference).with(1000).and_return "file_path" + allow(File).to receive(:exist?).with("file_path").and_return true + } + it "returns whether file path exists" do + expect(Deepblue::ProvenancePath).to receive(:path_for_reference) + expect(File).to receive(:exist?).with("file_path") + + expect(subject.provenance_log_entries?).to eq true + end + end + end + + describe "#parent_public?" do + context "calls DataSet.find on parent.id" do + before { + allow(subject).to receive(:parent).and_return OpenStruct.new(id: 333) + allow(DataSet).to receive(:find).with(333).and_return OpenStruct.new(public?: true) + } + it "returns value of public?" do + expect(DataSet).to receive(:find).with(333) + expect(subject.parent_public?).to eq true + end + end + end + + describe "#first_title" do + context "when the document has no title" do + before { + allow(subject).to receive(:title).and_return([]) + } + it "returns string \'File\'" do + expect(subject.first_title).to eq "File" + end + end + + context "when the document has at least one title" do + before { + allow(subject).to receive(:title).and_return(["Descriptive Title", "Obtuse Title"]) + } + it "returns first title" do + expect(subject.first_title).to eq "Descriptive Title" + end + end + end + + describe "#link_name" do + abilities = [{"admin" => true, "can" => true, "expected_result" => true}, + {"admin" => true, "can" => false, "expected_result" => true}, + {"admin" => false, "can" => true, "expected_result" => true}, + {"admin" => false, "can" => false, "expected_result" => false}] + + before { + allow(subject).to receive(:id).and_return 3000 + allow(subject).to receive(:first_title).and_return "Red Hot Spectacular" + } + + abilities.each do |ability| + context "when current_ability.admin? is #{ability[:admin]} and current_ability.can?[:read id] is #{ability[:can]}" do + before { + allow(subject.current_ability).to receive(:admin?).and_return ability[:admin] + allow(subject.current_ability).to receive(:can?).with(:read, 3000).and_return ability[:can] + } + + if ability[:expected_result] + it "calls first_title" do + expect(subject).to receive(:first_title) + expect(subject.link_name).to eq "Red Hot Spectacular" + end + else + it "returns string 'File'" do + expect(subject.link_name).to eq "File" + end + end + end + end + end + + describe "#file_name" do + before { + allow(subject).to receive(:link_name).and_return "link name" + } + + context "when tombstone has a value" do + it "return link_name" do + expect(subject).not_to receive(:file_size_too_large_to_download?) + expect(subject.file_name OpenStruct.new(tombstone: "monument"), "link to").to eq "link name" + end + end + + context "when tombstone does not have a value and file_size_too_large_to_download?" do + before { + allow(subject).to receive(:file_size_too_large_to_download?).and_return true + } + it "return link_name" do + expect(subject).to receive(:file_size_too_large_to_download?) + expect(subject.file_name OpenStruct.new(tombstone: nil), "link to").to eq "link name" + end + end + + context "when tombstone does not have a value and not file_size_too_large_to_download?" do + before { + allow(subject).to receive(:file_size_too_large_to_download?).and_return false + } + it "return link_to argument" do + expect(subject).to receive(:file_size_too_large_to_download?) + expect(subject.file_name OpenStruct.new(tombstone: nil), "link to").to eq "link to" + end + end + end + + describe "#file_size_too_large_to_download?" do + expected_results = [OpenStruct.new(file_size: nil, expected_result: false, comparison: "file size is nil" ), + OpenStruct.new(file_size: 6, expected_result: false, comparison: "file size is less than max_work_file_size_to_download" ), + OpenStruct.new(file_size: 7, expected_result: true, comparison: "file size is equal to max_work_file_size_to_download" ), + OpenStruct.new(file_size: 9, expected_result: true, comparison: "file size is greater than max_work_file_size_to_download" )] + before { + allow(DeepBlueDocs::Application.config).to receive(:max_work_file_size_to_download).and_return 7 + } + + expected_results.each { |n| + context "when #{n.comparison}" do + before { + subject.instance_variable_set(:@solr_document, OpenStruct.new(file_size: n.file_size)) + } + it "returns #{n.expected_result}" do + expect(subject.file_size_too_large_to_download?).to eq(n.expected_result) + end + end + } + end + +end diff --git a/spec/presenters/hyrax/generic_work_presenter_spec.rb b/spec/presenters/hyrax/generic_work_presenter_spec.rb index c8f65368..c26a0805 100644 --- a/spec/presenters/hyrax/generic_work_presenter_spec.rb +++ b/spec/presenters/hyrax/generic_work_presenter_spec.rb @@ -1,9 +1,22 @@ -# Generated via -# `rails generate hyrax:work GenericWork` require 'rails_helper' RSpec.describe Hyrax::GenericWorkPresenter do - it "has tests" do - skip "Add your tests here" + let(:user) { FactoryBot.create :user } + let(:attributes) do {} end + let(:solr_document) { SolrDocument.new(attributes) } + let(:current_ability) { instance_double(Ability, current_user: user ) } + + subject { described_class.new(solr_document, current_ability) } + + describe "delegates methods to solr_document:" do + [:identifier_orcid, :academic_affiliation, :other_affiliation, :contributor_affiliationumcampus, :alt_title, :date_issued, + :identifier_source, :peerreviewed, :bibliographic_citation, :relation_ispartofseries, :rights_statement, :type_none, + :language_none, :description_mapping, :description_abstract, :description_sponsorship, :description].each do + |method| + it "#{method}" do + expect(subject).to delegate_method(method).to(:solr_document) + end + end end + end diff --git a/spec/presenters/hyrax/homepage_presenter_spec.rb b/spec/presenters/hyrax/homepage_presenter_spec.rb new file mode 100644 index 00000000..be41a92f --- /dev/null +++ b/spec/presenters/hyrax/homepage_presenter_spec.rb @@ -0,0 +1,155 @@ +require 'rails_helper' + +RSpec.describe Hyrax::HomepagePresenter do + let(:user) { FactoryBot.create :user } + let(:current_ability) { instance_double(Ability, current_user: user ) } + let(:blacklight_config) { Blacklight::Configuration.new } + let(:collections) { Hyrax::CollectionSearchBuilder.new(self).rows(1) } + + subject{ described_class.new(current_ability, collections) } + + describe '#create_work_presenter_class' do + it do + expect(Hyrax::HomepagePresenter.create_work_presenter_class).instance_of?(Hyrax::SelectTypeListPresenter) + end + end + + describe '#initialize' do + it "sets instance variables using parameters" do + present = Hyrax::HomepagePresenter.new("current ability", "collections") + + present.instance_variable_get(:@current_ability) == "current ability" + present.instance_variable_get(:@collections) == "collections" + end + end + + describe "#display_share_button?" do + expected_results = [OpenStruct.new( user_unregistered: true, not_logged_in: true, can_create: true, expected_result: true ), + OpenStruct.new( user_unregistered: false, not_logged_in: false, can_create: true, expected_result: true ), + OpenStruct.new( user_unregistered: true, not_logged_in: false, can_create: false, expected_result: false ), + OpenStruct.new( user_unregistered: false, not_logged_in: true, can_create: false, expected_result: false ), + OpenStruct.new( user_unregistered: false, not_logged_in: false, can_create: false, expected_result: false )] + + expected_results.each { |n| + context "when user_unregistered? is #{n.user_unregistered}, display_share_button_when_not_logged_in? is #{n.not_logged_in}, and can_create_any_work? is #{n.can_create}" do + before { + allow(subject).to receive(:user_unregistered?).and_return n.user_unregistered + allow(Hyrax.config).to receive(:display_share_button_when_not_logged_in?).and_return n.not_logged_in + allow(subject.current_ability).to receive(:can_create_any_work?).and_return n.can_create + } + it "returns #{n.expected_result}" do + expect(subject).to receive(:user_unregistered?) + + if n.user_unregistered + expect(Hyrax.config).to receive(:display_share_button_when_not_logged_in?) + else + expect(Hyrax.config).not_to receive(:display_share_button_when_not_logged_in?) + end + + if !n.user_unregistered or !n.not_logged_in + expect(subject.current_ability).to receive(:can_create_any_work?) + else + expect(subject.current_ability).not_to receive(:can_create_any_work?) + end + + expect(subject.display_share_button?).to eq(n.expected_result) + end + end + } + end + + describe "#create_work_presenter" do + context "when @create_work_presenter has a value" do + before { + subject.instance_variable_set(:@create_work_presenter, "create work") + } + it 'returns @create_work_presenter value' do + expect(subject.create_work_presenter_class).not_to receive(:new).with(user) + expect(subject.create_work_presenter).to eq "create work" + end + end + + context "when @create_work_presenter has no value" do + before { + allow(subject.create_work_presenter_class).to receive(:new).with(user).and_return "new class" + } + it 'calls create_work_presenter_class.new and sets instance variable' do + expect(subject.create_work_presenter_class).to receive(:new).with(user) + expect(subject.create_work_presenter).to eq "new class" + + subject.instance_variable_get(:@create_work_presenter) == "new class" + end + end + end + + describe "#create_many_work_types?" do + before { + allow(subject.create_work_presenter).to receive(:many?).and_return true + } + + context "when Flipflop.only_use_data_set_work_type? is true" do + before { + allow(Flipflop).to receive(:only_use_data_set_work_type?).and_return true + } + it "returns false" do + expect(subject.create_work_presenter).not_to receive(:many?) + + expect(subject.create_many_work_types?).to eq false + end + end + + context "when Flipflop.only_use_data_set_work_type? is false" do + before { + allow(Flipflop).to receive(:only_use_data_set_work_type?).and_return false + } + it "returns create_work_presenter.many?" do + expect(subject.create_work_presenter).to receive(:many?) + + expect(subject.create_many_work_types?).to eq true + end + end + + after { + expect(Flipflop).to have_received(:only_use_data_set_work_type?) + } + end + + describe "#draw_select_work_modal?" do + factors = [{"display" => true, "create" => true, "expected_result" => true}, + {"display" => false, "create" => false, "expected_result" => false}, + {"display" => true, "create" => false, "expected_result" => false}, + {"display" => false, "create" => true, "expected_result" => false}] + + factors.each { |factor| + context "display_share_button? is #{factor[:display]} and create_many_work_types? is #{factor[:create]}" do |it| + before { + allow(subject).to receive(:display_share_button?).and_return factor[:display] + allow(subject).to receive(:create_many_work_types?).and_return factor[:create] + } + it "returns #{factor[:expected_result]}" do + expect(subject).to receive(:display_share_button?) + + if factor[:display] + expect(subject).to receive(:create_many_work_types?) + end + + expect(subject.draw_select_work_modal?).to eq factor[:expected_result] + end + end + } + end + + + describe "#first_work_type" do + context "create_work_presenter has at least one model" do + before { + allow(subject.create_work_presenter).to receive(:authorized_models).and_return ["Uno", "Dos"] + } + it "returns first model" do + expect(subject.first_work_type).to eq "Uno" + end + end + end + + +end diff --git a/spec/presenters/hyrax/member_presenter_factory_spec.rb b/spec/presenters/hyrax/member_presenter_factory_spec.rb new file mode 100644 index 00000000..18a4357a --- /dev/null +++ b/spec/presenters/hyrax/member_presenter_factory_spec.rb @@ -0,0 +1,116 @@ +require 'rails_helper' + +RSpec.describe Hyrax::MemberPresenterFactory do + + subject { described_class.new(double, double) } + + describe 'MemberPresenterFactory class' do + it do + expect(Hyrax::MemberPresenterFactory.file_presenter_class).instance_of? Hyrax::DsFileSetPresenter + expect(Hyrax::MemberPresenterFactory.work_presenter_class).instance_of? Hyrax::WorkShowPresenter + end + end + + describe '#initialize' do + it "sets instance variables" do + factory = Hyrax::MemberPresenterFactory.new("work", "ability", "request") + + factory.instance_variable_get(:@work) == "work" + factory.instance_variable_get(:@current_ability) == "ability" + factory.instance_variable_get(:@request) == "request" + end + end + + describe "delegates method to @work" do + it "#id" do + skip "Add a test" + end + end + + describe "#member_presenters" do + before { + allow(subject).to receive(:ordered_ids).and_return "ordered" + allow(subject).to receive(:composite_presenter_class).and_return "composite presenter" + allow(subject).to receive(:presenter_factory_arguments).and_return "arguments" + allow(Hyrax::PresenterFactory).to receive(:build_for).with(ids: "ordered", presenter_class: "composite presenter", presenter_args: "arguments") + .and_return "built with arguments" + } + it "calls PresenterFactory.build_for" do + expect(subject.member_presenters).to eq "built with arguments" + end + end + + describe "#file_set_presenters" do + context "when @file_set_presenters has a value" do + before { + subject.instance_variable_set(:@file_set_presenters, "create files") + } + it 'returns @file_set_presenters value' do + expect(subject).not_to receive(:member_presenters) + expect(subject.file_set_presenters).to eq "create files" + end + end + + # the & symbol is intersection for arrays in Ruby (| is union) + context "when @file_set_presenters has no value" do + before { + allow(subject).to receive(:ordered_ids).and_return [ 1, 2, 3, 4, 5, 6 ] + allow(subject).to receive(:file_set_ids).and_return [ 5, 6, 7, 8, 9, 10 ] + allow(subject).to receive(:member_presenters).with([ 5, 6 ]).and_return "file set presenters" + } + it 'calls member_presenters and sets the instance variable' do + expect(subject).to receive(:member_presenters).with( [ 5, 6 ] ) + expect(subject.file_set_presenters).to eq "file set presenters" + + subject.instance_variable_get(:@file_set_presenters) == "file set presenters" + end + end + end + + describe "#work_presenters" do + context "when @work_presenters has a value" do + before { + subject.instance_variable_set(:@work_presenters, "create work") + } + it 'returns @work_presenters value' do + expect(subject).not_to receive(:member_presenters) + expect(subject.work_presenters).to eq "create work" + end + end + + context "when @work_presenters has no value" do + before { + allow(subject).to receive(:ordered_ids).and_return [ 1, 2, 3, 4, 5, 6 ] + allow(subject).to receive(:file_set_ids).and_return [ 5, 6, 7 ] + allow(subject).to receive(:work_presenter_class).and_return "presenter class" + allow(subject).to receive(:member_presenters).with([ 1, 2, 3, 4 ], "presenter class").and_return "member presenters" + } + it 'calls member_presenters and sets the instance variable' do + expect(subject).to receive(:member_presenters).with( [ 1, 2, 3, 4 ], "presenter class" ) + expect(subject.work_presenters).to eq "member presenters" + + subject.instance_variable_get(:@work_presenters) == "member presenters" + end + end + end + + + describe "#ordered_ids" do + context "when @ordered_ids has a value" do + before { + subject.instance_variable_set(:@ordered_ids, "create ordered works") + } + it 'returns @ordered_ids value' do + expect(ActiveFedora::SolrService).not_to receive(:query) + expect(subject.ordered_ids).to eq "create ordered works" + end + end + end + + context "when @ordered_ids has no value" do + it "calls ActiveFedora::SolrService.query and sets the instance variable" do + skip "Add a test" + end + end + +end diff --git a/spec/presenters/hyrax/work_show_presenter_spec.rb b/spec/presenters/hyrax/work_show_presenter_spec.rb new file mode 100644 index 00000000..7d8726d0 --- /dev/null +++ b/spec/presenters/hyrax/work_show_presenter_spec.rb @@ -0,0 +1,99 @@ +require 'rails_helper' + +RSpec.describe Hyrax::WorkShowPresenter do + let(:user) { FactoryBot.create :user } + let(:attributes) do {} end + let(:solr_document) { SolrDocument.new(attributes) } + let(:current_ability) { instance_double(Ability, current_user: user ) } + let(:request) { double(host: 'example.org', base_url: 'http://example.org') } + + subject { described_class.new(solr_document, current_ability, request) } + + before do + allow(subject).to receive(:id).and_return("XYZ") + allow(subject).to receive(:title).and_return(["Fantastic Title", "Descriptive Title"]) + end + + # NOTE: relative_url_root function exactly the same in collection_presenter, work_show_presenter + describe "#relative_url_root" do + context "when DeepBlueDocs::Application.config.relative_url_root has value" do + before { + allow(DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return "site root" + } + it "returns value" do + expect(DeepBlueDocs::Application.config).to receive(:relative_url_root) + expect(subject.relative_url_root).to eq "site root" + end + end + + context "when DeepBlueDocs::Application.config.relative_url_root is nil or false" do + before { + allow(DeepBlueDocs::Application.config).to receive(:relative_url_root).and_return false + } + it "returns empty string" do + expect(DeepBlueDocs::Application.config).to receive(:relative_url_root) + expect(subject.relative_url_root).to be_blank + end + end + end + + describe '#page_title' do + before { + allow(I18n).to receive(:t).with( "hyrax.product_name").and_return "the best of the best" + } + + context "human_readable_type returns \'Work\'" do + before { + allow(subject).to receive(:human_readable_type).and_return("Work") + } + it 'returns page title when Work' do + expect(subject.page_title).to eq 'Data Set | Fantastic Title | ID: XYZ | the best of the best' + end + end + + context "human_readable_type returns \'Not Work\'" do + before { + allow(subject).to receive(:human_readable_type).and_return("Not Work") + } + it 'returns page title when not Work' do + expect(subject.page_title).to eq 'Not Work | Fantastic Title | ID: XYZ | the best of the best' + end + end + + after { + expect(I18n).to have_received(:t) + } + end + + + describe "#tombstone" do + before { + allow(Solrizer).to receive(:solr_name).with("tombstone", :symbol).and_return "mausoleum" + } + + context "when tombstone solr_name is blank" do + before { + subject.instance_variable_set(:@solr_document, {"mausoleum" => []}) + } + it "returns nil" do + expect(subject.tombstone.nil?) == true + end + end + + context "when tombstone solr_name has at least one value" do + before { + subject.instance_variable_set(:@solr_document, {"mausoleum" => ["raven", "crow"]}) + } + it "returns the first value" do + expect(subject.tombstone).to eq "raven" + end + end + end + + + describe "#tombstone_enabled?" do + it "returns true" do + expect(subject.tombstone_enabled?).to eq true + end + end +end diff --git a/spec/presenters/provenance_log_presenter_spec.rb b/spec/presenters/provenance_log_presenter_spec.rb new file mode 100644 index 00000000..b44c899a --- /dev/null +++ b/spec/presenters/provenance_log_presenter_spec.rb @@ -0,0 +1,108 @@ +require 'rails_helper' + +RSpec.describe ProvenanceLogPresenter do + + let(:controller) { instance_double(ProvenanceLogController) } + + subject { described_class.new(controller: controller) } + + describe "delegates methods to controller:" do + [:id, :id_msg, :id_invalid, :id_deleted, :id_valid?, :deleted_ids, :deleted_id_to_key_values_map].each do + |method| + it "#{method}" do + expect(subject).to delegate_method(method).to(:controller) + end + end + end + + + describe "#initialize" do + it "sets instance variable to arguments" do + ProvenanceLogPresenter.new controller: "controller" + + subject.instance_variable_get(:@controller) == "controller" + end + end + + + describe "#display_title" do + context "when argument is blank" do + it "returns empty string" do + expect(subject.display_title []).to be_blank + end + end + + context "when argument is array of strings" do + it "returns whitespace delimited concatenated string of array values" do + expect(subject.display_title ["uno", "dos", "tres"]).to eq "uno dos tres" + end + end + end + + + describe "#provenance_log_entries?" do + context "if id is blank" do + before { + allow(controller).to receive(:id).and_return(nil) + } + it "returns false" do + expect(::Deepblue::ProvenancePath).not_to receive(:path_for_reference) + + expect(subject.provenance_log_entries?).to eq false + end + end + + context "if id is not blank" do + before { + allow(controller).to receive(:id).and_return(3001) + allow(::Deepblue::ProvenancePath).to receive(:path_for_reference).with(3001).and_return "file path" + } + + context "when file exists" do + before { + allow(File).to receive(:exist?).and_return true + } + it "returns true" do + expect(subject.provenance_log_entries?).to eq true + end + end + + context "when file does not exist" do + before { + allow(File).to receive(:exist?).and_return false + } + it "returns false" do + expect(subject.provenance_log_entries?).to eq false + end + end + + after { + expect(::Deepblue::ProvenancePath).to have_received(:path_for_reference) + } + end + end + + + describe '#provenance_log_display_enabled?' do + it 'returns true' do + expect(subject.provenance_log_display_enabled?).to eq true + end + end + + + describe "#url_for" do + # Could not stub Rails.application.routes.url_helpers.url_for + it "returns url for provenance log with id" do + expect(subject.url_for(action: "show", id: 50)).to eq "/provenance_log/50" + end + end + + + describe "#url_for_deleted" do + it 'calls url_for' do + expect(subject).to receive(:url_for).with(action: "show", id: 101, only_path: true) + subject.url_for_deleted(id: 101) + end + end + +end diff --git a/spec/services/dataset/date_coverage_service_spec.rb b/spec/services/dataset/date_coverage_service_spec.rb new file mode 100644 index 00000000..d7e96505 --- /dev/null +++ b/spec/services/dataset/date_coverage_service_spec.rb @@ -0,0 +1,107 @@ +require 'rails_helper' + + +RSpec.describe Dataset::DateCoverageService do + + describe '#params_to_interval' do + + context "when called with year, month, and day for begin_date and end_date" do + it "returns interval with day precision" do + + params = { :date_coverage_begin_year => "2001", :date_coverage_begin_month => "1", :date_coverage_begin_day => "5", + :date_coverage_end_year => "2001", :date_coverage_end_month => "1", :date_coverage_end_day => "10" } + + expect(Dataset::DateCoverageService.params_to_interval params).to eq Date.edtf('2001-01-05/2001-01-10') + end + end + + context "when called with year and month for begin_date and end_date" do + it "returns interval with month precision" do + + params = { :date_coverage_begin_year => "2001", :date_coverage_begin_month => "1", :date_coverage_begin_day => "", + :date_coverage_end_year => "2001", :date_coverage_end_month => "2", :date_coverage_end_day => "" } + + expect(Dataset::DateCoverageService.params_to_interval params).to eq Date.edtf('2001-01/2001-02') + end + end + + context "when called with year begin_date and end_date" do + it "returns interval with year precision" do + + params = { :date_coverage_begin_year => "2002", :date_coverage_begin_month => "", :date_coverage_begin_day => "", + :date_coverage_end_year => "2003", :date_coverage_end_month => "", :date_coverage_end_day => "" } + + expect(Dataset::DateCoverageService.params_to_interval params).to eq Date.edtf('2002/2003') + end + end + + context "when called with begin_date but no end_date" do + it "returns Start Date" do + + params = { :date_coverage_begin_year => "2007", :date_coverage_begin_month => "5", :date_coverage_begin_day => "6", + :date_coverage_end_year => "", :date_coverage_end_month => "", :date_coverage_end_day => "" } + + expect(Dataset::DateCoverageService.params_to_interval params).to eq Date.edtf('2007-05-06') + end + end + + context "when called with end_date but no begin_date" do + it "returns unknown to End Date" do + + params = { :date_coverage_begin_year => "", :date_coverage_begin_month => "", :date_coverage_begin_day => "", + :date_coverage_end_year => "2010", :date_coverage_end_month => "10", :date_coverage_end_day => "10" } + + expect(Dataset::DateCoverageService.params_to_interval params).to eq Date.edtf('unknown/2010-10-10') + end + end + + context "when called with no date values" do + it "returns blank " do + params = { :date_coverage_begin_year => "", :date_coverage_begin_month => "", :date_coverage_begin_day => "", + :date_coverage_end_year => "", :date_coverage_end_month => "", :date_coverage_end_day => "" } + + expect(Dataset::DateCoverageService.params_to_interval(params)).to be_blank + end + end + + context "when called with reversed date values" do + it "returns blank" do + params = { :date_coverage_begin_year => "2001", :date_coverage_begin_month => "1", :date_coverage_begin_day => "6", + :date_coverage_end_year => "2001", :date_coverage_end_month => "1", :date_coverage_end_day => "5" } + + expect(Dataset::DateCoverageService.params_to_interval(params)).to be_blank + end + end + end + + + describe '#interval_to_params' do + + context "when called with an interval" do + it "returns date params of interval" do + + params = { :date_coverage_begin_year => "2002", :date_coverage_begin_month => "2", :date_coverage_begin_day => "6", + :date_coverage_end_year => "2004", :date_coverage_end_month => "8", :date_coverage_end_day => "16" } + + expect(Dataset::DateCoverageService.interval_to_params Date.edtf('2002-02-06/2004-08-16')).to eq params + end + end + + context "when called with argument that is not an interval" do + it "returns blank when not called with interval" do + + expect(Dataset::DateCoverageService.interval_to_params Date.new(2001,2,25) ).to be_blank + end + end + + context "when called with interval in reverse chronological order" do + it "returns blank" do + + expect(Dataset::DateCoverageService.interval_to_params Date.edtf('2004-08-16/2002-02-06') ).to be_blank + end + end + + + end + +end