1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Test::Web;
54 use base qw(Test::WWW::Mechanize);
55 use Scalar::Util qw(weaken);
56 use MIME::Base64 qw//;
58 BEGIN { require RT::Test; }
64 my ($class, @args) = @_;
66 push @args, app => $RT::Test::TEST_APP if $RT::Test::TEST_APP;
67 my $self = $instance = $class->SUPER::new(@args);
69 $self->cookie_jar(HTTP::Cookies->new);
77 if ( $url =~ s!^/!! ) {
78 $url = $self->rt_base_url . $url;
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;
88 return $RT::Test::existing_server if $RT::Test::existing_server;
89 return "http://localhost:" . RT->Config->Get('WebPort') . RT->Config->Get('WebPath') . "/";
94 my $user = shift || 'root';
95 my $pass = shift || 'password';
98 $self->logout if $args{logout};
100 my $url = $self->rt_base_url;
101 $self->get($url . "?user=$user;pass=$pass");
103 return 0 unless $self->logged_in_as($user);
105 unless ( $self->content =~ m/Logout/i ) {
106 Test::More::diag("error: page has no Logout");
114 my $user = shift || '';
116 unless ( $self->status == 200 ) {
117 Test::More::diag( "error: status is ". $self->status );
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");
131 my $url = $self->rt_base_url;
133 Test::More::diag( "error: status is ". $self->status )
134 unless $self->status == 200;
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;
146 if ( $self->content =~ /Logout/i ) {
147 Test::More::diag( "error: couldn't logout" );
156 unless ( $id && int $id ) {
157 Test::More::diag( "error: wrong id ". defined $id? $id : '(undef)' );
161 my $url = $self->rt_base_url;
162 $url .= "Ticket/Display.html?id=$id";
164 unless ( $self->status == 200 ) {
165 Test::More::diag( "error: status is ". $self->status );
171 sub goto_create_ticket {
178 } elsif ( $queue =~ /^\d+$/ ) {
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;
187 $self->get($self->rt_base_url . 'Ticket/Create.html?Queue='.$id);
194 local $Test::Builder::Level = $Test::Builder::Level + 1;
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');
206 my @warnings = @{ thaw $clone->content };
215 local $Test::Builder::Level = $Test::Builder::Level + 1;
217 my @warnings = $self->get_warnings;
218 if (@warnings == 0) {
219 Test::More::fail("no warnings emitted; expected 1");
222 elsif (@warnings > 1) {
223 Test::More::fail(scalar(@warnings) . " warnings emitted; expected 1");
225 Test::More::diag("got warning: $_");
230 return Test::More::like($warnings[0], $re, $name);
233 sub next_warning_like {
238 local $Test::Builder::Level = $Test::Builder::Level + 1;
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");
246 $self->{stashed_server_warnings} = \@warnings;
249 my $warning = shift @{ $self->{stashed_server_warnings} };
250 return Test::More::like($warning, $re, $name);
255 my $name = shift || "no warnings emitted";
257 local $Test::Builder::Level = $Test::Builder::Level + 1;
259 my @warnings = $self->get_warnings;
261 Test::More::is(@warnings, 0, $name);
263 Test::More::diag("got warning: $_");
266 return @warnings == 0 ? 1 : 0;
269 sub no_leftover_warnings_ok {
272 my $name = shift || "no leftover warnings";
274 local $Test::Builder::Level = $Test::Builder::Level + 1;
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} || [] };
280 Test::More::is(@warnings, 0, $name);
282 Test::More::diag("leftover warning: $_");
285 return @warnings == 0 ? 1 : 0;
292 $self->display_ticket( $id);
293 my ($got) = ($self->content =~ m{Status:\s*</td>\s*<td[^>]*?class="value"[^>]*?>\s*([\w ]+?)\s*</td>}ism);
295 Test::More::diag("Error: couldn't find status value on the page, may be regexp problem");
300 sub ticket_status_is {
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);
311 my $content = $self->content;
313 if ($content =~ /.*Ticket (\d+) created.*/g) {
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'});
323 sub set_custom_field {
329 my $field_name = $self->custom_field_input( $queue, $cf_name )
332 $self->field($field_name, $val);
336 sub custom_field_input {
341 my $cf_obj = RT::CustomField->new( $RT::SystemUser );
344 LookupType => RT::Ticket->CustomFieldLookupType,
347 unless ( $cf_obj->id ) {
348 Test::More::diag("Can not load custom field '$cf_name' in queue '$queue'");
351 my $cf_id = $cf_obj->id;
354 grep /^Object-RT::Ticket-\d*-CustomField(?::\w+)?-$cf_id-Values?$/,
356 $self->current_form->inputs;
358 Test::More::diag("Can not find input for custom field '$cf_name' #$cf_id");
368 my $input = $self->current_form->find_input( $field )
371 my @names = $input->value_names;
372 return $input->value unless @names;
374 my @values = $input->possible_values;
375 for ( my $i = 0; $i < @values; $i++ ) {
376 return $names[ $i ] if $values[ $i ] eq $input->value;
386 my %has = map {$_ => 1} @{ $args{'has'} };
387 my %has_no = map {$_ => 1} @{ $args{'has_no'} };
389 local $Test::Builder::Level = $Test::Builder::Level + 1;
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 };
398 if ( @found || keys %has ) {
399 Test::More::ok( 0, "expected links" );
400 Test::More::diag( "didn't expect, but found: ". join ', ', map "'$_'", @found )
402 Test::More::diag( "didn't find, but expected: ". join ', ', map "'$_'", keys %has )
406 return Test::More::ok( 1, "expected links" );
411 $self->default_header( $self->auth_header(@_) );
416 return Authorization => "Basic " .
417 MIME::Base64::encode( join(":", @_) );
422 Carp::croak("Can not get DOM, not HTML repsone")
423 unless $self->is_html;
425 return Mojo::DOM->new( $self->content );
430 if ( !$RT::Test::Web::DESTROY++ ) {
431 $self->no_warnings_ok;
436 return unless $instance;
437 return if RT::Test->builder->{Original_Pid} != $$;
438 $instance->no_warnings_ok if !$RT::Test::Web::DESTROY++;