d0b10d7a4416c7106ab61738120b68dcc11aaaf0
[koha_ffzg] / 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 => 5;
20
21 use C4::Items;
22
23 use t::lib::TestBuilder;
24
25 my $builder = t::lib::TestBuilder->new;
26
27 # NOTE This is a trick, if we want to populate the biblioitems table, we should not create a Biblio but a Biblioitem
28 my $from_biblio = $builder->build( { source => 'Biblioitem', } )->{_fk}{biblionumber};
29 my $to_biblio   = $builder->build( { source => 'Biblioitem', } )->{_fk}{biblionumber};
30 my $item1       = $builder->build(
31     {   source => 'Item',
32         value  => { biblionumber => $from_biblio->{biblionumber}, },
33     }
34 );
35 my $item2 = $builder->build(
36     {   source => 'Item',
37         value  => { biblionumber => $from_biblio->{biblionumber}, },
38     }
39 );
40 my $item3 = $builder->build(
41     {   source => 'Item',
42         value  => { biblionumber => $to_biblio->{biblionumber}, },
43     }
44 );
45
46 my $to_biblionumber_after_moved = C4::Items::MoveItemFromBiblio( $item2->{itemnumber}, $from_biblio->{biblionumber}, $to_biblio->{biblionumber} );
47
48 is( $to_biblionumber_after_moved, $to_biblio->{biblionumber}, 'MoveItemFromBiblio should return the to_biblionumber if success' );
49
50 $to_biblionumber_after_moved = C4::Items::MoveItemFromBiblio( $item2->{itemnumber}, $from_biblio->{biblionumber}, $to_biblio->{biblionumber} );
51
52 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' );
53
54 my $get_item1 = C4::Items::GetItem( $item1->{itemnumber} );
55 is( $get_item1->{biblionumber}, $from_biblio->{biblionumber}, 'The item1 should not have been moved' );
56 my $get_item2 = C4::Items::GetItem( $item2->{itemnumber} );
57 is( $get_item2->{biblionumber}, $to_biblio->{biblionumber}, 'The item2 should have been moved' );
58 my $get_item3 = C4::Items::GetItem( $item3->{itemnumber} );
59 is( $get_item3->{biblionumber}, $to_biblio->{biblionumber}, 'The item3 should not have been moved' );