This commit was manufactured by cvs2svn to create branch
[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 => 131;
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 $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
55 my @custom_fields = ($local_cf1, $local_cf2, $global_cf3);
56
57
58 $cfs = $ticket->CustomFields;
59 is( $cfs->Count, 3 );
60
61 # Check that record has no any CF values yet {{{
62 $cfvs = $ticket->CustomFieldValues;
63 is( $cfvs->Count, 0 );
64 is( $ticket->FirstCustomFieldValue, undef );
65
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 );
70
71 $cfvs = $ticket->CustomFieldValues( 'SomeUnexpedCustomFieldName' );
72 is( $cfvs->Count, 0 );
73 is( $ticket->FirstCustomFieldValue( 'SomeUnexpedCustomFieldName' ), undef );
74
75 for (@custom_fields) {
76         $cfvs = $ticket->CustomFieldValues( $_->id );
77         is( $cfvs->Count, 0 );
78
79         $cfvs = $ticket->CustomFieldValues( $_->Name );
80         is( $cfvs->Count, 0 );
81         is( $ticket->FirstCustomFieldValue( $_->id ), undef );
82         is( $ticket->FirstCustomFieldValue( $_->Name ), undef );
83 }
84 # }}}
85
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" );
91 # }}}
92
93 # {{{
94 SKIP: {
95
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');
100         
101                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->id , Value => 'SomeUnexpectedCFValue' );
102                 ok( !$status, 'value doesn\'t exist');
103         
104                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $_->Name , Value => 'SomeUnexpectedCFValue' );
105                 ok( !$status, 'value doesn\'t exist');
106         }
107         
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 );
113         }
114         
115 }
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' );
121 }
122
123 my $test_add_delete_cycle = sub {
124         my $cb = shift;
125         for (@custom_fields) {
126                 ($status, $msg) = $ticket->AddCustomFieldValue( Field => $cb->($_) , Value => 'Foo' );
127                 ok( $status, "message: $msg");
128         }
129         
130         # does it exist?
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");
136         
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");
141         }
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");
146         }
147         $cfvs = $ticket->CustomFieldValues;
148         is( $cfvs->Count, 3 );
149         for (@custom_fields) {
150                 $cfvs = $ticket->CustomFieldValues( $_->id );
151                 is( $cfvs->Count, 1 );
152         
153                 $cfvs = $ticket->CustomFieldValues( $_->Name );
154                 is( $cfvs->Count, 1 );
155                 is( $ticket->FirstCustomFieldValue( $_->id ), 'Bar' );
156                 is( $ticket->FirstCustomFieldValue( $_->Name ), 'Bar' );
157         }
158         # delete it
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");
162         }
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 );
168         
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" );
173         }
174 };
175
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] } );
180
181 #SKIP: {
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 } );
186 #}
187
188