Bug 8612: Use CSV profile for exporting basket
[koha.git] / t / db_dependent / Acquisition / GetBasketAsCSV.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;
10 use C4::Biblio;
11 use Koha::Database;
12 use Koha::CsvProfile;
13
14 use Koha::Acquisition::Order;
15
16 my $schema = Koha::Database->new()->schema();
17 $schema->storage->txn_begin();
18
19 my $dbh = C4::Context->dbh;
20 $dbh->{RaiseError} = 1;
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 $csv_profile = Koha::CsvProfile->new({
38     profile => 'my user profile',
39     type => 'export_basket',
40     csv_separator => ',',
41     content => 'autor=biblio.author|title=biblio.title|quantity=aqorders.quantity',
42 })->store;
43
44 my $basketno;
45 $basketno = NewBasket($vendor->id, 1);
46
47 my $biblio = MARC::Record->new();
48 $biblio->append_fields(
49     MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
50     MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
51 );
52 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
53
54 my $order = Koha::Acquisition::Order->new({
55     basketno => $basketno,
56     quantity => 3,
57     biblionumber => $biblionumber,
58     budget_id => $budget_id,
59     entrydate => '2016-01-02',
60 })->insert;
61
62 # Use user CSV profile
63 my $basket_csv1 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile->export_format_id);
64 is($basket_csv1, 'autor,title,quantity
65 "King, Stephen","Test Record",3
66 ', 'CSV should be generated with user profile');
67
68 # Use defautl template
69 my $basket_csv2 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
70 is($basket_csv2, 'Contract name,Order number,Entry date,ISBN,Author,Title,Publication year,Publisher,Collection title,Note for vendor,Quantity,RRP,Delivery place,Billing place
71
72 "",' . $order->{ordernumber}  . ',2016-01-02,,"King, Stephen","Test Record",,"","","",3,,"",""
73
74 ', 'CSV should be generated with default template');
75
76 $schema->storage->txn_rollback();