1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2012 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);
60 my ($class, @args) = @_;
62 push @args, app => $RT::Test::TEST_APP if $RT::Test::TEST_APP;
63 my $self = $class->SUPER::new(@args);
64 $self->cookie_jar(HTTP::Cookies->new);
72 if ( $url =~ s!^/!! ) {
73 $url = $self->rt_base_url . $url;
75 my $rv = $self->SUPER::get_ok($url, @_);
76 Test::More::diag( "Couldn't get $url" ) unless $rv;
81 return $RT::Test::existing_server if $RT::Test::existing_server;
82 return "http://localhost:" . RT->Config->Get('WebPort') . RT->Config->Get('WebPath') . "/";
87 my $user = shift || 'root';
88 my $pass = shift || 'password';
91 $self->logout if $args{logout};
93 my $url = $self->rt_base_url;
94 $self->get($url . "?user=$user;pass=$pass");
95 unless ( $self->status == 200 ) {
96 Test::More::diag( "error: status is ". $self->status );
99 unless ( $self->content =~ m/Logout/i ) {
100 Test::More::diag("error: page has no Logout");
103 unless ( $self->content =~ m{<span class="current-user">\Q$user\E</span>}i ) {
104 Test::More::diag("Page has no user name");
113 my $url = $self->rt_base_url;
115 Test::More::diag( "error: status is ". $self->status )
116 unless $self->status == 200;
118 if ( $self->content =~ /Logout/i ) {
119 $self->follow_link( text => 'Logout' );
120 Test::More::diag( "error: status is ". $self->status ." when tried to logout" )
121 unless $self->status == 200;
128 if ( $self->content =~ /Logout/i ) {
129 Test::More::diag( "error: couldn't logout" );
138 unless ( $id && int $id ) {
139 Test::More::diag( "error: wrong id ". defined $id? $id : '(undef)' );
143 my $url = $self->rt_base_url;
144 $url .= "Ticket/Display.html?id=$id";
146 unless ( $self->status == 200 ) {
147 Test::More::diag( "error: status is ". $self->status );
153 sub goto_create_ticket {
160 } elsif ( $queue =~ /^\d+$/ ) {
163 die "not yet implemented";
166 $self->get($self->rt_base_url . 'Ticket/Create.html?Queue='.$id);
173 local $Test::Builder::Level = $Test::Builder::Level + 1;
175 # We clone here so that when we fetch warnings, we don't disrupt the state
176 # of the test's mech. If we reuse the original mech then you can't
177 # test warnings immediately after fetching page XYZ, then fill out
178 # forms on XYZ. This is because the most recently fetched page has changed
179 # from XYZ to /__test_warnings, which has no form.
180 my $clone = $self->clone;
181 return unless $clone->get_ok('/__test_warnings');
185 my @warnings = @{ thaw $clone->content };
194 local $Test::Builder::Level = $Test::Builder::Level + 1;
196 my @warnings = $self->get_warnings;
197 if (@warnings == 0) {
198 Test::More::fail("no warnings emitted; expected 1");
201 elsif (@warnings > 1) {
202 Test::More::fail(scalar(@warnings) . " warnings emitted; expected 1");
204 Test::More::diag("got warning: $_");
209 return Test::More::like($warnings[0], $re, $name);
212 sub next_warning_like {
217 local $Test::Builder::Level = $Test::Builder::Level + 1;
219 if (@{ $self->{stashed_server_warnings} || [] } == 0) {
220 my @warnings = $self->get_warnings;
221 if (@warnings == 0) {
222 Test::More::fail("no warnings emitted; expected 1");
225 $self->{stashed_server_warnings} = \@warnings;
228 my $warning = shift @{ $self->{stashed_server_warnings} };
229 return Test::More::like($warning, $re, $name);
234 my $name = shift || "no warnings emitted";
236 local $Test::Builder::Level = $Test::Builder::Level + 1;
238 my @warnings = $self->get_warnings;
240 Test::More::is(@warnings, 0, $name);
242 Test::More::diag("got warning: $_");
245 return @warnings == 0 ? 1 : 0;
248 sub no_leftover_warnings_ok {
251 my $name = shift || "no leftover warnings";
253 local $Test::Builder::Level = $Test::Builder::Level + 1;
255 # we clear the warnings because we don't want to break later tests
256 # in case there *are* leftover warnings
257 my @warnings = splice @{ $self->{stashed_server_warnings} || [] };
259 Test::More::is(@warnings, 0, $name);
261 Test::More::diag("leftover warning: $_");
264 return @warnings == 0 ? 1 : 0;
271 $self->display_ticket( $id);
272 my ($got) = ($self->content =~ m{Status:\s*</td>\s*<td[^>]*?class="value"[^>]*?>\s*([\w ]+?)\s*</td>}ism);
274 Test::More::diag("Error: couldn't find status value on the page, may be regexp problem");
279 sub ticket_status_is {
283 my $desc = shift || "Status of the ticket #$id is '$status'";
284 local $Test::Builder::Level = $Test::Builder::Level + 1;
285 return Test::More::is($self->ticket_status( $id), $status, $desc);
290 my $content = $self->content;
292 if ($content =~ /.*Ticket (\d+) created.*/g) {
295 elsif ($content =~ /.*No permission to view newly created ticket #(\d+).*/g) {
296 Test::More::diag("\nNo permissions to view the ticket.\n") if($ENV{'TEST_VERBOSE'});
302 sub set_custom_field {
308 my $field_name = $self->custom_field_input( $queue, $cf_name )
311 $self->field($field_name, $val);
315 sub custom_field_input {
320 my $cf_obj = RT::CustomField->new( $RT::SystemUser );
321 $cf_obj->LoadByName( Queue => $queue, Name => $cf_name );
322 unless ( $cf_obj->id ) {
323 Test::More::diag("Can not load custom field '$cf_name' in queue '$queue'");
326 my $cf_id = $cf_obj->id;
329 grep /^Object-RT::Ticket-\d*-CustomField-$cf_id-Values?$/,
331 $self->current_form->inputs;
333 Test::More::diag("Can not find input for custom field '$cf_name' #$cf_id");
343 my %has = map {$_ => 1} @{ $args{'has'} };
344 my %has_no = map {$_ => 1} @{ $args{'has_no'} };
346 local $Test::Builder::Level = $Test::Builder::Level + 1;
350 my @links = $self->followable_links;
351 foreach my $text ( grep defined && length, map $_->text, @links ) {
352 push @found, $text if $has_no{ $text };
353 delete $has{ $text };
355 if ( @found || keys %has ) {
356 Test::More::ok( 0, "expected links" );
357 Test::More::diag( "didn't expect, but found: ". join ', ', map "'$_'", @found )
359 Test::More::diag( "didn't find, but expected: ". join ', ', map "'$_'", keys %has )
363 return Test::More::ok( 1, "expected links" );
368 if ( !$RT::Test::Web::DESTROY++ ) {
369 $self->no_warnings_ok;