Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Items.t
index c07c667..120d9b9 100755 (executable)
@@ -19,8 +19,8 @@ use Modern::Perl;
 use Data::Dumper;
 
 use MARC::Record;
-use C4::Items;
-use C4::Biblio;
+use C4::Items qw( ModItemTransfer GetHiddenItemnumbers GetItemsInfo SearchItems AddItemFromMarc ModItemFromMarc get_hostitemnumbers_of Item2Marc );
+use C4::Biblio qw( GetMarcFromKohaField EmbedItemsInMarcBiblio GetMarcBiblio AddBiblio );
 use Koha::Items;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
@@ -33,7 +33,7 @@ use Koha::AuthorisedValues;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
-use Test::More tests => 15;
+use Test::More tests => 14;
 
 use Test::Warn;
 
@@ -126,31 +126,6 @@ subtest 'General Add, Get and Del tests' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'ModItem tests' => sub {
-    plan tests => 6;
-
-    $schema->storage->txn_begin;
-
-    my $builder = t::lib::TestBuilder->new;
-    my $item    = $builder->build_sample_item();
-
-    my @fields = qw( itemlost withdrawn damaged );
-    for my $field (@fields) {
-        my $field_on = $field."_on";
-
-        $item->$field(1)->store;
-        $item->discard_changes;
-        is( output_pref({ str => $item->$field_on, dateonly => 1 }), output_pref({ dt => dt_from_string(), dateonly => 1 }), "When updating $field, $field_on is updated" );
-
-        $item->$field(0)->store;
-        $item->discard_changes;
-        is( $item->$field_on, undef, "When clearing $field, $field_on is cleared" );
-    }
-
-    $schema->storage->txn_rollback;
-
-};
-
 subtest 'ModItemTransfer tests' => sub {
     plan tests => 8;
 
@@ -171,7 +146,7 @@ subtest 'ModItemTransfer tests' => sub {
     );
 
     ModItemTransfer( $item->itemnumber, $library1->{branchcode},
-        $library2->{branchcode} );
+        $library2->{branchcode}, 'Manual' );
 
     my $transfers = Koha::Item::Transfers->search(
         {
@@ -184,7 +159,7 @@ subtest 'ModItemTransfer tests' => sub {
     is($item->holdingbranch, $library1->{branchcode}, "Items holding branch was updated to frombranch");
 
     ModItemTransfer( $item->itemnumber, $library2->{branchcode},
-        $library1->{branchcode} );
+        $library1->{branchcode}, 'Manual' );
     $transfers = Koha::Item::Transfers->search(
         { itemnumber => $item->itemnumber, },
         { order_by   => { '-asc' => 'branchtransfer_id' } }
@@ -193,8 +168,8 @@ subtest 'ModItemTransfer tests' => sub {
     is($transfers->count, 2, "Second transfer recorded on second call of ModItemTransfer");
     my $transfer1 = $transfers->next;
     my $transfer2 = $transfers->next;
-    isnt($transfer1->datearrived, undef, "First transfer marked as completed by ModItemTransfer");
-    like($transfer1->comments,qr/^Canceled/, "First transfer contains 'Canceled' comment");
+    isnt($transfer1->datecancelled, undef, "First transfer marked as cancelled by ModItemTransfer");
+    like($transfer1->cancellation_reason,qr/^Manual/, "First transfer contains cancellation_reason 'Manual'");
     is($transfer2->datearrived, undef, "Second transfer is now the active transfer");
     $item->discard_changes;
     is($item->holdingbranch, $library2->{branchcode}, "Items holding branch was updated to frombranch");
@@ -432,7 +407,7 @@ subtest q{Test Koha::Database->schema()->resultset('Item')->itemtype()} => sub {
 };
 
 subtest 'SearchItems test' => sub {
-    plan tests => 15;
+    plan tests => 17;
 
     $schema->storage->txn_begin;
     my $dbh = C4::Context->dbh;
@@ -566,9 +541,9 @@ subtest 'SearchItems test' => sub {
     $cache->clear_from_cache("default_value_for_mod_marc-");
     $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode");
 
-    my $item3_record = new MARC::Record;
+    my $item3_record = MARC::Record->new;
     $item3_record->append_fields(
-        new MARC::Field(
+        MARC::Field->new(
             $itemfield, '', '',
             'z' => 'foobar',
             'y' => $itemtype->{itemtype}
@@ -601,17 +576,17 @@ subtest 'SearchItems test' => sub {
 
     # Make sure the link is used
     my $item3 = Koha::Items->find($item3_itemnumber);
-    ok($item3->itemnotes eq 'foobar', 'itemnotes eq "foobar"');
+    is($item3->itemnotes, 'foobar', 'itemnotes eq "foobar"');
 
     # Do the same search again.
     # This time it will search in items.itemnotes
     ($items, $total_results) = SearchItems($filter);
-    ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
+    is(scalar(@$items), 1, 'found 1 item with itemnotes = "foobar"');
 
     my ($cpl_items_after) = SearchItems( { field => 'homebranch', query => $library1->{branchcode} } );
     is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItems should return something' );
 
-    # Issues count may be NULL
+    # Issues count = 0
     $filter = {
         conjunction => 'AND',
         filters => [
@@ -628,7 +603,30 @@ subtest 'SearchItems test' => sub {
         ],
     };
     ($items, $total_results) = SearchItems($filter);
-    is($total_results, 1, "Search items.issues is NULL with filter issues = 0");
+    is($total_results, 1, "Search items.issues issues = 0 returns result (items.issues defaults to 0)");
+
+    # Is null
+    $filter = {
+        conjunction => 'AND',
+        filters     => [
+            {
+                field    => 'new_status',
+                query    => 0,
+                operator => '='
+            },
+            {
+                field    => 'homebranch',
+                query    => $library1->{branchcode},
+                operator => '=',
+            },
+        ],
+    };
+    ($items, $total_results) = SearchItems($filter);
+    is($total_results, 0, 'found no item with new_status=0 without ifnull');
+
+    $filter->{filters}[0]->{ifnull} = 0;
+    ($items, $total_results) = SearchItems($filter);
+    is($total_results, 1, 'found all items of library1 with new_status=0 with ifnull = 0');
 
     $schema->storage->txn_rollback;
 };
@@ -895,14 +893,14 @@ subtest 'Check stockrotationitem relationship' => sub {
     $schema->storage->txn_begin();
 
     my $builder = t::lib::TestBuilder->new;
-    my $item = $builder->build({ source => 'Item' });
+    my $item = $builder->build_sample_item;
 
     $builder->build({
         source => 'Stockrotationitem',
-        value  => { itemnumber_id => $item->{itemnumber} }
+        value  => { itemnumber_id => $item->itemnumber }
     });
 
-    my $sritem = Koha::Items->find($item->{itemnumber})->stockrotationitem;
+    my $sritem = Koha::Items->find($item->itemnumber)->stockrotationitem;
     isa_ok( $sritem, 'Koha::StockRotationItem', "Relationship works and correctly creates Koha::Object." );
 
     $schema->storage->txn_rollback;
@@ -914,7 +912,7 @@ subtest 'Check add_to_rota method' => sub {
     $schema->storage->txn_begin();
 
     my $builder = t::lib::TestBuilder->new;
-    my $item = $builder->build({ source => 'Item' });
+    my $item = $builder->build_sample_item;
     my $rota = $builder->build({ source => 'Stockrotationrota' });
     my $srrota = Koha::StockRotationRotas->find($rota->{rota_id});
 
@@ -923,11 +921,11 @@ subtest 'Check add_to_rota method' => sub {
         value  => { rota_id => $rota->{rota_id} },
     });
 
-    my $sritem = Koha::Items->find($item->{itemnumber});
+    my $sritem = Koha::Items->find($item->itemnumber);
     $sritem->add_to_rota($rota->{rota_id});
 
     is(
-        Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
+        Koha::StockRotationItems->find($item->itemnumber)->stage_id,
         $srrota->stockrotationstages->next->stage_id,
         "Adding to a rota a new sritem item being assigned to its first stage."
     );
@@ -944,7 +942,7 @@ subtest 'Check add_to_rota method' => sub {
     $sritem->add_to_rota($newrota->{rota_id});
 
     is(
-        Koha::StockRotationItems->find($item->{itemnumber})->stage_id,
+        Koha::StockRotationItems->find($item->itemnumber)->stage_id,
         $srnewrota->stockrotationstages->next->stage_id,
         "Moving an item results in that sritem being assigned to the new first stage."
     );
@@ -957,8 +955,7 @@ subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
     $schema->storage->txn_begin;
 
     my $builder = t::lib::TestBuilder->new;
-    my $biblio = $builder->build({ source => 'Biblio', value => { frameworkcode => q{} } });
-    my $item = $builder->build({ source => 'Item', value => { biblionumber => $biblio->{biblionumber}, ccode => 'A|B' } });
+    my $item = $builder->build_sample_item({ ccode => 'A|B' });
 
     Koha::MarcSubfieldStructures->search({ tagfield => '952', tagsubfield => '8' })->delete; # theoretical precaution
     Koha::MarcSubfieldStructures->search({ kohafield => 'items.ccode' })->delete;
@@ -974,7 +971,7 @@ subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
     Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
 
     # Start testing
-    my $marc = C4::Items::Item2Marc( $item, $biblio->{biblionumber} );
+    my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
     my @subs = $marc->subfield( $mapping->tagfield, $mapping->tagsubfield );
     is( @subs, 2, 'Expect two subfields' );
     is( $subs[0], 'A', 'First subfield matches' );
@@ -984,7 +981,7 @@ subtest 'Split subfields in Item2Marc (Bug 21774)' => sub {
 };
 
 subtest 'ModItemFromMarc' => sub {
-    plan tests => 2;
+    plan tests => 6;
     $schema->storage->txn_begin;
 
     my $builder = t::lib::TestBuilder->new;
@@ -992,7 +989,7 @@ subtest 'ModItemFromMarc' => sub {
     my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' });
     my $biblio = $builder->build_sample_biblio;
     my ( $lost_tag, $lost_sf ) = GetMarcFromKohaField( 'items.itemlost' );
-    my $item_record = new MARC::Record;
+    my $item_record = MARC::Record->new;
     $item_record->append_fields(
         MARC::Field->new(
             $itemfield, '', '',
@@ -1009,7 +1006,9 @@ subtest 'ModItemFromMarc' => sub {
     my $item = Koha::Items->find($itemnumber);
     is( $item->itemlost, 1, 'itemlost picked from the item marc');
 
-    my $updated_item_record = new MARC::Record;
+    $item->new_status("this is something")->store;
+
+    my $updated_item_record = MARC::Record->new;
     $updated_item_record->append_fields(
         MARC::Field->new(
             $itemfield, '', '',
@@ -1019,6 +1018,112 @@ subtest 'ModItemFromMarc' => sub {
 
     my $updated_item = ModItemFromMarc($updated_item_record, $biblio->biblionumber, $itemnumber);
     is( $updated_item->{itemlost}, 0, 'itemlost should have been reset to the default value in DB' );
+    is( $updated_item->{new_status}, "this is something", "Non mapped field has not been reset" );
+    is( Koha::Items->find($itemnumber)->new_status, "this is something" );
+
+    subtest 'cn_sort' => sub {
+        plan tests => 3;
+
+        my $item = $builder->build_sample_item;
+        $item->set({ cn_source => 'ddc', itemcallnumber => 'xxx' })->store;
+        is( $item->cn_sort, 'XXX', 'init values set are expected' );
+
+        my $marc = C4::Items::Item2Marc( $item->get_from_storage->unblessed, $item->biblionumber );
+        ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+        is( $item->get_from_storage->cn_sort, 'XXX', 'cn_sort has not been updated' );
+
+        $marc = C4::Items::Item2Marc( { %{$item->unblessed}, itemcallnumber => 'yyy' }, $item->biblionumber );
+        ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+        is( $item->get_from_storage->cn_sort, 'YYY', 'cn_sort has been updated' );
+    };
+
+    subtest 'permanent_location' => sub {
+        plan tests => 10;
+
+        # Make sure items.permanent_location is not mapped
+        Koha::MarcSubfieldStructures->search(
+            {
+                frameworkcode => q{},
+                kohafield     => 'items.permanent_location',
+            }
+        )->delete;
+        Koha::MarcSubfieldStructures->search(
+            {
+                frameworkcode => q{},
+                tagfield     => '952',
+                tagsubfield     => 'C',
+            }
+        )->delete;
+        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+
+        my $item = $builder->build_sample_item;
+
+        # By default, setting location to something new should set permanent location to the same thing
+        # with the usual exceptions
+        $item->set({ location => 'A', permanent_location => 'A' })->store;
+        is( $item->location, 'A', 'initial location set as expected' );
+        is( $item->permanent_location, 'A', 'initial permanent location set as expected' );
+
+        $item->location('B');
+        my $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+        ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+
+        $item = $item->get_from_storage;
+        is( $item->location, 'B', 'new location set as expected' );
+        is( $item->permanent_location, 'B', 'new permanent location set as expected' );
+
+        # Added a marc mapping for permanent location, allows it to be edited independently
+        my $mapping = Koha::MarcSubfieldStructure->new(
+            {
+                frameworkcode => q{},
+                tagfield      => '952',
+                tagsubfield   => 'C',
+                kohafield     => 'items.permanent_location',
+                repeatable    => 0,
+                tab           => 10,
+                hidden        => 0,
+            }
+        )->store;
+        Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+
+        # Now if we change location, and also pass in a permanent location
+        # the permanent_location will not be overwritten by location
+        $item->location('C');
+        $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+        ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+        $item = $item->get_from_storage;
+        is( $item->location, 'C', 'next new location set as expected' );
+        is( $item->permanent_location, 'B', 'permanent location remains unchanged as expected' );
+
+        $item->permanent_location(undef)->more_subfields_xml(undef)->store;
+        # Clear values from the DB
+        $item = $item->get_from_storage;
+
+        # Update the location
+        $item->location('D');
+        $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+        # Remove the permanent_location field from the form
+        $marc->field('952')->delete_subfield("C");
+        ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+        $item = $item->get_from_storage;
+        is( $item->location, 'D', 'next new location set as expected' );
+        is( $item->permanent_location, 'D', 'permanent location is updated if not previously set and no value passed' );
+
+        # Clear values from the DB
+        $item->permanent_location(undef)->more_subfields_xml(undef)->store;
+        $item = $item->get_from_storage;
+
+        # This time nothing is set, but we pass an empty string
+        $item->permanent_location("");
+        $item->location('E');
+        $marc = C4::Items::Item2Marc( $item->unblessed, $item->biblionumber );
+        $marc->field('952')->add_subfields( "C", "" );
+        ModItemFromMarc( $marc, $item->biblionumber, $item->itemnumber );
+        $item = $item->get_from_storage;
+        is( $item->location, 'E', 'next new location set as expected' );
+        is( $item->permanent_location, undef, 'permanent location is not updated if previously set as blank string' );
+    };
 
     $schema->storage->txn_rollback;
-};
+    Koha::Caches->get_instance->clear_from_cache( "MarcSubfieldStructure-" );
+}