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