Bug 19575: Rebase and fix tests
[koha.git] / t / db_dependent / Breeding.t
old mode 100644 (file)
new mode 100755 (executable)
index 54a9ca9..0aad4e4
 use Modern::Perl;
 
 use FindBin;
-use Test::More tests => 3;
+use Test::More tests => 4;
 use Test::Warn;
+use t::lib::Mocks qw( mock_preference );
 
+use C4::Context;
 use C4::Breeding;
 use Koha::XSLT_Handler;
 
@@ -36,7 +38,7 @@ use Koha::XSLT_Handler;
 
 #Group 1: testing _build_query and _translate_query (part of Z3950Search)
 subtest '_build_query' => sub {
-    plan tests => 12;
+    plan tests => 14;
     test_build_translate_query();
 };
 #Group 2: testing _create_connection (part of Z3950Search)
@@ -49,19 +51,24 @@ subtest '_do_xslt_proc' => sub {
     plan tests => 6;
     test_do_xslt();
 };
+#Group 4: testing _add_rowdata (part of Z3950Search)
+subtest '_add_rowdata' => sub {
+    plan tests => 5;
+    test_add_rowdata();
+};
 
 #-------------------------------------------------------------------------------
 
 sub test_build_translate_query {
     my $str;
     #First pass no parameters
-    my @queries= C4::Breeding::_build_query( {} );
+    my @queries= C4::Breeding::_bib_build_query( {} );
     is( defined $queries[0] && $queries[0] eq '' && defined $queries[1] &&
-        $queries[1] eq '', 1, '_build_query gets no parameters');
+        $queries[1] eq '', 1, '_bib_build_query gets no parameters');
 
     #We now pass one parameter
     my $pars1= { isbn => '234567' };
-    @queries= C4::Breeding::_build_query( $pars1 );
+    @queries= C4::Breeding::_bib_build_query( $pars1 );
     #Passed only one par: zquery should start with @attr 1=\d+
     is( $queries[0] =~ /^\@attr 1=\d+/, 1, 'Z39.50 query with one parameter');
     $str=$pars1->{isbn};
@@ -82,7 +89,7 @@ sub test_build_translate_query {
 
     #We now pass two parameters
     my $pars2= { isbn => '123456', title => 'You should read this.' };
-    @queries= C4::Breeding::_build_query( $pars2 );
+    @queries= C4::Breeding::_bib_build_query( $pars2 );
     #The Z39.50 query should start with @and (we passed two pars)
     is( $queries[0] =~ /^\@and/, 1, 'Second Z39.50 query starts with @and');
     #We should also find two @attr 1=\d+
@@ -104,9 +111,23 @@ sub test_build_translate_query {
 
     #We now pass a third wrong parameter (should not make a difference)
     my $pars3= { isbn => '123456', title => 'You should read this.', xyz => 1 };
-    my @queries2= C4::Breeding::_build_query( $pars3 );
+    my @queries2= C4::Breeding::_bib_build_query( $pars3 );
     is( $queries[0] eq $queries2[0] && $queries[1] eq $queries2[1], 1,
         'Third query makes no difference');
+
+    # Check that indexes with equal signs are ok
+    $server = { sru_fields => 'subjectsubdiv=aut.type=ram_pe and aut.accesspoint' };
+    my $pars4 = { subjectsubdiv => 'mysubjectsubdiv' };
+    @queries = C4::Breeding::_auth_build_query( $pars4 );
+    my $zquery = C4::Breeding::_translate_query( $server, $queries[1] );
+    is ( $zquery, 'aut.type=ram_pe and aut.accesspoint="mysubjectsubdiv"', 'SRU query with equal sign in index');
+
+    # Check that indexes with double-quotes are ok
+    $server = { sru_fields => 'subject=(aut.type any "geo ram_nc ram_ge ram_pe ram_co") and aut.accesspoint' };
+    my $pars5 = { subject => 'mysubject' };
+    @queries = C4::Breeding::_auth_build_query( $pars5 );
+    $zquery = C4::Breeding::_translate_query( $server, $queries[1] );
+    is ( $zquery, '(aut.type any "geo ram_nc ram_ge ram_pe ram_co") and aut.accesspoint="mysubject"', 'SRU query with double quotes in index');
 }
 
 sub test_create_connection {
@@ -175,3 +196,44 @@ sub test_do_xslt {
     is ( $res[0]->subfield('245','a'), 'Just a title',
         'At least the title is the same :)' );
 }
+
+sub test_add_rowdata {
+    t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch','');
+
+    my $row = {
+       biblionumber => 0,
+       server => "testServer",
+       breedingid => 0
+   };
+
+    my $biblio = MARC::Record->new();
+    $biblio->append_fields(
+        MARC::Field->new('245', ' ', ' ', a => 'Just a title'), #title
+    );
+
+    my $returned_row = C4::Breeding::_add_rowdata($row, $biblio);
+
+    is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio");
+    is($returned_row->{addnumberfields}[0], undef, "_add_rowdata returns undef if it has no additionnal field");
+
+    t::lib::Mocks::mock_preference('AdditionalFieldsInZ3950ResultSearch',"245\$a, 035\$a");
+
+    $row = {
+       biblionumber => 0,
+       server => "testServer",
+       breedingid => 0
+   };
+   $biblio = MARC::Record->new();
+   $biblio->append_fields(
+        MARC::Field->new('245', ' ', ' ', a => 'Just a title'), #title
+        MARC::Field->new('035', ' ', ' ', a => 'First 035'),
+        MARC::Field->new('035', ' ', ' ', a => 'Second 035')
+   );
+   $returned_row = C4::Breeding::_add_rowdata($row, $biblio);
+
+   is($returned_row->{title}, "Just a title", "_add_rowdata returns the title of a biblio");
+   is($returned_row->{addnumberfields}[0], "245\$a", "_add_rowdata returns the field number chosen in the AdditionalFieldsInZ3950ResultSearch preference");
+
+   # Test repeatble tags,the trailing whitespace is a normal side-effect of _add_custom_row_data
+   is_deeply(\$returned_row->{"035\$a"}, \["First 035 ", "Second 035 "],"_add_rowdata supports repeatable tags");
+}