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

Last change on this file since 1240 was 1240, checked in by Dominic Hargreaves, 13 years ago

Add the ability to whitelist hosts who can change moderated nodes
without explicit moderation - thanks Oliver (fixes #203)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 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                     moderate_whitelist   => "",
70                   }
71    );
72
73    eval { require Wiki::Toolkit::Search::Plucene; };
74    if ( $@ ) { $config->use_plucene ( 0 ) };
75       
76    return $config;
77}
78
79=item B<write_data>
80
81  my $config = OpenGuides::Test->make_basic_config;
82  my $guide = OpenGuides->new( config => $config );
83
84  OpenGuides::Test->write_data(
85                                guide      => $guide,
86                                node       => "Crabtree Tavern",
87                                os_x       => 523465,
88                                os_y       => 177490,
89                                categories => "Pubs\r\nPub Food",
90                              );
91
92This method calls the C<make_cgi_object> method to make its CGI
93object; you can supply values for any key mentioned there.  You should
94supply them exactly as they would come from a CGI form, eg lines in a
95textarea are separated by C<\r\n>.
96
97This method will automatically grab the checksum from the database, so
98even if the node already exists your data will still be written.  If you
99don't want this behaviour (for example, if you're testing edit conflicts)
100then pass in a true value to the C<omit_checksum> parameter:
101
102  OpenGuides::Test->write_data(
103                                guide         => $guide,
104                                node          => "Crabtree Tavern",
105                                omit_checksum => 1,
106                              );
107
108If you want to grab the output, pass a true value to C<return_output>:
109
110  my $output = OpenGuides::Test->write_data(
111                                             guide        => $guide,
112                                             node         => "Crabtree Tavern",
113                                             return_output => 1,
114                                           );
115
116Similarly, if you pass a true value to C<return_tt_vars>, the return value
117will be the variables which would have been passed to the template for output:
118
119  my %vars = OpenGuides::Test->write_data(
120                                             guide        => $guide,
121                                             node         => "Crabtree Tavern",
122                                             return_tt_vars => 1,
123                                           );
124
125=cut
126
127sub write_data {
128    my ($class, %args) = @_;
129
130    my $guide = delete $args{guide};
131    my $node  = delete $args{node};
132
133    my $q = $class->make_cgi_object( %args );
134   
135    # Get the checksum of the current contents if necessary.
136    unless ( $args{omit_checksum} ) {
137        my $wiki = $guide->wiki;
138        if ( $wiki->node_exists( $node ) ) {
139            my %data = $wiki->retrieve_node( $node );
140            $q->param( -name => "checksum", -value => $data{checksum} );
141        }
142    }
143 
144    if ( $args{return_output} ) {
145        return $guide->commit_node(
146                                          return_output => 1,
147                                          id => $node,
148                                          cgi_obj => $q,
149                                        );
150    } elsif ( $args{return_tt_vars} ) {
151        return $guide->commit_node(
152                                          return_tt_vars => 1,
153                                          id => $node,
154                                          cgi_obj => $q,
155                                        );
156    } else {
157        $guide->commit_node(
158                                   id => $node,
159                                   cgi_obj => $q,
160                                 );
161    }
162}
163
164=over 4
165
166=item B<make_cgi_object>
167
168  my $q = OpenGuides::Test->make_cgi_object;
169
170You can supply values for the following keys: C<content>, C<categories>,
171C<locales>, C<node_image>, C<node_image_licence>,
172C<node_image_copyright>, C<node_image_url>, C<phone>, C<fax>,
173C<website>, C<hours_text>, C<address>, C<postcode>, C<map_link>,
174C<os_x>, C<os_y>, C<osie_x>, C<osie_y>, C<latitude>, C<longitude>,
175C<summary>, C<username>, C<comment>, C<edit_type>.  You should supply
176them exactly as they would come from a CGI form, eg lines in a textarea
177are separated by C<\r\n>.
178
179=cut
180
181sub make_cgi_object {
182    my ( $class, %args ) = @_;
183
184    # Set up CGI parameters ready for a node write.
185    # Most of these are in here to avoid uninitialised value warnings.
186    my $q = CGI->new( "" );
187    $args{content} ||= "foo";
188    $args{edit_type} ||= "Normal edit";
189    for my $param ( qw( content categories locales node_image node_image_licence
190                        node_image_copyright node_image_url phone fax website
191                        hours_text address postcode map_link os_x os_y osie_x osie_y
192                        latitude longitude summary username comment edit_type
193                      )
194                  ) {
195        if (defined $args{$param}) {
196            $q->param( -name => $param, -value => $args{$param} );
197        } else {
198            $q->param( -name => $param, -value => '' );
199        }
200    }
201    $ENV{REMOTE_ADDR} = "127.0.0.1";
202
203    return $q;
204}
205
206=back
207
208=head1 AUTHOR
209
210The OpenGuides Project (openguides-dev@lists.openguides.org)
211
212=head1 COPYRIGHT
213
214  Copyright (C) 2004-2008 The OpenGuides Project.  All Rights Reserved.
215
216This module is free software; you can redistribute it and/or modify it
217under the same terms as Perl itself.
218
219=cut
220
2211;
Note: See TracBrowser for help on using the repository browser.