Bug 11137: regression tests for QP search field alias bug
[koha.git] / t / QueryParser.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Deep;
8 use Module::Load::Conditional qw(can_load);
9
10 BEGIN {
11     use_ok( 'Koha::QueryParser::Driver::PQF' );
12 }
13
14 my $QParser = Koha::QueryParser::Driver::PQF->new();
15
16 ok(defined $QParser, 'Successfully created empty QP object');
17 ok($QParser->load_config('./etc/searchengine/queryparser.yaml'), 'Loaded QP config');
18
19 is($QParser->search_class_count, 4, 'Initialized 4 search classes');
20 is (scalar(@{$QParser->search_fields()->{'keyword'}}), 107, "Correct number of search fields for 'keyword' class");
21
22 is($QParser->target_syntax('biblioserver', 'smith'), '@or @or @attr 1=1016 @attr 4=6 "smith" @attr 9=20 @attr 2=102 @attr 4=6 "smith" @attr 9=34 @attr 2=102 @attr 4=6 "smith"', 'super simple keyword query');
23 is($QParser->target_syntax('biblioserver', 'au:smith'), '@attr 1=1003 @attr 4=6 "smith"', 'simple author query');
24 is($QParser->target_syntax('biblioserver', 'keyword|publisher:smith'), '@attr 1=1018 @attr 4=6 "smith"', 'fielded publisher query');
25 is($QParser->target_syntax('biblioserver', 'ti:"little engine that could"'), '@attr 1=4 @attr 4=1 "little engine that could"', 'phrase query');
26 is($QParser->target_syntax('biblioserver', 'keyword|titlekw:smith'), '@attr 1=4 @attr 9=20 @attr 2=102 @attr 4=6 "smith"', 'relevance-bumped query');
27 is($QParser->target_syntax('biblioserver', 'au:smith && johnson'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=1003 @attr 4=6 "johnson"', 'query with boolean &&');
28 is($QParser->target_syntax('biblioserver', 'au:smith && ti:johnson'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 1=4 @attr 4=6 "johnson"', 'query with boolean &&');
29 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(-2008)'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 4=4 @attr 1=pubdate @attr 2=2 "2008"', 'keyword search with pubdate limited to -2008');
30 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008-)'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 4=4 @attr 1=pubdate @attr 2=4 "2008"', 'keyword search with pubdate limited to 2008-');
31 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(2008)'), '@and @attr 1=1003 @attr 4=6 "smith" @attr 4=4 @attr 1=pubdate "2008"', 'keyword search with pubdate limited to 2008');
32 is($QParser->target_syntax('biblioserver', 'au:smith pubdate(1980,2008)'), '@and @attr 1=1003 @attr 4=6 "smith" @or @attr 4=4 @attr 1=pubdate "1980" @attr 4=4 @attr 1=pubdate "2008"', 'keyword search with pubdate limited to 1980, 2008');
33 is($QParser->target_syntax('biblioserver', 'au:smith #acqdate_dsc'), '@or @attr 1=32 @attr 7=1 0 @attr 1=1003 @attr 4=6 "smith"', 'keyword search sorted by acqdate descending');
34 is($QParser->bib1_mapping_by_attr('field', 'biblioserver', {'1' => '1004'})->{'field'}, 'personal', 'retrieve field by attr');
35 is($QParser->bib1_mapping_by_attr_string('field', 'biblioserver', '@attr 1=1004')->{'field'}, 'personal', 'retrieve field by attrstring');
36
37 is ($QParser->clear_all_mappings, $QParser, 'clear all mappings returns self');
38 is ($QParser->clear_all_configuration, $QParser, 'clear all configuration returns self');
39 is (scalar(keys(%{$QParser->search_fields})), 0, "All mapping erased");
40
41 $QParser->add_bib1_field_map('author' => 'personal' => 'biblioserver' => { '1' => '1004' } );
42 $QParser->add_bib1_modifier_map('relevance' => 'biblioserver' => { '2' => '102' } );
43 my $desired_config = {
44   'field_mappings' => {
45     'author' => {
46       'personal' => {
47         'aliases' => [
48           'personal'
49         ],
50         'bib1_mapping' => {
51           'biblioserver' => {
52             '1' => '1004'
53           }
54         },
55         'enabled' => '1',
56         'index' => 'personal',
57         'label' => 'Personal'
58       }
59     }
60   },
61   'filter_mappings' => {},
62   'modifier_mappings' => {
63     'relevance' => {
64       'bib1_mapping' => {
65         'biblioserver' => {
66           '2' => '102'
67         }
68       },
69       'enabled' => 1,
70       'label' => 'Relevance'
71     }
72   },
73   'relevance_bumps' => {}
74 };
75
76 SKIP: {
77     my $got_config;
78     skip 'YAML is unavailable', 2 unless can_load('modules' => { 'YAML::Any' => undef });
79     $got_config = YAML::Any::Load($QParser->serialize_mappings());
80     ok(ref $got_config, 'serialized YAML valid');
81     is_deeply($got_config, $desired_config, 'Mappings serialized correctly to YAML');
82
83     skip 'JSON is unavailable', 2 unless can_load('modules' => { 'JSON' => undef });
84     undef $got_config;
85     eval {
86         $got_config = JSON::from_json($QParser->serialize_mappings('json'));
87     };
88     is($@, '', 'serialized JSON valid');
89     is_deeply($got_config, $desired_config, 'Mappings serialized correctly to JSON');
90 }
91
92 $QParser->clear_all_mappings;
93 is($QParser->TEST_SETUP, $QParser, 'TEST_SETUP returns self');
94 is($QParser->search_class_count, 4, 'Initialized 4 search classes in test setup');
95
96 done_testing();