File Coverage

blib/lib/OpenGuides/Test.pm
Criterion Covered Total %
statement 45 47 95.7
branch 10 12 83.3
condition 4 4 100.0
subroutine 9 9 100.0
pod 4 4 100.0
total 72 76 94.7


line stmt bran cond sub pod time code
1             package OpenGuides::Test;
2              
3 93     93   85758 use OpenGuides::Config;
  93         173  
  93         661  
4 93     93   19705 use Wiki::Toolkit::Setup::SQLite;
  93         604137  
  93         1979  
5              
6 93     93   372 use strict;
  93         100  
  93         2082  
7 93     93   284 use vars qw( $VERSION );
  93         105  
  93         3460  
8             $VERSION = '0.07';
9              
10 93     93   1622 use CGI;
  93         39212  
  93         558  
11              
12             =head1 NAME
13              
14             OpenGuides::Test - Methods to help test OpenGuides applications.
15              
16             =head1 DESCRIPTION
17              
18             Provides methods to help when writing tests for OpenGuides.
19             Distributed and installed as part of the OpenGuides project, not
20             intended for independent installation. This documentation is probably
21             only 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
48              
49             my $config = OpenGuides::Test->make_basic_config;
50             $config->default_language( "nl" );
51              
52             Makes an L object with needed fields pre-filled. You can
53             mess with it as you like then.
54              
55             =cut
56              
57             sub make_basic_config {
58 87     87 1 19095847 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 87         334 eval { require Wiki::Toolkit::Search::Plucene; };
  87         39222  
77 87 50       4835612 if ( $@ ) { $config->use_plucene ( 0 ) };
  0         0  
78            
79 87         357 return $config;
80             }
81              
82             =item B
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              
95             This method calls the C method to make its CGI
96             object; you can supply values for any key mentioned there. You should
97             supply them exactly as they would come from a CGI form, eg lines in a
98             textarea are separated by C<\r\n>.
99              
100             This method will automatically grab the checksum from the database, so
101             even if the node already exists your data will still be written. If you
102             don't want this behaviour (for example, if you're testing edit conflicts)
103             then pass in a true value to the C parameter:
104              
105             OpenGuides::Test->write_data(
106             guide => $guide,
107             node => "Crabtree Tavern",
108             omit_checksum => 1,
109             );
110              
111             If you want to grab the output, pass a true value to C:
112              
113             my $output = OpenGuides::Test->write_data(
114             guide => $guide,
115             node => "Crabtree Tavern",
116             return_output => 1,
117             );
118              
119             Similarly, if you pass a true value to C, the return value
120             will 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              
130             sub write_data {
131 317     317 1 542108 my ($class, %args) = @_;
132              
133 317         755 my $guide = delete $args{guide};
134 317         616 my $node = delete $args{node};
135              
136 317         1224 my $q = $class->make_cgi_object( %args );
137              
138             # Get the checksum of the current contents if necessary.
139 317 100       1219 unless ( $args{omit_checksum} ) {
140 315         1084 my $wiki = $guide->wiki;
141 315 100       1368 if ( $wiki->node_exists( $node ) ) {
142 16         19731 my %data = $wiki->retrieve_node( $node );
143 16         16210 $q->param( -name => "checksum", -value => $data{checksum} );
144             }
145             }
146              
147 317 100       165708 if ( $args{return_output} ) {
    50          
148 275         1349 return $guide->commit_node(
149             return_output => 1,
150             id => $node,
151             cgi_obj => $q,
152             );
153             } elsif ( $args{return_tt_vars} ) {
154 0         0 return $guide->commit_node(
155             return_tt_vars => 1,
156             id => $node,
157             cgi_obj => $q,
158             );
159             } else {
160 42         232 $guide->commit_node(
161             id => $node,
162             cgi_obj => $q,
163             );
164             }
165             }
166              
167             =item B
168              
169             my $q = OpenGuides::Test->make_cgi_object;
170              
171             You can supply values for the following keys: C, C,
172             C, C, C,
173             C, C, C, C,
174             C, C, C
, C, C,
175             C, C, C, C, C, C,
176             C, C, C, C. You should supply
177             them exactly as they would come from a CGI form, eg lines in a textarea
178             are separated by C<\r\n>.
179              
180             =cut
181              
182             sub make_cgi_object {
183 329     329 1 10996 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 329         1321 my $q = CGI->new( "" );
188 329   100     44043 $args{content} ||= "foo";
189 329   100     1491 $args{edit_type} ||= "Normal edit";
190 329         1068 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 7896 100       404521 if (defined $args{$param}) {
197 1276         3668 $q->param( -name => $param, -value => $args{$param} );
198             } else {
199 6620         14103 $q->param( -name => $param, -value => '' );
200             }
201             }
202 329         18322 $ENV{REMOTE_ADDR} = "127.0.0.1";
203              
204 329         904 return $q;
205             }
206              
207             =item B
208              
209             Openguides::Test::refresh_db();
210              
211             Unlink the existing SQLite database t/node.db and Plucene/Lucy indexes.
212             Then create a new SQLite database t/node.db
213              
214             =cut
215             sub refresh_db {
216 90     90 1 1100676 unlink "t/node.db";
217 90         49662 unlink ;
218 90         840 Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
219             }
220              
221              
222             =back
223              
224             =head1 AUTHOR
225              
226             The OpenGuides Project (openguides-dev@lists.openguides.org)
227              
228             =head1 COPYRIGHT
229              
230             Copyright (C) 2004-2013 The OpenGuides Project. All Rights Reserved.
231              
232             This module is free software; you can redistribute it and/or modify it
233             under the same terms as Perl itself.
234              
235             =cut
236              
237             1;