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

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

Fixed bug where search results page was slightly ambiguous when no results found,

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 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 => 18;
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    unlike( $output, qr/matches found, showing/i,
58            "doesn't output 'matches found, showing' if term not found" );
59
60    # Pop some data in and search again.
61    my $wiki = $search->{wiki}; # white boxiness
62    $wiki->write_node( "Banana", "banana" );
63    $wiki->write_node( "Monkey", "banana brains" );
64    $wiki->write_node( "Monkey Brains", "BRANES" );
65    $wiki->write_node( "Want Pie Now", "weebl" );
66    $wiki->write_node( "Punctuation", "*" );
67    $wiki->write_node( "Choice", "Eenie meenie minie mo");
68
69    # Test with two hits first - simpler.
70    %tt_vars = $search->run(
71                             return_tt_vars => 1,
72                             vars           => { search => "banana" },
73                           );
74    my @found = map { $_->{name} } @{ $tt_vars{results} || [] };
75    is( scalar @found, 2, "search finds single word twice" );
76    is_deeply( [ sort @found ], [ "Banana", "Monkey" ],
77               "...in the right places" );
78    print "# Found in $_\n" foreach @found;
79
80    # Make sure that $output matches too - we're testing the template here.
81    $output =  $search->run(
82                             return_output => 1,
83                             vars           => { search => "banana" },
84                           );
85    like( $output, qr/<a href="http:\/\/example.com\/wiki.cgi\?Banana">/,
86          "...and link is included in template output" );
87
88    # One hit in body only should show result list.
89    $output = $search->run(
90                            return_output => 1,
91                            vars          => { search => "weebl" },
92                          );
93    unlike( $output, qr/Status: 302 Moved/,
94            "no redirect if match only in body");
95
96    # One hit in title should redirect to that page.
97    $output = $search->run(
98                            return_output => 1,
99                            vars          => { search => "want pie now" },
100                          );
101    like( $output, qr/Status: 302 Moved/,
102          "prints redirect on single hit and match in title" );
103    # Old versions of CGI.pm mistakenly print location: instead of Location:
104    like( $output,
105          qr/[lL]ocation: http:\/\/example.com\/wiki.cgi\?Want_Pie_Now/,
106          "...and node name munged correctly in URL" );
107
108    # Test the AND search
109    %tt_vars = $search->run(
110                            return_tt_vars => 1,
111                            vars           => { search => "monkey banana" },
112                           );
113    @found = map { $_->{name} } @{ $tt_vars{results} || [] };
114    is_deeply( \@found, [ "Monkey" ], "AND search returns right results" );
115
116    # Test the OR search
117    %tt_vars = $search->run(
118                             return_tt_vars => 1,
119                             vars           => { search => "brains, pie" },
120                           );
121    @found = sort map { $_->{name} } @{ $tt_vars{results} || [] };
122    is_deeply( \@found, [ "Monkey", "Monkey Brains", "Want Pie Now" ],
123               "OR search returns right results" );
124    print "# Found in $_\n" foreach @found;
125
126    SKIP: {
127        skip "NOT search not done yet", 1;
128    # Test the NOT search
129    %tt_vars = $search->run(
130                             return_tt_vars => 1,
131                             vars           => { search => "banana -monkey" },
132                           );
133    @found = sort map { $_->{name} } @{ $tt_vars{results} || [] };
134    is_deeply( \@found, [ "Banana" ], "NOT search returns right results" );
135    } # end of SKIP
136
137    # Test the phrase search
138    $output = $search->run(
139                            return_output => 1,
140                            vars          => { search => '"monkey brains"' },
141                           );
142    like( $output,
143          qr/[lL]ocation: http:\/\/example.com\/wiki.cgi\?Monkey_Brains/,   
144          "phrase search returns right results and redirects to page"
145        );
146
147    #####
148    ##### Test numbering when we have more than a page of results.
149    #####
150
151    foreach my $i ( 1 .. 30 ) {
152        $wiki->write_node( "Node $i", "wombat" ) or die "Can't write Node $i";
153    }
154    $output = $search->run(
155                            return_output => 1,
156                            vars          => {
157                                               search => "wombat",
158                                               next   => 20,
159                                             },
160                          );
161    like( $output, qr/ol start="21"/,
162          "second page of results starts with right numbering" );
163}
Note: See TracBrowser for help on using the repository browser.