Bug 31649: Add unit tests
[koha.git] / t / db_dependent / Acquisition / GetBasketGroupAsCSV.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use CGI;
6
7 use Test::More tests => 2;
8
9 use C4::Acquisition qw( NewBasket NewBasketgroup GetBasketGroupAsCSV );
10 use C4::Biblio qw( AddBiblio );
11 use Koha::Database;
12 use Koha::CsvProfiles;
13 use Koha::Acquisition::Orders;
14 use Koha::Biblios;
15
16 use t::lib::Mocks;
17 use Try::Tiny;
18
19 my $schema = Koha::Database->new()->schema();
20 $schema->storage->txn_begin();
21
22 my $query = CGI->new();
23
24 my $vendor = Koha::Acquisition::Bookseller->new({
25     name => 'my vendor',
26     address1 => 'vendor address',
27     active => 1,
28     deliverytime => 5,
29 })->store;
30
31 my $budget_id = C4::Budgets::AddBudget({
32     budget_code => 'my_budget_code',
33     budget_name => 'My budget name',
34 });
35 my $budget = C4::Budgets::GetBudget( $budget_id );
36
37 my $basketno = C4::Acquisition::NewBasket($vendor->id, 1);
38
39 my $basketgroupid = C4::Acquisition::NewBasketgroup(
40     {
41         booksellerid  => $vendor->id,
42         basketlist    => [ $basketno ],
43     }
44 );
45
46 my $biblio = MARC::Record->new();
47 $biblio->append_fields(
48     MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
49     MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
50 );
51 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
52
53 my $order = Koha::Acquisition::Order->new({
54     basketno => $basketno,
55     quantity => 3,
56     biblionumber => $biblionumber,
57     budget_id => $budget_id,
58     entrydate => '2016-01-02',
59 })->store;
60
61 my $basketgroup_csv1 = C4::Acquisition::GetBasketGroupAsCSV($basketgroupid, $query);
62 is($basketgroup_csv1, 'Account number,Basket name,Order number,Author,Title,Publisher,Publication year,Collection title,ISBN,Quantity,RRP tax included,RRP tax excluded,Discount,Estimated cost tax included,Estimated cost tax excluded,Note for vendor,Entry date,Bookseller name,Bookseller physical address,Bookseller postal address,Contract number,Contract name,Basket group delivery place,Basket group billing place,Basket delivery place,Basket billing place
63 ,"",' . $order->ordernumber  . ',"King, Stephen","Test Record","",,"",,3,0.00,0.00,,0.00,0.00,"",2016-01-02,"my vendor","vendor address","",,"","","","",""
64 ', 'CSV should be generated');
65
66 Koha::Biblios->find($biblionumber)->delete;
67 my $basketgroup_csv2 = C4::Acquisition::GetBasketGroupAsCSV($basketgroupid, $query);
68 is($basketgroup_csv2, 'Account number,Basket name,Order number,Author,Title,Publisher,Publication year,Collection title,ISBN,Quantity,RRP tax included,RRP tax excluded,Discount,Estimated cost tax included,Estimated cost tax excluded,Note for vendor,Entry date,Bookseller name,Bookseller physical address,Bookseller postal address,Contract number,Contract name,Basket group delivery place,Basket group billing place,Basket delivery place,Basket billing place
69 ,"",' . $order->ordernumber  . ',"","","",,"",,3,0.00,0.00,,0.00,0.00,"",2016-01-02,"my vendor","vendor address","",,"","","","",""
70 ', 'CSV should not fail if biblio does not exist');
71
72
73 $schema->storage->txn_rollback();