fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / cf_select_one.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => undef;
6
7 my ($baseurl, $m) = RT::Test->started_ok;
8 ok $m->login, 'logged in as root';
9
10 my $cf_name = 'test select one value';
11
12 my $cfid;
13 diag "Create a CF";
14 {
15     $m->follow_link( id => 'admin-custom-fields-create');
16     $m->submit_form(
17         form_name => "ModifyCustomField",
18         fields => {
19             Name          => $cf_name,
20             TypeComposite => 'Select-1',
21             LookupType    => 'RT::Queue-RT::Ticket',
22         },
23     );
24     $m->content_contains('Object created', 'created CF sucessfully' );
25     $cfid = $m->form_name('ModifyCustomField')->value('id');
26     ok $cfid, "found id of the CF in the form, it's #$cfid";
27 }
28
29 diag "add 'qwe', 'ASD', '0' and ' foo ' as values to the CF";
30 {
31     foreach my $value(qw(qwe ASD 0), 'foo ') {
32         $m->submit_form(
33             form_name => "ModifyCustomField",
34             fields => {
35                 "CustomField-". $cfid ."-Value-new-Name" => $value,
36             },
37             button => 'Update',
38         );
39         $m->content_contains('Object created', 'added a value to the CF' ); # or diag $m->content;
40         my $v = $value;
41         $v =~ s/^\s+$//;
42         $v =~ s/\s+$//;
43         $m->content_contains("value=\"$v\"", 'the added value is right' );
44     }
45 }
46
47 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
48 ok $queue && $queue->id, 'loaded or created queue';
49
50 diag "apply the CF to General queue";
51 {
52     $m->follow_link( id => 'admin-queues');
53     $m->follow_link( text => 'General' );
54     $m->title_is(q/Configuration for queue General/, 'admin-queue: general');
55     $m->follow_link( id => 'page-custom-fields-tickets');
56     $m->title_is(q/Custom Fields for queue General/, 'admin-queue: general cfid');
57
58     $m->form_name('EditCustomFields');
59     $m->tick( "AddCustomField" => $cfid );
60     $m->click('UpdateCFs');
61
62     $m->content_contains('Object created', 'TCF added to the queue' );
63 }
64
65 my $tid;
66 diag "create a ticket using API with 'asd'(not 'ASD') as value of the CF";
67 {
68     my $ticket = RT::Ticket->new( RT->SystemUser );
69     my ($txnid, $msg);
70     ($tid, $txnid, $msg) = $ticket->Create(
71         Subject => 'test',
72         Queue => $queue->id,
73         "CustomField-$cfid" => 'asd',
74     );
75     ok $tid, "created ticket";
76     diag $msg if $msg;
77
78     # we use lc as we really don't care about case
79     # so if later we'll add canonicalization of value
80     # test should work
81     is lc $ticket->FirstCustomFieldValue( $cf_name ),
82        'asd', 'assigned value of the CF';
83 }
84
85 diag "check that values of the CF are case insensetive(asd vs. ASD)";
86 {
87     ok $m->goto_ticket( $tid ), "opened ticket's page";
88     $m->follow_link( id => 'page-basics');
89     $m->title_like(qr/Modify ticket/i, 'modify ticket');
90     $m->content_contains($cf_name, 'CF on the page');
91
92     my $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
93     is lc $value, 'asd', 'correct value is selected';
94     $m->submit;
95     $m->content_unlike(qr/\Q$cf_name\E.*?changed/mi, 'field is not changed');
96
97     $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
98     is lc $value, 'asd', 'the same value is still selected';
99
100     my $ticket = RT::Ticket->new( RT->SystemUser );
101     $ticket->Load( $tid );
102     ok $ticket->id, 'loaded the ticket';
103     is lc $ticket->FirstCustomFieldValue( $cf_name ),
104        'asd', 'value is still the same';
105 }
106
107 diag "check that 0 is ok value of the CF";
108 {
109     ok $m->goto_ticket( $tid ), "opened ticket's page";
110     $m->follow_link( id => 'page-basics');
111     $m->title_like(qr/Modify ticket/i, 'modify ticket');
112     $m->content_contains($cf_name, 'CF on the page');
113
114     my $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
115     is lc $value, 'asd', 'correct value is selected';
116     $m->select("Object-RT::Ticket-$tid-CustomField-$cfid-Values" => 0 );
117     $m->submit;
118     $m->content_like(qr/\Q$cf_name\E.*?changed/mi, 'field is changed');
119     $m->content_lacks('0 is no longer a value for custom field', 'no bad message in results');
120
121     $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
122     is lc $value, '0', 'new value is selected';
123
124     my $ticket = RT::Ticket->new( RT->SystemUser );
125     $ticket->Load( $tid );
126     ok $ticket->id, 'loaded the ticket';
127     is lc $ticket->FirstCustomFieldValue( $cf_name ),
128        '0', 'API returns correct value';
129 }
130
131 diag "check that we can set empty value when the current is 0";
132 {
133     ok $m->goto_ticket( $tid ), "opened ticket's page";
134     $m->follow_link( id => 'page-basics');
135     $m->title_like(qr/Modify ticket/i, 'modify ticket');
136     $m->content_contains($cf_name, 'CF on the page');
137
138     my $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
139     is lc $value, '0', 'correct value is selected';
140     $m->select("Object-RT::Ticket-$tid-CustomField-$cfid-Values" => '' );
141     $m->submit;
142     $m->content_contains('0 is no longer a value for custom field', '0 is no longer a value');
143
144     $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
145     is $value, '', '(no value) is selected';
146
147     my $ticket = RT::Ticket->new( RT->SystemUser );
148     $ticket->Load( $tid );
149     ok $ticket->id, 'loaded the ticket';
150     is $ticket->FirstCustomFieldValue( $cf_name ),
151        undef, 'API returns correct value';
152 }
153
154 diag 'retain selected cf values when adding attachments';
155 {
156     my ( $ticket, $id );
157     $m->submit_form(
158         form_name => "CreateTicketInQueue",
159         fields    => { Queue => 'General' },
160     );
161     $m->content_contains($cf_name, 'Found cf field' );
162
163     $m->submit_form_ok(
164                        { form_name => "TicketCreate",
165           fields    => {
166               Subject        => 'test defaults',
167               Content        => 'test',
168               "Object-RT::Ticket--CustomField-$cfid-Values" => 'qwe',
169             },
170             button => 'AddMoreAttach',
171         },
172         'Add an attachment on create'
173     );
174
175     $m->form_name("TicketCreate");
176     is($m->value("Object-RT::Ticket--CustomField-$cfid-Values"),
177        "qwe",
178        "Selected value still on form" );
179 }
180
181 undef $m;
182 done_testing;