Bug 9031: (QA follow-up) Pass the same timezone in Calendar.t
[koha.git] / t / db_dependent / Items.t
index 19a8a36..f257f78 100755 (executable)
@@ -26,7 +26,7 @@ use Koha::Library;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
-use Test::More tests => 10;
+use Test::More tests => 12;
 
 use Test::Warn;
 
@@ -418,7 +418,11 @@ subtest 'SearchItems test' => sub {
 
     my $item3_record = new MARC::Record;
     $item3_record->append_fields(
-        new MARC::Field($itemfield, '', '', 'z' => 'foobar')
+        new MARC::Field(
+            $itemfield, '', '',
+            'z' => 'foobar',
+            'y' => $itemtype->{itemtype}
+        )
     );
     my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
         $biblionumber);
@@ -553,7 +557,7 @@ subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
 
     my ($itemfield) =
       C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' );
-    my $record = C4::Biblio::GetMarcBiblio($biblionumber);
+    my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
     warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
     { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
       'Should crap is no record passed.';
@@ -652,6 +656,18 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
         }
     });
 
+    my $mss_itemtype = $builder->build({
+        source => 'MarcSubfieldStructure',
+        value => {
+            frameworkcode => $framework->{frameworkcode},
+            kohafield => 'items.itype',
+            tagfield => '952',
+            tagsubfield => 'y',
+        }
+    });
+
+    my $itemtype = $builder->build({ source => 'Itemtype' })->{itemtype};
+
     # Create a record with a barcode
     my ($biblionumber) = get_biblio( $framework->{frameworkcode} );
     my $item_record = new MARC::Record;
@@ -659,6 +675,11 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
     my $barcode_field = MARC::Field->new(
         '952', ' ', ' ',
         p => $a_barcode,
+        y => $itemtype
+    );
+    my $itemtype_field = MARC::Field->new(
+        '952', ' ', ' ',
+        y => $itemtype
     );
     $item_record->append_fields( $barcode_field );
     my (undef, undef, $item_itemnumber) = AddItemFromMarc($item_record, $biblionumber);
@@ -669,6 +690,7 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
 
     # Delete the barcode field and save the record
     $item_record->delete_fields( $barcode_field );
+    $item_record->append_fields( $itemtype_field ); # itemtype is mandatory
     ModItemFromMarc($item_record, $biblionumber, $item_itemnumber);
     $item = GetItem($item_itemnumber);
     is( $item->{barcode}, undef, 'The default value should have been set to the barcode, the field is mapped to a kohafield' );
@@ -710,6 +732,71 @@ subtest 'C4::Items::_build_default_values_for_mod_marc' => sub {
     $schema->storage->txn_rollback;
 };
 
+subtest '_mod_item_dates' => sub {
+    plan tests => 11;
+
+    is( C4::Items::_mod_item_dates(), undef, 'Call without parameters' );
+    is( C4::Items::_mod_item_dates(1), undef, 'Call without hashref' );
+
+    my $orgitem;
+    my $item = {
+        itemcallnumber  => 'V II 149 1963',
+        barcode         => '109304',
+    };
+    $orgitem = { %$item };
+    C4::Items::_mod_item_dates($item);
+    is_deeply( $item, $orgitem, 'No dates passed to _mod_item_dates' );
+
+    # add two correct dates
+    t::lib::Mocks::mock_preference('dateformat', 'us');
+    $item->{dateaccessioned} = '01/31/2016';
+    $item->{onloan} =  $item->{dateaccessioned};
+    $orgitem = { %$item };
+    C4::Items::_mod_item_dates($item);
+    is( $item->{dateaccessioned}, '2016-01-31', 'dateaccessioned is fine' );
+    is( $item->{onloan}, '2016-01-31', 'onloan is fine too' );
+
+
+    # add some invalid dates
+    $item->{notexistingcolumndate} = '13/1/2015'; # wrong format
+    $item->{anotherdate} = 'tralala'; # even worse
+    $item->{myzerodate} = '0000-00-00'; # wrong too
+    C4::Items::_mod_item_dates($item);
+    is( $item->{notexistingcolumndate}, undef, 'Invalid date became NULL' );
+    is( $item->{anotherdate}, undef, 'Second invalid date became NULL too' );
+    is( $item->{myzerodate}, undef, '0000-00-00 became NULL too' );
+
+    # check if itemlost_on was not touched
+    $item->{itemlost_on} = '12345678';
+    $item->{withdrawn_on} = '12/31/2015 23:59:00';
+    $orgitem = { %$item };
+    C4::Items::_mod_item_dates($item);
+    is_deeply( $item, $orgitem, 'Colums with _on are not touched' );
+
+    t::lib::Mocks::mock_preference('dateformat', 'metric');
+    $item->{dateaccessioned} = '01/31/2016'; #wrong
+    $item->{yetanotherdatetime} = '20/01/2016 13:58:00'; #okay
+    C4::Items::_mod_item_dates($item);
+    is( $item->{dateaccessioned}, undef, 'dateaccessioned wrong format' );
+    is( $item->{yetanotherdatetime}, '2016-01-20 13:58:00',
+        'yetanotherdatetime is ok' );
+};
+
+subtest 'get_hostitemnumbers_of' => sub {
+    plan tests => 1;
+
+    my $bib = MARC::Record->new();
+    $bib->append_fields(
+        MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'),
+        MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'),
+        MARC::Field->new('773', ' ', ' ', b => 'b without 0 or 9'),
+    );
+    my ($biblionumber, $bibitemnum) = AddBiblio($bib, '');
+
+    my @itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
+    is( @itemnumbers, 0, );
+};
+
 # Helper method to set up a Biblio.
 sub get_biblio {
     my ( $frameworkcode ) = @_;