4 use warnings FATAL => 'all';
5 use Test::More tests => 131;
11 # Before we get going, ditch all object_cfs; this will remove
12 # all custom fields systemwide;
13 my $object_cfs = RT::ObjectCustomFields->new($RT::SystemUser);
14 $object_cfs->UnLimit();
15 while (my $ocf = $object_cfs->Next) {
20 my $queue = RT::Queue->new( $RT::SystemUser );
21 $queue->Create( Name => 'RecordCustomFields-'.$$ );
22 ok ($queue->id, "Created the queue");
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' );
55 my @custom_fields = ($local_cf1, $local_cf2, $global_cf3);
58 $cfs = $ticket->CustomFields;
61 # Check that record has no any CF values yet {{{
62 $cfvs = $ticket->CustomFieldValues;
63 is( $cfvs->Count, 0 );
64 is( $ticket->FirstCustomFieldValue, undef );
66 # CF with ID -1 shouldnt exist at all
67 $cfvs = $ticket->CustomFieldValues( -1 );
68 is( $cfvs->Count, 0 );
69 is( $ticket->FirstCustomFieldValue( -1 ), undef );
71 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
72 is( $cfvs->Count, 0 );
73 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
75 for (@custom_fields) {
76 $cfvs = $ticket->CustomFieldValues( $_->id );
77 is( $cfvs->Count, 0 );
79 $cfvs = $ticket->CustomFieldValues( $_->Name );
80 is( $cfvs->Count, 0 );
81 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
82 is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
86 # try to add field value with fields that do not exist {{{
87 my ($status, $msg) = $ticket->AddCustomFieldValue( Field => -1 , Value => 'foo' );
88 ok(!$status, "shouldn't add value" );
89 ($status, $msg) = $ticket->AddCustomFieldValue( Field => 'SomeUnexpedCustomFieldName' , Value => 'foo' );
90 ok(!$status, "shouldn't add value" );
96 skip "TODO: We want fields that are not allowed to set unexpected values", 10;
97 for (@custom_fields) {
98 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'SomeUnexpectedCFValue' );
99 ok( !$status, 'value doesn\'t exist');
101 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
102 ok( !$status, 'value doesn\'t exist');
104 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
105 ok( !$status, 'value doesn\'t exist');
108 # Let check that we did not add value to be sure
109 # using only FirstCustomFieldValue sub because
110 # we checked other variants allready
111 for (@custom_fields) {
112 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
116 # Add some values to our custom fields
117 for (@custom_fields) {
118 # this should be tested elsewhere
119 $_->AddValue( Name => 'Foo' );
120 $_->AddValue( Name => 'Bar' );
123 my $test_add_delete_cycle = sub {
125 for (@custom_fields) {
126 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
127 ok( $status, "message: $msg");
131 $cfvs = $ticket->CustomFieldValues;
132 is( $cfvs->Count, 3, "We found all three custom fields on our ticket" );
133 for (@custom_fields) {
134 $cfvs = $ticket->CustomFieldValues( $_->id );
135 is( $cfvs->Count, 1 , "we found one custom field when searching by id");
137 $cfvs = $ticket->CustomFieldValues( $_->Name );
138 is( $cfvs->Count, 1 , " We found one custom field when searching by name for " . $_->Name);
139 is( $ticket->FirstCustomFieldValue( $_->id ), 'Foo' , "first value by id is foo");
140 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Foo' , "first value by name is foo");
142 # because our CFs are SingleValue then new value addition should override
143 for (@custom_fields) {
144 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'Bar' );
145 ok( $status, "message: $msg");
147 $cfvs = $ticket->CustomFieldValues;
148 is( $cfvs->Count, 3 );
149 for (@custom_fields) {
150 $cfvs = $ticket->CustomFieldValues( $_->id );
151 is( $cfvs->Count, 1 );
153 $cfvs = $ticket->CustomFieldValues( $_->Name );
154 is( $cfvs->Count, 1 );
155 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
156 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
159 for (@custom_fields ) {
160 ($status, $msg) = $ticket->DeleteCustomFieldValue( Field => $_ , Value => 'Bar' );
161 ok( $status, "Deleted a custom field value 'Bar' for field ".$_->id.": $msg");
163 $cfvs = $ticket->CustomFieldValues;
164 is( $cfvs->Count, 0, "The ticket (".$ticket->id.") no longer has any custom field values" );
165 for (@custom_fields) {
166 $cfvs = $ticket->CustomFieldValues( $_->id );
167 is( $cfvs->Count, 0, $ticket->id." has no values for cf ".$_->id );
169 $cfvs = $ticket->CustomFieldValues( $_->Name );
170 is( $cfvs->Count, 0 , $ticket->id." has no values for cf '".$_->Name. "'" );
171 is( $ticket->FirstCustomFieldValue( $_->id ), undef , "There is no first custom field value when loading by id" );
172 is( $ticket->FirstCustomFieldValue( $_->Name ), undef, "There is no first custom field value when loading by Name" );
176 # lets test cycle via CF id
177 $test_add_delete_cycle->( sub { return $_[0]->id } );
178 # lets test cycle via CF object reference
179 $test_add_delete_cycle->( sub { return $_[0] } );
182 # skip "TODO: should we add CF values to objects via CF Name?", 48;
183 # names are not unique
184 # lets test cycle via CF Name
185 # $test_add_delete_cycle->( sub { return $_[0]->Name } );