| 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 | |
|---|
| 9 | if ( $@ ) { |
|---|
| 10 | my ($error) = $@ =~ /^(.*?)\n/; |
|---|
| 11 | plan skip_all => "DBD::SQLite could not be used - no database to test with. ($error)"; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | plan tests => 10; |
|---|
| 15 | |
|---|
| 16 | # Clear out the database from any previous runs. |
|---|
| 17 | unlink "t/node.db"; |
|---|
| 18 | unlink <t/indexes/*>; |
|---|
| 19 | |
|---|
| 20 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 21 | my $config = OpenGuides::Config->new( |
|---|
| 22 | vars => { |
|---|
| 23 | dbtype => "sqlite", |
|---|
| 24 | dbname => "t/node.db", |
|---|
| 25 | indexing_directory => "t/indexes", |
|---|
| 26 | script_name => "wiki.cgi", |
|---|
| 27 | script_url => "http://example.com/", |
|---|
| 28 | site_name => "Test Site", |
|---|
| 29 | template_path => "./templates", |
|---|
| 30 | } |
|---|
| 31 | ); |
|---|
| 32 | |
|---|
| 33 | # Plucene is the recommended searcher now. |
|---|
| 34 | eval { require Wiki::Toolkit::Search::Plucene; }; |
|---|
| 35 | if ( $@ ) { $config->use_plucene( 0 ) }; |
|---|
| 36 | |
|---|
| 37 | my $search = OpenGuides::Search->new( config => $config ); |
|---|
| 38 | |
|---|
| 39 | # Add some data. We write it twice to avoid hitting the redirect. |
|---|
| 40 | my $wiki = $search->{wiki}; # white boxiness |
|---|
| 41 | $wiki->write_node( "Calthorpe Arms", "Serves beer.", undef, |
|---|
| 42 | { category => "Pubs", locale => "Holborn" } ); |
|---|
| 43 | $wiki->write_node( "Penderel's Oak", "Serves beer.", undef, |
|---|
| 44 | { category => "Pubs", locale => "Holborn" } ); |
|---|
| 45 | $wiki->write_node( "British Museum", "Huge museum, lots of artifacts.", undef, |
|---|
| 46 | { category => ["Museums", "Major Attractions"] |
|---|
| 47 | , locale => ["Holborn", "Bloomsbury"] } ); |
|---|
| 48 | |
|---|
| 49 | # Check that a search on its category works. |
|---|
| 50 | my %tt_vars = $search->run( |
|---|
| 51 | return_tt_vars => 1, |
|---|
| 52 | vars => { search => "Pubs" }, |
|---|
| 53 | ); |
|---|
| 54 | my @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 55 | is_deeply( \@found, [ "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 56 | "simple search looks in category" ); |
|---|
| 57 | |
|---|
| 58 | %tt_vars = $search->run( |
|---|
| 59 | return_tt_vars => 1, |
|---|
| 60 | vars => { search => "pubs" }, |
|---|
| 61 | ); |
|---|
| 62 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 63 | is_deeply( \@found, [ "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 64 | "...and is case-insensitive" ); |
|---|
| 65 | |
|---|
| 66 | # Check that a search on its locale works. |
|---|
| 67 | %tt_vars = $search->run( |
|---|
| 68 | return_tt_vars => 1, |
|---|
| 69 | vars => { search => "Holborn" }, |
|---|
| 70 | ); |
|---|
| 71 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 72 | is_deeply( \@found, [ "British Museum", "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 73 | "simple search looks in locale" ); |
|---|
| 74 | |
|---|
| 75 | %tt_vars = $search->run( |
|---|
| 76 | return_tt_vars => 1, |
|---|
| 77 | vars => { search => "holborn" }, |
|---|
| 78 | ); |
|---|
| 79 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 80 | is_deeply( \@found, [ "British Museum", "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 81 | "...and is case-insensitive" ); |
|---|
| 82 | |
|---|
| 83 | # Test AND search in various combinations. |
|---|
| 84 | %tt_vars = $search->run( |
|---|
| 85 | return_tt_vars => 1, |
|---|
| 86 | vars => { search => "Holborn Pubs" }, |
|---|
| 87 | ); |
|---|
| 88 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 89 | is_deeply( \@found, [ "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 90 | "AND search works between category and locale" ); |
|---|
| 91 | |
|---|
| 92 | %tt_vars = $search->run( |
|---|
| 93 | return_tt_vars => 1, |
|---|
| 94 | vars => { search => "Holborn Penderel" }, |
|---|
| 95 | ); |
|---|
| 96 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 97 | is_deeply( \@found, [ "Penderel's Oak" ], |
|---|
| 98 | "AND search works between title and locale" ); |
|---|
| 99 | |
|---|
| 100 | %tt_vars = $search->run( |
|---|
| 101 | return_tt_vars => 1, |
|---|
| 102 | vars => { search => "Pubs Penderel" }, |
|---|
| 103 | ); |
|---|
| 104 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 105 | is_deeply( \@found, [ "Penderel's Oak" ], |
|---|
| 106 | "AND search works between title and category" ); |
|---|
| 107 | |
|---|
| 108 | %tt_vars = $search->run( |
|---|
| 109 | return_tt_vars => 1, |
|---|
| 110 | vars => { search => "Holborn beer" }, |
|---|
| 111 | ); |
|---|
| 112 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 113 | is_deeply( \@found, [ "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 114 | "...and between body and locale" ); |
|---|
| 115 | |
|---|
| 116 | %tt_vars = $search->run( |
|---|
| 117 | return_tt_vars => 1, |
|---|
| 118 | vars => { search => "Pubs beer" }, |
|---|
| 119 | ); |
|---|
| 120 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 121 | is_deeply( \@found, [ "Calthorpe Arms", "Penderel's Oak" ], |
|---|
| 122 | "...and between body and category" ); |
|---|
| 123 | |
|---|
| 124 | %tt_vars = $search->run( |
|---|
| 125 | return_tt_vars => 1, |
|---|
| 126 | vars => { search => '"major attractions"' }, |
|---|
| 127 | ); |
|---|
| 128 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 129 | is_deeply( \@found, [ "British Museum", ], |
|---|
| 130 | "Multi word category name" ); |
|---|