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 92     92   116950 use OpenGuides::Config;
  92         301  
  92         1000  
4 92     92   32827 use Wiki::Toolkit::Setup::SQLite;
  92         869396  
  92         2870  
5              
6 92     92   643 use strict;
  92         151  
  92         4429  
7 92     92   456 use vars qw( $VERSION );
  92         143  
  92         5903  
8             $VERSION = '0.07';
9              
10 92     92   2548 use CGI;
  92         53689  
  92         744  
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 86     86 1 20033110 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 86         445 eval { require Wiki::Toolkit::Search::Plucene; };
  86         56971  
77 86 50       7364384 if ( $@ ) { $config->use_plucene ( 0 ) };
  0         0  
78            
79 86         473 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 315     315 1 835825 my ($class, %args) = @_;
132              
133 315         1010 my $guide = delete $args{guide};
134 315         878 my $node = delete $args{node};
135              
136 315         1568 my $q = $class->make_cgi_object( %args );
137              
138             # Get the checksum of the current contents if necessary.
139 315 100       1436 unless ( $args{omit_checksum} ) {
140 313         1512 my $wiki = $guide->wiki;
141 313 100       1768 if ( $wiki->node_exists( $node ) ) {
142 16         28891 my %data = $wiki->retrieve_node( $node );
143 16         23765 $q->param( -name => "checksum", -value => $data{checksum} );
144             }
145             }
146              
147 315 100       230911 if ( $args{return_output} ) {
    50          
148 273         1818 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         239 $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 327     327 1 14947 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 327         1839 my $q = CGI->new( "" );
188 327   100     59344 $args{content} ||= "foo";
189 327   100     1976 $args{edit_type} ||= "Normal edit";
190 327         1453 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 7848 100       600616 if (defined $args{$param}) {
197 1267         5137 $q->param( -name => $param, -value => $args{$param} );
198             } else {
199 6581         20573 $q->param( -name => $param, -value => '' );
200             }
201             }
202 327         27320 $ENV{REMOTE_ADDR} = "127.0.0.1";
203              
204 327         1351 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 89     89 1 1570278 unlink "t/node.db";
217 89         64249 unlink ;
218 89         1173 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;