Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / XSLT.t
old mode 100644 (file)
new mode 100755 (executable)
index 2d9e9db..5215789
@@ -17,7 +17,8 @@
 
 use Modern::Perl;
 
-use Test::More tests => 2;
+use MARC::Record;
+use Test::More tests => 4;
 use Test::Warn;
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -25,7 +26,7 @@ use t::lib::Mocks;
 use Koha::ItemTypes;
 
 BEGIN {
-    use_ok('C4::XSLT');
+    use_ok('C4::XSLT', qw( transformMARCXML4XSLT getAuthorisedValues4MARCSubfields buildKohaItemsNamespace ));
 }
 
 my $schema  = Koha::Database->new->schema;
@@ -33,8 +34,22 @@ my $builder = t::lib::TestBuilder->new;
 
 $schema->storage->txn_begin;
 
+subtest 'transformMARCXML4XSLT tests' => sub {
+    plan tests => 1;
+    my $mock_xslt =  Test::MockModule->new("C4::XSLT");
+    $mock_xslt->mock( getAuthorisedValues4MARCSubfields => sub { return { 942 => { 'n' => 1 } } } );
+    $mock_xslt->mock( GetAuthorisedValueDesc => sub { warn "called"; });
+    my $record = MARC::Record->new();
+    my $suppress_field = MARC::Field->new( 942, ' ', ' ', n => '1' );
+    $record->append_fields($suppress_field);
+    warning_is { C4::XSLT::transformMARCXML4XSLT( 3,$record ) } undef, "942n auth value not translated";
+};
+
 subtest 'buildKohaItemsNamespace status tests' => sub {
-    plan tests => 13;
+    plan tests => 14;
+
+    t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
+
     my $itype = $builder->build_object({ class => 'Koha::ItemTypes' });
     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
     my $item  = $builder->build_sample_item({ itype => $itype->itemtype });
@@ -45,7 +60,6 @@ subtest 'buildKohaItemsNamespace status tests' => sub {
 
     # notforloan
     {
-
         t::lib::Mocks::mock_preference('item-level_itypes', 0);
         $item->notforloan(0)->store;
         Koha::ItemTypes->find($item->itype)->notforloan(0)->store;
@@ -69,6 +83,12 @@ subtest 'buildKohaItemsNamespace status tests' => sub {
         $item->notforloan(1)->store;
         $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
         like($xml,qr{<status>reference</status>},"reference if positive notforloan value");
+
+        # But now make status notforloan==1 count under Not available
+        t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '2');
+        $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
+        like($xml,qr{<status>reallynotforloan</status>},"reallynotforloan when we change Reference_NFL_Statuses");
+        t::lib::Mocks::mock_preference('Reference_NFL_Statuses', '1|2');
     }
 
     $item->onloan('2001-01-01')->store;
@@ -90,6 +110,7 @@ subtest 'buildKohaItemsNamespace status tests' => sub {
     $builder->build({ source => "Branchtransfer", value => {
         itemnumber  => $item->itemnumber,
         datearrived => undef,
+        datecancelled => undef,
         }
     });
     $xml = C4::XSLT::buildKohaItemsNamespace( $item->biblionumber,[]);
@@ -116,3 +137,72 @@ subtest 'buildKohaItemsNamespace status tests' => sub {
 };
 
 $schema->storage->txn_rollback;
+
+subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
+
+    plan tests => 20;
+
+    $schema->storage->txn_begin;
+
+    my $biblio = $builder->build_sample_biblio;
+
+    # Have two known libraries for testing purposes
+    my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
+    my $library_2 = $builder->build_object({ class => 'Koha::Libraries' });
+    my $library_3 = $builder->build_object({ class => 'Koha::Libraries' });
+
+    my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_1->id });
+    my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_2->id });
+    my $item_3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber, library => $library_3->id });
+
+    my $items_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } });
+
+    ## Test passing items_rs only
+    my $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $items_rs );
+
+    my $library_1_name = $library_1->branchname;
+    my $library_2_name = $library_2->branchname;
+    my $library_3_name = $library_3->branchname;
+
+    like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
+    like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
+    ## Test passing one item in hidden_items and items_rs
+    $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
+
+    unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
+    like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
+
+    ## Test passing both items in hidden_items and items_rs
+    $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber ], $items_rs->reset );
+
+    unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
+    is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
+
+    ## Test passing both items in hidden_items and no items_rs
+    $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber, $item_2->itemnumber, $item_3->itemnumber ] );
+
+    unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 not present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
+    is( $xml, '<items xmlns="http://www.koha-community.org/items"></items>', 'Empty XML' );
+
+    ## Test passing one item in hidden_items and items_rs
+    $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ] );
+
+    unlike( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 not present in the XML' );
+    like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
+    like(   $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
+
+    ## Test not passing any param
+    $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber );
+
+    like( $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
+    like( $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
+    like( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 present in the XML' );
+
+    $schema->storage->txn_rollback;
+};