5 use warnings FATAL => 'all';
7 use FS::UID qw(adminsuidsetup);
11 use Class::Accessor 'antlers';
12 use Class::Load qw(load_class);
16 our $VERSION = '0.03';
20 Freeside testing suite
24 use Test::More 'tests' => 1;
26 my $FS = FS::Test->new;
27 $FS->post('/edit/cust_main.cgi', ... ); # form fields
36 The content of the most recent page fetched from the UI.
40 The redirect location (relative to the Freeside root) of the redirect
41 returned from the UI, if there was one.
47 Creates a test session. OPTIONS may contain:
49 - user: the Freeside test username [test]
50 - base: the fake base URL for Mason to use [http://fake.freeside.biz]
54 has user => ( is => 'rw' );
55 has base => ( is => 'ro' );
56 has fs_interp => ( is => 'rw' );
57 has path => ( is => 'rw' );
58 has page => ( is => 'ro' );
59 has error => ( is => 'rw' );
60 has dbh => ( is => 'rw' );
61 has redirect => ( is => 'rw' );
69 base => 'http://fake.freeside.biz',
72 $self->{base} = URI->new($self->{base});
75 adminsuidsetup($self->user);
76 load_class('FS::Mason');
77 $self->dbh( FS::UID::dbh() );
79 my ($fs_interp) = FS::Mason::mason_interps('standalone',
80 outbuf => \($self->{page})
82 $fs_interp->error_mode('fatal');
83 $fs_interp->error_format('brief');
85 $self->fs_interp( $fs_interp );
99 =item post PATH, PARAMS
103 Submits a request to PATH, through the Mason UI, with arguments in PARAMS.
104 This will be converted to a URL query string. Anything returned by the UI
105 will be in the C<page()> property.
107 Alternatively, takes an L<HTML::Form> object (with fields filled in, via
108 the C<param()> method) and submits it.
116 local $CGI::LIST_CONTEXT_WARN = 0;
119 if ( UNIVERSAL::isa($_[0], 'HTML::Form') ) {
121 my $request = $form->make_request;
122 $path = $request->uri->path;
123 $query = $request->content;
127 if (scalar(@params) == 0) {
128 # possibly path?query syntax, or else no query string at all
129 ($path, $query) = split('\?', $path);
130 } elsif (scalar(@params) == 1) {
131 $query = uri_escape($params[0]); # keyword style
134 $query .= uri_escape(shift @params) . '=' .
135 uri_escape(shift @params);
136 $query .= ';' if @params;
140 # remember which page this is
143 local $FS::Mason::Request::FSURL = $self->base->as_string;
144 local $FS::Mason::Request::QUERY_STRING = $query;
145 # because we're going to construct an actual CGI object in here
146 local $ENV{SERVER_NAME} = $self->base->host;
147 local $ENV{SCRIPT_NAME} = $self->base->path . $path;
149 my $mason_request = $self->fs_interp->make_request(comp => $path);
151 $mason_request->exec();
155 if ( ref $@ eq 'HTML::Mason::Exception' ) {
156 $self->error($@->message);
160 } elsif ( $mason_request->notes('error') ) {
161 $self->error($mason_request->notes('error'));
166 if ( my $loc = $mason_request->redirect ) {
167 my $base = $self->base->as_string;
169 $self->redirect($loc);
173 ''; # return error? HTTP status? something?
178 If the last request returned a redirect, follow it.
184 if ($self->redirect) {
185 $self->post($self->redirect);
192 For the most recently returned page, returns a list of L<HTML::Form>s found.
198 my $formbase = $self->base->as_string . $self->path;
199 return HTML::Form->parse( $self->page, base => $formbase );
204 For the most recently returned page, returns an L<HTML::Form> object
205 representing the form named NAME. You can then call methods like
206 C<value(inputname, inputvalue)> to set the values of inputs on the form,
207 and then pass the form object to L</post> to submit it.
214 my ($form) = grep { ($_->attr('name') || '') eq $name } $self->forms;
218 =item qsearch ARGUMENTS
220 Searches the database, like L<FS::Record::qsearch>.
222 =item qsearchs ARGUMENTS
224 Searches the database for a single record, like L<FS::Record::qsearchs>.
230 FS::Record::qsearch(@_);
235 FS::Record::qsearchs(@_);
238 =item new_customer FIRSTNAME
240 Returns an L<FS::cust_main> object full of default test data, ready to be inserted.
241 This doesn't insert the customer, because you might want to change some things first.
242 FIRSTNAME is recommended so you know which test the customer was used for.
248 my $first = shift || 'No Name';
249 my $location = FS::cust_location->new({
250 address1 => '123 Example Street',
251 city => 'Sacramento',
256 my $cust = FS::cust_main->new({
261 invoice_email => 'newcustomer@fake.freeside.biz',
263 bill_location => $location,
264 ship_location => $location,