source: trunk/t/31_search.t @ 716

Last change on this file since 716 was 716, checked in by Earle Martin, 16 years ago

Renaming OpenGuides::SuperSearch? to OpenGuides::Search and supersearch.cgi to search.cgi - bug #17.

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