import rt 3.8.7
[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 => 41;
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' and '0' as values to the CF" if $ENV{'TEST_VERBOSE'};
35 {
36     foreach my $value(qw(qwe ASD 0)) {
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     }
46 }
47
48 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
49 ok $queue && $queue->id, 'loaded or created queue';
50
51 diag "apply the CF to General queue" if $ENV{'TEST_VERBOSE'};
52 {
53     $m->follow_link( text => 'Queues' );
54     $m->title_is(q/Admin queues/, 'admin-queues screen');
55     $m->follow_link( text => 'General' );
56     $m->title_is(q/Editing Configuration for queue General/, 'admin-queue: general');
57     $m->follow_link( text => 'Ticket Custom Fields' );
58     $m->title_is(q/Edit Custom Fields for General/, 'admin-queue: general cfid');
59
60     $m->form_name('EditCustomFields');
61     $m->field( "Object-". $queue->id ."-CF-$cfid" => 1 );
62     $m->submit;
63
64     $m->content_like( qr/Object created/, 'TCF added to the queue' );
65 }
66
67 my $tid;
68 diag "create a ticket using API with 'asd'(not 'ASD') as value of the CF"
69     if $ENV{'TEST_VERBOSE'};
70 {
71     my $ticket = RT::Ticket->new( $RT::SystemUser );
72     my ($txnid, $msg);
73     ($tid, $txnid, $msg) = $ticket->Create(
74         Subject => 'test',
75         Queue => $queue->id,
76         "CustomField-$cfid" => 'asd',
77     );
78     ok $tid, "created ticket";
79     diag $msg if $msg && $ENV{'TEST_VERBOSE'};
80
81     # we use lc as we really don't care about case
82     # so if later we'll add canonicalization of value
83     # test should work
84     is lc $ticket->FirstCustomFieldValue( $cf_name ),
85        'asd', 'assigned value of the CF';
86 }
87
88 diag "check that values of the CF are case insensetive(asd vs. ASD)"
89     if $ENV{'TEST_VERBOSE'};
90 {
91     ok $m->goto_ticket( $tid ), "opened ticket's page";
92     $m->follow_link( text => 'Custom Fields' );
93     $m->title_like(qr/Modify ticket/i, 'modify ticket');
94     $m->content_like(qr/\Q$cf_name/, 'CF on the page');
95
96     my $value = $m->form_number(3)->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
97     is lc $value, 'asd', 'correct value is selected';
98     $m->submit;
99     $m->content_unlike(qr/\Q$cf_name\E.*?changed/mi, 'field is not changed');
100
101     $value = $m->form_number(3)->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
102     is lc $value, 'asd', 'the same value is still selected';
103
104     my $ticket = RT::Ticket->new( $RT::SystemUser );
105     $ticket->Load( $tid );
106     ok $ticket->id, 'loaded the ticket';
107     is lc $ticket->FirstCustomFieldValue( $cf_name ),
108        'asd', 'value is still the same';
109 }
110
111 diag "check that 0 is ok value of the CF"
112     if $ENV{'TEST_VERBOSE'};
113 {
114     ok $m->goto_ticket( $tid ), "opened ticket's page";
115     $m->follow_link( text => 'Custom Fields' );
116     $m->title_like(qr/Modify ticket/i, 'modify ticket');
117     $m->content_like(qr/\Q$cf_name/, 'CF on the page');
118
119     my $value = $m->form_number(3)->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
120     is lc $value, 'asd', 'correct value is selected';
121     $m->select("Object-RT::Ticket-$tid-CustomField-$cfid-Values" => 0 );
122     $m->submit;
123     $m->content_like(qr/\Q$cf_name\E.*?changed/mi, 'field is changed');
124     $m->content_unlike(qr/0 is no longer a value for custom field/mi, 'no bad message in results');
125
126     $value = $m->form_number(3)->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
127     is lc $value, '0', 'new value is selected';
128
129     my $ticket = RT::Ticket->new( $RT::SystemUser );
130     $ticket->Load( $tid );
131     ok $ticket->id, 'loaded the ticket';
132     is lc $ticket->FirstCustomFieldValue( $cf_name ),
133        '0', 'API returns correct value';
134 }
135
136 diag "check that we can set empty value when the current is 0"
137     if $ENV{'TEST_VERBOSE'};
138 {
139     ok $m->goto_ticket( $tid ), "opened ticket's page";
140     $m->follow_link( text => 'Custom Fields' );
141     $m->title_like(qr/Modify ticket/i, 'modify ticket');
142     $m->content_like(qr/\Q$cf_name/, 'CF on the page');
143
144     my $value = $m->form_number(3)->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
145     is lc $value, '0', 'correct value is selected';
146     $m->select("Object-RT::Ticket-$tid-CustomField-$cfid-Values" => '' );
147     $m->submit;
148     $m->content_like(qr/0 is no longer a value for custom field/mi, '0 is no longer a value');
149
150     $value = $m->form_number(3)->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
151     is $value, '', '(no value) is selected';
152
153     my $ticket = RT::Ticket->new( $RT::SystemUser );
154     $ticket->Load( $tid );
155     ok $ticket->id, 'loaded the ticket';
156     is $ticket->FirstCustomFieldValue( $cf_name ),
157        undef, 'API returns correct value';
158 }
159