Bug 32394: (follow-up) Add param for Koha::BackgroundJob::StageMARCForImport->enqueue
[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 => 5;
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 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 $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     description => 'csv profile',
43 })->store;
44
45 my $csv_profile2 = Koha::CsvProfile->new({
46     profile => 'my user profile',
47     type => 'export_basket',
48     csv_separator => ',',
49     content => 'biblio.author | title = biblio.title|quantity=aqorders.quantity',
50     description => 'csv profile 2',
51 })->store;
52
53 my $basketno;
54 $basketno = NewBasket($vendor->id, 1);
55
56 my $biblio = MARC::Record->new();
57 $biblio->append_fields(
58     MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
59     MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
60 );
61 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
62
63 my $order = Koha::Acquisition::Order->new({
64     basketno => $basketno,
65     quantity => 3,
66     biblionumber => $biblionumber,
67     budget_id => $budget_id,
68     entrydate => '2016-01-02',
69 })->store;
70
71 # Use user CSV profile
72 my $basket_csv1 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile->export_format_id);
73 is($basket_csv1, 'autor,title,quantity
74 "King, Stephen","Test Record",3
75 ', 'CSV should be generated with user profile');
76
77 # Use default template
78 t::lib::Mocks::mock_preference('CSVDelimiter', ',');
79 my $basket_csv2 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
80 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
81 "",' . $order->ordernumber  . ',2016-01-02,,"King, Stephen","Test Record",,"","","",3,,"",""
82 ', 'CSV should be generated with default template');
83
84 my $basket_csv3 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile2->export_format_id);
85 is($basket_csv3, 'biblio.author,title,quantity
86 "King, Stephen","Test Record",3
87 ', 'CSV should be generated with user profile which does not have all headers defined');
88
89 try {
90     my $basket_csv4 = C4::Acquisition::GetBasketAsCSV($basketno, $query, 'non_existant_profile_id');
91     fail("It is not possible to export basket using non-existant profile");
92 } catch {
93     ok($_->isa("Koha::Exceptions::ObjectNotFound"), "Using non-existant profile should throw ObjectNotFound exception");
94 };
95
96 Koha::Biblios->find($biblionumber)->delete;
97 my $basket_csv4 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
98 is($basket_csv4, 'Contract name,Order number,Entry date,ISBN,Author,Title,Publication year,Publisher,Collection title,Note for vendor,Quantity,RRP,Delivery place,Billing place
99 "",' . $order->ordernumber . ',2016-01-02,,"","",,"","","",3,,"",""
100 ', 'CSV should not fail if biblio does not exist');
101
102
103 $schema->storage->txn_rollback();