This commit was generated by cvs2svn to compensate for changes in r8690,
[freeside.git] / rt / lib / RT / Test / Web.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 #                                          <jesse@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
56 require RT::Test;
57 require Test::More;
58
59 sub get_ok {
60     my $self = shift;
61     my $url = shift;
62     if ( $url =~ s!^/!! ) {
63         $url = $self->rt_base_url . $url;
64     }
65     my $rv = $self->SUPER::get_ok($url, @_);
66     Test::More::diag( "Couldn't get $url" ) unless $rv;
67     return $rv;
68 }
69
70 sub rt_base_url {
71     return $RT::Test::existing_server if $RT::Test::existing_server;
72     return "http://localhost:" . RT->Config->Get('WebPort') . RT->Config->Get('WebPath') . "/";
73 }
74
75 sub login {
76     my $self = shift;
77     my $user = shift || 'root';
78     my $pass = shift || 'password';
79
80     my $url = $self->rt_base_url;
81
82     $self->get($url);
83     Test::More::diag( "error: status is ". $self->status )
84         unless $self->status == 200;
85     if ( $self->content =~ qr/Logout/i ) {
86         $self->follow_link( text => 'Logout' );
87     }
88
89     $self->get($url . "?user=$user;pass=$pass");
90     unless ( $self->status == 200 ) {
91         Test::More::diag( "error: status is ". $self->status );
92         return 0;
93     }
94     unless ( $self->content =~ qr/Logout/i ) {
95         Test::More::diag("error: page has no Logout");
96         return 0;
97     }
98     return 1;
99 }
100
101 sub goto_ticket {
102     my $self = shift;
103     my $id   = shift;
104     unless ( $id && int $id ) {
105         Test::More::diag( "error: wrong id ". defined $id? $id : '(undef)' );
106         return 0;
107     }
108
109     my $url = $self->rt_base_url;
110     $url .= "/Ticket/Display.html?id=$id";
111     $self->get($url);
112     unless ( $self->status == 200 ) {
113         Test::More::diag( "error: status is ". $self->status );
114         return 0;
115     }
116     return 1;
117 }
118
119 sub goto_create_ticket {
120     my $self = shift;
121     my $queue = shift;
122
123     my $id;
124     if ( ref $queue ) {
125         $id = $queue->id;
126     } elsif ( $queue =~ /^\d+$/ ) {
127         $id = $queue;
128     } else {
129         die "not yet implemented";
130     }
131
132     $self->get('/');
133     $self->form_name('CreateTicketInQueue');
134     $self->select( 'Queue', $id );
135     $self->submit;
136
137     return 1;
138 }
139
140 sub get_warnings {
141     my $self = shift;
142     my $server_class = 'RT::Interface::Web::Standalone';
143
144     my $url = $server_class->test_warning_path;
145
146     local $Test::Builder::Level = $Test::Builder::Level + 1;
147     return unless $self->get_ok($url);
148
149     my @warnings = $server_class->decode_warnings($self->content);
150     return @warnings;
151 }
152
153 sub warning_like {
154     my $self = shift;
155     my $re   = shift;
156     my $name = shift;
157
158     local $Test::Builder::Level = $Test::Builder::Level + 1;
159
160     my @warnings = $self->get_warnings;
161     if (@warnings == 0) {
162         Test::More::fail("no warnings emitted; expected 1");
163         return 0;
164     }
165     elsif (@warnings > 1) {
166         Test::More::fail(scalar(@warnings) . " warnings emitted; expected 1");
167         for (@warnings) {
168             Test::More::diag("got warning: $_");
169         }
170         return 0;
171     }
172
173     return Test::More::like($warnings[0], $re, $name);
174 }
175
176 sub no_warnings_ok {
177     my $self = shift;
178     my $name = shift || "no warnings emitted";
179
180     local $Test::Builder::Level = $Test::Builder::Level + 1;
181
182     my @warnings = $self->get_warnings;
183
184     Test::More::is(@warnings, 0, $name);
185     for (@warnings) {
186         Test::More::diag("got warning: $_");
187     }
188
189     return @warnings == 0 ? 1 : 0;
190 }
191
192 1;