Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Items / MoveItemFromBiblio.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Test::More tests => 8;
20
21 use C4::Items qw( MoveItemFromBiblio );
22 use C4::Reserves;
23 use Koha::Database;
24 use Koha::Holds;
25 use Koha::Items;
26
27 use t::lib::TestBuilder;
28 use Data::Dumper qw|Dumper|;
29
30 my $schema  = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32 my $builder = t::lib::TestBuilder->new;
33
34 my $from_biblio = $builder->build_sample_biblio;
35 my $to_biblio = $builder->build_sample_biblio;
36
37 my $item1 = $builder->build_sample_item(
38     { biblionumber => $from_biblio->biblionumber, } );
39 my $item2 = $builder->build_sample_item(
40     { biblionumber => $from_biblio->biblionumber, } );
41 my $item3 = $builder->build_sample_item(
42     { biblionumber => $to_biblio->biblionumber, } );
43
44
45 my $bib_level_hold_not_to_move = $builder->build(
46     {   source => 'Reserve',
47         value  => { biblionumber => $from_biblio->biblionumber, },
48     }
49 );
50 my $item_level_hold_not_to_move = $builder->build(
51     {   source => 'Reserve',
52         value  => { biblionumber => $from_biblio->biblionumber, itemnumber => $item1->itemnumber },
53     }
54 );
55 my $item_level_hold_to_move = $builder->build(
56     {   source => 'Reserve',
57         value  => { biblionumber => $from_biblio->biblionumber, itemnumber => $item2->itemnumber },
58     }
59 );
60
61 my $to_biblionumber_after_moved = C4::Items::MoveItemFromBiblio( $item2->itemnumber, $from_biblio->biblionumber, $to_biblio->biblionumber );
62
63 is( $to_biblionumber_after_moved, $to_biblio->biblionumber, 'MoveItemFromBiblio should return the to_biblionumber if success' );
64
65 $to_biblionumber_after_moved = C4::Items::MoveItemFromBiblio( $item2->itemnumber, $from_biblio->biblionumber, $to_biblio->biblionumber );
66
67 is( $to_biblionumber_after_moved, undef, 'MoveItemFromBiblio should return undef if the move has failed. If called twice, the item is not attached to the first biblio anymore' );
68
69 my $get_item1 = Koha::Items->find( $item1->itemnumber );
70 is( $get_item1->biblionumber, $from_biblio->biblionumber, 'The item1 should not have been moved' );
71 my $get_item2 = Koha::Items->find( $item2->itemnumber );
72 is( $get_item2->biblionumber, $to_biblio->biblionumber, 'The item2 should have been moved' );
73 my $get_item3 = Koha::Items->find( $item3->itemnumber );
74 is( $get_item3->biblionumber, $to_biblio->biblionumber, 'The item3 should not have been moved' );
75
76 my $get_bib_level_hold    = Koha::Holds->find( $bib_level_hold_not_to_move->{reserve_id} );
77 my $get_item_level_hold_1 = Koha::Holds->find( $item_level_hold_not_to_move->{reserve_id} );
78 my $get_item_level_hold_2 = Koha::Holds->find( $item_level_hold_to_move->{reserve_id} );
79
80 is( $get_bib_level_hold->biblionumber,    $from_biblio->biblionumber, 'MoveItemFromBiblio should not have moved the biblio-level hold' );
81 is( $get_item_level_hold_1->biblionumber, $from_biblio->biblionumber, 'MoveItemFromBiblio should not have moved the item-level hold placed on item 1' );
82 is( $get_item_level_hold_2->biblionumber, $to_biblio->biblionumber,   'MoveItemFromBiblio should have moved the item-level hold placed on item 2' );
83
84 $schema->storage->txn_rollback;
85