3 use warnings FATAL => 'all';
5 use RT::Test nodata => 1, tests => 145;
8 # Before we get going, ditch all object_cfs; this will remove
9 # all custom fields systemwide;
10 my $object_cfs = RT::ObjectCustomFields->new(RT->SystemUser);
11 $object_cfs->UnLimit();
12 while (my $ocf = $object_cfs->Next) {
17 my $queue = RT::Queue->new( RT->SystemUser );
18 $queue->Create( Name => 'RecordCustomFields-'.$$ );
19 ok ($queue->id, "Created the queue");
21 my $queue2 = RT::Queue->new( RT->SystemUser );
22 $queue2->Create( Name => 'RecordCustomFields2' );
24 my $ticket = RT::Ticket->new( RT->SystemUser );
27 Requestor => 'root@localhost',
28 Subject => 'RecordCustomFields1',
31 my $cfs = $ticket->CustomFields;
34 # Check that record has no any CF values yet {{{
35 my $cfvs = $ticket->CustomFieldValues;
36 is( $cfvs->Count, 0 );
37 is( $ticket->FirstCustomFieldValue, undef );
39 my $local_cf1 = RT::CustomField->new( RT->SystemUser );
40 $local_cf1->Create( Name => 'RecordCustomFields1-'.$$, Type => 'SelectSingle', Queue => $queue->id );
41 $local_cf1->AddValue( Name => 'RecordCustomFieldValues11' );
42 $local_cf1->AddValue( Name => 'RecordCustomFieldValues12' );
44 my $local_cf2 = RT::CustomField->new( RT->SystemUser );
45 $local_cf2->Create( Name => 'RecordCustomFields2-'.$$, Type => 'SelectSingle', Queue => $queue->id );
46 $local_cf2->AddValue( Name => 'RecordCustomFieldValues21' );
47 $local_cf2->AddValue( Name => 'RecordCustomFieldValues22' );
49 my $global_cf3 = RT::CustomField->new( RT->SystemUser );
50 $global_cf3->Create( Name => 'RecordCustomFields3-'.$$, Type => 'SelectSingle', Queue => 0 );
51 $global_cf3->AddValue( Name => 'RecordCustomFieldValues31' );
52 $global_cf3->AddValue( Name => 'RecordCustomFieldValues32' );
54 my $local_cf4 = RT::CustomField->new( RT->SystemUser );
55 $local_cf4->Create( Name => 'RecordCustomFields4', Type => 'SelectSingle', Queue => $queue2->id );
56 $local_cf4->AddValue( Name => 'RecordCustomFieldValues41' );
57 $local_cf4->AddValue( Name => 'RecordCustomFieldValues42' );
60 my @custom_fields = ($local_cf1, $local_cf2, $global_cf3);
63 $cfs = $ticket->CustomFields;
66 # Check that record has no any CF values yet {{{
67 $cfvs = $ticket->CustomFieldValues;
68 is( $cfvs->Count, 0 );
69 is( $ticket->FirstCustomFieldValue, undef );
71 # CF with ID -1 shouldnt exist at all
73 $cfvs = $ticket->CustomFieldValues( -1 );
74 } qr{Couldn't load custom field};
75 is( $cfvs->Count, 0 );
77 is( $ticket->FirstCustomFieldValue( -1 ), undef );
78 } qr{Couldn't load custom field};
81 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
82 } qr{Couldn't load custom field};
83 is( $cfvs->Count, 0 );
85 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
86 } qr{Couldn't load custom field};
88 for (@custom_fields) {
89 $cfvs = $ticket->CustomFieldValues( $_->id );
90 is( $cfvs->Count, 0 );
92 $cfvs = $ticket->CustomFieldValues( $_->Name );
93 is( $cfvs->Count, 0 );
94 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
95 is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
98 # try to add field value with fields that do not exist {{{
99 my ($status, $msg) = $ticket->AddCustomFieldValue( Field => -1 , Value => 'foo' );
100 ok(!$status, "shouldn't add value" );
101 ($status, $msg) = $ticket->AddCustomFieldValue( Field => 'SomeUnexpedCustomFieldName' , Value => 'foo' );
102 ok(!$status, "shouldn't add value" );
106 skip "TODO: We want fields that are not allowed to set unexpected values", 10;
107 for (@custom_fields) {
108 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'SomeUnexpectedCFValue' );
109 ok( !$status, 'value doesn\'t exist');
111 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
112 ok( !$status, 'value doesn\'t exist');
114 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
115 ok( !$status, 'value doesn\'t exist');
118 # Let check that we did not add value to be sure
119 # using only FirstCustomFieldValue sub because
120 # we checked other variants allready
121 for (@custom_fields) {
122 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
126 # Add some values to our custom fields
127 for (@custom_fields) {
128 # this should be tested elsewhere
129 $_->AddValue( Name => 'Foo' );
130 $_->AddValue( Name => 'Bar' );
133 my $test_add_delete_cycle = sub {
135 for (@custom_fields) {
136 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
137 ok( $status, "message: $msg");
141 $cfvs = $ticket->CustomFieldValues;
142 is( $cfvs->Count, 3, "We found all three custom fields on our ticket" );
143 for (@custom_fields) {
144 $cfvs = $ticket->CustomFieldValues( $_->id );
145 is( $cfvs->Count, 1 , "we found one custom field when searching by id");
147 $cfvs = $ticket->CustomFieldValues( $_->Name );
148 is( $cfvs->Count, 1 , " We found one custom field when searching by name for " . $_->Name);
149 is( $ticket->FirstCustomFieldValue( $_->id ), 'Foo' , "first value by id is foo");
150 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Foo' , "first value by name is foo");
152 # because our CFs are SingleValue then new value addition should override
153 for (@custom_fields) {
154 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'Bar' );
155 ok( $status, "message: $msg");
157 $cfvs = $ticket->CustomFieldValues;
158 is( $cfvs->Count, 3 );
159 for (@custom_fields) {
160 $cfvs = $ticket->CustomFieldValues( $_->id );
161 is( $cfvs->Count, 1 );
163 $cfvs = $ticket->CustomFieldValues( $_->Name );
164 is( $cfvs->Count, 1 );
165 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
166 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
169 for (@custom_fields ) {
170 ($status, $msg) = $ticket->DeleteCustomFieldValue( Field => $_ , Value => 'Bar' );
171 ok( $status, "Deleted a custom field value 'Bar' for field ".$_->id.": $msg");
173 $cfvs = $ticket->CustomFieldValues;
174 is( $cfvs->Count, 0, "The ticket (".$ticket->id.") no longer has any custom field values" );
175 for (@custom_fields) {
176 $cfvs = $ticket->CustomFieldValues( $_->id );
177 is( $cfvs->Count, 0, $ticket->id." has no values for cf ".$_->id );
179 $cfvs = $ticket->CustomFieldValues( $_->Name );
180 is( $cfvs->Count, 0 , $ticket->id." has no values for cf '".$_->Name. "'" );
181 is( $ticket->FirstCustomFieldValue( $_->id ), undef , "There is no first custom field value when loading by id" );
182 is( $ticket->FirstCustomFieldValue( $_->Name ), undef, "There is no first custom field value when loading by Name" );
186 # lets test cycle via CF id
187 $test_add_delete_cycle->( sub { return $_[0]->id } );
188 # lets test cycle via CF object reference
189 $test_add_delete_cycle->( sub { return $_[0] } );
191 $ticket->AddCustomFieldValue( Field => $local_cf2->id , Value => 'Baz' );
192 $ticket->AddCustomFieldValue( Field => $global_cf3->id , Value => 'Baz' );
193 # now if we ask for cf values on RecordCustomFields4 we should not get any
195 $cfvs = $ticket->CustomFieldValues( 'RecordCustomFields4' );
196 } qr{Couldn't load custom field};
197 is( $cfvs->Count, 0, "No custom field values for non-Queue cf" );
199 is( $ticket->FirstCustomFieldValue( 'RecordCustomFields4' ), undef, "No first custom field value for non-Queue cf" );
200 } qr{Couldn't load custom field};
203 my $cfname = $global_cf3->Name;
204 ($status, $msg) = $global_cf3->SetDisabled(1);
205 ok($status, "Disabled CF named $cfname");
207 my $load = RT::CustomField->new( RT->SystemUser );
208 $load->LoadByName( Name => $cfname);
209 ok($load->Id, "Loaded CF named $cfname");
210 is($load->Id, $global_cf3->Id, "Can load disabled CFs");
212 my $dup = RT::CustomField->new( RT->SystemUser );
213 $dup->Create( Name => $cfname, Type => 'SelectSingle', Queue => 0 );
214 ok($dup->Id, "Created CF with duplicate name");
216 $load->LoadByName( Name => $cfname);
217 is($load->Id, $dup->Id, "Loading by name gets non-disabled first");
219 $dup->SetDisabled(1);
220 $global_cf3->SetDisabled(0);
222 $load->LoadByName( Name => $cfname);
223 is($load->Id, $global_cf3->Id, "Loading by name gets non-disabled first, even with order swapped");
227 # skip "TODO: should we add CF values to objects via CF Name?", 48;
228 # names are not unique
229 # lets test cycle via CF Name
230 # $test_add_delete_cycle->( sub { return $_[0]->Name } );