Bug 32594: (QA follow-up) Fix POD
[koha-ffzg.git] / t / Search.t
index 286d36a..9eaf5ba 100755 (executable)
 
 use Modern::Perl;
 
-use Test::More tests => 3;
+use C4::Biblio;
+use Test::More;
+use Test::MockModule;
+use Test::Warn;
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use Module::Load::Conditional qw/check_install/;
 
 BEGIN {
-    use_ok('C4::Search');
-    can_ok('C4::Search',
-        qw/_build_initial_query/);
+    if ( check_install( module => 'Test::DBIx::Class' ) ) {
+        plan tests => 4;
+    } else {
+        plan skip_all => "Need Test::DBIx::Class"
+    }
 }
 
+# Mock the DB connexion and C4::Context
+use Test::DBIx::Class;
+
+use_ok('C4::Search');
+can_ok('C4::Search',
+    qw/_build_initial_query/);
+
 subtest "_build_initial_query tests" => sub {
 
     plan tests => 20;
@@ -48,7 +64,7 @@ subtest "_build_initial_query tests" => sub {
         C4::Search::_build_initial_query($params);
     is( $query, "query operator parsed_operand",
         "\$query built correctly");
-    is( $query_cgi, "query_cgi&op=%20operator%20&idx=index&q=original_operand",
+    is( $query_cgi, "query_cgi&op=operator&idx=index&q=original_operand",
         "\$query_cgi built correctly");
     is( $query_desc, "query_desc operator index_plus original_operand",
         "\$query_desc build correctly");
@@ -71,11 +87,11 @@ subtest "_build_initial_query tests" => sub {
 
     ($query,$query_cgi,$query_desc,$previous_operand) =
         C4::Search::_build_initial_query($params);
-    is( $query, "query and parsed_operand",
+    is( $query, "query AND parsed_operand",
         "\$query built correctly (no operator)");
-    is( $query_cgi, "query_cgi&op=%20and%20&idx=index&q=original_operand",
+    is( $query_cgi, "query_cgi&op=AND&idx=index&q=original_operand",
         "\$query_cgi built correctly (no operator)");
-    is( $query_desc, "query_desc and index_plus original_operand",
+    is( $query_desc, "query_desc AND index_plus original_operand",
         "\$query_desc build correctly (no operator)");
     is( $previous_operand, "previous_operand",
         "\$query build correctly (no operator)");
@@ -96,11 +112,11 @@ subtest "_build_initial_query tests" => sub {
 
     ($query,$query_cgi,$query_desc,$previous_operand) =
         C4::Search::_build_initial_query($params);
-    is( $query, "queryparsed_operand",
+    is( $query, "query  parsed_operand",
         "\$query built correctly (no previous operand)");
     is( $query_cgi, "query_cgi&idx=index&q=original_operand",
         "\$query_cgi built correctly (no previous operand)");
-    is( $query_desc, "query_descindex_plus original_operand",
+    is( $query_desc, "query_desc  index_plus original_operand",
         "\$query_desc build correctly (no previous operand)");
     is( $previous_operand, 1,
         "\$query build correctly (no previous operand)");
@@ -123,7 +139,7 @@ subtest "_build_initial_query tests" => sub {
         C4::Search::_build_initial_query($params);
     is( $query, "query operator parsed_operand",
         "\$query built correctly (no index passed)");
-    is( $query_cgi, "query_cgi&op=%20operator%20&q=original_operand",
+    is( $query_cgi, "query_cgi&op=operator&q=original_operand",
         "\$query_cgi built correctly (no index passed)");
     is( $query_desc, "query_desc operator index_plus original_operand",
         "\$query_desc build correctly (no index passed)");
@@ -148,7 +164,7 @@ subtest "_build_initial_query tests" => sub {
         C4::Search::_build_initial_query($params);
     is( $query, "query operator parsed_operand",
         "\$query built correctly (no index_plus passed)");
-    is( $query_cgi, "query_cgi&op=%20operator%20&idx=index&q=original_operand",
+    is( $query_cgi, "query_cgi&op=operator&idx=index&q=original_operand",
         "\$query_cgi built correctly (no index_plus passed)");
     is( $query_desc, "query_desc operator  original_operand",
         "\$query_desc build correctly (no index_plus passed)");
@@ -157,5 +173,35 @@ subtest "_build_initial_query tests" => sub {
 
 };
 
+subtest "searchResults PassItemMarcToXSLT test" => sub {
+
+    plan tests => 2;
+
+    t::lib::Mocks::mock_preference('OPACXSLTResultsDisplay','default');
+    t::lib::Mocks::mock_preference('marcflavour','MARC21');
+    my $mock_xslt = Test::MockModule->new("C4::Search");
+    $mock_xslt->mock( XSLTParse4Display => sub {
+        my $params = shift;
+        my $record = $params->{record};
+        warn $record->field('952') ? "Item here" : "No item";
+        return;
+    });
+
+    my $builder = t::lib::TestBuilder->new;
 
-1;
+    my $item = $builder->build_sample_item();
+    my $record = $item->biblio->metadata->record({ embed_items => 1 });
+
+    t::lib::Mocks::mock_preference('PassItemMarcToXSLT','1');
+
+    warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $record->as_xml_record ] ,undef) }
+        [qr/Item here/],
+        "Item field returned from default XSLT if pref set";
+
+    t::lib::Mocks::mock_preference('PassItemMarcToXSLT','0');
+
+    warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $record->as_xml_record ] ,undef) }
+        [qr/No item/],
+        "Item field returned from default XSLT if pref set";
+
+}