3 # Copyright 2016 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 3;
24 use Koha::Item::Transfer;
25 use Koha::Item::Transfers;
29 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
36 my $library_from = $builder->build( { source => 'Branch' } );
37 my $library_to = $builder->build( { source => 'Branch' } );
38 my $item = $builder->build( { source => 'Item', value => { holding_branch => $library_from->{branchcode}, homebranch => $library_to->{branchcode} } } );
40 my $nb_of_transfers = Koha::Item::Transfers->search->count;
41 my $new_transfer_1 = Koha::Item::Transfer->new(
42 { itemnumber => $item->{itemnumber},
43 frombranch => $library_from->{branchcode},
44 tobranch => $library_to->{branchcode},
45 datearrived => dt_from_string,
46 datesent => dt_from_string,
49 my $new_transfer_2 = Koha::Item::Transfer->new(
50 { itemnumber => $item->{itemnumber},
51 frombranch => $library_from->{branchcode},
52 tobranch => $library_to->{branchcode},
54 datesent => dt_from_string,
58 is( Koha::Item::Transfers->search->count, $nb_of_transfers + 2, 'The 2 transfers should have been added' );
60 my $retrieved_transfer_1 = Koha::Item::Transfers->search( { itemnumber => $new_transfer_1->itemnumber })->next;
61 is( $retrieved_transfer_1->itemnumber, $new_transfer_1->itemnumber, 'Find a transfer by id should return the correct transfer' );
63 # FIXME: This does not pass and should be fixed later
64 # "Operation requires a primary key to be declared on 'Branchtransfer' via set_primary_key"
65 #$retrieved_transfer_1->delete;
66 #is( Koha::Item::Transfers->search->count, $nb_of_transfers + 1, 'Delete should have deleted the transfer' );
68 $schema->storage->txn_rollback;
70 subtest 'daterequested tests' => sub {
74 $schema->storage->txn_begin;
75 my $library_from = $builder->build( { source => 'Branch' } );
76 my $library_to = $builder->build( { source => 'Branch' } );
77 my $item = $builder->build(
81 holding_branch => $library_from->{branchcode},
82 homebranch => $library_to->{branchcode}
87 my $now = dt_from_string;
88 my $transfer = Koha::Item::Transfer->new(
90 itemnumber => $item->{itemnumber},
91 frombranch => $library_from->{branchcode},
92 tobranch => $library_to->{branchcode}
95 $transfer->discard_changes;
97 ok( $transfer->daterequested, 'daterequested set on creation' );
98 is( t::lib::Dates::compare( $transfer->daterequested, $now ),
99 0, 'daterequested was set correctly' );
101 my $new_date = $now->clone->add( hours => 1 );
102 $transfer->set({ datesent => $new_date })->store;
103 $transfer->discard_changes;
105 is( t::lib::Dates::compare( $transfer->daterequested, $now ),
106 0, 'daterequested is not updated when other fields are updated' );
108 $schema->storage->txn_rollback;