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 => 90;
14 use_ok('C4::Acquisition');
15 use_ok('C4::Bookseller');
17 use_ok('C4::Budgets');
18 use_ok('C4::Bookseller');
19 use_ok('Koha::Acquisition::Order');
22 # Sub used for testing C4::Acquisition subs returning order(s):
23 # GetOrdersByStatus, GetOrders, GetDeletedOrders, GetOrder etc.
24 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,$test_nbr_fields) =
25 # _check_fields_of_order ($exp_fields, $original_order_content, $order_to_check);
27 # $exp_fields : arrayref whose elements are the keys we expect to find
28 # $original_order_content : hashref whose 2 keys str and num contains hashrefs
29 # containing content fields of the order created with Koha::Acquisition::Order
30 # $order_to_check : hashref whose keys/values are the content of an order
31 # returned by the C4::Acquisition sub we are testing
33 # \@test_missing_fields : arrayref void if ok ; otherwise contains the list of
34 # fields missing in $order_to_check
35 # \@test_extra_fields : arrayref void if ok ; otherwise contains the list of
36 # fields unexpected in $order_to_check
37 # \@test_different_fields : arrayref void if ok ; otherwise contains the list of
38 # fields which value is not the same in between $order_to_check and
39 # $test_nbr_fields : contains the number of fields of $order_to_check
41 sub _check_fields_of_order {
42 my ( $exp_fields, $original_order_content, $order_to_check ) = @_;
43 my @test_missing_fields = ();
44 my @test_extra_fields = ();
45 my @test_different_fields = ();
46 my $test_nbr_fields = scalar( keys %$order_to_check );
47 foreach my $field (@$exp_fields) {
48 push @test_missing_fields, $field
49 unless exists( $order_to_check->{$field} );
51 foreach my $field ( keys %$order_to_check ) {
52 push @test_extra_fields, $field
53 unless grep ( /^$field$/, @$exp_fields );
55 foreach my $field ( keys %{ $original_order_content->{str} } ) {
56 push @test_different_fields, $field
57 unless ( !exists $order_to_check->{$field} )
58 or ( $original_order_content->{str}->{$field} eq
59 $order_to_check->{$field} );
61 foreach my $field ( keys %{ $original_order_content->{num} } ) {
62 push @test_different_fields, $field
63 unless ( !exists $order_to_check->{$field} )
64 or ( $original_order_content->{num}->{$field} ==
65 $order_to_check->{$field} );
68 \@test_missing_fields, \@test_extra_fields,
69 \@test_different_fields, $test_nbr_fields
73 # Sub used for testing C4::Acquisition subs returning several orders
74 # (\@test_missing_fields,\@test_extra_fields,\@test_different_fields,\@test_nbr_fields) =
75 # _check_fields_of_orders ($exp_fields, $original_orders_content, $orders_to_check)
76 sub _check_fields_of_orders {
77 my ( $exp_fields, $original_orders_content, $orders_to_check ) = @_;
78 my @test_missing_fields = ();
79 my @test_extra_fields = ();
80 my @test_different_fields = ();
81 my @test_nbr_fields = ();
82 foreach my $order_to_check (@$orders_to_check) {
83 my $original_order_content =
84 ( grep { $_->{str}->{ordernumber} eq $order_to_check->{ordernumber} }
85 @$original_orders_content )[0];
87 $t_missing_fields, $t_extra_fields,
88 $t_different_fields, $t_nbr_fields
90 = _check_fields_of_order( $exp_fields, $original_order_content,
92 push @test_missing_fields, @$t_missing_fields;
93 push @test_extra_fields, @$t_extra_fields;
94 push @test_different_fields, @$t_different_fields;
95 push @test_nbr_fields, $t_nbr_fields;
97 @test_missing_fields = keys %{ { map { $_ => 1 } @test_missing_fields } };
98 @test_extra_fields = keys %{ { map { $_ => 1 } @test_extra_fields } };
99 @test_different_fields =
100 keys %{ { map { $_ => 1 } @test_different_fields } };
102 \@test_missing_fields, \@test_extra_fields,
103 \@test_different_fields, \@test_nbr_fields
107 my $dbh = C4::Context->dbh;
108 $dbh->{AutoCommit} = 0;
109 $dbh->{RaiseError} = 1;
111 # Creating some orders
112 my $booksellerid = C4::Bookseller::AddBookseller(
115 address1 => "bookseller's address",
122 my $booksellerinfo = C4::Bookseller::GetBookSellerFromId($booksellerid);
124 is( $booksellerinfo->{deliverytime},
125 5, 'set deliverytime when creating vendor (Bug 10556)' );
127 my ( $basket, $basketno );
129 $basketno = NewBasket( $booksellerid, 1 ),
130 "NewBasket( $booksellerid , 1 ) returns $basketno"
132 ok( $basket = GetBasket($basketno), "GetBasket($basketno) returns $basket" );
134 my $budgetid = C4::Budgets::AddBudget(
136 budget_code => "budget_code_test_getordersbybib",
137 budget_name => "budget_name_test_getordersbybib",
140 my $budget = C4::Budgets::GetBudget($budgetid);
143 my ( $biblionumber1, $biblioitemnumber1 ) = AddBiblio( MARC::Record->new, '' );
144 my ( $biblionumber2, $biblioitemnumber2 ) = AddBiblio( MARC::Record->new, '' );
145 my ( $biblionumber3, $biblioitemnumber3 ) = AddBiblio( MARC::Record->new, '' );
146 my ( $biblionumber4, $biblioitemnumber4 ) = AddBiblio( MARC::Record->new, '' );
148 # Prepare 5 orders, and make distinction beween fields to be tested with eq and with ==
149 # Ex : a price of 50.1 will be stored internally as 5.100000
151 my @order_content = (
154 basketno => $basketno,
155 biblionumber => $biblionumber1,
156 budget_id => $budget->{budget_id},
158 order_internalnote => "internal note",
159 order_vendornote => "vendor note",
164 listprice => 50.121111,
173 basketno => $basketno,
174 biblionumber => $biblionumber2,
175 budget_id => $budget->{budget_id}
177 num => { quantity => 42 }
181 basketno => $basketno,
182 biblionumber => $biblionumber2,
183 budget_id => $budget->{budget_id},
185 order_internalnote => "internal note",
186 order_vendornote => "vendor note"
201 basketno => $basketno,
202 biblionumber => $biblionumber3,
203 budget_id => $budget->{budget_id},
204 order_internalnote => "internal note",
205 order_vendornote => "vendor note"
221 basketno => $basketno,
222 biblionumber => $biblionumber4,
223 budget_id => $budget->{budget_id},
224 order_internalnote => "internal note",
225 order_vendornote => "vendor note"
241 # Create 4 orders in database
244 @ocontent{ keys %{ $order_content[$_]->{num} } } =
245 values %{ $order_content[$_]->{num} };
246 @ocontent{ keys %{ $order_content[$_]->{str} } } =
247 values %{ $order_content[$_]->{str} };
248 $ordernumbers[$_] = Koha::Acquisition::Order->new( \%ocontent )->insert->{ordernumber};
249 $order_content[$_]->{str}->{ordernumber} = $ordernumbers[$_];
252 # Test UT sub _check_fields_of_order
255 $test_missing_fields, $test_extra_fields,
256 $test_different_fields, $test_nbr_fields
258 = _check_fields_of_order(
260 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
261 { a => "blabla", f => "f", b => "105", c => 15.1200, g => '' }
265 ( $test_nbr_fields == 5 )
266 and ( join( " ", sort @$test_missing_fields ) eq 'd e' )
267 and ( join( " ", sort @$test_extra_fields ) eq 'f g' )
268 and ( join( " ", @$test_different_fields ) eq 'a' )
270 "_check_fields_of_order can check an order (test 1)"
273 $test_missing_fields, $test_extra_fields,
274 $test_different_fields, $test_nbr_fields
276 = _check_fields_of_order(
278 { str => { a => "bla", b => "105" }, num => { c => 15.00 } },
279 { a => "bla", b => "105", c => 15 }
283 ( $test_nbr_fields == 3 )
284 and ( scalar @$test_missing_fields == 0 )
285 and ( scalar @$test_extra_fields == 0 )
286 and ( scalar @$test_different_fields == 0 )
288 "_check_fields_of_order can check an order (test 2)"
291 $test_missing_fields, $test_extra_fields,
292 $test_different_fields, $test_nbr_fields
294 = _check_fields_of_order(
296 { str => { a => "bla", b => "105" }, num => { c => 15.12 } },
297 { a => "blabla", b => "105", c => 15, d => "error" }
301 ( $test_nbr_fields == 4 )
302 and ( join( " ", sort @$test_missing_fields ) eq 'e' )
303 and ( scalar @$test_extra_fields == 0 )
304 and ( join( " ", @$test_different_fields ) eq 'a c' )
306 "_check_fields_of_order can check an order (test 3)"
313 my @expectedfields = qw(
328 datecancellationprinted
367 estimateddeliverydate
375 $test_missing_fields, $test_extra_fields,
376 $test_different_fields, $test_nbr_fields
378 = _check_fields_of_order( \@expectedfields, $order_content[0],
379 GetOrder( $ordernumbers[0] ) );
382 scalar @expectedfields,
383 "GetOrder gets an order with the right number of fields"
385 is( join( " ", @$test_missing_fields ),
386 '', "GetOrder gets an order with no missing fields" );
387 is( join( " ", @$test_extra_fields ),
388 '', "GetOrder gets an order with no unexpected fields" );
389 is( join( " ", @$test_different_fields ),
390 '', "GetOrder gets an order with the right content in every fields" );
396 my @base_expectedfields = qw(
438 editionresponsibility
484 datecancellationprinted
488 ( @base_expectedfields,
489 ( 'transferred_from_timestamp', 'transferred_from' ) );
490 is( GetOrders(), undef, "GetOrders with no params returns undef" );
491 DelOrder( $order_content[3]->{str}->{biblionumber}, $ordernumbers[3] );
492 my @get_orders = GetOrders($basketno);
494 $test_missing_fields, $test_extra_fields,
495 $test_different_fields, $test_nbr_fields
497 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
499 $$test_nbr_fields[0],
500 scalar @expectedfields,
501 "GetOrders gets orders with the right number of fields"
503 is( join( " ", @$test_missing_fields ),
504 '', "GetOrders gets orders with no missing fields" );
505 is( join( " ", @$test_extra_fields ),
506 '', "GetOrders gets orders with no unexpected fields" );
507 is( join( " ", @$test_different_fields ),
508 '', "GetOrders gets orders with the right content in every fields" );
511 ( scalar @get_orders == 4 )
512 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
514 "GetOrders only gets non-cancelled orders"
518 # Test GetCancelledOrders
522 ( @base_expectedfields, ( 'transferred_to_timestamp', 'transferred_to' ) );
523 is( GetCancelledOrders(), undef,
524 "GetCancelledOrders with no params returns undef" );
525 @get_orders = GetCancelledOrders($basketno);
527 $test_missing_fields, $test_extra_fields,
528 $test_different_fields, $test_nbr_fields
530 = _check_fields_of_orders( \@expectedfields, \@order_content, \@get_orders );
532 $$test_nbr_fields[0],
533 scalar @expectedfields,
534 "GetCancelledOrders gets orders with the right number of fields"
536 is( join( " ", @$test_missing_fields ),
537 '', "GetCancelledOrders gets orders with no missing fields" );
538 is( join( " ", @$test_extra_fields ),
539 '', "GetCancelledOrders gets orders with no unexpected fields" );
540 is( join( " ", @$test_different_fields ),
542 "GetCancelledOrders gets orders with the right content in every fields" );
545 ( scalar @get_orders == 1 )
546 and grep ( $_->{ordernumber} eq $ordernumbers[3], @get_orders )
548 "GetCancelledOrders only gets cancelled orders"
555 @expectedfields = qw (
609 datecancellationprinted
616 # note that authorisedby was added to the return of SearchOrder by the
617 # patch for bug 11777
619 my $invoiceid = AddInvoice(
620 invoicenumber => 'invoice',
621 booksellerid => $booksellerid,
625 my ($datereceived, $new_ordernumber) = ModReceiveOrder(
627 biblionumber => $biblionumber4,
628 ordernumber => $ordernumbers[4],
629 quantityreceived => 1,
632 invoiceid => $invoiceid,
634 budget_id => $order_content[4]->{str}->{budget_id},
638 my $search_orders = SearchOrders({
639 booksellerid => $booksellerid,
640 basketno => $basketno
642 isa_ok( $search_orders, 'ARRAY' );
644 $test_missing_fields, $test_extra_fields,
645 $test_different_fields, $test_nbr_fields
647 = _check_fields_of_orders( \@expectedfields, \@order_content,
650 $$test_nbr_fields[0],
651 scalar @expectedfields,
652 "SearchOrders gets orders with the right number of fields"
654 is( join( " ", @$test_missing_fields ),
655 '', "SearchOrders gets orders with no missing fields" );
656 is( join( " ", @$test_extra_fields ),
657 '', "SearchOrders gets orders with no unexpected fields" );
658 is( join( " ", @$test_different_fields ),
659 '', "SearchOrders gets orders with the right content in every fields" );
662 ( scalar @$search_orders == 4 )
663 and !grep ( $_->{ordernumber} eq $ordernumbers[3], @$search_orders )
665 "SearchOrders only gets non-cancelled orders"
668 $search_orders = SearchOrders({
669 booksellerid => $booksellerid,
670 basketno => $basketno,
675 ( scalar @$search_orders == 3 ) and !grep ( (
676 ( $_->{ordernumber} eq $ordernumbers[3] )
677 or ( $_->{ordernumber} eq $ordernumbers[4] )
681 "SearchOrders with pending params gets only pending orders (bug 10723)"
684 $search_orders = SearchOrders({
685 booksellerid => $booksellerid,
686 basketno => $basketno,
690 is( scalar (@$search_orders), 0, "SearchOrders with pending and ordered params gets only pending ordered orders (bug 11170)" );
692 $search_orders = SearchOrders({
693 ordernumber => $ordernumbers[4]
695 is( scalar (@$search_orders), 1, "SearchOrders takes into account the ordernumber filter" );
697 $search_orders = SearchOrders({
698 biblionumber => $biblionumber4
700 is( scalar (@$search_orders), 1, "SearchOrders takes into account the biblionumber filter" );
702 $search_orders = SearchOrders({
703 biblionumber => $biblionumber4,
706 is( scalar (@$search_orders), 0, "SearchOrders takes into account the biblionumber and pending filters" );
709 # Test GetBudgetByOrderNumber
711 ok( GetBudgetByOrderNumber( $ordernumbers[0] )->{'budget_id'} eq $budgetid,
712 "GetBudgetByOrderNumber returns expected budget" );
718 @expectedfields = qw (
731 estimateddeliverydate
742 my @lateorders = GetLateOrders(0);
743 is( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
744 0, "GetLateOrders does not get orders from opened baskets" );
745 C4::Acquisition::CloseBasket($basketno);
746 @lateorders = GetLateOrders(0);
747 isnt( scalar grep ( $_->{basketno} eq $basketno, @lateorders ),
748 0, "GetLateOrders gets orders from closed baskets" );
749 ok( !grep ( $_->{ordernumber} eq $ordernumbers[3], @lateorders ),
750 "GetLateOrders does not gets cancelled orders" );
751 ok( !grep ( $_->{ordernumber} eq $ordernumbers[4], @lateorders ),
752 "GetLateOrders does not gets reveived orders" );
754 $test_missing_fields, $test_extra_fields,
755 $test_different_fields, $test_nbr_fields
757 = _check_fields_of_orders( \@expectedfields, \@order_content, \@lateorders );
759 $$test_nbr_fields[0],
760 scalar @expectedfields,
761 "GetLateOrders gets orders with the right number of fields"
763 is( join( " ", @$test_missing_fields ),
764 '', "GetLateOrders gets orders with no missing fields" );
765 is( join( " ", @$test_extra_fields ),
766 '', "GetLateOrders gets orders with no unexpected fields" );
767 is( join( " ", @$test_different_fields ),
768 '', "GetLateOrders gets orders with the right content in every fields" );
770 $search_orders = SearchOrders({
771 booksellerid => $booksellerid,
772 basketno => $basketno,
776 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)" );
782 my $order = $lateorders[0];
783 AddClaim( $order->{ordernumber} );
784 my $neworder = GetOrder( $order->{ordernumber} );
786 $neworder->{claimed_date},
787 strftime( "%Y-%m-%d", localtime(time) ),
788 "AddClaim : Check claimed_date"
791 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
793 biblionumber => $biblionumber2,
794 ordernumber => $ordernumbers[1],
795 quantityreceived => 2,
798 invoiceid => $invoiceid,
800 order_internalnote => "my notes",
801 order_vendornote => "my vendor notes",
804 my $order2 = GetOrder( $ordernumbers[1] );
805 is( $order2->{'quantityreceived'},
806 0, 'Splitting up order did not receive any on original order' );
807 is( $order2->{'quantity'}, 40, '40 items on original order' );
808 is( $order2->{'budget_id'}, $budgetid,
809 'Budget on original order is unchanged' );
810 is( $order2->{order_internalnote}, "my notes",
811 'ModReceiveOrder and GetOrder deal with internal notes' );
812 is( $order2->{order_vendornote}, "my vendor notes",
813 'ModReceiveOrder and GetOrder deal with vendor notes' );
815 $neworder = GetOrder($new_ordernumber);
816 is( $neworder->{'quantity'}, 2, '2 items on new order' );
817 is( $neworder->{'quantityreceived'},
818 2, 'Splitting up order received items on new order' );
819 is( $neworder->{'budget_id'}, $budgetid, 'Budget on new order is unchanged' );
821 is( $neworder->{ordernumber}, $new_ordernumber, 'Split: test ordernumber' );
822 is( $neworder->{parent_ordernumber}, $ordernumbers[1], 'Split: test parent_ordernumber' );
824 my ( $orders ) = GetHistory( ordernumber => $ordernumbers[1] );
825 is( scalar( @$orders ), 1, 'GetHistory with a given ordernumber returns 1 order' );
826 ( $orders ) = GetHistory( ordernumber => $ordernumbers[1], search_children_too => 1 );
827 is( scalar( @$orders ), 2, 'GetHistory with a given ordernumber and search_children_too set returns 2 orders' );
829 my $budgetid2 = C4::Budgets::AddBudget(
831 budget_code => "budget_code_test_modrecv",
832 budget_name => "budget_name_test_modrecv",
836 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
838 biblionumber => $biblionumber2,
839 ordernumber => $ordernumbers[2],
840 quantityreceived => 2,
843 invoiceid => $invoiceid,
845 budget_id => $budgetid2,
846 order_internalnote => "my other notes",
850 my $order3 = GetOrder( $ordernumbers[2] );
851 is( $order3->{'quantityreceived'},
852 0, 'Splitting up order did not receive any on original order' );
853 is( $order3->{'quantity'}, 2, '2 items on original order' );
854 is( $order3->{'budget_id'}, $budgetid,
855 'Budget on original order is unchanged' );
856 is( $order3->{order_internalnote}, "my other notes",
857 'ModReceiveOrder and GetOrder deal with notes' );
859 $neworder = GetOrder($new_ordernumber);
860 is( $neworder->{'quantity'}, 2, '2 items on new order' );
861 is( $neworder->{'quantityreceived'},
862 2, 'Splitting up order received items on new order' );
863 is( $neworder->{'budget_id'}, $budgetid2, 'Budget on new order is changed' );
865 ( $datereceived, $new_ordernumber ) = ModReceiveOrder(
867 biblionumber => $biblionumber2,
868 ordernumber => $ordernumbers[2],
869 quantityreceived => 2,
872 invoiceid => $invoiceid,
874 budget_id => $budgetid2,
875 order_internalnote => "my third notes",
879 $order3 = GetOrder( $ordernumbers[2] );
880 is( $order3->{'quantityreceived'}, 2, 'Order not split up' );
881 is( $order3->{'quantity'}, 2, '2 items on order' );
882 is( $order3->{'budget_id'}, $budgetid2, 'Budget has changed' );
883 is( $order3->{order_internalnote}, "my third notes", 'ModReceiveOrder and GetOrder deal with notes' );
885 my $nonexistent_order = GetOrder();
886 is( $nonexistent_order, undef, 'GetOrder returns undef if no ordernumber is given' );
887 $nonexistent_order = GetOrder( 424242424242 );
888 is( $nonexistent_order, undef, 'GetOrder returns undef if a nonexistent ordernumber is given' );
891 my $order1 = GetOrder($ordernumbers[0]);
892 my $error = DelOrder($order1->{biblionumber}, $order1->{ordernumber});
893 ok((not defined $error), "DelOrder does not fail");
894 $order1 = GetOrder($order1->{ordernumber});
895 ok((defined $order1->{datecancellationprinted}), "order is cancelled");
896 ok((not defined $order1->{cancellationreason}), "order has no cancellation reason");
897 ok((defined GetBiblio($order1->{biblionumber})), "biblio still exists");
899 $order2 = GetOrder($ordernumbers[1]);
900 $error = DelOrder($order2->{biblionumber}, $order2->{ordernumber}, 1);
901 ok((not defined $error), "DelOrder does not fail");
902 $order2 = GetOrder($order2->{ordernumber});
903 ok((defined $order2->{datecancellationprinted}), "order is cancelled");
904 ok((not defined $order2->{cancellationreason}), "order has no cancellation reason");
905 ok((not defined GetBiblio($order2->{biblionumber})), "biblio does not exist anymore");
907 my $order4 = GetOrder($ordernumbers[3]);
908 $error = DelOrder($order4->{biblionumber}, $order4->{ordernumber}, 1, "foobar");
909 ok((not defined $error), "DelOrder does not fail");
910 $order4 = GetOrder($order4->{ordernumber});
911 ok((defined $order4->{datecancellationprinted}), "order is cancelled");
912 ok(($order4->{cancellationreason} eq "foobar"), "order has cancellation reason \"foobar\"");
913 ok((not defined GetBiblio($order4->{biblionumber})), "biblio does not exist anymore");
914 # End of tests for DelOrder