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 => 74;
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, '' );
147 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
148 # Ex : a price of 50.1 will be stored internally as 5.100000
150 my @order_content = (
153 basketno => $basketno,
154 biblionumber => $biblionumber1,
155 budget_id => $budget->{budget_id},
157 order_internalnote => "internal note",
158 order_vendornote => "vendor note",
163 listprice => 50.121111,
172 basketno => $basketno,
173 biblionumber => $biblionumber2,
174 budget_id => $budget->{budget_id}
176 num => { quantity => 42 }
180 basketno => $basketno,
181 biblionumber => $biblionumber2,
182 budget_id => $budget->{budget_id},
184 order_internalnote => "internal note",
185 order_vendornote => "vendor note"
200 basketno => $basketno,
201 biblionumber => $biblionumber3,
202 budget_id => $budget->{budget_id},
203 order_internalnote => "internal note",
204 order_vendornote => "vendor note"
220 basketno => $basketno,
221 biblionumber => $biblionumber4,
222 budget_id => $budget->{budget_id},
223 order_internalnote => "internal note",
224 order_vendornote => "vendor note"
240 # Create 4 orders in database
243 @ocontent{ keys %{ $order_content[$_]->{num} } } =
244 values %{ $order_content[$_]->{num} };
245 @ocontent{ keys %{ $order_content[$_]->{str} } } =
246 values %{ $order_content[$_]->{str} };
247 $ordernumbers[$_] = C4::Acquisition::NewOrder( \%ocontent );
248 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
251 # Test UT sub _check_fields_of_order
254 $test_missing_fields, $test_extra_fields,
255 $test_different_fields, $test_nbr_fields
257 = _check_fields_of_order(
259 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
260 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
264 ( $test_nbr_fields == 5 )
265 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
266 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
267 and ( join( " ", @$test_different_fields ) eq 'a' )
269 "_check_fields_of_order can check an order (test 1)"
272 $test_missing_fields, $test_extra_fields,
273 $test_different_fields, $test_nbr_fields
275 = _check_fields_of_order(
277 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
278 { a => "bla", b => "105", c => 15 }
282 ( $test_nbr_fields == 3 )
283 and ( scalar @$test_missing_fields == 0 )
284 and ( scalar @$test_extra_fields == 0 )
285 and ( scalar @$test_different_fields == 0 )
287 "_check_fields_of_order can check an order (test 2)"
290 $test_missing_fields, $test_extra_fields,
291 $test_different_fields, $test_nbr_fields
293 = _check_fields_of_order(
295 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
296 { a => "blabla", b => "105", c => 15, d => "error" }
300 ( $test_nbr_fields == 4 )
301 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
302 and ( scalar @$test_extra_fields == 0 )
303 and ( join( " ", @$test_different_fields ) eq 'a c' )
305 "_check_fields_of_order can check an order (test 3)"
312 my @expectedfields = qw(
328 datecancellationprinted
367 estimateddeliverydate
374 $test_missing_fields, $test_extra_fields,
375 $test_different_fields, $test_nbr_fields
377 = _check_fields_of_order( \@expectedfields, $order_content[0],
378 GetOrder( $ordernumbers[0] ) );
381 scalar @expectedfields,
382 "GetOrder gets an order with the right number of fields"
384 is( join( " ", @$test_missing_fields ),
385 '', "GetOrder gets an order with no missing fields" );
386 is( join( " ", @$test_extra_fields ),
387 '', "GetOrder gets an order with no unexpected fields" );
388 is( join( " ", @$test_different_fields ),
389 '', "GetOrder gets an order with the right content in every fields" );
395 my @base_expectedfields = qw(
438 editionresponsibility
484 datecancellationprinted
487 ( @base_expectedfields,
488 ( 'transferred_from_timestamp', 'transferred_from' ) );
489 is( GetOrders(), undef, "GetOrders with no params returns undef" );
490 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
491 my @get_orders = GetOrders($basketno);
493 $test_missing_fields, $test_extra_fields,
494 $test_different_fields, $test_nbr_fields
496 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
498 $$test_nbr_fields[0],
499 scalar @expectedfields,
500 "GetOrders gets orders with the right number of fields"
502 is( join( " ", @$test_missing_fields ),
503 '', "GetOrders gets orders with no missing fields" );
504 is( join( " ", @$test_extra_fields ),
505 '', "GetOrders gets orders with no unexpected fields" );
506 is( join( " ", @$test_different_fields ),
507 '', "GetOrders gets orders with the right content in every fields" );
510 ( scalar @get_orders == 4 )
511 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
513 "GetOrders only gets non-cancelled orders"
517 # Test GetCancelledOrders
521 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
522 is( GetCancelledOrders(), undef,
523 "GetCancelledOrders with no params returns undef" );
524 @get_orders = GetCancelledOrders($basketno);
526 $test_missing_fields, $test_extra_fields,
527 $test_different_fields, $test_nbr_fields
529 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
531 $$test_nbr_fields[0],
532 scalar @expectedfields,
533 "GetCancelledOrders gets orders with the right number of fields"
535 is( join( " ", @$test_missing_fields ),
536 '', "GetCancelledOrders gets orders with no missing fields" );
537 is( join( " ", @$test_extra_fields ),
538 '', "GetCancelledOrders gets orders with no unexpected fields" );
539 is( join( " ", @$test_different_fields ),
541 "GetCancelledOrders gets orders with the right content in every fields" );
544 ( scalar @get_orders == 1 )
545 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
547 "GetCancelledOrders only gets cancelled orders"
554 @expectedfields = qw (
609 datecancellationprinted
615 # note that authorisedby was added to the return of SearchOrder by the
616 # patch for bug 11777
618 my $invoiceid = AddInvoice(
619 invoicenumber => 'invoice',
620 booksellerid => $booksellerid,
624 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
626 biblionumber => $biblionumber4,
627 ordernumber => $ordernumbers[4],
628 quantityreceived => 1,
631 invoiceid => $invoiceid,
633 budget_id => $order_content[4]->{str}->{budget_id},
637 my $search_orders = SearchOrders({
638 booksellerid => $booksellerid,
639 basketno => $basketno
641 isa_ok( $search_orders, 'ARRAY' );
643 $test_missing_fields, $test_extra_fields,
644 $test_different_fields, $test_nbr_fields
646 = _check_fields_of_orders( \@expectedfields, \@order_content,
649 $$test_nbr_fields[0],
650 scalar @expectedfields,
651 "SearchOrders gets orders with the right number of fields"
653 is( join( " ", @$test_missing_fields ),
654 '', "SearchOrders gets orders with no missing fields" );
655 is( join( " ", @$test_extra_fields ),
656 '', "SearchOrders gets orders with no unexpected fields" );
657 is( join( " ", @$test_different_fields ),
658 '', "SearchOrders gets orders with the right content in every fields" );
661 ( scalar @$search_orders == 4 )
662 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
664 "SearchOrders only gets non-cancelled orders"
667 $search_orders = SearchOrders({
668 booksellerid => $booksellerid,
669 basketno => $basketno,
674 ( scalar @$search_orders == 3 ) and !grep ( (
675 ( $_->{ordernumber} eq $ordernumbers[3] )
676 or ( $_->{ordernumber} eq $ordernumbers[4] )
680 "SearchOrders with pending params gets only pending orders (bug 10723)"
683 $search_orders = SearchOrders({
684 booksellerid => $booksellerid,
685 basketno => $basketno,
689 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
691 $search_orders = SearchOrders({
692 ordernumber => $ordernumbers[4]
694 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
696 $search_orders = SearchOrders({
697 biblionumber => $biblionumber4
699 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
701 $search_orders = SearchOrders({
702 biblionumber => $biblionumber4,
705 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
708 # Test GetBudgetByOrderNumber
710 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
711 "GetBudgetByOrderNumber returns expected budget" );
717 @expectedfields = qw (
730 estimateddeliverydate
741 my @lateorders = GetLateOrders(0);
742 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
743 0, "GetLateOrders does not get orders from opened baskets" );
744 C4::Acquisition::CloseBasket($basketno);
745 @lateorders = GetLateOrders(0);
746 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
747 0, "GetLateOrders gets orders from closed baskets" );
748 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
749 "GetLateOrders does not gets cancelled orders" );
750 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
751 "GetLateOrders does not gets reveived orders" );
753 $test_missing_fields, $test_extra_fields,
754 $test_different_fields, $test_nbr_fields
756 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
758 $$test_nbr_fields[0],
759 scalar @expectedfields,
760 "GetLateOrders gets orders with the right number of fields"
762 is( join( " ", @$test_missing_fields ),
763 '', "GetLateOrders gets orders with no missing fields" );
764 is( join( " ", @$test_extra_fields ),
765 '', "GetLateOrders gets orders with no unexpected fields" );
766 is( join( " ", @$test_different_fields ),
767 '', "GetLateOrders gets orders with the right content in every fields" );
769 $search_orders = SearchOrders({
770 booksellerid => $booksellerid,
771 basketno => $basketno,
775 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)" );
781 my $order = $lateorders[0];
782 AddClaim( $order->{ordernumber} );
783 my $neworder = GetOrder( $order->{ordernumber} );
785 $neworder->{claimed_date},
786 strftime( "%Y-%m-%d", localtime(time) ),
787 "AddClaim : Check claimed_date"
790 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
792 biblionumber => $biblionumber2,
793 ordernumber => $ordernumbers[1],
794 quantityreceived => 2,
797 invoiceid => $invoiceid,
799 order_internalnote => "my notes",
800 order_vendornote => "my vendor notes",
803 my $order2 = GetOrder( $ordernumbers[1] );
804 is( $order2->{'quantityreceived'},
805 0, 'Splitting up order did not receive any on original order' );
806 is( $order2->{'quantity'}, 40, '40 items on original order' );
807 is( $order2->{'budget_id'}, $budgetid,
808 'Budget on original order is unchanged' );
809 is( $order2->{order_internalnote}, "my notes",
810 'ModReceiveOrder and GetOrder deal with internal notes' );
811 is( $order2->{order_vendornote}, "my vendor notes",
812 'ModReceiveOrder and GetOrder deal with vendor notes' );
814 $neworder = GetOrder($new_ordernumber);
815 is( $neworder->{'quantity'}, 2, '2 items on new order' );
816 is( $neworder->{'quantityreceived'},
817 2, 'Splitting up order received items on new order' );
818 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
820 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
821 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
823 my ( $orders ) = GetHistory( ordernumber => $ordernumbers[1] );
824 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
825 ( $orders ) = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
826 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
828 my $budgetid2 = C4::Budgets::AddBudget(
830 budget_code => "budget_code_test_modrecv",
831 budget_name => "budget_name_test_modrecv",
835 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
837 biblionumber => $biblionumber2,
838 ordernumber => $ordernumbers[2],
839 quantityreceived => 2,
842 invoiceid => $invoiceid,
844 budget_id => $budgetid2,
845 order_internalnote => "my other notes",
849 my $order3 = GetOrder( $ordernumbers[2] );
850 is( $order3->{'quantityreceived'},
851 0, 'Splitting up order did not receive any on original order' );
852 is( $order3->{'quantity'}, 2, '2 items on original order' );
853 is( $order3->{'budget_id'}, $budgetid,
854 'Budget on original order is unchanged' );
855 is( $order3->{order_internalnote}, "my other notes",
856 'ModReceiveOrder and GetOrder deal with notes' );
858 $neworder = GetOrder($new_ordernumber);
859 is( $neworder->{'quantity'}, 2, '2 items on new order' );
860 is( $neworder->{'quantityreceived'},
861 2, 'Splitting up order received items on new order' );
862 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
864 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
866 biblionumber => $biblionumber2,
867 ordernumber => $ordernumbers[2],
868 quantityreceived => 2,
871 invoiceid => $invoiceid,
873 budget_id => $budgetid2,
874 order_internalnote => "my third notes",
878 $order3 = GetOrder( $ordernumbers[2] );
879 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
880 is( $order3->{'quantity'}, 2, '2 items on order' );
881 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
882 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
884 my $nonexistent_order = GetOrder();
885 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
886 $nonexistent_order = GetOrder( 424242424242 );
887 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );