| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Search; |
|---|
| 4 | use OpenGuides::Test; |
|---|
| 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 => 18; |
|---|
| 14 | |
|---|
| 15 | # Clear out the database from any previous runs. |
|---|
| 16 | unlink "t/node.db"; |
|---|
| 17 | unlink <t/indexes/*>; |
|---|
| 18 | |
|---|
| 19 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 20 | my $config = OpenGuides::Test->make_basic_config; |
|---|
| 21 | $config->script_name( "wiki.cgi" ); |
|---|
| 22 | $config->script_url( "http://example.com/" ); |
|---|
| 23 | |
|---|
| 24 | # Plucene is the recommended searcher now. |
|---|
| 25 | eval { require Wiki::Toolkit::Search::Plucene; }; |
|---|
| 26 | if ( $@ ) { $config->use_plucene( 0 ) }; |
|---|
| 27 | |
|---|
| 28 | my $search = OpenGuides::Search->new( config => $config ); |
|---|
| 29 | isa_ok( $search, "OpenGuides::Search" ); |
|---|
| 30 | |
|---|
| 31 | my $output = $search->run( return_output => 1 ); |
|---|
| 32 | unlike( $output, qr/no items matched/i, |
|---|
| 33 | "doesn't output 'no items matched' if no terms supplied" ); |
|---|
| 34 | unlike( $output, qr/action=edit/, |
|---|
| 35 | "doesn't offer edit link" ); |
|---|
| 36 | |
|---|
| 37 | my %tt_vars = $search->run( |
|---|
| 38 | return_tt_vars => 1, |
|---|
| 39 | vars => { search => "banana" }, |
|---|
| 40 | ); |
|---|
| 41 | is( $tt_vars{first_num}, 0, "first_num set to 0 when no hits" ); |
|---|
| 42 | is( scalar @{ $tt_vars{results} }, 0, "...and results array empty" ); |
|---|
| 43 | |
|---|
| 44 | $output = $search->run( |
|---|
| 45 | return_output => 1, |
|---|
| 46 | vars => { search => "banana" } |
|---|
| 47 | ); |
|---|
| 48 | like( $output, qr/no items matched/i, |
|---|
| 49 | "outputs 'no items matched' if term not found" ); |
|---|
| 50 | unlike( $output, qr/matches found, showing/i, |
|---|
| 51 | "doesn't output 'matches found, showing' if term not found" ); |
|---|
| 52 | |
|---|
| 53 | # Pop some data in and search again. |
|---|
| 54 | my $wiki = $search->{wiki}; # white boxiness |
|---|
| 55 | $wiki->write_node( "Banana", "banana" ); |
|---|
| 56 | $wiki->write_node( "Monkey", "banana brains" ); |
|---|
| 57 | $wiki->write_node( "Monkey Brains", "BRANES" ); |
|---|
| 58 | $wiki->write_node( "Want Pie Now", "weebl" ); |
|---|
| 59 | $wiki->write_node( "Punctuation", "*" ); |
|---|
| 60 | $wiki->write_node( "Choice", "Eenie meenie minie mo"); |
|---|
| 61 | |
|---|
| 62 | # Test with two hits first - simpler. |
|---|
| 63 | %tt_vars = $search->run( |
|---|
| 64 | return_tt_vars => 1, |
|---|
| 65 | vars => { search => "banana" }, |
|---|
| 66 | ); |
|---|
| 67 | my @found = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 68 | is( scalar @found, 2, "search finds single word twice" ); |
|---|
| 69 | is_deeply( [ sort @found ], [ "Banana", "Monkey" ], |
|---|
| 70 | "...in the right places" ); |
|---|
| 71 | print "# Found in $_\n" foreach @found; |
|---|
| 72 | |
|---|
| 73 | # Make sure that $output matches too - we're testing the template here. |
|---|
| 74 | $output = $search->run( |
|---|
| 75 | return_output => 1, |
|---|
| 76 | vars => { search => "banana" }, |
|---|
| 77 | ); |
|---|
| 78 | like( $output, qr/<a href="http:\/\/example.com\/wiki.cgi\?Banana">/, |
|---|
| 79 | "...and link is included in template output" ); |
|---|
| 80 | |
|---|
| 81 | # One hit in body only should show result list. |
|---|
| 82 | $output = $search->run( |
|---|
| 83 | return_output => 1, |
|---|
| 84 | vars => { search => "weebl" }, |
|---|
| 85 | ); |
|---|
| 86 | unlike( $output, qr/Status: 302/, |
|---|
| 87 | "no redirect if match only in body"); |
|---|
| 88 | |
|---|
| 89 | # One hit in title should redirect to that page. |
|---|
| 90 | $output = $search->run( |
|---|
| 91 | return_output => 1, |
|---|
| 92 | vars => { search => "want pie now" }, |
|---|
| 93 | ); |
|---|
| 94 | like( $output, qr/Status: 302/, |
|---|
| 95 | "prints redirect on single hit and match in title" ); |
|---|
| 96 | # Old versions of CGI.pm mistakenly print location: instead of Location: |
|---|
| 97 | like( $output, |
|---|
| 98 | qr/[lL]ocation: http:\/\/example.com\/wiki.cgi\?Want_Pie_Now/, |
|---|
| 99 | "...and node name munged correctly in URL" ); |
|---|
| 100 | |
|---|
| 101 | # Test the AND search |
|---|
| 102 | %tt_vars = $search->run( |
|---|
| 103 | return_tt_vars => 1, |
|---|
| 104 | vars => { search => "monkey banana" }, |
|---|
| 105 | ); |
|---|
| 106 | @found = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 107 | is_deeply( \@found, [ "Monkey" ], "AND search returns right results" ); |
|---|
| 108 | |
|---|
| 109 | # Test the OR search |
|---|
| 110 | %tt_vars = $search->run( |
|---|
| 111 | return_tt_vars => 1, |
|---|
| 112 | vars => { search => "brains, pie" }, |
|---|
| 113 | ); |
|---|
| 114 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 115 | is_deeply( \@found, [ "Monkey", "Monkey Brains", "Want Pie Now" ], |
|---|
| 116 | "OR search returns right results" ); |
|---|
| 117 | print "# Found in $_\n" foreach @found; |
|---|
| 118 | |
|---|
| 119 | SKIP: { |
|---|
| 120 | skip "NOT search not done yet", 1; |
|---|
| 121 | # Test the NOT search |
|---|
| 122 | %tt_vars = $search->run( |
|---|
| 123 | return_tt_vars => 1, |
|---|
| 124 | vars => { search => "banana -monkey" }, |
|---|
| 125 | ); |
|---|
| 126 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 127 | is_deeply( \@found, [ "Banana" ], "NOT search returns right results" ); |
|---|
| 128 | } # end of SKIP |
|---|
| 129 | |
|---|
| 130 | # Test the phrase search |
|---|
| 131 | $output = $search->run( |
|---|
| 132 | return_output => 1, |
|---|
| 133 | vars => { search => '"monkey brains"' }, |
|---|
| 134 | ); |
|---|
| 135 | like( $output, |
|---|
| 136 | qr/[lL]ocation: http:\/\/example.com\/wiki.cgi\?Monkey_Brains/, |
|---|
| 137 | "phrase search returns right results and redirects to page" |
|---|
| 138 | ); |
|---|
| 139 | |
|---|
| 140 | ##### |
|---|
| 141 | ##### Test numbering when we have more than a page of results. |
|---|
| 142 | ##### |
|---|
| 143 | |
|---|
| 144 | foreach my $i ( 1 .. 30 ) { |
|---|
| 145 | $wiki->write_node( "Node $i", "wombat" ) or die "Can't write Node $i"; |
|---|
| 146 | } |
|---|
| 147 | $output = $search->run( |
|---|
| 148 | return_output => 1, |
|---|
| 149 | vars => { |
|---|
| 150 | search => "wombat", |
|---|
| 151 | next => 20, |
|---|
| 152 | }, |
|---|
| 153 | ); |
|---|
| 154 | like( $output, qr/ol start="21"/, |
|---|
| 155 | "second page of results starts with right numbering" ); |
|---|