This commit was generated by cvs2svn to compensate for changes in r10640,
[freeside.git] / rt / lib / RT / Test / Web.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2011 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
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     $self->logout;
81
82     my $url = $self->rt_base_url;
83     $self->get($url . "?user=$user;pass=$pass");
84     unless ( $self->status == 200 ) {
85         Test::More::diag( "error: status is ". $self->status );
86         return 0;
87     }
88     unless ( $self->content =~ qr/Logout/i ) {
89         Test::More::diag("error: page has no Logout");
90         return 0;
91     }
92     unless ( $self->content =~ m{<span>\Q$user\E</span>}i ) {
93         Test::More::diag("Page has no user name");
94         return 0;
95     }
96     return 1;
97 }
98
99 sub logout {
100     my $self = shift;
101
102     my $url = $self->rt_base_url;
103     $self->get($url);
104     Test::More::diag( "error: status is ". $self->status )
105         unless $self->status == 200;
106
107     if ( $self->content =~ qr/Logout/i ) {
108         $self->follow_link( text => 'Logout' );
109         Test::More::diag( "error: status is ". $self->status ." when tried to logout" )
110             unless $self->status == 200;
111     }
112     else {
113         return 1;
114     }
115
116     $self->get($url);
117     if ( $self->content =~ qr/Logout/i ) {
118         Test::More::diag( "error: couldn't logout" );
119         return 0;
120     }
121     return 1;
122 }
123
124 sub goto_ticket {
125     my $self = shift;
126     my $id   = shift;
127     unless ( $id && int $id ) {
128         Test::More::diag( "error: wrong id ". defined $id? $id : '(undef)' );
129         return 0;
130     }
131
132     my $url = $self->rt_base_url;
133     $url .= "/Ticket/Display.html?id=$id";
134     $self->get($url);
135     unless ( $self->status == 200 ) {
136         Test::More::diag( "error: status is ". $self->status );
137         return 0;
138     }
139     return 1;
140 }
141
142 sub goto_create_ticket {
143     my $self = shift;
144     my $queue = shift;
145
146     my $id;
147     if ( ref $queue ) {
148         $id = $queue->id;
149     } elsif ( $queue =~ /^\d+$/ ) {
150         $id = $queue;
151     } else {
152         die "not yet implemented";
153     }
154
155     $self->get('/');
156     $self->form_name('CreateTicketInQueue');
157     $self->select( 'Queue', $id );
158     $self->submit;
159
160     return 1;
161 }
162
163 sub get_warnings {
164     my $self = shift;
165     my $server_class = 'RT::Interface::Web::Standalone';
166
167     my $url = $server_class->test_warning_path;
168
169     local $Test::Builder::Level = $Test::Builder::Level + 1;
170     return unless $self->get_ok($url);
171
172     my @warnings = $server_class->decode_warnings($self->content);
173     return @warnings;
174 }
175
176 sub warning_like {
177     my $self = shift;
178     my $re   = shift;
179     my $name = shift;
180
181     local $Test::Builder::Level = $Test::Builder::Level + 1;
182
183     my @warnings = $self->get_warnings;
184     if (@warnings == 0) {
185         Test::More::fail("no warnings emitted; expected 1");
186         return 0;
187     }
188     elsif (@warnings > 1) {
189         Test::More::fail(scalar(@warnings) . " warnings emitted; expected 1");
190         for (@warnings) {
191             Test::More::diag("got warning: $_");
192         }
193         return 0;
194     }
195
196     return Test::More::like($warnings[0], $re, $name);
197 }
198
199 sub no_warnings_ok {
200     my $self = shift;
201     my $name = shift || "no warnings emitted";
202
203     local $Test::Builder::Level = $Test::Builder::Level + 1;
204
205     my @warnings = $self->get_warnings;
206
207     Test::More::is(@warnings, 0, $name);
208     for (@warnings) {
209         Test::More::diag("got warning: $_");
210     }
211
212     return @warnings == 0 ? 1 : 0;
213 }
214
215 1;