3 # This Koha test module is a stub!
4 # Add more tests here!!!
7 use POSIX
qw(strftime);
9 use C4
::Bookseller
qw( GetBookSellerFromId );
11 use Test
::More tests
=> 71;
14 use_ok
('C4::Acquisition');
15 use_ok
('C4::Bookseller');
17 use_ok
('C4::Budgets');
18 use_ok
('C4::Bookseller');
21 # Sub used for testing C4::Acquisition subs returning order(s):
22 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
23 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
24 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
26 # $exp_fields : arrayref whose elements are the keys we expect to find
27 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
28 # containing content fields of the order created with NewOrder
29 # $order_to_check : hashref whose keys/values are the content of an order
30 # returned by the C4::Acquisition sub we are testing
32 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
33 # fields missing in $order_to_check
34 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
35 # fields unexpected in $order_to_check
36 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
37 # fields which value is not the same in between $order_to_check and
38 # $test_nbr_fields : contains the number of fields of $order_to_check
40 sub _check_fields_of_order
{
41 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
42 my @test_missing_fields = ();
43 my @test_extra_fields = ();
44 my @test_different_fields = ();
45 my $test_nbr_fields = scalar( keys %$order_to_check );
46 foreach my $field (@
$exp_fields) {
47 push @test_missing_fields, $field
48 unless exists( $order_to_check->{$field} );
50 foreach my $field ( keys %$order_to_check ) {
51 push @test_extra_fields, $field
52 unless grep ( /^$field$/, @
$exp_fields );
54 foreach my $field ( keys %{ $original_order_content->{str
} } ) {
55 push @test_different_fields, $field
56 unless ( !exists $order_to_check->{$field} )
57 or ( $original_order_content->{str
}->{$field} eq
58 $order_to_check->{$field} );
60 foreach my $field ( keys %{ $original_order_content->{num
} } ) {
61 push @test_different_fields, $field
62 unless ( !exists $order_to_check->{$field} )
63 or ( $original_order_content->{num
}->{$field} ==
64 $order_to_check->{$field} );
67 \
@test_missing_fields, \
@test_extra_fields,
68 \
@test_different_fields, $test_nbr_fields
72 # Sub used for testing C4::Acquisition subs returning several orders
73 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
74 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
75 sub _check_fields_of_orders
{
76 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
77 my @test_missing_fields = ();
78 my @test_extra_fields = ();
79 my @test_different_fields = ();
80 my @test_nbr_fields = ();
81 foreach my $order_to_check (@
$orders_to_check) {
82 my $original_order_content =
83 ( grep { $_->{str
}->{ordernumber
} eq $order_to_check->{ordernumber
} }
84 @
$original_orders_content )[0];
86 $t_missing_fields, $t_extra_fields,
87 $t_different_fields, $t_nbr_fields
89 = _check_fields_of_order
( $exp_fields, $original_order_content,
91 push @test_missing_fields, @
$t_missing_fields;
92 push @test_extra_fields, @
$t_extra_fields;
93 push @test_different_fields, @
$t_different_fields;
94 push @test_nbr_fields, $t_nbr_fields;
96 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
97 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
98 @test_different_fields =
99 keys %{ { map { $_ => 1 } @test_different_fields } };
101 \
@test_missing_fields, \
@test_extra_fields,
102 \
@test_different_fields, \
@test_nbr_fields
106 my $dbh = C4
::Context
->dbh;
107 $dbh->{AutoCommit
} = 0;
108 $dbh->{RaiseError
} = 1;
110 # Creating some orders
111 my $booksellerid = C4
::Bookseller
::AddBookseller
(
114 address1
=> "bookseller's address",
121 my $booksellerinfo = C4
::Bookseller
::GetBookSellerFromId
($booksellerid);
123 is
( $booksellerinfo->{deliverytime
},
124 5, 'set deliverytime when creating vendor (Bug 10556)' );
126 my ( $basket, $basketno );
128 $basketno = NewBasket
( $booksellerid, 1 ),
129 "NewBasket( $booksellerid , 1 ) returns $basketno"
131 ok
( $basket = GetBasket
($basketno), "GetBasket($basketno) returns $basket" );
133 my $budgetid = C4
::Budgets
::AddBudget
(
135 budget_code
=> "budget_code_test_getordersbybib",
136 budget_name
=> "budget_name_test_getordersbybib",
139 my $budget = C4
::Budgets
::GetBudget
($budgetid);
142 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio
( MARC
::Record
->new, '' );
143 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio
( MARC
::Record
->new, '' );
144 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio
( MARC
::Record
->new, '' );
145 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio
( MARC
::Record
->new, '' );
151 my ( $mandatoryparams, $return_error, $basketnum );
153 # returns undef and croaks if basketno, quantity, biblionumber or budget_id is missing
154 eval { ( $basketnum, $ordernumbers[0] ) = C4
::Acquisition
::NewOrder
() };
157 ( !( defined $basketnum || defined $ordernumbers[0] ) )
158 && ( defined $return_error ),
159 "NewOrder with no params returns undef and croaks"
163 basketno
=> $basketno,
165 biblionumber
=> $biblionumber1,
166 budget_id
=> $budget->{budget_id
},
168 my @mandatoryparams_keys = keys %$mandatoryparams;
169 foreach my $mandatoryparams_key (@mandatoryparams_keys) {
170 my %test_missing_mandatoryparams = %$mandatoryparams;
171 delete $test_missing_mandatoryparams{$mandatoryparams_key};
173 ( $basketnum, $ordernumbers[0] ) =
174 C4
::Acquisition
::NewOrder
( \
%test_missing_mandatoryparams );
177 my $expected_error = "Mandatory parameter $mandatoryparams_key missing";
179 ( !( defined $basketnum || defined $ordernumbers[0] ) )
180 && ( index( $return_error, $expected_error ) >= 0 ),
181 "NewOrder with no $mandatoryparams_key returns undef and croaks with expected error message"
185 # FIXME to do : test the other features of NewOrder
187 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
188 # Ex : a price of 50.1 will be stored internally as 5.100000
190 my @order_content = (
193 basketno
=> $basketno,
194 biblionumber
=> $biblionumber1,
195 budget_id
=> $budget->{budget_id
},
197 notes
=> "some notes",
201 listprice
=> 50.121111,
210 basketno
=> $basketno,
211 biblionumber
=> $biblionumber2,
212 budget_id
=> $budget->{budget_id
}
214 num
=> { quantity
=> 42 }
218 basketno
=> $basketno,
219 biblionumber
=> $biblionumber2,
220 budget_id
=> $budget->{budget_id
},
222 notes
=> "ordernotes"
237 basketno
=> $basketno,
238 biblionumber
=> $biblionumber3,
239 budget_id
=> $budget->{budget_id
},
240 notes
=> "ordernotes"
256 basketno
=> $basketno,
257 biblionumber
=> $biblionumber4,
258 budget_id
=> $budget->{budget_id
},
259 notes
=> "ordernotes"
275 # Create 4 orders in database
278 @ocontent{ keys %{ $order_content[$_]->{num
} } } =
279 values %{ $order_content[$_]->{num
} };
280 @ocontent{ keys %{ $order_content[$_]->{str
} } } =
281 values %{ $order_content[$_]->{str
} };
282 ( undef, $ordernumbers[$_] ) = C4
::Acquisition
::NewOrder
( \
%ocontent );
283 $order_content[$_]->{str
}->{ordernumber
} = $ordernumbers[$_];
286 # Test UT sub _check_fields_of_order
289 $test_missing_fields, $test_extra_fields,
290 $test_different_fields, $test_nbr_fields
292 = _check_fields_of_order
(
294 { str
=> { a
=> "bla", b
=> "105" }, num
=> { c
=> 15.12 } },
295 { a
=> "blabla", f
=> "f", b
=> "105", c
=> 15.1200, g
=> '' }
299 ( $test_nbr_fields == 5 )
300 and ( join( " ", sort @
$test_missing_fields ) eq 'd e' )
301 and ( join( " ", sort @
$test_extra_fields ) eq 'f g' )
302 and ( join( " ", @
$test_different_fields ) eq 'a' )
304 "_check_fields_of_order can check an order (test 1)"
307 $test_missing_fields, $test_extra_fields,
308 $test_different_fields, $test_nbr_fields
310 = _check_fields_of_order
(
312 { str
=> { a
=> "bla", b
=> "105" }, num
=> { c
=> 15.00 } },
313 { a
=> "bla", b
=> "105", c
=> 15 }
317 ( $test_nbr_fields == 3 )
318 and ( scalar @
$test_missing_fields == 0 )
319 and ( scalar @
$test_extra_fields == 0 )
320 and ( scalar @
$test_different_fields == 0 )
322 "_check_fields_of_order can check an order (test 2)"
325 $test_missing_fields, $test_extra_fields,
326 $test_different_fields, $test_nbr_fields
328 = _check_fields_of_order
(
330 { str
=> { a
=> "bla", b
=> "105" }, num
=> { c
=> 15.12 } },
331 { a
=> "blabla", b
=> "105", c
=> 15, d
=> "error" }
335 ( $test_nbr_fields == 4 )
336 and ( join( " ", sort @
$test_missing_fields ) eq 'e' )
337 and ( scalar @
$test_extra_fields == 0 )
338 and ( join( " ", @
$test_different_fields ) eq 'a c' )
340 "_check_fields_of_order can check an order (test 3)"
347 my @expectedfields = qw(
361 datecancellationprinted
401 estimateddeliverydate
408 $test_missing_fields, $test_extra_fields,
409 $test_different_fields, $test_nbr_fields
411 = _check_fields_of_order
( \
@expectedfields, $order_content[0],
412 GetOrder
( $ordernumbers[0] ) );
415 scalar @expectedfields,
416 "GetOrder gets an order with the right number of fields"
418 is
( join( " ", @
$test_missing_fields ),
419 '', "GetOrder gets an order with no missing fields" );
420 is
( join( " ", @
$test_extra_fields ),
421 '', "GetOrder gets an order with no unexpected fields" );
422 is
( join( " ", @
$test_different_fields ),
423 '', "GetOrder gets an order with the right content in every fields" );
429 my @base_expectedfields = qw(
469 editionresponsibility
516 datecancellationprinted
519 ( @base_expectedfields,
520 ( 'transferred_from_timestamp', 'transferred_from' ) );
521 is
( GetOrders
(), undef, "GetOrders with no params returns undef" );
522 DelOrder
( $order_content[3]->{str
}->{biblionumber
}, $ordernumbers[3] );
523 my @get_orders = GetOrders
($basketno);
525 $test_missing_fields, $test_extra_fields,
526 $test_different_fields, $test_nbr_fields
528 = _check_fields_of_orders
( \
@expectedfields, \
@order_content, \
@get_orders );
530 $$test_nbr_fields[0],
531 scalar @expectedfields,
532 "GetOrders gets orders with the right number of fields"
534 is
( join( " ", @
$test_missing_fields ),
535 '', "GetOrders gets orders with no missing fields" );
536 is
( join( " ", @
$test_extra_fields ),
537 '', "GetOrders gets orders with no unexpected fields" );
538 is
( join( " ", @
$test_different_fields ),
539 '', "GetOrders gets orders with the right content in every fields" );
542 ( scalar @get_orders == 4 )
543 and !grep ( $_->{ordernumber
} eq $ordernumbers[3], @get_orders )
545 "GetOrders only gets non-cancelled orders"
549 # Test GetCancelledOrders
553 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
554 is
( GetCancelledOrders
(), undef,
555 "GetCancelledOrders with no params returns undef" );
556 @get_orders = GetCancelledOrders
($basketno);
558 $test_missing_fields, $test_extra_fields,
559 $test_different_fields, $test_nbr_fields
561 = _check_fields_of_orders
( \
@expectedfields, \
@order_content, \
@get_orders );
563 $$test_nbr_fields[0],
564 scalar @expectedfields,
565 "GetCancelledOrders gets orders with the right number of fields"
567 is
( join( " ", @
$test_missing_fields ),
568 '', "GetCancelledOrders gets orders with no missing fields" );
569 is
( join( " ", @
$test_extra_fields ),
570 '', "GetCancelledOrders gets orders with no unexpected fields" );
571 is
( join( " ", @
$test_different_fields ),
573 "GetCancelledOrders gets orders with the right content in every fields" );
576 ( scalar @get_orders == 1 )
577 and grep ( $_->{ordernumber
} eq $ordernumbers[3], @get_orders )
579 "GetCancelledOrders only gets cancelled orders"
586 @expectedfields = qw
(
639 datecancellationprinted
645 # note that authorisedby was added to the return of SearchOrder by the
646 # patch for bug 11777
648 my $invoiceid = AddInvoice
(
649 invoicenumber
=> 'invoice',
650 booksellerid
=> $booksellerid,
654 my ( $datereceived, $new_ordernumber ) =
655 ModReceiveOrder
( $biblionumber4, $ordernumbers[4], 1, undef, 10, 10,
656 $invoiceid, 10, $order_content[4]->{str
}->{budget_id
} );
658 my $search_orders = SearchOrders
({
659 booksellerid
=> $booksellerid,
660 basketno
=> $basketno
662 isa_ok
( $search_orders, 'ARRAY' );
664 $test_missing_fields, $test_extra_fields,
665 $test_different_fields, $test_nbr_fields
667 = _check_fields_of_orders
( \
@expectedfields, \
@order_content,
670 $$test_nbr_fields[0],
671 scalar @expectedfields,
672 "SearchOrders gets orders with the right number of fields"
674 is
( join( " ", @
$test_missing_fields ),
675 '', "SearchOrders gets orders with no missing fields" );
676 is
( join( " ", @
$test_extra_fields ),
677 '', "SearchOrders gets orders with no unexpected fields" );
678 is
( join( " ", @
$test_different_fields ),
679 '', "SearchOrders gets orders with the right content in every fields" );
682 ( scalar @
$search_orders == 4 )
683 and !grep ( $_->{ordernumber
} eq $ordernumbers[3], @
$search_orders )
685 "SearchOrders only gets non-cancelled orders"
688 $search_orders = SearchOrders
({
689 booksellerid
=> $booksellerid,
690 basketno
=> $basketno,
695 ( scalar @
$search_orders == 3 ) and !grep ( (
696 ( $_->{ordernumber
} eq $ordernumbers[3] )
697 or ( $_->{ordernumber
} eq $ordernumbers[4] )
701 "SearchOrders with pending params gets only pending orders (bug 10723)"
704 $search_orders = SearchOrders
({
705 booksellerid
=> $booksellerid,
706 basketno
=> $basketno,
710 is
( scalar (@
$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
714 # Test GetBudgetByOrderNumber
716 ok
( GetBudgetByOrderNumber
( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
717 "GetBudgetByOrderNumber returns expected budget" );
723 @expectedfields = qw
(
736 estimateddeliverydate
744 my @lateorders = GetLateOrders
(0);
745 is
( scalar grep ( $_->{basketno
} eq $basketno, @lateorders ),
746 0, "GetLateOrders does not get orders from opened baskets" );
747 C4
::Acquisition
::CloseBasket
($basketno);
748 @lateorders = GetLateOrders
(0);
749 isnt
( scalar grep ( $_->{basketno
} eq $basketno, @lateorders ),
750 0, "GetLateOrders gets orders from closed baskets" );
751 ok
( !grep ( $_->{ordernumber
} eq $ordernumbers[3], @lateorders ),
752 "GetLateOrders does not gets cancelled orders" );
753 ok
( !grep ( $_->{ordernumber
} eq $ordernumbers[4], @lateorders ),
754 "GetLateOrders does not gets reveived orders" );
756 $test_missing_fields, $test_extra_fields,
757 $test_different_fields, $test_nbr_fields
759 = _check_fields_of_orders
( \
@expectedfields, \
@order_content, \
@lateorders );
761 $$test_nbr_fields[0],
762 scalar @expectedfields,
763 "GetLateOrders gets orders with the right number of fields"
765 is
( join( " ", @
$test_missing_fields ),
766 '', "GetLateOrders gets orders with no missing fields" );
767 is
( join( " ", @
$test_extra_fields ),
768 '', "GetLateOrders gets orders with no unexpected fields" );
769 is
( join( " ", @
$test_different_fields ),
770 '', "GetLateOrders gets orders with the right content in every fields" );
772 $search_orders = SearchOrders
({
773 booksellerid
=> $booksellerid,
774 basketno
=> $basketno,
778 is
( scalar (@
$search_orders), 3, "SearchOrders with pending and ordered params gets only pending ordered orders. After closing the basket, orders are marked as 'ordered' (bug 11170)" );
785 my $order = $lateorders[0];
786 AddClaim
( $order->{ordernumber
} );
787 my $neworder = GetOrder
( $order->{ordernumber
} );
789 $neworder->{claimed_date
},
790 strftime
( "%Y-%m-%d", localtime(time) ),
791 "AddClaim : Check claimed_date"
794 ( $datereceived, $new_ordernumber ) =
795 ModReceiveOrder
( $biblionumber2, $ordernumbers[1], 2, undef, 12, 12,
796 $invoiceid, 42, undef, undef, undef, "my notes");
797 my $order2 = GetOrder
( $ordernumbers[1] );
798 is
( $order2->{'quantityreceived'},
799 0, 'Splitting up order did not receive any on original order' );
800 is
( $order2->{'quantity'}, 40, '40 items on original order' );
801 is
( $order2->{'budget_id'}, $budgetid,
802 'Budget on original order is unchanged' );
803 is
( $order2->{notes
}, "my notes",
804 'ModReceiveOrder and GetOrder deal with notes' );
806 $neworder = GetOrder
($new_ordernumber);
807 is
( $neworder->{'quantity'}, 2, '2 items on new order' );
808 is
( $neworder->{'quantityreceived'},
809 2, 'Splitting up order received items on new order' );
810 is
( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
812 my $budgetid2 = C4
::Budgets
::AddBudget
(
814 budget_code
=> "budget_code_test_modrecv",
815 budget_name
=> "budget_name_test_modrecv",
819 ( $datereceived, $new_ordernumber ) =
820 ModReceiveOrder
( $biblionumber2, $ordernumbers[2], 2, undef, 12, 12,
821 $invoiceid, 42, $budgetid2, undef, undef, "my other notes" );
823 my $order3 = GetOrder
( $ordernumbers[2] );
824 is
( $order3->{'quantityreceived'},
825 0, 'Splitting up order did not receive any on original order' );
826 is
( $order3->{'quantity'}, 2, '2 items on original order' );
827 is
( $order3->{'budget_id'}, $budgetid,
828 'Budget on original order is unchanged' );
829 is
( $order3->{notes
}, "my other notes",
830 'ModReceiveOrder and GetOrder deal with notes' );
832 $neworder = GetOrder
($new_ordernumber);
833 is
( $neworder->{'quantity'}, 2, '2 items on new order' );
834 is
( $neworder->{'quantityreceived'},
835 2, 'Splitting up order received items on new order' );
836 is
( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
838 ( $datereceived, $new_ordernumber ) =
839 ModReceiveOrder
( $biblionumber2, $ordernumbers[2], 2, undef, 12, 12,
840 $invoiceid, 42, $budgetid2, undef, undef, "my third notes" );
842 $order3 = GetOrder
( $ordernumbers[2] );
843 is
( $order3->{'quantityreceived'}, 2, 'Order not split up' );
844 is
( $order3->{'quantity'}, 2, '2 items on order' );
845 is
( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
846 is
( $order3->{notes
}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
848 my $nonexistent_order = GetOrder
();
849 is
( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
850 $nonexistent_order = GetOrder
( 424242424242 );
851 is
( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );