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 => 79;
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 { $ordernumbers[0] = C4::Acquisition::NewOrder() };
157 ( ! 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};
174 C4::Acquisition::NewOrder( \%test_missing_mandatoryparams );
177 my $expected_error = "Cannot insert order: Mandatory parameter $mandatoryparams_key is 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 order_internalnote => "internal note",
198 order_vendornote => "vendor note",
203 listprice => 50.121111,
212 basketno => $basketno,
213 biblionumber => $biblionumber2,
214 budget_id => $budget->{budget_id}
216 num => { quantity => 42 }
220 basketno => $basketno,
221 biblionumber => $biblionumber2,
222 budget_id => $budget->{budget_id},
224 order_internalnote => "internal note",
225 order_vendornote => "vendor note"
240 basketno => $basketno,
241 biblionumber => $biblionumber3,
242 budget_id => $budget->{budget_id},
243 order_internalnote => "internal note",
244 order_vendornote => "vendor note"
260 basketno => $basketno,
261 biblionumber => $biblionumber4,
262 budget_id => $budget->{budget_id},
263 order_internalnote => "internal note",
264 order_vendornote => "vendor note"
280 # Create 4 orders in database
283 @ocontent{ keys %{ $order_content[$_]->{num} } } =
284 values %{ $order_content[$_]->{num} };
285 @ocontent{ keys %{ $order_content[$_]->{str} } } =
286 values %{ $order_content[$_]->{str} };
287 $ordernumbers[$_] = C4::Acquisition::NewOrder( \%ocontent );
288 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
291 # Test UT sub _check_fields_of_order
294 $test_missing_fields, $test_extra_fields,
295 $test_different_fields, $test_nbr_fields
297 = _check_fields_of_order(
299 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
300 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
304 ( $test_nbr_fields == 5 )
305 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
306 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
307 and ( join( " ", @$test_different_fields ) eq 'a' )
309 "_check_fields_of_order can check an order (test 1)"
312 $test_missing_fields, $test_extra_fields,
313 $test_different_fields, $test_nbr_fields
315 = _check_fields_of_order(
317 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
318 { a => "bla", b => "105", c => 15 }
322 ( $test_nbr_fields == 3 )
323 and ( scalar @$test_missing_fields == 0 )
324 and ( scalar @$test_extra_fields == 0 )
325 and ( scalar @$test_different_fields == 0 )
327 "_check_fields_of_order can check an order (test 2)"
330 $test_missing_fields, $test_extra_fields,
331 $test_different_fields, $test_nbr_fields
333 = _check_fields_of_order(
335 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
336 { a => "blabla", b => "105", c => 15, d => "error" }
340 ( $test_nbr_fields == 4 )
341 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
342 and ( scalar @$test_extra_fields == 0 )
343 and ( join( " ", @$test_different_fields ) eq 'a c' )
345 "_check_fields_of_order can check an order (test 3)"
352 my @expectedfields = qw(
368 datecancellationprinted
407 estimateddeliverydate
414 $test_missing_fields, $test_extra_fields,
415 $test_different_fields, $test_nbr_fields
417 = _check_fields_of_order( \@expectedfields, $order_content[0],
418 GetOrder( $ordernumbers[0] ) );
421 scalar @expectedfields,
422 "GetOrder gets an order with the right number of fields"
424 is( join( " ", @$test_missing_fields ),
425 '', "GetOrder gets an order with no missing fields" );
426 is( join( " ", @$test_extra_fields ),
427 '', "GetOrder gets an order with no unexpected fields" );
428 is( join( " ", @$test_different_fields ),
429 '', "GetOrder gets an order with the right content in every fields" );
435 my @base_expectedfields = qw(
478 editionresponsibility
524 datecancellationprinted
527 ( @base_expectedfields,
528 ( 'transferred_from_timestamp', 'transferred_from' ) );
529 is( GetOrders(), undef, "GetOrders with no params returns undef" );
530 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
531 my @get_orders = GetOrders($basketno);
533 $test_missing_fields, $test_extra_fields,
534 $test_different_fields, $test_nbr_fields
536 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
538 $$test_nbr_fields[0],
539 scalar @expectedfields,
540 "GetOrders gets orders with the right number of fields"
542 is( join( " ", @$test_missing_fields ),
543 '', "GetOrders gets orders with no missing fields" );
544 is( join( " ", @$test_extra_fields ),
545 '', "GetOrders gets orders with no unexpected fields" );
546 is( join( " ", @$test_different_fields ),
547 '', "GetOrders gets orders with the right content in every fields" );
550 ( scalar @get_orders == 4 )
551 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
553 "GetOrders only gets non-cancelled orders"
557 # Test GetCancelledOrders
561 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
562 is( GetCancelledOrders(), undef,
563 "GetCancelledOrders with no params returns undef" );
564 @get_orders = GetCancelledOrders($basketno);
566 $test_missing_fields, $test_extra_fields,
567 $test_different_fields, $test_nbr_fields
569 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
571 $$test_nbr_fields[0],
572 scalar @expectedfields,
573 "GetCancelledOrders gets orders with the right number of fields"
575 is( join( " ", @$test_missing_fields ),
576 '', "GetCancelledOrders gets orders with no missing fields" );
577 is( join( " ", @$test_extra_fields ),
578 '', "GetCancelledOrders gets orders with no unexpected fields" );
579 is( join( " ", @$test_different_fields ),
581 "GetCancelledOrders gets orders with the right content in every fields" );
584 ( scalar @get_orders == 1 )
585 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
587 "GetCancelledOrders only gets cancelled orders"
594 @expectedfields = qw (
649 datecancellationprinted
655 # note that authorisedby was added to the return of SearchOrder by the
656 # patch for bug 11777
658 my $invoiceid = AddInvoice(
659 invoicenumber => 'invoice',
660 booksellerid => $booksellerid,
664 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
666 biblionumber => $biblionumber4,
667 ordernumber => $ordernumbers[4],
668 quantityreceived => 1,
671 invoiceid => $invoiceid,
673 budget_id => $order_content[4]->{str}->{budget_id},
677 my $search_orders = SearchOrders({
678 booksellerid => $booksellerid,
679 basketno => $basketno
681 isa_ok( $search_orders, 'ARRAY' );
683 $test_missing_fields, $test_extra_fields,
684 $test_different_fields, $test_nbr_fields
686 = _check_fields_of_orders( \@expectedfields, \@order_content,
689 $$test_nbr_fields[0],
690 scalar @expectedfields,
691 "SearchOrders gets orders with the right number of fields"
693 is( join( " ", @$test_missing_fields ),
694 '', "SearchOrders gets orders with no missing fields" );
695 is( join( " ", @$test_extra_fields ),
696 '', "SearchOrders gets orders with no unexpected fields" );
697 is( join( " ", @$test_different_fields ),
698 '', "SearchOrders gets orders with the right content in every fields" );
701 ( scalar @$search_orders == 4 )
702 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
704 "SearchOrders only gets non-cancelled orders"
707 $search_orders = SearchOrders({
708 booksellerid => $booksellerid,
709 basketno => $basketno,
714 ( scalar @$search_orders == 3 ) and !grep ( (
715 ( $_->{ordernumber} eq $ordernumbers[3] )
716 or ( $_->{ordernumber} eq $ordernumbers[4] )
720 "SearchOrders with pending params gets only pending orders (bug 10723)"
723 $search_orders = SearchOrders({
724 booksellerid => $booksellerid,
725 basketno => $basketno,
729 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
731 $search_orders = SearchOrders({
732 ordernumber => $ordernumbers[4]
734 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
736 $search_orders = SearchOrders({
737 biblionumber => $biblionumber4
739 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
741 $search_orders = SearchOrders({
742 biblionumber => $biblionumber4,
745 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
748 # Test GetBudgetByOrderNumber
750 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
751 "GetBudgetByOrderNumber returns expected budget" );
757 @expectedfields = qw (
770 estimateddeliverydate
781 my @lateorders = GetLateOrders(0);
782 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
783 0, "GetLateOrders does not get orders from opened baskets" );
784 C4::Acquisition::CloseBasket($basketno);
785 @lateorders = GetLateOrders(0);
786 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
787 0, "GetLateOrders gets orders from closed baskets" );
788 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
789 "GetLateOrders does not gets cancelled orders" );
790 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
791 "GetLateOrders does not gets reveived orders" );
793 $test_missing_fields, $test_extra_fields,
794 $test_different_fields, $test_nbr_fields
796 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
798 $$test_nbr_fields[0],
799 scalar @expectedfields,
800 "GetLateOrders gets orders with the right number of fields"
802 is( join( " ", @$test_missing_fields ),
803 '', "GetLateOrders gets orders with no missing fields" );
804 is( join( " ", @$test_extra_fields ),
805 '', "GetLateOrders gets orders with no unexpected fields" );
806 is( join( " ", @$test_different_fields ),
807 '', "GetLateOrders gets orders with the right content in every fields" );
809 $search_orders = SearchOrders({
810 booksellerid => $booksellerid,
811 basketno => $basketno,
815 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)" );
821 my $order = $lateorders[0];
822 AddClaim( $order->{ordernumber} );
823 my $neworder = GetOrder( $order->{ordernumber} );
825 $neworder->{claimed_date},
826 strftime( "%Y-%m-%d", localtime(time) ),
827 "AddClaim : Check claimed_date"
830 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
832 biblionumber => $biblionumber2,
833 ordernumber => $ordernumbers[1],
834 quantityreceived => 2,
837 invoiceid => $invoiceid,
839 order_internalnote => "my notes",
840 order_vendornote => "my vendor notes",
843 my $order2 = GetOrder( $ordernumbers[1] );
844 is( $order2->{'quantityreceived'},
845 0, 'Splitting up order did not receive any on original order' );
846 is( $order2->{'quantity'}, 40, '40 items on original order' );
847 is( $order2->{'budget_id'}, $budgetid,
848 'Budget on original order is unchanged' );
849 is( $order2->{order_internalnote}, "my notes",
850 'ModReceiveOrder and GetOrder deal with internal notes' );
851 is( $order2->{order_vendornote}, "my vendor notes",
852 'ModReceiveOrder and GetOrder deal with vendor notes' );
854 $neworder = GetOrder($new_ordernumber);
855 is( $neworder->{'quantity'}, 2, '2 items on new order' );
856 is( $neworder->{'quantityreceived'},
857 2, 'Splitting up order received items on new order' );
858 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
860 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
861 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
863 my ( $orders ) = GetHistory( ordernumber => $ordernumbers[1] );
864 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
865 ( $orders ) = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
866 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
868 my $budgetid2 = C4::Budgets::AddBudget(
870 budget_code => "budget_code_test_modrecv",
871 budget_name => "budget_name_test_modrecv",
875 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
877 biblionumber => $biblionumber2,
878 ordernumber => $ordernumbers[2],
879 quantityreceived => 2,
882 invoiceid => $invoiceid,
884 budget_id => $budgetid2,
885 order_internalnote => "my other notes",
889 my $order3 = GetOrder( $ordernumbers[2] );
890 is( $order3->{'quantityreceived'},
891 0, 'Splitting up order did not receive any on original order' );
892 is( $order3->{'quantity'}, 2, '2 items on original order' );
893 is( $order3->{'budget_id'}, $budgetid,
894 'Budget on original order is unchanged' );
895 is( $order3->{order_internalnote}, "my other notes",
896 'ModReceiveOrder and GetOrder deal with notes' );
898 $neworder = GetOrder($new_ordernumber);
899 is( $neworder->{'quantity'}, 2, '2 items on new order' );
900 is( $neworder->{'quantityreceived'},
901 2, 'Splitting up order received items on new order' );
902 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
904 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
906 biblionumber => $biblionumber2,
907 ordernumber => $ordernumbers[2],
908 quantityreceived => 2,
911 invoiceid => $invoiceid,
913 budget_id => $budgetid2,
914 order_internalnote => "my third notes",
918 $order3 = GetOrder( $ordernumbers[2] );
919 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
920 is( $order3->{'quantity'}, 2, '2 items on order' );
921 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
922 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
924 my $nonexistent_order = GetOrder();
925 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
926 $nonexistent_order = GetOrder( 424242424242 );
927 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );