| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Test; |
|---|
| 4 | use OpenGuides; |
|---|
| 5 | use Test::More; |
|---|
| 6 | |
|---|
| 7 | eval { require DBD::SQLite; }; |
|---|
| 8 | if ( $@ ) { |
|---|
| 9 | my ($error) = $@ =~ /^(.*?)\n/; |
|---|
| 10 | plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | plan tests => 8; |
|---|
| 14 | |
|---|
| 15 | my ( $config, $guide, $wiki ); |
|---|
| 16 | |
|---|
| 17 | # Clear out database from previous runs, set up a guide. |
|---|
| 18 | refresh_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. |
|---|
| 26 | my %nodes = map { $_ => "A pub." } ( "Red Lion", "Farmers Arms", "Angel" ); |
|---|
| 27 | foreach 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. |
|---|
| 36 | my $output = $guide->display_random_page( return_output => 1 ); |
|---|
| 37 | |
|---|
| 38 | # Old versions of CGI.pm mistakenly print location: instead of Location: |
|---|
| 39 | like( $output, qr/[lL]ocation: http:\/\/www.example.com\/wiki.cgi/, |
|---|
| 40 | "->display_random_page makes a redirect" ); |
|---|
| 41 | |
|---|
| 42 | my $node = get_node_from_output( $output ); |
|---|
| 43 | print "# Random node chosen: $node\n"; |
|---|
| 44 | ok( $nodes{$node}, "...to an existing node" ); |
|---|
| 45 | |
|---|
| 46 | # Clear the database and write some data including categories and locales. |
|---|
| 47 | refresh_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. |
|---|
| 55 | OpenGuides::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 ); |
|---|
| 72 | print "# Random node chosen: $node\n"; |
|---|
| 73 | isnt( $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 ); |
|---|
| 85 | print "# Random node chosen: $node\n"; |
|---|
| 86 | isnt( $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 |
|---|
| 90 | refresh_db(); |
|---|
| 91 | $config = OpenGuides::Test->make_basic_config; |
|---|
| 92 | $guide = OpenGuides->new( config => $config ); |
|---|
| 93 | |
|---|
| 94 | OpenGuides::Test->write_data( |
|---|
| 95 | guide => $guide, |
|---|
| 96 | node => "Red Lion", |
|---|
| 97 | locales => "Hammersmith", |
|---|
| 98 | categories => "Pubs", |
|---|
| 99 | return_output => 1, |
|---|
| 100 | ); |
|---|
| 101 | OpenGuides::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 ); |
|---|
| 111 | print "# Random node chosen: $node\n"; |
|---|
| 112 | is( $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 ); |
|---|
| 118 | print "# Random node chosen: $node\n"; |
|---|
| 119 | is( $node, "Poppy Hana", "can ask for a random thing in Bermondsey " |
|---|
| 120 | . "(this test may sometimes pass when it shouldn't)" ); |
|---|
| 121 | |
|---|
| 122 | OpenGuides::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 ); |
|---|
| 134 | print "# Random node chosen: $node\n"; |
|---|
| 135 | is( $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 | ); |
|---|
| 143 | unlike( $output, qr/Status: 302/, |
|---|
| 144 | "don't get a redirect if we ask for category/locale with no pages in" ); |
|---|
| 145 | |
|---|
| 146 | sub refresh_db { |
|---|
| 147 | unlink "t/node.db"; |
|---|
| 148 | unlink <t/indexes/*>; |
|---|
| 149 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | sub 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 | } |
|---|