source: trunk/t/31_supersearch.t @ 469

Last change on this file since 469 was 469, checked in by kake, 17 years ago

Added option of using Plucene for searching.

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