# search_bugs.t - unit tests for lib/TWiki/Search.pm
# ----------------------------------------------------------------------
# Why this file in addition to UnitTestContrib/test/unit/Fn_SEARCH.pm?
# Well, that file tests expansions of TWiki's %SEARCH{}% function as a
# whole, but not the individual subs within the rather long module
# that is Search.pm.  Recently we have received bug reports which can
# easily be traced back to functions which don't do what they should.
# Such misbehavings can easily be tracked with "traditional" Perl unit
# tests, but it is rather cumbersome to set up the test environment to
# verify the same stuff using a %SEARCH{}% invocation.
# ----------------------------------------------------------------------
# Copyright 2017 Harald.Joerg@arcor.de
# License: GPL V3
# ----------------------------------------------------------------------

use strict;
use warnings;

use Test::More;

use TWiki::Search;

# Create the object
my $searcher = TWiki::Search->new(TWiki->new);

# Check whether we've got our objects.  This might help with debugging
# if something breaks in one of the BEGIN blocks we have in our
# codebase.
isa_ok($searcher,'TWiki::Search','The "TWiki::Search" object');
isa_ok($searcher->{session}, 'TWiki','The "session" attribute');

# Coding style: The test cases are wrapped in blocks to make sure that
# each block sees his own "my" variables only

# Item7822
# When more than one web is searched, then the processing of the
# search tokens for the first web would permanently destroy the
# negation of the token because the evaluation is in-place.
# The test makes sure that the token remains unchanged.
{
    my $token  = '!foo';
    my @tokens = ($token);
    $searcher->_searchTopics('WhateverWeb','text','keyword',{},\@tokens,());
    is ($tokens[0],$token,"Item7282: _searchTopics doesn't garble search tokens");
}

# Item7823
# Negated phrases like '-"Squidaped Oyt" weren't correctly processed
# because the left quote was only removed at the beginning of the
# string, without taking the possibility of a leading '-' into
# account.
{
    my $searchString = '-"Squidaped Oyt"'; # look for topics without that phrase
    my @tokens = $searcher->_tokensFromSearchString($searchString,'keyword');
    cmp_ok(scalar @tokens, '==', 1,
           'quoted phrase is rendered as one token');
    is($tokens[0],'!Squidaped Oyt',
       'Item 7283: Quotes correctly removed from a negated phrase');
}

done_testing;
