rt 4.2.14 (#13852)
[freeside.git] / rt / t / web / cf_groupings.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5
6 my @groupings = qw/Basics Dates People Links More/;
7 RT->Config->Set( 'CustomFieldGroupings',
8     'RT::Ticket' => {
9         map { +($_ => ["Test$_"]) } @groupings,
10     },
11 );
12
13 my %CF;
14 for my $grouping (@groupings) {
15     my $name = "Test$grouping";
16     my $cf = RT::CustomField->new( RT->SystemUser );
17     my ($id, $msg) = $cf->Create(
18         Name => $name,
19         Queue => '0',
20         Description => 'A Testing custom field',
21         Type => 'FreeformSingle',
22         Pattern => '^(?!bad value).*$',
23     );
24     ok $id, "custom field '$name' correctly created";
25     $CF{$grouping} = $id;
26 }
27
28 my $queue = RT::Test->load_or_create_queue( Name => 'General' );
29
30 my ( $baseurl, $m ) = RT::Test->started_ok;
31 ok $m->login, 'logged in as root';
32
33 my %location = (
34     Basics => ".ticket-info-basics",
35     Dates  => ".ticket-info-dates",
36     People => "#ticket-create-message",
37     Links  => ".ticket-info-links",
38     More   => ".ticket-info-cfs",
39 );
40 {
41     note "testing Create";
42     $m->goto_create_ticket($queue);
43
44     my $prefix = 'Object-RT::Ticket--CustomField:';
45     my $dom = $m->dom;
46     $m->form_name('TicketCreate');
47     $m->field("Subject", "CF grouping test");
48
49     for my $grouping (@groupings) {
50         my $input_name = $prefix . "$grouping-$CF{$grouping}-Value";
51         is $dom->find(qq{input[name="$input_name"]})->size, 1, "only one CF input on the page";
52         ok $dom->at(qq{$location{$grouping} input[name="$input_name"]}), "CF is in the right place";
53         $m->field( $input_name, "Test" . $grouping . "Value" );
54     }
55     $m->submit;
56 }
57
58 my $id = $m->get_ticket_id;
59 {
60     note "testing Display";
61     ok $id, "created a ticket";
62     my $dom = $m->dom;
63
64     $location{People} = ".ticket-info-people";
65     foreach my $grouping (@groupings) {
66         my $row_id = "CF-$CF{$grouping}-ShowRow";
67         is $dom->find(qq{#$row_id})->size, 1, "CF on the page";
68         like $dom->at(qq{#$row_id})->all_text, qr/Test$grouping:\s*Test${grouping}Value/, "value is set";
69         ok $dom->at(qq{$location{$grouping} #$row_id}), "CF is in the right place";
70     }
71 }
72
73 {
74     note "testing Basics/People/Dates/Links pages";
75     my $prefix = 'Object-RT::Ticket-'. $id .'-CustomField:';
76     { # Basics and More both show up on "Basics"
77         for my $name (qw/Basics More/) {
78             $m->follow_link_ok({id => 'page-basics'}, 'Ticket -> Basics');
79             is $m->dom->find(qq{input[name^="$prefix"][name\$="-Value"]})->size, 2,
80                 "two CF inputs on the page";
81
82             my $input_name = "$prefix$name-$CF{$name}-Value";
83             ok $m->dom->at(qq{$location{$name} input[name="$input_name"]}),
84                 "CF is in the right place";
85             $m->submit_form_ok({
86                 with_fields => { $input_name => "Test${name}Changed" },
87                 button      => 'SubmitTicket',
88             });
89             $m->content_like(qr{to Test${name}Changed});
90
91             $m->submit_form_ok({
92                 with_fields => { $input_name => "bad value" },
93                 button      => 'SubmitTicket',
94             });
95             $m->content_like(qr{Test\Q$name\E: Input must match});
96         }
97     }
98
99     # Everything else gets its own page
100     foreach my $name ( qw(People Dates Links) ) {
101         $m->follow_link_ok({id => "page-\L$name"}, "Ticket's $name page");
102         is $m->dom->find(qq{input[name^="$prefix"][name\$="-Value"]})->size, 1,
103             "only one CF input on the page";
104         my $input_name = "$prefix$name-$CF{$name}-Value";
105         $m->submit_form_ok({
106             with_fields => { $input_name => "Test${name}Changed" },
107             button      => 'SubmitTicket',
108         });
109         $m->content_like(qr{to Test${name}Changed});
110
111         $m->submit_form_ok({
112             with_fields => { $input_name => "bad value" },
113             button      => 'SubmitTicket',
114         });
115         $m->content_like(qr{Could not add new custom field value: Input must match});
116     }
117 }
118
119 {
120     note "testing Jumbo";
121     my $prefix = 'Object-RT::Ticket-'. $id .'-CustomField:';
122     $m->follow_link_ok({id => "page-jumbo"}, "Ticket's Jumbo page");
123     my $dom = $m->dom;
124     $m->form_name("TicketModifyAll");
125
126     foreach my $name ( qw(Basics People Dates Links More) ) {
127         my $input_name = "$prefix$name-$CF{$name}-Value";
128         is $dom->find(qq{input[name="$input_name"]})->size, 1,
129             "only one CF input on the page";
130         $m->field( $input_name, "Test${name}Again" );
131     }
132     $m->click('SubmitTicket');
133     foreach my $name ( qw(Basics People Dates Links More) ) {
134         $m->content_like(qr{to Test${name}Again});
135     }
136 }
137
138 {
139     note "Reconfigure to place one CF in multiple boxes";
140     $m->no_warnings_ok;
141     RT::Test->stop_server;
142
143     RT->Config->Set( 'CustomFieldGroupings',
144         'RT::Ticket' => {
145             Basics => [ 'TestMore' ],
146             More   => [ 'TestMore' ],
147         },
148     );
149
150     ( $baseurl, $m ) = RT::Test->started_ok;
151     ok $m->login, 'logged in as root';
152 }
153
154 {
155     note "Testing one CF in multiple boxes";
156     $m->goto_create_ticket($queue);
157
158     my $prefix = 'Object-RT::Ticket--CustomField:';
159     my $dom = $m->dom;
160     $m->form_name('TicketCreate');
161
162     my $cf = $CF{More};
163     is $m->dom->find(qq{input[name^="$prefix"][name\$="-$cf-Value"]})->size, 2,
164         "Two 'More' CF inputs on the page";
165     for my $grouping (qw/Basics More/) {
166         my $input_name = $prefix . "$grouping-$cf-Value";
167         is $dom->find(qq{input[name="$input_name"]})->size, 1, "Found the $grouping grouping";
168         ok $dom->at(qq{$location{$grouping} input[name="$input_name"]}), "CF is in the right place";
169         $m->field( $input_name, "TestMoreValue" );
170     }
171     $m->submit;
172     $m->no_warnings_ok( "Submitting CF with two (identical) values had no warnings" );
173 }
174
175 $id = $m->get_ticket_id;
176 my $ticket = RT::Ticket->new ( RT->SystemUser );
177 $ticket->Load( $id );
178 is $ticket->CustomFieldValuesAsString( "TestMore", Separator => "|" ), "TestMoreValue",
179     "Value submitted twice is set correctly (and only once)";
180
181 my $cf = $CF{More};
182 my $prefix = 'Object-RT::Ticket-'. $id .'-CustomField:';
183 {
184     note "Updating with multiple appearances of a CF";
185     $m->follow_link_ok({id => 'page-basics'}, 'Ticket -> Basics');
186
187     is $m->dom->find(qq{input[name^="$prefix"][name\$="-$cf-Value"]})->size, 2,
188         "Two 'More' CF inputs on the page";
189     my @inputs;
190     for my $grouping (qw/Basics More/) {
191         my $input_name =  "$prefix$grouping-$cf-Value";
192         push @inputs, $input_name;
193         ok $m->dom->at(qq{$location{$grouping} input[name="$input_name"]}),
194             "CF is in the right place";
195     }
196     $m->submit_form_ok({
197         with_fields => {
198             map {+($_ => "TestMoreChanged")} @inputs,
199         },
200         button => 'SubmitTicket',
201     });
202     $m->no_warnings_ok;
203     $m->content_like(qr{to TestMoreChanged});
204
205     $ticket->Load( $id );
206     is $ticket->CustomFieldValuesAsString( "TestMore", Separator => "|" ), "TestMoreChanged",
207         "Updated value submitted twice is set correctly (and only once)";
208 }
209
210 {
211     note "Updating with _differing_ values in multiple appearances of a CF";
212
213     my %inputs = map {+($_ => "$prefix$_-$cf-Value")} qw/Basics More/;
214     $m->submit_form_ok({
215         with_fields => {
216             $inputs{Basics} => "BasicsValue",
217             $inputs{More}   => "MoreValue",
218         },
219         button => 'SubmitTicket',
220     });
221     $m->warning_like(qr{CF $cf submitted with multiple differing values});
222     $m->content_like(qr{to BasicsValue}, "Arbitrarily chose first value");
223
224     $ticket->Load( $id );
225     is $ticket->CustomFieldValuesAsString( "TestMore", Separator => "|" ), "BasicsValue",
226         "Conflicting value submitted twice is set correctly (and only once)";
227 }
228
229 {
230     note "Configuring CF to be a select-multiple";
231     my $custom_field = RT::CustomField->new( RT->SystemUser );
232     $custom_field->Load( $cf );
233     $custom_field->SetType( "Select" );
234     $custom_field->SetMaxValues( 0 );
235     $custom_field->AddValue( Name => $_ ) for 1..9;
236 }
237
238 {
239     note "Select multiples do not interfere with each other when appearing multiple times";
240     $m->follow_link_ok({id => 'page-basics'}, 'Ticket -> Basics');
241
242     $m->form_name('TicketModify');
243     my %inputs = map {+($_ => "$prefix$_-$cf-Values")} qw/Basics More/;
244     ok $m->dom->at(qq{select[name="$inputs{Basics}"]}), "Found 'More' CF in Basics box";
245     ok $m->dom->at(qq{select[name="$inputs{More}"]}),   "Found 'More' CF in More box";
246
247     $m->select( $inputs{Basics} => [1, 3, 9] );
248     $m->select( $inputs{More}   => [1, 3, 9] );
249     $m->click( 'SubmitTicket' );
250     $m->no_warnings_ok;
251     $m->content_like(qr{$_ added as a value for TestMore}) for 1, 3, 9;
252     $m->content_like(qr{BasicsValue is no longer a value for custom field TestMore});
253
254     $ticket->Load( $id );
255     is $ticket->CustomFieldValuesAsString( "TestMore", Separator => "|" ), "1|3|9",
256         "Multi-select values submitted correctly";
257 }
258
259 {
260     note "Submit multiples correctly choose one set of values when conflicting information is submitted";
261     $m->form_name('TicketModify');
262     my %inputs = map {+($_ => "$prefix$_-$cf-Values")} qw/Basics More/;
263     $m->select( $inputs{Basics} => [2, 3, 4] );
264     $m->select( $inputs{More}   => [8, 9] );
265     $m->click( 'SubmitTicket' );
266     $m->warning_like(qr{CF $cf submitted with multiple differing values});
267     $m->content_like(qr{$_ added as a value for TestMore}) for 2, 4;
268     $m->content_unlike(qr{$_ added as a value for TestMore}) for 8;
269     $m->content_like(qr{$_ is no longer a value for custom field TestMore}) for 1, 9;
270
271     $ticket->Load( $id );
272     is $ticket->CustomFieldValuesAsString( "TestMore", Separator => "|" ), "3|2|4",
273         "Multi-select values submitted correctly";
274 }
275
276 undef $m;
277 done_testing;