source: trunk/lib/OpenGuides/Test.pm @ 1163

Last change on this file since 1163 was 1163, checked in by Dominic Hargreaves, 14 years ago

update version numbers and copyright years

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