@@ -39,4 +39,45 @@ class PersonTest < ActiveSupport::TestCase
3939 person = people ( :horace )
4040 assert_respond_to person , :person_links
4141 end
42+
43+ test 'should allow optional profile association' do
44+ person = Person . create ( full_name : 'John Doe' )
45+ assert person . valid?
46+ assert_nil person . profile
47+ end
48+
49+ test 'should automatically link to profile by orcid on save' do
50+ profile = profiles ( :trainer_one_profile )
51+ # The trainer_one_profile has orcid: https://orcid.org/000-0002-1825-0097
52+ person = Person . create ( full_name : 'Josiah Carberry' , orcid : 'https://orcid.org/000-0002-1825-0097' )
53+ assert person . valid?
54+ assert_equal profile , person . profile
55+ end
56+
57+ test 'should automatically link to profile using short orcid format' do
58+ profile = profiles ( :trainer_one_profile )
59+ # The trainer_one_profile has orcid: https://orcid.org/000-0002-1825-0097
60+ person = Person . create ( full_name : 'Josiah Carberry' , orcid : '000-0002-1825-0097' )
61+ assert person . valid?
62+ assert_equal profile , person . profile
63+ end
64+
65+ test 'should not link to profile if no matching orcid' do
66+ person = Person . create ( full_name : 'John Doe' , orcid : '0000-0001-9999-9999' )
67+ assert person . valid?
68+ assert_nil person . profile
69+ end
70+
71+ test 'should not override existing profile link' do
72+ profile1 = profiles ( :trainer_one_profile )
73+ profile2 = profiles ( :admin_trainer_profile )
74+
75+ # First, manually set a profile
76+ person = Person . create ( full_name : 'John Doe' , profile : profile2 )
77+ assert_equal profile2 , person . profile
78+
79+ # Even if we update the ORCID to match profile1, it should keep profile2
80+ person . update ( orcid : 'https://orcid.org/000-0002-1825-0097' )
81+ assert_equal profile2 , person . profile
82+ end
4283end
0 commit comments