Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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 => 4;
8
9 use C4::Acquisition qw( NewBasket GetBasket GetBasketAsCSV );
10 use C4::Biblio qw( AddBiblio );
11 use Koha::Database;
12 use Koha::CsvProfiles;
13 use Koha::Acquisition::Orders;
14
15 use t::lib::Mocks;
16 use Try::Tiny;
17
18 my $schema = Koha::Database->new()->schema();
19 $schema->storage->txn_begin();
20
21 my $query = CGI->new();
22
23 my $vendor = Koha::Acquisition::Bookseller->new({
24     name => 'my vendor',
25     address1 => 'vendor address',
26     active => 1,
27     deliverytime => 5,
28 })->store;
29
30 my $budget_id = C4::Budgets::AddBudget({
31     budget_code => 'my_budget_code',
32     budget_name => 'My budget name',
33 });
34 my $budget = C4::Budgets::GetBudget( $budget_id );
35
36 my $csv_profile = Koha::CsvProfile->new({
37     profile => 'my user profile',
38     type => 'export_basket',
39     csv_separator => ',',
40     content => 'autor=biblio.author|title=biblio.title|quantity=aqorders.quantity',
41     description => 'csv profile',
42 })->store;
43
44 my $csv_profile2 = Koha::CsvProfile->new({
45     profile => 'my user profile',
46     type => 'export_basket',
47     csv_separator => ',',
48     content => 'biblio.author | title = biblio.title|quantity=aqorders.quantity',
49     description => 'csv profile 2',
50 })->store;
51
52 my $basketno;
53 $basketno = NewBasket($vendor->id, 1);
54
55 my $biblio = MARC::Record->new();
56 $biblio->append_fields(
57     MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
58     MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
59 );
60 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
61
62 my $order = Koha::Acquisition::Order->new({
63     basketno => $basketno,
64     quantity => 3,
65     biblionumber => $biblionumber,
66     budget_id => $budget_id,
67     entrydate => '2016-01-02',
68 })->store;
69
70 # Use user CSV profile
71 my $basket_csv1 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile->export_format_id);
72 is($basket_csv1, 'autor,title,quantity
73 "King, Stephen","Test Record",3
74 ', 'CSV should be generated with user profile');
75
76 # Use default template
77 t::lib::Mocks::mock_preference('CSVDelimiter', ',');
78 my $basket_csv2 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
79 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
80 "",' . $order->ordernumber  . ',2016-01-02,,"King, Stephen","Test Record",,"","","",3,,"",""
81 ', 'CSV should be generated with default template');
82
83 my $basket_csv3 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile2->export_format_id);
84 is($basket_csv3, 'biblio.author,title,quantity
85 "King, Stephen","Test Record",3
86 ', 'CSV should be generated with user profile which does not have all headers defined');
87
88 try {
89     my $basket_csv4 = C4::Acquisition::GetBasketAsCSV($basketno, $query, 'non_existant_profile_id');
90     fail("It is not possible to export basket using non-existant profile");
91 } catch {
92     ok($_->isa("Koha::Exceptions::ObjectNotFound"), "Using non-existant profile should throw ObjectNotFound exception");
93 };
94
95 $schema->storage->txn_rollback();