1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2015 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);
57 BEGIN { require RT::Test; }
63 my ($class, @args) = @_;
65 push @args, app => $RT::Test::TEST_APP if $RT::Test::TEST_APP;
66 my $self = $instance = $class->SUPER::new(@args);
68 $self->cookie_jar(HTTP::Cookies->new);
76 if ( $url =~ s!^/!! ) {
77 $url = $self->rt_base_url . $url;
79 my $rv = $self->SUPER::get_ok($url, @_);
80 Test::More::diag( "Couldn't get $url" ) unless $rv;
85 return $RT::Test::existing_server if $RT::Test::existing_server;
86 return "http://localhost:" . RT->Config->Get('WebPort') . RT->Config->Get('WebPath') . "/";
91 my $user = shift || 'root';
92 my $pass = shift || 'password';
95 $self->logout if $args{logout};
97 my $url = $self->rt_base_url;
98 $self->get($url . "?user=$user;pass=$pass");
99 unless ( $self->status == 200 ) {
100 Test::More::diag( "error: status is ". $self->status );
103 unless ( $self->content =~ m/Logout/i ) {
104 Test::More::diag("error: page has no Logout");
107 RT::Interface::Web::EscapeUTF8(\$user);
108 unless ( $self->content =~ m{<span class="current-user">\Q$user\E</span>}i ) {
109 Test::More::diag("Page has no user name");
118 my $url = $self->rt_base_url;
120 Test::More::diag( "error: status is ". $self->status )
121 unless $self->status == 200;
123 if ( $self->content =~ /Logout/i ) {
124 $self->follow_link( text => 'Logout' );
125 Test::More::diag( "error: status is ". $self->status ." when tried to logout" )
126 unless $self->status == 200;
133 if ( $self->content =~ /Logout/i ) {
134 Test::More::diag( "error: couldn't logout" );
143 unless ( $id && int $id ) {
144 Test::More::diag( "error: wrong id ". defined $id? $id : '(undef)' );
148 my $url = $self->rt_base_url;
149 $url .= "Ticket/Display.html?id=$id";
151 unless ( $self->status == 200 ) {
152 Test::More::diag( "error: status is ". $self->status );
158 sub goto_create_ticket {
165 } elsif ( $queue =~ /^\d+$/ ) {
168 die "not yet implemented";
171 $self->get($self->rt_base_url . 'Ticket/Create.html?Queue='.$id);
178 local $Test::Builder::Level = $Test::Builder::Level + 1;
180 # We clone here so that when we fetch warnings, we don't disrupt the state
181 # of the test's mech. If we reuse the original mech then you can't
182 # test warnings immediately after fetching page XYZ, then fill out
183 # forms on XYZ. This is because the most recently fetched page has changed
184 # from XYZ to /__test_warnings, which has no form.
185 my $clone = $self->clone;
186 return unless $clone->get_ok('/__test_warnings');
190 my @warnings = @{ thaw $clone->content };
199 local $Test::Builder::Level = $Test::Builder::Level + 1;
201 my @warnings = $self->get_warnings;
202 if (@warnings == 0) {
203 Test::More::fail("no warnings emitted; expected 1");
206 elsif (@warnings > 1) {
207 Test::More::fail(scalar(@warnings) . " warnings emitted; expected 1");
209 Test::More::diag("got warning: $_");
214 return Test::More::like($warnings[0], $re, $name);
217 sub next_warning_like {
222 local $Test::Builder::Level = $Test::Builder::Level + 1;
224 if (@{ $self->{stashed_server_warnings} || [] } == 0) {
225 my @warnings = $self->get_warnings;
226 if (@warnings == 0) {
227 Test::More::fail("no warnings emitted; expected 1");
230 $self->{stashed_server_warnings} = \@warnings;
233 my $warning = shift @{ $self->{stashed_server_warnings} };
234 return Test::More::like($warning, $re, $name);
239 my $name = shift || "no warnings emitted";
241 local $Test::Builder::Level = $Test::Builder::Level + 1;
243 my @warnings = $self->get_warnings;
245 Test::More::is(@warnings, 0, $name);
247 Test::More::diag("got warning: $_");
250 return @warnings == 0 ? 1 : 0;
253 sub no_leftover_warnings_ok {
256 my $name = shift || "no leftover warnings";
258 local $Test::Builder::Level = $Test::Builder::Level + 1;
260 # we clear the warnings because we don't want to break later tests
261 # in case there *are* leftover warnings
262 my @warnings = splice @{ $self->{stashed_server_warnings} || [] };
264 Test::More::is(@warnings, 0, $name);
266 Test::More::diag("leftover warning: $_");
269 return @warnings == 0 ? 1 : 0;
276 $self->display_ticket( $id);
277 my ($got) = ($self->content =~ m{Status:\s*</td>\s*<td[^>]*?class="value"[^>]*?>\s*([\w ]+?)\s*</td>}ism);
279 Test::More::diag("Error: couldn't find status value on the page, may be regexp problem");
284 sub ticket_status_is {
288 my $desc = shift || "Status of the ticket #$id is '$status'";
289 local $Test::Builder::Level = $Test::Builder::Level + 1;
290 return Test::More::is($self->ticket_status( $id), $status, $desc);
295 my $content = $self->content;
297 if ($content =~ /.*Ticket (\d+) created.*/g) {
300 elsif ($content =~ /.*No permission to view newly created ticket #(\d+).*/g) {
301 Test::More::diag("\nNo permissions to view the ticket.\n") if($ENV{'TEST_VERBOSE'});
307 sub set_custom_field {
313 my $field_name = $self->custom_field_input( $queue, $cf_name )
316 $self->field($field_name, $val);
320 sub custom_field_input {
325 my $cf_obj = RT::CustomField->new( $RT::SystemUser );
326 $cf_obj->LoadByName( Queue => $queue, Name => $cf_name );
327 unless ( $cf_obj->id ) {
328 Test::More::diag("Can not load custom field '$cf_name' in queue '$queue'");
331 my $cf_id = $cf_obj->id;
334 grep /^Object-RT::Ticket-\d*-CustomField-$cf_id-Values?$/,
336 $self->current_form->inputs;
338 Test::More::diag("Can not find input for custom field '$cf_name' #$cf_id");
348 my %has = map {$_ => 1} @{ $args{'has'} };
349 my %has_no = map {$_ => 1} @{ $args{'has_no'} };
351 local $Test::Builder::Level = $Test::Builder::Level + 1;
355 my @links = $self->followable_links;
356 foreach my $text ( grep defined && length, map $_->text, @links ) {
357 push @found, $text if $has_no{ $text };
358 delete $has{ $text };
360 if ( @found || keys %has ) {
361 Test::More::ok( 0, "expected links" );
362 Test::More::diag( "didn't expect, but found: ". join ', ', map "'$_'", @found )
364 Test::More::diag( "didn't find, but expected: ". join ', ', map "'$_'", keys %has )
368 return Test::More::ok( 1, "expected links" );
373 if ( !$RT::Test::Web::DESTROY++ ) {
374 $self->no_warnings_ok;
379 return unless $instance;
380 return if RT::Test->builder->{Original_Pid} != $$;
381 $instance->no_warnings_ok if !$RT::Test::Web::DESTROY++;