| 1 | use strict; |
|---|
| 2 | use CGI; |
|---|
| 3 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 4 | use OpenGuides::Config; |
|---|
| 5 | use OpenGuides::Search; |
|---|
| 6 | use Test::More; |
|---|
| 7 | |
|---|
| 8 | eval { require DBD::SQLite; }; |
|---|
| 9 | if ( $@ ) { |
|---|
| 10 | plan skip_all => "DBD::SQLite not installed"; |
|---|
| 11 | exit 0; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | eval { require Wiki::Toolkit::Search::Plucene; }; |
|---|
| 15 | if ( $@ ) { |
|---|
| 16 | plan skip_all => "Plucene not installed"; |
|---|
| 17 | exit 0; |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | # Strictly speaking we don't need to skip _all_ tests if we don't have |
|---|
| 21 | # the modules below. Revisit this when not in a hurry. |
|---|
| 22 | # We only actually need them for the tests where lat/long are converted. |
|---|
| 23 | eval { require Geography::NationalGrid; }; |
|---|
| 24 | if ( $@ ) { |
|---|
| 25 | plan skip_all => "Geography::NationalGrid not installed"; |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | eval { require Geo::Coordinates::UTM; }; |
|---|
| 29 | if ( $@ ) { |
|---|
| 30 | plan skip_all => "Geo::Coordinates::UTM not installed"; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | plan tests => 19; |
|---|
| 34 | |
|---|
| 35 | # Clear out the database from any previous runs. |
|---|
| 36 | unlink "t/node.db"; |
|---|
| 37 | unlink <t/indexes/*>; |
|---|
| 38 | |
|---|
| 39 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 40 | my $config = OpenGuides::Config->new( |
|---|
| 41 | vars => { |
|---|
| 42 | dbtype => "sqlite", |
|---|
| 43 | dbname => "t/node.db", |
|---|
| 44 | indexing_directory => "t/indexes", |
|---|
| 45 | script_name => "wiki.cgi", |
|---|
| 46 | script_url => "http://example.com/", |
|---|
| 47 | site_name => "Test Site", |
|---|
| 48 | template_path => "./templates", |
|---|
| 49 | use_plucene => 1, |
|---|
| 50 | geo_handler => 1, # British National Grid |
|---|
| 51 | } |
|---|
| 52 | ); |
|---|
| 53 | |
|---|
| 54 | # Check the British National Grid case. |
|---|
| 55 | my $q = CGI->new( "" ); |
|---|
| 56 | $q->param( -name => "os_x", -value => 500000 ); |
|---|
| 57 | $q->param( -name => "os_y", -value => 200000 ); |
|---|
| 58 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 59 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 60 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 61 | my %vars = $q->Vars(); |
|---|
| 62 | my $search = OpenGuides::Search->new( config => $config ); |
|---|
| 63 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 64 | is( $search->{distance_in_metres}, 500, |
|---|
| 65 | "os_dist picked up when OS co-ords given and using British grid" ); |
|---|
| 66 | is( $search->{x}, 500000, "...x set from os_x" ); |
|---|
| 67 | is( $search->{y}, 200000, "...y set from os_y" ); |
|---|
| 68 | |
|---|
| 69 | $q = CGI->new( "" ); |
|---|
| 70 | $q->param( -name => "osie_x", -value => 500000 ); |
|---|
| 71 | $q->param( -name => "osie_y", -value => 200000 ); |
|---|
| 72 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 73 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 74 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 75 | %vars = $q->Vars(); |
|---|
| 76 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 77 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 78 | ok( !defined $search->{distance_in_metres}, |
|---|
| 79 | "OSIE co-ords ignored when using British grid" ); |
|---|
| 80 | |
|---|
| 81 | $q = CGI->new( "" ); |
|---|
| 82 | $q->param( -name => "latitude", -value => 51 ); |
|---|
| 83 | $q->param( -name => "longitude", -value => 1 ); |
|---|
| 84 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 85 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 86 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 87 | %vars = $q->Vars(); |
|---|
| 88 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 89 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 90 | is( $search->{distance_in_metres}, 700, |
|---|
| 91 | "latlong_dist picked up when lat/long given and using British grid" ); |
|---|
| 92 | ok( defined $search->{x}, "...x set" ); |
|---|
| 93 | ok( defined $search->{y}, "...y set" ); |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | # Check the Irish National Grid case. |
|---|
| 97 | $config->geo_handler( 2 ); |
|---|
| 98 | |
|---|
| 99 | $q = CGI->new( "" ); |
|---|
| 100 | $q->param( -name => "osie_x", -value => 500000 ); |
|---|
| 101 | $q->param( -name => "osie_y", -value => 200000 ); |
|---|
| 102 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 103 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 104 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 105 | %vars = $q->Vars(); |
|---|
| 106 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 107 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 108 | is( $search->{distance_in_metres}, 600, |
|---|
| 109 | "osie_dist picked up when OS co-ords given and using Irish grid" ); |
|---|
| 110 | is( $search->{x}, 500000, "...x set from osie_x" ); |
|---|
| 111 | is( $search->{y}, 200000, "...y set from osie_y" ); |
|---|
| 112 | |
|---|
| 113 | $q = CGI->new( "" ); |
|---|
| 114 | $q->param( -name => "os_x", -value => 500000 ); |
|---|
| 115 | $q->param( -name => "os_y", -value => 200000 ); |
|---|
| 116 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 117 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 118 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 119 | %vars = $q->Vars(); |
|---|
| 120 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 121 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 122 | ok( !defined $search->{distance_in_metres}, |
|---|
| 123 | "OS co-ords ignored when using Irish grid" ); |
|---|
| 124 | |
|---|
| 125 | $q = CGI->new( "" ); |
|---|
| 126 | $q->param( -name => "latitude", -value => 55 ); |
|---|
| 127 | $q->param( -name => "longitude", -value => -5 ); |
|---|
| 128 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 129 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 130 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 131 | %vars = $q->Vars(); |
|---|
| 132 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 133 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 134 | is( $search->{distance_in_metres}, 700, |
|---|
| 135 | "latlong_dist picked up when lat/long given and using Irish grid" ); |
|---|
| 136 | ok( defined $search->{x}, "...x set" ); |
|---|
| 137 | ok( defined $search->{y}, "...y set" ); |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | # Check the UTM case. |
|---|
| 141 | $config->geo_handler( 3 ); |
|---|
| 142 | $config->ellipsoid( "Airy" ); |
|---|
| 143 | |
|---|
| 144 | $q = CGI->new( "" ); |
|---|
| 145 | $q->param( -name => "os_x", -value => 500000 ); |
|---|
| 146 | $q->param( -name => "os_y", -value => 200000 ); |
|---|
| 147 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 148 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 149 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 150 | %vars = $q->Vars(); |
|---|
| 151 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 152 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 153 | ok( !defined $search->{distance_in_metres}, |
|---|
| 154 | "OS co-ords ignored when using UTM" ); |
|---|
| 155 | |
|---|
| 156 | $q = CGI->new( "" ); |
|---|
| 157 | $q->param( -name => "osie_x", -value => 500000 ); |
|---|
| 158 | $q->param( -name => "osie_y", -value => 200000 ); |
|---|
| 159 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 160 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 161 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 162 | %vars = $q->Vars(); |
|---|
| 163 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 164 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 165 | ok( !defined $search->{distance_in_metres}, |
|---|
| 166 | "OSIE co-ords ignored when using UTM" ); |
|---|
| 167 | |
|---|
| 168 | $q = CGI->new( "" ); |
|---|
| 169 | $q->param( -name => "latitude", -value => 51 ); |
|---|
| 170 | $q->param( -name => "longitude", -value => 1 ); |
|---|
| 171 | $q->param( -name => "os_dist", -value => 500 ); |
|---|
| 172 | $q->param( -name => "osie_dist", -value => 600 ); |
|---|
| 173 | $q->param( -name => "latlong_dist", -value => 700 ); |
|---|
| 174 | %vars = $q->Vars(); |
|---|
| 175 | $search = OpenGuides::Search->new( config => $config ); |
|---|
| 176 | $search->run( vars => \%vars, return_output => 1 ); |
|---|
| 177 | is( $search->{distance_in_metres}, 700, |
|---|
| 178 | "latlong_dist picked up when lat/long given and using UTM" ); |
|---|
| 179 | ok( defined $search->{x}, "...x set" ); |
|---|
| 180 | ok( defined $search->{y}, "...y set" ); |
|---|