4 use warnings FATAL => 'all';
6 use RT::Test nodata => 1, tests => 139;
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
72 $cfvs = $ticket->CustomFieldValues( -1 );
73 is( $cfvs->Count, 0 );
74 is( $ticket->FirstCustomFieldValue( -1 ), undef );
76 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
77 is( $cfvs->Count, 0 );
78 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
80 for (@custom_fields) {
81 $cfvs = $ticket->CustomFieldValues( $_->id );
82 is( $cfvs->Count, 0 );
84 $cfvs = $ticket->CustomFieldValues( $_->Name );
85 is( $cfvs->Count, 0 );
86 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
87 is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
90 # try to add field value with fields that do not exist {{{
91 my ($status, $msg) = $ticket->AddCustomFieldValue( Field => -1 , Value => 'foo' );
92 ok(!$status, "shouldn't add value" );
93 ($status, $msg) = $ticket->AddCustomFieldValue( Field => 'SomeUnexpedCustomFieldName' , Value => 'foo' );
94 ok(!$status, "shouldn't add value" );
98 skip "TODO: We want fields that are not allowed to set unexpected values", 10;
99 for (@custom_fields) {
100 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'SomeUnexpectedCFValue' );
101 ok( !$status, 'value doesn\'t exist');
103 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
104 ok( !$status, 'value doesn\'t exist');
106 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
107 ok( !$status, 'value doesn\'t exist');
110 # Let check that we did not add value to be sure
111 # using only FirstCustomFieldValue sub because
112 # we checked other variants allready
113 for (@custom_fields) {
114 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
118 # Add some values to our custom fields
119 for (@custom_fields) {
120 # this should be tested elsewhere
121 $_->AddValue( Name => 'Foo' );
122 $_->AddValue( Name => 'Bar' );
125 my $test_add_delete_cycle = sub {
127 for (@custom_fields) {
128 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
129 ok( $status, "message: $msg");
133 $cfvs = $ticket->CustomFieldValues;
134 is( $cfvs->Count, 3, "We found all three custom fields on our ticket" );
135 for (@custom_fields) {
136 $cfvs = $ticket->CustomFieldValues( $_->id );
137 is( $cfvs->Count, 1 , "we found one custom field when searching by id");
139 $cfvs = $ticket->CustomFieldValues( $_->Name );
140 is( $cfvs->Count, 1 , " We found one custom field when searching by name for " . $_->Name);
141 is( $ticket->FirstCustomFieldValue( $_->id ), 'Foo' , "first value by id is foo");
142 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Foo' , "first value by name is foo");
144 # because our CFs are SingleValue then new value addition should override
145 for (@custom_fields) {
146 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'Bar' );
147 ok( $status, "message: $msg");
149 $cfvs = $ticket->CustomFieldValues;
150 is( $cfvs->Count, 3 );
151 for (@custom_fields) {
152 $cfvs = $ticket->CustomFieldValues( $_->id );
153 is( $cfvs->Count, 1 );
155 $cfvs = $ticket->CustomFieldValues( $_->Name );
156 is( $cfvs->Count, 1 );
157 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
158 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
161 for (@custom_fields ) {
162 ($status, $msg) = $ticket->DeleteCustomFieldValue( Field => $_ , Value => 'Bar' );
163 ok( $status, "Deleted a custom field value 'Bar' for field ".$_->id.": $msg");
165 $cfvs = $ticket->CustomFieldValues;
166 is( $cfvs->Count, 0, "The ticket (".$ticket->id.") no longer has any custom field values" );
167 for (@custom_fields) {
168 $cfvs = $ticket->CustomFieldValues( $_->id );
169 is( $cfvs->Count, 0, $ticket->id." has no values for cf ".$_->id );
171 $cfvs = $ticket->CustomFieldValues( $_->Name );
172 is( $cfvs->Count, 0 , $ticket->id." has no values for cf '".$_->Name. "'" );
173 is( $ticket->FirstCustomFieldValue( $_->id ), undef , "There is no first custom field value when loading by id" );
174 is( $ticket->FirstCustomFieldValue( $_->Name ), undef, "There is no first custom field value when loading by Name" );
178 # lets test cycle via CF id
179 $test_add_delete_cycle->( sub { return $_[0]->id } );
180 # lets test cycle via CF object reference
181 $test_add_delete_cycle->( sub { return $_[0] } );
183 $ticket->AddCustomFieldValue( Field => $local_cf2->id , Value => 'Baz' );
184 $ticket->AddCustomFieldValue( Field => $global_cf3->id , Value => 'Baz' );
185 # now if we ask for cf values on RecordCustomFields4 we should not get any
186 $cfvs = $ticket->CustomFieldValues( 'RecordCustomFields4' );
187 is( $cfvs->Count, 0, "No custom field values for non-Queue cf" );
188 is( $ticket->FirstCustomFieldValue( 'RecordCustomFields4' ), undef, "No first custom field value for non-Queue cf" );
191 my $cfname = $global_cf3->Name;
192 ($status, $msg) = $global_cf3->SetDisabled(1);
193 ok($status, "Disabled CF named $cfname");
195 my $load = RT::CustomField->new( RT->SystemUser );
196 $load->LoadByName( Name => $cfname);
197 ok($load->Id, "Loaded CF named $cfname");
198 is($load->Id, $global_cf3->Id, "Can load disabled CFs");
200 my $dup = RT::CustomField->new( RT->SystemUser );
201 $dup->Create( Name => $cfname, Type => 'SelectSingle', Queue => 0 );
202 ok($dup->Id, "Created CF with duplicate name");
204 $load->LoadByName( Name => $cfname);
205 is($load->Id, $dup->Id, "Loading by name gets non-disabled first");
207 $dup->SetDisabled(1);
208 $global_cf3->SetDisabled(0);
210 $load->LoadByName( Name => $cfname);
211 is($load->Id, $global_cf3->Id, "Loading by name gets non-disabled first, even with order swapped");
215 # skip "TODO: should we add CF values to objects via CF Name?", 48;
216 # names are not unique
217 # lets test cycle via CF Name
218 # $test_add_delete_cycle->( sub { return $_[0]->Name } );