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