rt 4.2.15
[freeside.git] / rt / lib / RT / Test / Web.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 package RT::Test::Web;
50
51 use strict;
52 use warnings;
53
54 use base qw(Test::WWW::Mechanize);
55 use Scalar::Util qw(weaken);
56 use MIME::Base64 qw//;
57
58 BEGIN { require RT::Test; }
59 require Test::More;
60
61 my $instance;
62
63 sub new {
64     my ($class, @args) = @_;
65
66     push @args, app => $RT::Test::TEST_APP if $RT::Test::TEST_APP;
67     my $self = $instance = $class->SUPER::new(@args);
68     weaken $instance;
69     $self->cookie_jar(HTTP::Cookies->new);
70
71     return $self;
72 }
73
74 sub get_ok {
75     my $self = shift;
76     my $url = shift;
77     if ( $url =~ s!^/!! ) {
78         $url = $self->rt_base_url . $url;
79     }
80
81     local $Test::Builder::Level = $Test::Builder::Level + 1;
82     my $rv = $self->SUPER::get_ok($url, @_);
83     Test::More::diag( "Couldn't get $url" ) unless $rv;
84     return $rv;
85 }
86
87 sub rt_base_url {
88     return $RT::Test::existing_server if $RT::Test::existing_server;
89     return "http://localhost:" . RT->Config->Get('WebPort') . RT->Config->Get('WebPath') . "/";
90 }
91
92 sub login {
93     my $self = shift;
94     my $user = shift || 'root';
95     my $pass = shift || 'password';
96     my %args = @_;
97     
98     $self->logout if $args{logout};
99
100     my $url = $self->rt_base_url;
101     $self->get($url . "?user=$user;pass=$pass");
102
103     return 0 unless $self->logged_in_as($user);
104
105     unless ( $self->content =~ m/Logout/i ) {
106         Test::More::diag("error: page has no Logout");
107         return 0;
108     }
109     return 1;
110 }
111
112 sub logged_in_as {
113     my $self = shift;
114     my $user = shift || '';
115
116     unless ( $self->status == 200 ) {
117         Test::More::diag( "error: status is ". $self->status );
118         return 0;
119     }
120     RT::Interface::Web::EscapeHTML(\$user);
121     unless ( $self->content =~ m{<span class="current-user">\Q$user\E</span>}i ) {
122         Test::More::diag("Page has no user name");
123         return 0;
124     }
125     return 1;
126 }
127
128 sub logout {
129     my $self = shift;
130
131     my $url = $self->rt_base_url;
132     $self->get($url);
133     Test::More::diag( "error: status is ". $self->status )
134         unless $self->status == 200;
135
136     if ( $self->content =~ /Logout/i ) {
137         $self->follow_link( text => 'Logout' );
138         Test::More::diag( "error: status is ". $self->status ." when tried to logout" )
139             unless $self->status == 200;
140     }
141     else {
142         return 1;
143     }
144
145     $self->get($url);
146     if ( $self->content =~ /Logout/i ) {
147         Test::More::diag( "error: couldn't logout" );
148         return 0;
149     }
150     return 1;
151 }
152
153 sub goto_ticket {
154     my $self = shift;
155     my $id   = shift;
156     unless ( $id && int $id ) {
157         Test::More::diag( "error: wrong id ". defined $id? $id : '(undef)' );
158         return 0;
159     }
160
161     my $url = $self->rt_base_url;
162     $url .= "Ticket/Display.html?id=$id";
163     $self->get($url);
164     unless ( $self->status == 200 ) {
165         Test::More::diag( "error: status is ". $self->status );
166         return 0;
167     }
168     return 1;
169 }
170
171 sub goto_create_ticket {
172     my $self = shift;
173     my $queue = shift;
174
175     my $id;
176     if ( ref $queue ) {
177         $id = $queue->id;
178     } elsif ( $queue =~ /^\d+$/ ) {
179         $id = $queue;
180     } else {
181         my $queue_obj = RT::Queue->new(RT->SystemUser);
182         my ($ok, $msg) = $queue_obj->Load($queue);
183         die "Unable to load queue '$queue': $msg" if !$ok;
184         $id = $queue_obj->id;
185     }
186
187     $self->get($self->rt_base_url . 'Ticket/Create.html?Queue='.$id);
188
189     return 1;
190 }
191
192 sub get_warnings {
193     my $self = shift;
194     local $Test::Builder::Level = $Test::Builder::Level + 1;
195
196     # We clone here so that when we fetch warnings, we don't disrupt the state
197     # of the test's mech. If we reuse the original mech then you can't
198     # test warnings immediately after fetching page XYZ, then fill out
199     # forms on XYZ. This is because the most recently fetched page has changed
200     # from XYZ to /__test_warnings, which has no form.
201     my $clone = $self->clone;
202     return unless $clone->get_ok('/__test_warnings');
203
204     use Storable 'thaw';
205
206     my @warnings = @{ thaw $clone->content };
207     return @warnings;
208 }
209
210 sub warning_like {
211     my $self = shift;
212     my $re   = shift;
213     my $name = shift;
214
215     local $Test::Builder::Level = $Test::Builder::Level + 1;
216
217     my @warnings = $self->get_warnings;
218     if (@warnings == 0) {
219         Test::More::fail("no warnings emitted; expected 1");
220         return 0;
221     }
222     elsif (@warnings > 1) {
223         Test::More::fail(scalar(@warnings) . " warnings emitted; expected 1");
224         for (@warnings) {
225             Test::More::diag("got warning: $_");
226         }
227         return 0;
228     }
229
230     return Test::More::like($warnings[0], $re, $name);
231 }
232
233 sub next_warning_like {
234     my $self = shift;
235     my $re   = shift;
236     my $name = shift;
237
238     local $Test::Builder::Level = $Test::Builder::Level + 1;
239
240     if (@{ $self->{stashed_server_warnings} || [] } == 0) {
241         my @warnings = $self->get_warnings;
242         if (@warnings == 0) {
243             Test::More::fail("no warnings emitted; expected 1");
244             return 0;
245         }
246         $self->{stashed_server_warnings} = \@warnings;
247     }
248
249     my $warning = shift @{ $self->{stashed_server_warnings} };
250     return Test::More::like($warning, $re, $name);
251 }
252
253 sub no_warnings_ok {
254     my $self = shift;
255     my $name = shift || "no warnings emitted";
256
257     local $Test::Builder::Level = $Test::Builder::Level + 1;
258
259     my @warnings = $self->get_warnings;
260
261     Test::More::is(@warnings, 0, $name);
262     for (@warnings) {
263         Test::More::diag("got warning: $_");
264     }
265
266     return @warnings == 0 ? 1 : 0;
267 }
268
269 sub no_leftover_warnings_ok {
270     my $self = shift;
271
272     my $name = shift || "no leftover warnings";
273
274     local $Test::Builder::Level = $Test::Builder::Level + 1;
275
276     # we clear the warnings because we don't want to break later tests
277     # in case there *are* leftover warnings
278     my @warnings = splice @{ $self->{stashed_server_warnings} || [] };
279
280     Test::More::is(@warnings, 0, $name);
281     for (@warnings) {
282         Test::More::diag("leftover warning: $_");
283     }
284
285     return @warnings == 0 ? 1 : 0;
286 }
287
288 sub ticket_status {
289     my $self = shift;
290     my $id = shift;
291     
292     $self->display_ticket( $id);
293     my ($got) = ($self->content =~ m{Status:\s*</td>\s*<td[^>]*?class="value"[^>]*?>\s*([\w ]+?)\s*</td>}ism);
294     unless ( $got ) {
295         Test::More::diag("Error: couldn't find status value on the page, may be regexp problem");
296     }
297     return $got;
298 }
299
300 sub ticket_status_is {
301     my $self = shift;
302     my $id = shift;
303     my $status = shift;
304     my $desc = shift || "Status of the ticket #$id is '$status'";
305     local $Test::Builder::Level = $Test::Builder::Level + 1;
306     return Test::More::is($self->ticket_status( $id), $status, $desc);
307 }
308
309 sub get_ticket_id {
310     my $self = shift;
311     my $content = $self->content;
312     my $id = 0;
313     if ($content =~ /.*Ticket (\d+) created.*/g) {
314         $id = $1;
315     }
316     elsif ($content =~ /.*No permission to view newly created ticket #(\d+).*/g) {
317         Test::More::diag("\nNo permissions to view the ticket.\n") if($ENV{'TEST_VERBOSE'});
318         $id = $1;
319     }
320     return $id;
321 }
322
323 sub set_custom_field {
324     my $self   = shift;
325     my $queue   = shift;
326     my $cf_name = shift;
327     my $val     = shift;
328     
329     my $field_name = $self->custom_field_input( $queue, $cf_name )
330         or return 0;
331
332     $self->field($field_name, $val);
333     return 1;
334 }
335
336 sub custom_field_input {
337     my $self   = shift;
338     my $queue   = shift;
339     my $cf_name = shift;
340
341     my $cf_obj = RT::CustomField->new( $RT::SystemUser );
342     $cf_obj->LoadByName(
343         Name => $cf_name,
344         LookupType => RT::Ticket->CustomFieldLookupType,
345         ObjectId => $queue,
346     );
347     unless ( $cf_obj->id ) {
348         Test::More::diag("Can not load custom field '$cf_name' in queue '$queue'");
349         return undef;
350     }
351     my $cf_id = $cf_obj->id;
352     
353     my ($res) =
354         grep /^Object-RT::Ticket-\d*-CustomField(?::\w+)?-$cf_id-Values?$/,
355         map $_->name,
356         $self->current_form->inputs;
357     unless ( $res ) {
358         Test::More::diag("Can not find input for custom field '$cf_name' #$cf_id");
359         return undef;
360     }
361     return $res;
362 }
363
364 sub value_name {
365     my $self = shift;
366     my $field = shift;
367
368     my $input = $self->current_form->find_input( $field )
369         or return undef;
370
371     my @names = $input->value_names;
372     return $input->value unless @names;
373
374     my @values = $input->possible_values;
375     for ( my $i = 0; $i < @values; $i++ ) {
376         return $names[ $i ] if $values[ $i ] eq $input->value;
377     }
378     return undef;
379 }
380
381
382 sub check_links {
383     my $self = shift;
384     my %args = @_;
385
386     my %has = map {$_ => 1} @{ $args{'has'} };
387     my %has_no = map {$_ => 1} @{ $args{'has_no'} };
388
389     local $Test::Builder::Level = $Test::Builder::Level + 1;
390
391     my @found;
392
393     my @links = $self->followable_links;
394     foreach my $text ( grep defined && length, map $_->text, @links ) {
395         push @found, $text if $has_no{ $text };
396         delete $has{ $text };
397     }
398     if ( @found || keys %has ) {
399         Test::More::ok( 0, "expected links" );
400         Test::More::diag( "didn't expect, but found: ". join ', ', map "'$_'", @found )
401             if @found;
402         Test::More::diag( "didn't find, but expected: ". join ', ', map "'$_'", keys %has )
403             if keys %has;
404         return 0;
405     }
406     return Test::More::ok( 1, "expected links" );
407 }
408
409 sub auth {
410     my $self = shift;
411     $self->default_header( $self->auth_header(@_) );
412 }
413
414 sub auth_header {
415     my $self = shift;
416     return Authorization => "Basic " .
417         MIME::Base64::encode( join(":", @_) );
418 }
419
420 sub dom {
421     my $self = shift;
422     Carp::croak("Can not get DOM, not HTML repsone")
423         unless $self->is_html;
424     require Mojo::DOM;
425     return Mojo::DOM->new( $self->content );
426 }
427
428 sub DESTROY {
429     my $self = shift;
430     if ( !$RT::Test::Web::DESTROY++ ) {
431         $self->no_warnings_ok;
432     }
433 }
434
435 END {
436     return unless $instance;
437     return if RT::Test->builder->{Original_Pid} != $$;
438     $instance->no_warnings_ok if !$RT::Test::Web::DESTROY++;
439 }
440
441 1;