| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Config; |
|---|
| 4 | use OpenGuides::Search; |
|---|
| 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 | eval { require Plucene; }; |
|---|
| 14 | if ( $@ ) { |
|---|
| 15 | plan skip_all => "Plucene not installed"; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | # Strictly speaking we don't need to skip _all_ tests if we don't have |
|---|
| 19 | # the modules below. Revisit this when not in a hurry. |
|---|
| 20 | # We only actually need the former for the National Grid tests and the |
|---|
| 21 | # latter for the UTM tests. |
|---|
| 22 | eval { require Geography::NationalGrid; }; |
|---|
| 23 | if ( $@ ) { |
|---|
| 24 | plan skip_all => "Geography::NationalGrid not installed"; |
|---|
| 25 | } |
|---|
| 26 | |
|---|
| 27 | eval { require Geo::Coordinates::UTM; }; |
|---|
| 28 | if ( $@ ) { |
|---|
| 29 | plan skip_all => "Geo::Coordinates::UTM not installed"; |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | plan tests => 10; |
|---|
| 33 | |
|---|
| 34 | # Clear out the database from any previous runs. |
|---|
| 35 | unlink "t/node.db"; |
|---|
| 36 | unlink <t/indexes/*>; |
|---|
| 37 | |
|---|
| 38 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 39 | my $config = OpenGuides::Config->new( |
|---|
| 40 | vars => { |
|---|
| 41 | dbtype => "sqlite", |
|---|
| 42 | dbname => "t/node.db", |
|---|
| 43 | indexing_directory => "t/indexes", |
|---|
| 44 | script_name => "wiki.cgi", |
|---|
| 45 | script_url => "http://example.com/", |
|---|
| 46 | site_name => "Test Site", |
|---|
| 47 | template_path => "./templates", |
|---|
| 48 | geo_handler => 1, |
|---|
| 49 | } |
|---|
| 50 | ); |
|---|
| 51 | |
|---|
| 52 | # Plucene is the recommended searcher now. |
|---|
| 53 | eval { require Wiki::Toolkit::Search::Plucene; }; |
|---|
| 54 | if ( $@ ) { $config->use_plucene( 0 ) }; |
|---|
| 55 | |
|---|
| 56 | my $search = OpenGuides::Search->new( config => $config ); |
|---|
| 57 | |
|---|
| 58 | # Write some data. |
|---|
| 59 | my $wiki = $search->{wiki}; |
|---|
| 60 | $wiki->write_node( "Wandsworth Common", "A common.", undef, |
|---|
| 61 | { category => "Parks" } ) |
|---|
| 62 | or die "Can't write node"; |
|---|
| 63 | $wiki->write_node( "Hammersmith", "A page about Hammersmith." ) |
|---|
| 64 | or die "Can't write node"; |
|---|
| 65 | |
|---|
| 66 | # Check that the search forgets input search term between invocations. |
|---|
| 67 | $search->run( |
|---|
| 68 | return_output => 1, |
|---|
| 69 | vars => { search => "parks" }, |
|---|
| 70 | ); |
|---|
| 71 | ok( $search->{search_string}, "search_string set" ); |
|---|
| 72 | $search->run( |
|---|
| 73 | return_output => 1, |
|---|
| 74 | ); |
|---|
| 75 | ok( !$search->{search_string}, "...and forgotten" ); |
|---|
| 76 | |
|---|
| 77 | # Sanity check. |
|---|
| 78 | my (@results, %tt_vars); |
|---|
| 79 | %tt_vars = $search->run( |
|---|
| 80 | return_tt_vars => 1, |
|---|
| 81 | vars => { search => "parks" }, |
|---|
| 82 | ); |
|---|
| 83 | @results = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 84 | is_deeply( \@results, [ "Wandsworth Common" ], |
|---|
| 85 | "first search returns expected results" ); |
|---|
| 86 | %tt_vars = $search->run( |
|---|
| 87 | return_tt_vars => 1, |
|---|
| 88 | vars => { search => "hammersmith" }, |
|---|
| 89 | ); |
|---|
| 90 | @results = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 91 | is_deeply( \@results, [ "Hammersmith" ], |
|---|
| 92 | "so does second" ); |
|---|
| 93 | |
|---|
| 94 | # Check that the search forgets input geodata between invocations. |
|---|
| 95 | # First with British National Grid. |
|---|
| 96 | $search->run( |
|---|
| 97 | return_output => 1, |
|---|
| 98 | vars => { os_x => 500000, os_y => 100000, os_dist => 1000 }, |
|---|
| 99 | ); |
|---|
| 100 | ok( $search->{x}, "x-coord set" ); |
|---|
| 101 | $search->run( |
|---|
| 102 | return_output => 1, |
|---|
| 103 | vars => { search => "foo" }, |
|---|
| 104 | ); |
|---|
| 105 | ok( !$search->{x}, "...and forgotten" ); |
|---|
| 106 | |
|---|
| 107 | # Now with Irish National Grid. |
|---|
| 108 | $config->geo_handler( 2 ); |
|---|
| 109 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 110 | $search->run( |
|---|
| 111 | return_output => 1, |
|---|
| 112 | vars => { osie_x => 100000, osie_y => 200000, osie_dist => 100 }, |
|---|
| 113 | ); |
|---|
| 114 | ok( $search->{x}, "x-coord set" ); |
|---|
| 115 | $search->run( |
|---|
| 116 | return_output => 1, |
|---|
| 117 | vars => { search => "foo" }, |
|---|
| 118 | ); |
|---|
| 119 | ok( !$search->{x}, "...and forgotten" ); |
|---|
| 120 | |
|---|
| 121 | # Now with UTM. |
|---|
| 122 | $config->geo_handler( 3 ); |
|---|
| 123 | $config->ellipsoid( "Airy" ); |
|---|
| 124 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 125 | $search->run( |
|---|
| 126 | return_output => 1, |
|---|
| 127 | vars => { latitude => 10, longitude => 0, latlong_dist => 1000 }, |
|---|
| 128 | ); |
|---|
| 129 | ok( $search->{x}, "x-coord set" ); |
|---|
| 130 | $search->run( |
|---|
| 131 | return_output => 1, |
|---|
| 132 | vars => { search => "foo" }, |
|---|
| 133 | ); |
|---|
| 134 | ok( !$search->{x}, "...and forgotten" ); |
|---|
| 135 | |
|---|