Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / t / customfields / api.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings FATAL => 'all';
5
6 use RT::Test nodata => 1, tests => 139;
7
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) {
13         $ocf->Delete();
14 }
15
16
17 my $queue = RT::Queue->new( RT->SystemUser );
18 $queue->Create( Name => 'RecordCustomFields-'.$$ );
19 ok ($queue->id, "Created the queue");
20
21 my $queue2 = RT::Queue->new( RT->SystemUser );
22 $queue2->Create( Name => 'RecordCustomFields2' );
23
24 my $ticket = RT::Ticket->new( RT->SystemUser );
25 $ticket->Create(
26         Queue => $queue->Id,
27         Requestor => 'root@localhost',
28         Subject => 'RecordCustomFields1',
29 );
30
31 my $cfs = $ticket->CustomFields;
32 is( $cfs->Count, 0 );
33
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 );
38
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' );
43
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' );
48
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' );
53
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' );
58
59
60 my @custom_fields = ($local_cf1, $local_cf2, $global_cf3);
61
62
63 $cfs = $ticket->CustomFields;
64 is( $cfs->Count, 3 );
65
66 # Check that record has no any CF values yet {{{
67 $cfvs = $ticket->CustomFieldValues;
68 is( $cfvs->Count, 0 );
69 is( $ticket->FirstCustomFieldValue, undef );
70
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 );
75
76 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
77 is( $cfvs->Count, 0 );
78 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
79
80 for (@custom_fields) {
81         $cfvs = $ticket->CustomFieldValues( $_->id );
82         is( $cfvs->Count, 0 );
83
84         $cfvs = $ticket->CustomFieldValues( $_->Name );
85         is( $cfvs->Count, 0 );
86         is( $ticket->FirstCustomFieldValue( $_->id ), undef );
87         is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
88 }
89
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" );
95
96 SKIP: {
97
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');
102         
103                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
104                 ok( !$status, 'value doesn\'t exist');
105         
106                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
107                 ok( !$status, 'value doesn\'t exist');
108         }
109         
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 );
115         }
116         
117 }
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' );
123 }
124
125 my $test_add_delete_cycle = sub {
126         my $cb = shift;
127         for (@custom_fields) {
128                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
129                 ok( $status, "message: $msg");
130         }
131         
132         # does it exist?
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");
138         
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");
143         }
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");
148         }
149         $cfvs = $ticket->CustomFieldValues;
150         is( $cfvs->Count, 3 );
151         for (@custom_fields) {
152                 $cfvs = $ticket->CustomFieldValues( $_->id );
153                 is( $cfvs->Count, 1 );
154         
155                 $cfvs = $ticket->CustomFieldValues( $_->Name );
156                 is( $cfvs->Count, 1 );
157                 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
158                 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
159         }
160         # delete it
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");
164         }
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 );
170         
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" );
175         }
176 };
177
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] } );
182
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" );
189
190 {
191     my $cfname = $global_cf3->Name;
192     ($status, $msg) = $global_cf3->SetDisabled(1);
193     ok($status, "Disabled CF named $cfname");
194
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");
199
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");
203
204     $load->LoadByName( Name => $cfname);
205     is($load->Id, $dup->Id, "Loading by name gets non-disabled first");
206
207     $dup->SetDisabled(1);
208     $global_cf3->SetDisabled(0);
209
210     $load->LoadByName( Name => $cfname);
211     is($load->Id, $global_cf3->Id, "Loading by name gets non-disabled first, even with order swapped");
212 }
213
214 #SKIP: {
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 } );
219 #}
220
221