source: trunk/lib/OpenGuides/Test.pm

Last change on this file was 1281, checked in by Dominic Hargreaves, 12 years ago

bump module version numbers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.5 KB
Line 
1package OpenGuides::Test;
2
3use OpenGuides::Config;
4use Wiki::Toolkit::Setup::SQLite;
5
6use strict;
7use vars qw( $VERSION );
8$VERSION = '0.06';
9
10use CGI;
11
12=head1 NAME
13
14OpenGuides::Test - Methods to help test OpenGuides applications.
15
16=head1 DESCRIPTION
17
18Provides methods to help when writing tests for OpenGuides.
19Distributed and installed as part of the OpenGuides project, not
20intended for independent installation.  This documentation is probably
21only useful to OpenGuides developers.
22
23=head1 SYNOPSIS
24
25  use OpenGuides;
26  use OpenGuides::Test;
27
28  OpenGuides::Test::refresh_db();
29
30  my $config = OpenGuides::Test->make_basic_config;
31  $config->default_language( "nl" );
32
33  my $guide = OpenGuides->new( config => $config );
34
35  OpenGuides::Test->write_data(
36                                guide      => $guide,
37                                node       => "Crabtree Tavern",
38                                os_x       => 523465,
39                                os_y       => 177490,
40                                categories => "Pubs",
41                              );
42
43=head1 METHODS
44
45=over 4
46
47=item B<make_basic_config>
48
49  my $config = OpenGuides::Test->make_basic_config;
50  $config->default_language( "nl" );
51
52Makes an L<OpenGuides::Config> object with needed fields pre-filled.  You can
53mess with it as you like then.
54
55=cut
56
57sub make_basic_config {
58    my $config = OpenGuides::Config->new(
59           vars => {
60                     dbtype               => "sqlite",
61                     dbname               => "t/node.db",
62                     indexing_directory   => "t/indexes",
63                     script_url           => "",
64                     script_name          => "",
65                     site_name            => "Test",
66                     template_path        => "./templates",
67                     custom_template_path => "./custom-templates",
68                     home_name            => "Home",
69                     geo_handler          => 1,
70                     force_wgs84          => 1,
71                     contact_email        => 'admins@example.org',
72                     moderate_whitelist   => "",
73                   }
74    );
75
76    eval { require Wiki::Toolkit::Search::Plucene; };
77    if ( $@ ) { $config->use_plucene ( 0 ) };
78       
79    return $config;
80}
81
82=item B<write_data>
83
84  my $config = OpenGuides::Test->make_basic_config;
85  my $guide = OpenGuides->new( config => $config );
86
87  OpenGuides::Test->write_data(
88                                guide      => $guide,
89                                node       => "Crabtree Tavern",
90                                os_x       => 523465,
91                                os_y       => 177490,
92                                categories => "Pubs\r\nPub Food",
93                              );
94
95This method calls the C<make_cgi_object> method to make its CGI
96object; you can supply values for any key mentioned there.  You should
97supply them exactly as they would come from a CGI form, eg lines in a
98textarea are separated by C<\r\n>.
99
100This method will automatically grab the checksum from the database, so
101even if the node already exists your data will still be written.  If you
102don't want this behaviour (for example, if you're testing edit conflicts)
103then pass in a true value to the C<omit_checksum> parameter:
104
105  OpenGuides::Test->write_data(
106                                guide         => $guide,
107                                node          => "Crabtree Tavern",
108                                omit_checksum => 1,
109                              );
110
111If you want to grab the output, pass a true value to C<return_output>:
112
113  my $output = OpenGuides::Test->write_data(
114                                             guide        => $guide,
115                                             node         => "Crabtree Tavern",
116                                             return_output => 1,
117                                           );
118
119Similarly, if you pass a true value to C<return_tt_vars>, the return value
120will be the variables which would have been passed to the template for output:
121
122  my %vars = OpenGuides::Test->write_data(
123                                             guide        => $guide,
124                                             node         => "Crabtree Tavern",
125                                             return_tt_vars => 1,
126                                           );
127
128=cut
129
130sub write_data {
131    my ($class, %args) = @_;
132
133    my $guide = delete $args{guide};
134    my $node  = delete $args{node};
135
136    my $q = $class->make_cgi_object( %args );
137   
138    # Get the checksum of the current contents if necessary.
139    unless ( $args{omit_checksum} ) {
140        my $wiki = $guide->wiki;
141        if ( $wiki->node_exists( $node ) ) {
142            my %data = $wiki->retrieve_node( $node );
143            $q->param( -name => "checksum", -value => $data{checksum} );
144        }
145    }
146 
147    if ( $args{return_output} ) {
148        return $guide->commit_node(
149                                          return_output => 1,
150                                          id => $node,
151                                          cgi_obj => $q,
152                                        );
153    } elsif ( $args{return_tt_vars} ) {
154        return $guide->commit_node(
155                                          return_tt_vars => 1,
156                                          id => $node,
157                                          cgi_obj => $q,
158                                        );
159    } else {
160        $guide->commit_node(
161                                   id => $node,
162                                   cgi_obj => $q,
163                                 );
164    }
165}
166
167=item B<make_cgi_object>
168
169  my $q = OpenGuides::Test->make_cgi_object;
170
171You can supply values for the following keys: C<content>, C<categories>,
172C<locales>, C<node_image>, C<node_image_licence>,
173C<node_image_copyright>, C<node_image_url>, C<phone>, C<fax>,
174C<website>, C<hours_text>, C<address>, C<postcode>, C<map_link>,
175C<os_x>, C<os_y>, C<osie_x>, C<osie_y>, C<latitude>, C<longitude>,
176C<summary>, C<username>, C<comment>, C<edit_type>.  You should supply
177them exactly as they would come from a CGI form, eg lines in a textarea
178are separated by C<\r\n>.
179
180=cut
181
182sub make_cgi_object {
183    my ( $class, %args ) = @_;
184
185    # Set up CGI parameters ready for a node write.
186    # Most of these are in here to avoid uninitialised value warnings.
187    my $q = CGI->new( "" );
188    $args{content} ||= "foo";
189    $args{edit_type} ||= "Normal edit";
190    for my $param ( qw( content categories locales node_image node_image_licence
191                        node_image_copyright node_image_url phone fax website
192                        hours_text address postcode map_link os_x os_y osie_x osie_y
193                        latitude longitude summary username comment edit_type
194                      )
195                  ) {
196        if (defined $args{$param}) {
197            $q->param( -name => $param, -value => $args{$param} );
198        } else {
199            $q->param( -name => $param, -value => '' );
200        }
201    }
202    $ENV{REMOTE_ADDR} = "127.0.0.1";
203
204    return $q;
205}
206
207=item B<refresh_db>
208
209  Openguides::Test::refresh_db();
210
211Unlink the existing SQLite database t/node.db and plucene indexes. Then create a new SQLite database t/node.db
212
213=cut 
214sub refresh_db {
215    unlink "t/node.db";
216    unlink <t/indexes/*>;
217    Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
218}
219
220
221=back
222
223=head1 AUTHOR
224
225The OpenGuides Project (openguides-dev@lists.openguides.org)
226
227=head1 COPYRIGHT
228
229  Copyright (C) 2004-2009 The OpenGuides Project.  All Rights Reserved.
230
231This module is free software; you can redistribute it and/or modify it
232under the same terms as Perl itself.
233
234=cut
235
2361;
Note: See TracBrowser for help on using the repository browser.