first pass RT4 merge, RT#13852
[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 => 45;
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";
15 {
16     $m->follow_link( id => 'tools-config-custom-fields-create');
17     $m->submit_form(
18         form_name => "ModifyCustomField",
19         fields => {
20             Name          => $cf_name,
21             TypeComposite => 'Select-1',
22             LookupType    => 'RT::Queue-RT::Ticket',
23         },
24     );
25     $m->content_contains('Object created', 'created CF sucessfully' );
26     $cfid = $m->form_name('ModifyCustomField')->value('id');
27     ok $cfid, "found id of the CF in the form, it's #$cfid";
28 }
29
30 diag "add 'qwe', 'ASD', '0' and ' foo ' as values to the CF";
31 {
32     foreach my $value(qw(qwe ASD 0), 'foo ') {
33         $m->submit_form(
34             form_name => "ModifyCustomField",
35             fields => {
36                 "CustomField-". $cfid ."-Value-new-Name" => $value,
37             },
38             button => 'Update',
39         );
40         $m->content_contains('Object created', 'added a value to the CF' ); # or diag $m->content;
41         my $v = $value;
42         $v =~ s/^\s+$//;
43         $v =~ s/\s+$//;
44         $m->content_contains("value=\"$v\"", 'the added value is right' );
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";
52 {
53     $m->follow_link( id => 'tools-config-queues');
54     $m->follow_link( text => 'General' );
55     $m->title_is(q/Configuration for queue General/, 'admin-queue: general');
56     $m->follow_link( id => 'page-ticket-custom-fields');
57     $m->title_is(q/Custom Fields for queue General/, 'admin-queue: general cfid');
58
59     $m->form_name('EditCustomFields');
60     $m->tick( "AddCustomField" => $cfid );
61     $m->click('UpdateCFs');
62
63     $m->content_contains('Object created', 'TCF added to the queue' );
64 }
65
66 my $tid;
67 diag "create a ticket using API with 'asd'(not 'ASD') as value of the CF";
68 {
69     my $ticket = RT::Ticket->new( RT->SystemUser );
70     my ($txnid, $msg);
71     ($tid, $txnid, $msg) = $ticket->Create(
72         Subject => 'test',
73         Queue => $queue->id,
74         "CustomField-$cfid" => 'asd',
75     );
76     ok $tid, "created ticket";
77     diag $msg if $msg;
78
79     # we use lc as we really don't care about case
80     # so if later we'll add canonicalization of value
81     # test should work
82     is lc $ticket->FirstCustomFieldValue( $cf_name ),
83        'asd', 'assigned value of the CF';
84 }
85
86 diag "check that values of the CF are case insensetive(asd vs. ASD)";
87 {
88     ok $m->goto_ticket( $tid ), "opened ticket's page";
89     $m->follow_link( id => 'page-basics');
90     $m->title_like(qr/Modify ticket/i, 'modify ticket');
91     $m->content_contains($cf_name, 'CF on the page');
92
93     my $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
94     is lc $value, 'asd', 'correct value is selected';
95     $m->submit;
96     $m->content_unlike(qr/\Q$cf_name\E.*?changed/mi, 'field is not changed');
97
98     $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
99     is lc $value, 'asd', 'the same value is still selected';
100
101     my $ticket = RT::Ticket->new( RT->SystemUser );
102     $ticket->Load( $tid );
103     ok $ticket->id, 'loaded the ticket';
104     is lc $ticket->FirstCustomFieldValue( $cf_name ),
105        'asd', 'value is still the same';
106 }
107
108 diag "check that 0 is ok value of the CF";
109 {
110     ok $m->goto_ticket( $tid ), "opened ticket's page";
111     $m->follow_link( id => 'page-basics');
112     $m->title_like(qr/Modify ticket/i, 'modify ticket');
113     $m->content_contains($cf_name, 'CF on the page');
114
115     my $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
116     is lc $value, 'asd', 'correct value is selected';
117     $m->select("Object-RT::Ticket-$tid-CustomField-$cfid-Values" => 0 );
118     $m->submit;
119     $m->content_like(qr/\Q$cf_name\E.*?changed/mi, 'field is changed');
120     $m->content_lacks('0 is no longer a value for custom field', 'no bad message in results');
121
122     $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
123     is lc $value, '0', 'new value is selected';
124
125     my $ticket = RT::Ticket->new( RT->SystemUser );
126     $ticket->Load( $tid );
127     ok $ticket->id, 'loaded the ticket';
128     is lc $ticket->FirstCustomFieldValue( $cf_name ),
129        '0', 'API returns correct value';
130 }
131
132 diag "check that we can set empty value when the current is 0";
133 {
134     ok $m->goto_ticket( $tid ), "opened ticket's page";
135     $m->follow_link( id => 'page-basics');
136     $m->title_like(qr/Modify ticket/i, 'modify ticket');
137     $m->content_contains($cf_name, 'CF on the page');
138
139     my $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
140     is lc $value, '0', 'correct value is selected';
141     $m->select("Object-RT::Ticket-$tid-CustomField-$cfid-Values" => '' );
142     $m->submit;
143     $m->content_contains('0 is no longer a value for custom field', '0 is no longer a value');
144
145     $value = $m->form_name('TicketModify')->value("Object-RT::Ticket-$tid-CustomField-$cfid-Values");
146     is $value, '', '(no value) is selected';
147
148     my $ticket = RT::Ticket->new( RT->SystemUser );
149     $ticket->Load( $tid );
150     ok $ticket->id, 'loaded the ticket';
151     is $ticket->FirstCustomFieldValue( $cf_name ),
152        undef, 'API returns correct value';
153 }
154