root/trunk/t/57_random_page.t

Revision 1027, 6.2 kB (checked in by kake, 21 months ago)

Added category and locale parameters to action=random and made a new macro to go with it.

Line 
1use strict;
2use Wiki::Toolkit::Setup::SQLite;
3use OpenGuides::Test;
4use OpenGuides;
5use Test::More;
6
7eval { require DBD::SQLite; };
8if ( $@ ) {
9    my ($error) = $@ =~ /^(.*?)\n/;
10    plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)";
11}
12
13plan tests => 8;
14
15my ( $config, $guide, $wiki );
16
17# Clear out database from previous runs, set up a guide.
18refresh_db();
19$config = OpenGuides::Test->make_basic_config;
20$config->script_url( "http://www.example.com/" );
21$config->script_name( "wiki.cgi" );
22$guide = OpenGuides->new( config => $config );
23$wiki = $guide->wiki;
24
25# Write some data.
26my %nodes = map { $_ => "A pub." } ( "Red Lion", "Farmers Arms", "Angel" );
27foreach my $node ( keys %nodes ) {
28  OpenGuides::Test->write_data(
29                                guide         => $guide,
30                                node          => $node,
31                                return_output => 1,
32                              );
33}
34
35# See what we get when we ask for a random page.
36my $output = $guide->display_random_page( return_output => 1 );
37
38# Old versions of CGI.pm mistakenly print location: instead of Location:
39like( $output, qr/[lL]ocation: http:\/\/www.example.com\/wiki.cgi/,
40      "->display_random_page makes a redirect" );
41
42my $node = get_node_from_output( $output );
43print "# Random node chosen: $node\n";
44ok( $nodes{$node}, "...to an existing node" );
45
46# Clear the database and write some data including categories and locales.
47refresh_db();
48$config = OpenGuides::Test->make_basic_config;
49$config->script_url( "http://www.example.com/" );
50$config->script_name( "wiki.cgi" );
51$guide = OpenGuides->new( config => $config );
52$wiki = $guide->wiki;
53
54# Write data including some categories/locales.
55OpenGuides::Test->write_data(
56                              guide         => $guide,
57                              node          => "Red Lion",
58                              locales       => "Hammersmith",
59                              categories    => "Pubs",
60                              return_output => 1,
61                            );
62
63# Check we can turn off locales.
64$config = OpenGuides::Test->make_basic_config;
65$config->script_url( "http://www.example.com/" );
66$config->script_name( "wiki.cgi" );
67$config->random_page_omits_locales( 1 );
68$guide = OpenGuides->new( config => $config );
69$wiki = $guide->wiki;
70$output = $guide->display_random_page( return_output => 1 );
71$node = get_node_from_output( $output );
72print "# Random node chosen: $node\n";
73isnt( $node, "Locale Hammersmith", "locale nodes not picked up as random page "
74                       . "(this test may sometimes pass when it shouldn't)" );
75
76# Check we can turn off categories.
77$config = OpenGuides::Test->make_basic_config;
78$config->script_url( "http://www.example.com/" );
79$config->script_name( "wiki.cgi" );
80$config->random_page_omits_categories( 1 );
81$guide = OpenGuides->new( config => $config );
82$wiki = $guide->wiki;
83$output = $guide->display_random_page( return_output => 1 );
84$node = get_node_from_output( $output );
85print "# Random node chosen: $node\n";
86isnt( $node, "Category Pubs", "category nodes not picked up as random page "
87                       . "(this test may sometimes pass when it shouldn't)" );
88
89# Now make sure we can pick things up from specific categories/locales if asked
90refresh_db();
91$config = OpenGuides::Test->make_basic_config;
92$guide = OpenGuides->new( config => $config );
93
94OpenGuides::Test->write_data(
95                              guide         => $guide,
96                              node          => "Red Lion",
97                              locales       => "Hammersmith",
98                              categories    => "Pubs",
99                              return_output => 1,
100                            );
101OpenGuides::Test->write_data(
102                              guide         => $guide,
103                              node          => "Poppy Hana",
104                              locales       => "Bermondsey",
105                              categories    => "Restaurants",
106                              return_output => 1,
107                            );
108$output = $guide->display_random_page( category => "Pubs",
109                                       return_output => 1 );
110$node = get_node_from_output( $output );
111print "# Random node chosen: $node\n";
112is( $node, "Red Lion", "can ask for a random pub "
113                       . "(this test may sometimes pass when it shouldn't)" );
114
115$output = $guide->display_random_page( locale => "Bermondsey",
116                                       return_output => 1 );
117$node = get_node_from_output( $output );
118print "# Random node chosen: $node\n";
119is( $node, "Poppy Hana", "can ask for a random thing in Bermondsey "
120                       . "(this test may sometimes pass when it shouldn't)" );
121
122OpenGuides::Test->write_data(
123                              guide         => $guide,
124                              node          => "Stanley Arms",
125                              locales       => "Bermondsey",
126                              categories    => "Pubs",
127                              return_output => 1,
128                            );
129$output = $guide->display_random_page( locale => "Bermondsey",
130                                       category => "Pubs",
131                                       return_output => 1
132                                      );
133$node = get_node_from_output( $output );
134print "# Random node chosen: $node\n";
135is( $node, "Stanley Arms", "can ask for a random pub in Bermondsey "
136                       . "(this test may sometimes pass when it shouldn't)" );
137
138
139$output = $guide->display_random_page( locale => "Islington",
140                                       category => "Cinemas",
141                                       return_output => 1
142                                     );
143unlike( $output, qr/Status: 302/,
144       "don't get a redirect if we ask for category/locale with no pages in" );
145
146sub refresh_db {
147    unlink "t/node.db";
148    unlink <t/indexes/*>;
149    Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
150}
151
152sub get_node_from_output {
153    my $node_param = shift;
154    $node_param =~ s/^.*\?//s;
155    $node_param =~ s/\s+$//;
156    my $formatter = $guide->wiki->formatter;
157    my $node = $formatter->node_param_to_node_name( $node_param );
158    return $node;
159}
Note: See TracBrowser for help on using the browser.