From 1bca1d22bbc1b903324a6c750e0045fa5f6e9cb8 Mon Sep 17 00:00:00 2001 From: "Zak B. Elep" Date: Fri, 30 Jun 2017 16:52:46 +0800 Subject: [PATCH 1/3] Trim leading and trailing whitespace from search query Fixes #1621. --- lib/MetaCPAN/Web/Controller/Search.pm | 4 ++++ t/controller/search/precision.t | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/MetaCPAN/Web/Controller/Search.pm b/lib/MetaCPAN/Web/Controller/Search.pm index ddbbf96952..64fc5b0f5a 100644 --- a/lib/MetaCPAN/Web/Controller/Search.pm +++ b/lib/MetaCPAN/Web/Controller/Search.pm @@ -30,6 +30,10 @@ sub index : Path : Args(0) { $query =~ s{\.pm\b}{}; } + # trim leading/traling whitespace + $query =~ s/^\s+//; + $query =~ s/\s+$//; + my $model = $c->model('API::Module'); my $from = ( $req->page - 1 ) * $page_size; if ( diff --git a/t/controller/search/precision.t b/t/controller/search/precision.t index 77dffc81a4..62d04563a2 100644 --- a/t/controller/search/precision.t +++ b/t/controller/search/precision.t @@ -18,6 +18,14 @@ my %tests = ( 'dbix class resultset' => 'DBIx::Class::ResultSet', ); +my %authors = ( + ' LLAP ' => 'LLAP', + 'PERLER ' => 'PERLER', + ' NEILB' => 'NEILB', + 'RWSTAUNER' => 'RWSTAUNER', + ' OALDERS ' => 'OALDERS', +); + test_psgi app, sub { my $cb = shift; for my $k ( sort keys %tests ) { @@ -29,6 +37,17 @@ test_psgi app, sub { '//div[@class="module-result"][1]/big[1]//a[1]'); is( $module, $v, "$v is first result" ); } + + for my $k ( sort keys %authors ) { + ok( my $res = $cb->( GET "/search?q=$k" ), qq{search for "$k"} ); + my $v = $authors{$k}; + my $tx = tx($res); + my $author + = $tx->find_value( + '//div[@class="author-results"]/ul[@class="authors clearfix"]/li[1]/a[1]' + ); + like( $author, qr/\b$v\b/, "$v is first result" ); + } }; done_testing; From acf436cc6304224265651a84c37acda5e342dabf Mon Sep 17 00:00:00 2001 From: "Zak B. Elep" Date: Fri, 30 Jun 2017 17:21:17 +0800 Subject: [PATCH 2/3] Fix comment, wrong spelling --- lib/MetaCPAN/Web/Controller/Search.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/MetaCPAN/Web/Controller/Search.pm b/lib/MetaCPAN/Web/Controller/Search.pm index 64fc5b0f5a..890274fe98 100644 --- a/lib/MetaCPAN/Web/Controller/Search.pm +++ b/lib/MetaCPAN/Web/Controller/Search.pm @@ -30,7 +30,7 @@ sub index : Path : Args(0) { $query =~ s{\.pm\b}{}; } - # trim leading/traling whitespace + # remove leading and/or trailing whitespace $query =~ s/^\s+//; $query =~ s/\s+$//; From c5a3e5aec7752438ad29e84670cd6c7cc9ab79f9 Mon Sep 17 00:00:00 2001 From: "Zak B. Elep" Date: Fri, 30 Jun 2017 17:38:32 +0800 Subject: [PATCH 3/3] Remove comment On second thought, its redundant. --- lib/MetaCPAN/Web/Controller/Search.pm | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/MetaCPAN/Web/Controller/Search.pm b/lib/MetaCPAN/Web/Controller/Search.pm index 890274fe98..a69560f0f3 100644 --- a/lib/MetaCPAN/Web/Controller/Search.pm +++ b/lib/MetaCPAN/Web/Controller/Search.pm @@ -30,7 +30,6 @@ sub index : Path : Args(0) { $query =~ s{\.pm\b}{}; } - # remove leading and/or trailing whitespace $query =~ s/^\s+//; $query =~ s/\s+$//;