diff --git a/app/models/assets_creator.rb b/app/models/assets_creator.rb index d2b7db74f9..2f8fa268e3 100644 --- a/app/models/assets_creator.rb +++ b/app/models/assets_creator.rb @@ -11,6 +11,8 @@ class AssetsCreator < ApplicationRecord include Seek::OrcidSupport include Seek::BioSchema::Support + before_validation :associate_creator_from_orcid + update_rdf_on_change :asset default_scope { order(:pos) } @@ -74,4 +76,10 @@ def rdf_resource end end + private + + def associate_creator_from_orcid + return if orcid.blank? || creator.present? + self.creator = Person.find_by_orcid(orcid) + end end diff --git a/test/functional/data_files_controller_test.rb b/test/functional/data_files_controller_test.rb index 5d5a242b0b..c6d13b7e96 100644 --- a/test/functional/data_files_controller_test.rb +++ b/test/functional/data_files_controller_test.rb @@ -4236,7 +4236,7 @@ def register_content_blob(skip_provide_metadata:false) creators: [someone], contributor: FactoryBot.create(:person, first_name: 'Joe', last_name: 'Bloggs', orcid: 'https://orcid.org/0000-0002-1694-233X') ).latest_version - thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0002-1694-233X') + thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0001-9842-9718') get :show, params: { id: thing.parent.id, version: thing.version, format: :datacite_xml } @@ -4252,7 +4252,7 @@ def register_content_blob(skip_provide_metadata:false) phil = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Collins, Phil']").first jane = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Bloggs, Jane']").first assert_equal 'Collins, Phil', phil.xpath('./xmlns:creatorName').first.text - assert_equal 'https://orcid.org/0000-0002-1694-233X', phil.xpath('./xmlns:nameIdentifier').first.text + assert_equal 'https://orcid.org/0000-0001-9842-9718', phil.xpath('./xmlns:nameIdentifier').first.text assert_equal 'Bloggs, Jane', jane.xpath('./xmlns:creatorName').first.text assert_nil jane.xpath('./xmlns:nameIdentifier').first assert_equal 'ORCID', resource.xpath('./xmlns:creators/xmlns:creator/xmlns:nameIdentifier/@nameIdentifierScheme').first.text diff --git a/test/unit/assets_creators_test.rb b/test/unit/assets_creators_test.rb index f47501b5b4..11ec3db3bf 100644 --- a/test/unit/assets_creators_test.rb +++ b/test/unit/assets_creators_test.rb @@ -173,4 +173,32 @@ def teardown end end end + + test 'automatically links to creator based on orcid' do + explicitly_linked_person = FactoryBot.create(:person, first_name: 'Link', orcid: 'https://orcid.org/0000-0002-5111-7263') + orcid_linked_person = FactoryBot.create(:person_not_in_project, first_name: 'Orc', orcid: 'https://orcid.org/0000-0002-1825-0097') + should_not_be_linked = FactoryBot.create(:person_not_in_project, orcid: 'https://orcid.org/0000-0001-9842-9718') + + sop = FactoryBot.create(:sop) + + disable_authorization_checks do + assert_difference('AssetsCreator.count', 2) do + sop.update(assets_creators_attributes: { + '4634' => { + orcid: 'https://orcid.org/0000-0001-9842-9718', + creator_id: explicitly_linked_person.id + }, + '123' => { + orcid: 'https://orcid.org/0000-0002-1825-0097' + } + }) + + ac = sop.reload.assets_creators.to_a + assert_equal 2, ac.length + assert_equal orcid_linked_person, ac.detect { |a| a.given_name == 'Orc' }.creator + assert_equal explicitly_linked_person, ac.detect { |a| a.given_name == 'Link' }.creator, + 'If creator is explicitly set, it should not attempt to link a different creator via orcid' + end + end + end end diff --git a/test/unit/datacite_metadata_test.rb b/test/unit/datacite_metadata_test.rb index c43c76154e..676d93c6c2 100644 --- a/test/unit/datacite_metadata_test.rb +++ b/test/unit/datacite_metadata_test.rb @@ -82,7 +82,8 @@ class DataciteMetadataTest < ActiveSupport::TestCase creators: [someone], contributor: FactoryBot.create(:person, first_name: 'Joe', last_name: 'Bloggs', orcid: 'https://orcid.org/0000-0002-1694-233X') ).latest_version - thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0002-1694-233X') + thing.assets_creators.create!(given_name: 'Phil', family_name: 'Collins', orcid: 'https://orcid.org/0000-0001-9842-9718') + thing.assets_creators.create!(orcid: 'https://orcid.org/0000-0002-1694-233X') metadata = thing.datacite_metadata xml = metadata.build @@ -93,13 +94,16 @@ class DataciteMetadataTest < ActiveSupport::TestCase resource = parsed.xpath('//xmlns:resource').first assert_equal 'The title', resource.xpath('./xmlns:titles/xmlns:title').first.text assert_equal 'The description', resource.xpath('./xmlns:descriptions/xmlns:description').first.text - assert_equal 2, resource.xpath('./xmlns:creators/xmlns:creator').length + assert_equal 3, resource.xpath('./xmlns:creators/xmlns:creator').length phil = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Collins, Phil']").first jane = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Bloggs, Jane']").first + joe = parsed.xpath("//xmlns:resource/xmlns:creators/xmlns:creator[xmlns:creatorName/text()='Bloggs, Joe']").first assert_equal 'Collins, Phil', phil.xpath('./xmlns:creatorName').first.text - assert_equal 'https://orcid.org/0000-0002-1694-233X', phil.xpath('./xmlns:nameIdentifier').first.text + assert_equal 'https://orcid.org/0000-0001-9842-9718', phil.xpath('./xmlns:nameIdentifier').first.text assert_equal 'Bloggs, Jane', jane.xpath('./xmlns:creatorName').first.text assert_nil jane.xpath('./xmlns:nameIdentifier').first + assert_equal 'Bloggs, Joe', joe.xpath('./xmlns:creatorName').first.text + assert_equal 'https://orcid.org/0000-0002-1694-233X', joe.xpath('./xmlns:nameIdentifier').first.text assert_equal 'ORCID', resource.xpath('./xmlns:creators/xmlns:creator/xmlns:nameIdentifier/@nameIdentifierScheme').first.text assert_equal 'https://orcid.org', resource.xpath('./xmlns:creators/xmlns:creator/xmlns:nameIdentifier/@schemeURI').first.text assert_equal thing.created_at.year.to_s, resource.xpath('./xmlns:publicationYear').first.text