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 { ( $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 order_internalnote => "internal note",
198 order_vendornote => "vendor note"
202 listprice => 50.121111,
211 basketno => $basketno,
212 biblionumber => $biblionumber2,
213 budget_id => $budget->{budget_id}
215 num => { quantity => 42 }
219 basketno => $basketno,
220 biblionumber => $biblionumber2,
221 budget_id => $budget->{budget_id},
223 order_internalnote => "internal note",
224 order_vendornote => "vendor note"
239 basketno => $basketno,
240 biblionumber => $biblionumber3,
241 budget_id => $budget->{budget_id},
242 order_internalnote => "internal note",
243 order_vendornote => "vendor note"
259 basketno => $basketno,
260 biblionumber => $biblionumber4,
261 budget_id => $budget->{budget_id},
262 order_internalnote => "internal note",
263 order_vendornote => "vendor note"
279 # Create 4 orders in database
282 @ocontent{ keys %{ $order_content[$_]->{num} } } =
283 values %{ $order_content[$_]->{num} };
284 @ocontent{ keys %{ $order_content[$_]->{str} } } =
285 values %{ $order_content[$_]->{str} };
286 ( undef, $ordernumbers[$_] ) = C4::Acquisition::NewOrder( \%ocontent );
287 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
290 # Test UT sub _check_fields_of_order
293 $test_missing_fields, $test_extra_fields,
294 $test_different_fields, $test_nbr_fields
296 = _check_fields_of_order(
298 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
299 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
303 ( $test_nbr_fields == 5 )
304 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
305 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
306 and ( join( " ", @$test_different_fields ) eq 'a' )
308 "_check_fields_of_order can check an order (test 1)"
311 $test_missing_fields, $test_extra_fields,
312 $test_different_fields, $test_nbr_fields
314 = _check_fields_of_order(
316 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
317 { a => "bla", b => "105", c => 15 }
321 ( $test_nbr_fields == 3 )
322 and ( scalar @$test_missing_fields == 0 )
323 and ( scalar @$test_extra_fields == 0 )
324 and ( scalar @$test_different_fields == 0 )
326 "_check_fields_of_order can check an order (test 2)"
329 $test_missing_fields, $test_extra_fields,
330 $test_different_fields, $test_nbr_fields
332 = _check_fields_of_order(
334 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
335 { a => "blabla", b => "105", c => 15, d => "error" }
339 ( $test_nbr_fields == 4 )
340 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
341 and ( scalar @$test_extra_fields == 0 )
342 and ( join( " ", @$test_different_fields ) eq 'a c' )
344 "_check_fields_of_order can check an order (test 3)"
351 my @expectedfields = qw(
367 datecancellationprinted
406 estimateddeliverydate
413 $test_missing_fields, $test_extra_fields,
414 $test_different_fields, $test_nbr_fields
416 = _check_fields_of_order( \@expectedfields, $order_content[0],
417 GetOrder( $ordernumbers[0] ) );
420 scalar @expectedfields,
421 "GetOrder gets an order with the right number of fields"
423 is( join( " ", @$test_missing_fields ),
424 '', "GetOrder gets an order with no missing fields" );
425 is( join( " ", @$test_extra_fields ),
426 '', "GetOrder gets an order with no unexpected fields" );
427 is( join( " ", @$test_different_fields ),
428 '', "GetOrder gets an order with the right content in every fields" );
434 my @base_expectedfields = qw(
477 editionresponsibility
523 datecancellationprinted
526 ( @base_expectedfields,
527 ( 'transferred_from_timestamp', 'transferred_from' ) );
528 is( GetOrders(), undef, "GetOrders with no params returns undef" );
529 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
530 my @get_orders = GetOrders($basketno);
532 $test_missing_fields, $test_extra_fields,
533 $test_different_fields, $test_nbr_fields
535 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
537 $$test_nbr_fields[0],
538 scalar @expectedfields,
539 "GetOrders gets orders with the right number of fields"
541 is( join( " ", @$test_missing_fields ),
542 '', "GetOrders gets orders with no missing fields" );
543 is( join( " ", @$test_extra_fields ),
544 '', "GetOrders gets orders with no unexpected fields" );
545 is( join( " ", @$test_different_fields ),
546 '', "GetOrders gets orders with the right content in every fields" );
549 ( scalar @get_orders == 4 )
550 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
552 "GetOrders only gets non-cancelled orders"
556 # Test GetCancelledOrders
560 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
561 is( GetCancelledOrders(), undef,
562 "GetCancelledOrders with no params returns undef" );
563 @get_orders = GetCancelledOrders($basketno);
565 $test_missing_fields, $test_extra_fields,
566 $test_different_fields, $test_nbr_fields
568 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
570 $$test_nbr_fields[0],
571 scalar @expectedfields,
572 "GetCancelledOrders gets orders with the right number of fields"
574 is( join( " ", @$test_missing_fields ),
575 '', "GetCancelledOrders gets orders with no missing fields" );
576 is( join( " ", @$test_extra_fields ),
577 '', "GetCancelledOrders gets orders with no unexpected fields" );
578 is( join( " ", @$test_different_fields ),
580 "GetCancelledOrders gets orders with the right content in every fields" );
583 ( scalar @get_orders == 1 )
584 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
586 "GetCancelledOrders only gets cancelled orders"
593 @expectedfields = qw (
648 datecancellationprinted
654 # note that authorisedby was added to the return of SearchOrder by the
655 # patch for bug 11777
657 my $invoiceid = AddInvoice(
658 invoicenumber => 'invoice',
659 booksellerid => $booksellerid,
663 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
665 biblionumber => $biblionumber4,
666 ordernumber => $ordernumbers[4],
667 quantityreceived => 1,
670 invoiceid => $invoiceid,
672 budget_id => $order_content[4]->{str}->{budget_id},
676 my $search_orders = SearchOrders({
677 booksellerid => $booksellerid,
678 basketno => $basketno
680 isa_ok( $search_orders, 'ARRAY' );
682 $test_missing_fields, $test_extra_fields,
683 $test_different_fields, $test_nbr_fields
685 = _check_fields_of_orders( \@expectedfields, \@order_content,
688 $$test_nbr_fields[0],
689 scalar @expectedfields,
690 "SearchOrders gets orders with the right number of fields"
692 is( join( " ", @$test_missing_fields ),
693 '', "SearchOrders gets orders with no missing fields" );
694 is( join( " ", @$test_extra_fields ),
695 '', "SearchOrders gets orders with no unexpected fields" );
696 is( join( " ", @$test_different_fields ),
697 '', "SearchOrders gets orders with the right content in every fields" );
700 ( scalar @$search_orders == 4 )
701 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
703 "SearchOrders only gets non-cancelled orders"
706 $search_orders = SearchOrders({
707 booksellerid => $booksellerid,
708 basketno => $basketno,
713 ( scalar @$search_orders == 3 ) and !grep ( (
714 ( $_->{ordernumber} eq $ordernumbers[3] )
715 or ( $_->{ordernumber} eq $ordernumbers[4] )
719 "SearchOrders with pending params gets only pending orders (bug 10723)"
722 $search_orders = SearchOrders({
723 booksellerid => $booksellerid,
724 basketno => $basketno,
728 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
730 $search_orders = SearchOrders({
731 ordernumber => $ordernumbers[4]
733 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
735 $search_orders = SearchOrders({
736 biblionumber => $biblionumber4
738 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
740 $search_orders = SearchOrders({
741 biblionumber => $biblionumber4,
744 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
747 # Test GetBudgetByOrderNumber
749 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
750 "GetBudgetByOrderNumber returns expected budget" );
756 @expectedfields = qw (
769 estimateddeliverydate
780 my @lateorders = GetLateOrders(0);
781 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
782 0, "GetLateOrders does not get orders from opened baskets" );
783 C4::Acquisition::CloseBasket($basketno);
784 @lateorders = GetLateOrders(0);
785 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
786 0, "GetLateOrders gets orders from closed baskets" );
787 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
788 "GetLateOrders does not gets cancelled orders" );
789 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
790 "GetLateOrders does not gets reveived orders" );
792 $test_missing_fields, $test_extra_fields,
793 $test_different_fields, $test_nbr_fields
795 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
797 $$test_nbr_fields[0],
798 scalar @expectedfields,
799 "GetLateOrders gets orders with the right number of fields"
801 is( join( " ", @$test_missing_fields ),
802 '', "GetLateOrders gets orders with no missing fields" );
803 is( join( " ", @$test_extra_fields ),
804 '', "GetLateOrders gets orders with no unexpected fields" );
805 is( join( " ", @$test_different_fields ),
806 '', "GetLateOrders gets orders with the right content in every fields" );
808 $search_orders = SearchOrders({
809 booksellerid => $booksellerid,
810 basketno => $basketno,
814 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)" );
820 my $order = $lateorders[0];
821 AddClaim( $order->{ordernumber} );
822 my $neworder = GetOrder( $order->{ordernumber} );
824 $neworder->{claimed_date},
825 strftime( "%Y-%m-%d", localtime(time) ),
826 "AddClaim : Check claimed_date"
829 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
831 biblionumber => $biblionumber2,
832 ordernumber => $ordernumbers[1],
833 quantityreceived => 2,
836 invoiceid => $invoiceid,
838 order_internalnote => "my notes",
839 order_vendornote => "my vendor notes",
842 my $order2 = GetOrder( $ordernumbers[1] );
843 is( $order2->{'quantityreceived'},
844 0, 'Splitting up order did not receive any on original order' );
845 is( $order2->{'quantity'}, 40, '40 items on original order' );
846 is( $order2->{'budget_id'}, $budgetid,
847 'Budget on original order is unchanged' );
848 is( $order2->{order_internalnote}, "my notes",
849 'ModReceiveOrder and GetOrder deal with internal notes' );
850 is( $order2->{order_vendornote}, "my vendor notes",
851 'ModReceiveOrder and GetOrder deal with vendor notes' );
853 $neworder = GetOrder($new_ordernumber);
854 is( $neworder->{'quantity'}, 2, '2 items on new order' );
855 is( $neworder->{'quantityreceived'},
856 2, 'Splitting up order received items on new order' );
857 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
859 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
860 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
862 my ( $orders ) = GetHistory( ordernumber => $ordernumbers[1] );
863 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
864 ( $orders ) = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
865 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
867 my $budgetid2 = C4::Budgets::AddBudget(
869 budget_code => "budget_code_test_modrecv",
870 budget_name => "budget_name_test_modrecv",
874 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
876 biblionumber => $biblionumber2,
877 ordernumber => $ordernumbers[2],
878 quantityreceived => 2,
881 invoiceid => $invoiceid,
883 budget_id => $budgetid2,
884 order_internalnote => "my other notes",
888 my $order3 = GetOrder( $ordernumbers[2] );
889 is( $order3->{'quantityreceived'},
890 0, 'Splitting up order did not receive any on original order' );
891 is( $order3->{'quantity'}, 2, '2 items on original order' );
892 is( $order3->{'budget_id'}, $budgetid,
893 'Budget on original order is unchanged' );
894 is( $order3->{order_internalnote}, "my other notes",
895 'ModReceiveOrder and GetOrder deal with notes' );
897 $neworder = GetOrder($new_ordernumber);
898 is( $neworder->{'quantity'}, 2, '2 items on new order' );
899 is( $neworder->{'quantityreceived'},
900 2, 'Splitting up order received items on new order' );
901 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
903 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
905 biblionumber => $biblionumber2,
906 ordernumber => $ordernumbers[2],
907 quantityreceived => 2,
910 invoiceid => $invoiceid,
912 budget_id => $budgetid2,
913 order_internalnote => "my third notes",
917 $order3 = GetOrder( $ordernumbers[2] );
918 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
919 is( $order3->{'quantity'}, 2, '2 items on order' );
920 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
921 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
923 my $nonexistent_order = GetOrder();
924 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
925 $nonexistent_order = GetOrder( 424242424242 );
926 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );