import rt 3.8.9
[freeside.git] / rt / lib / t / regression / 09record_cf_api.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings FATAL => 'all';
5 use Test::More tests => 133;
6
7 use RT;
8 RT::LoadConfig();
9 RT::Init();
10
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) {
16         $ocf->Delete();
17 }
18
19
20 my $queue = RT::Queue->new( $RT::SystemUser );
21 $queue->Create( Name => 'RecordCustomFields-'.$$ );
22 ok ($queue->id, "Created the queue");
23
24 my $queue2 = RT::Queue->new( $RT::SystemUser );
25 $queue2->Create( Name => 'RecordCustomFields2' );
26
27 my $ticket = RT::Ticket->new( $RT::SystemUser );
28 $ticket->Create(
29         Queue => $queue->Id,
30         Requestor => 'root@localhost',
31         Subject => 'RecordCustomFields1',
32 );
33
34 my $cfs = $ticket->CustomFields;
35 is( $cfs->Count, 0 );
36
37 # Check that record has no any CF values yet {{{
38 my $cfvs = $ticket->CustomFieldValues;
39 is( $cfvs->Count, 0 );
40 is( $ticket->FirstCustomFieldValue, undef );
41
42 my $local_cf1 = RT::CustomField->new( $RT::SystemUser );
43 $local_cf1->Create( Name => 'RecordCustomFields1-'.$$, Type => 'SelectSingle', Queue => $queue->id );
44 $local_cf1->AddValue( Name => 'RecordCustomFieldValues11' );
45 $local_cf1->AddValue( Name => 'RecordCustomFieldValues12' );
46
47 my $local_cf2 = RT::CustomField->new( $RT::SystemUser );
48 $local_cf2->Create( Name => 'RecordCustomFields2-'.$$, Type => 'SelectSingle', Queue => $queue->id );
49 $local_cf2->AddValue( Name => 'RecordCustomFieldValues21' );
50 $local_cf2->AddValue( Name => 'RecordCustomFieldValues22' );
51
52 my $global_cf3 = RT::CustomField->new( $RT::SystemUser );
53 $global_cf3->Create( Name => 'RecordCustomFields3-'.$$, Type => 'SelectSingle', Queue => 0 );
54 $global_cf3->AddValue( Name => 'RecordCustomFieldValues31' );
55 $global_cf3->AddValue( Name => 'RecordCustomFieldValues32' );
56
57 my $local_cf4 = RT::CustomField->new( $RT::SystemUser );
58 $local_cf4->Create( Name => 'RecordCustomFields4', Type => 'SelectSingle', Queue => $queue2->id );
59 $local_cf4->AddValue( Name => 'RecordCustomFieldValues41' );
60 $local_cf4->AddValue( Name => 'RecordCustomFieldValues42' );
61
62
63 my @custom_fields = ($local_cf1, $local_cf2, $global_cf3);
64
65
66 $cfs = $ticket->CustomFields;
67 is( $cfs->Count, 3 );
68
69 # Check that record has no any CF values yet {{{
70 $cfvs = $ticket->CustomFieldValues;
71 is( $cfvs->Count, 0 );
72 is( $ticket->FirstCustomFieldValue, undef );
73
74 # CF with ID -1 shouldnt exist at all
75 $cfvs = $ticket->CustomFieldValues( -1 );
76 is( $cfvs->Count, 0 );
77 is( $ticket->FirstCustomFieldValue( -1 ), undef );
78
79 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
80 is( $cfvs->Count, 0 );
81 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
82
83 for (@custom_fields) {
84         $cfvs = $ticket->CustomFieldValues( $_->id );
85         is( $cfvs->Count, 0 );
86
87         $cfvs = $ticket->CustomFieldValues( $_->Name );
88         is( $cfvs->Count, 0 );
89         is( $ticket->FirstCustomFieldValue( $_->id ), undef );
90         is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
91 }
92 # }}}
93
94 # try to add field value with fields that do not exist {{{
95 my ($status, $msg) = $ticket->AddCustomFieldValue( Field => -1 , Value => 'foo' );
96 ok(!$status, "shouldn't add value" );
97 ($status, $msg) = $ticket->AddCustomFieldValue( Field => 'SomeUnexpedCustomFieldName' , Value => 'foo' );
98 ok(!$status, "shouldn't add value" );
99 # }}}
100
101 # {{{
102 SKIP: {
103
104         skip "TODO: We want fields that are not allowed to set unexpected values", 10;
105         for (@custom_fields) {
106                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'SomeUnexpectedCFValue' );
107                 ok( !$status, 'value doesn\'t exist');
108         
109                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
110                 ok( !$status, 'value doesn\'t exist');
111         
112                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
113                 ok( !$status, 'value doesn\'t exist');
114         }
115         
116         # Let check that we did not add value to be sure
117         # using only FirstCustomFieldValue sub because
118         # we checked other variants allready
119         for (@custom_fields) {
120                 is( $ticket->FirstCustomFieldValue( $_->id ), undef );
121         }
122         
123 }
124 # Add some values to our custom fields
125 for (@custom_fields) {
126         # this should be tested elsewhere
127         $_->AddValue( Name => 'Foo' );
128         $_->AddValue( Name => 'Bar' );
129 }
130
131 my $test_add_delete_cycle = sub {
132         my $cb = shift;
133         for (@custom_fields) {
134                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
135                 ok( $status, "message: $msg");
136         }
137         
138         # does it exist?
139         $cfvs = $ticket->CustomFieldValues;
140         is( $cfvs->Count, 3, "We found all three custom fields on our ticket" );
141         for (@custom_fields) {
142                 $cfvs = $ticket->CustomFieldValues( $_->id );
143                 is( $cfvs->Count, 1 , "we found one custom field when searching by id");
144         
145                 $cfvs = $ticket->CustomFieldValues( $_->Name );
146                 is( $cfvs->Count, 1 , " We found one custom field when searching by name for " . $_->Name);
147                 is( $ticket->FirstCustomFieldValue( $_->id ), 'Foo' , "first value by id is foo");
148                 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Foo' , "first value by name is foo");
149         }
150         # because our CFs are SingleValue then new value addition should override
151         for (@custom_fields) {
152                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_ , Value => 'Bar' );
153                 ok( $status, "message: $msg");
154         }
155         $cfvs = $ticket->CustomFieldValues;
156         is( $cfvs->Count, 3 );
157         for (@custom_fields) {
158                 $cfvs = $ticket->CustomFieldValues( $_->id );
159                 is( $cfvs->Count, 1 );
160         
161                 $cfvs = $ticket->CustomFieldValues( $_->Name );
162                 is( $cfvs->Count, 1 );
163                 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
164                 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
165         }
166         # delete it
167         for (@custom_fields ) { 
168                 ($status, $msg) = $ticket->DeleteCustomFieldValue( Field => $_ , Value => 'Bar' );
169                 ok( $status, "Deleted a custom field value 'Bar' for field ".$_->id.": $msg");
170         }
171         $cfvs = $ticket->CustomFieldValues;
172         is( $cfvs->Count, 0, "The ticket (".$ticket->id.") no longer has any custom field values"  );
173         for (@custom_fields) {
174                 $cfvs = $ticket->CustomFieldValues( $_->id );
175                 is( $cfvs->Count, 0,  $ticket->id." has no values for cf  ".$_->id );
176         
177                 $cfvs = $ticket->CustomFieldValues( $_->Name );
178                 is( $cfvs->Count, 0 , $ticket->id." has no values for cf  '".$_->Name. "'" );
179                 is( $ticket->FirstCustomFieldValue( $_->id ), undef , "There is no first custom field value when loading by id" );
180                 is( $ticket->FirstCustomFieldValue( $_->Name ), undef, "There is no first custom field value when loading by Name" );
181         }
182 };
183
184 # lets test cycle via CF id
185 $test_add_delete_cycle->( sub { return $_[0]->id } );
186 # lets test cycle via CF object reference
187 $test_add_delete_cycle->( sub { return $_[0] } );
188
189 $ticket->AddCustomFieldValue( Field => $local_cf2->id , Value => 'Baz' );
190 $ticket->AddCustomFieldValue( Field => $global_cf3->id , Value => 'Baz' );
191 # now if we ask for cf values on RecordCustomFields4 we should not get any
192 $cfvs = $ticket->CustomFieldValues( 'RecordCustomFields4' );
193 is( $cfvs->Count, 0, "No custom field values for non-Queue cf" );
194 is( $ticket->FirstCustomFieldValue( 'RecordCustomFields4' ), undef, "No first custom field value for non-Queue cf" );
195
196
197 #SKIP: {
198 #       skip "TODO: should we add CF values to objects via CF Name?", 48;
199 # names are not unique
200         # lets test cycle via CF Name
201 #       $test_add_delete_cycle->( sub { return $_[0]->Name } );
202 #}
203
204