Bug 8612: [Follow-up] Fix unit test GetBasketAsCSV.t
[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 => 3;
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 $query = CGI->new();
20
21 my $vendor = Koha::Acquisition::Bookseller->new({
22     name => 'my vendor',
23     address1 => 'vendor address',
24     active => 1,
25     deliverytime => 5,
26 })->store;
27
28 my $budget_id = C4::Budgets::AddBudget({
29     budget_code => 'my_budget_code',
30     budget_name => 'My budget name',
31 });
32 my $budget = C4::Budgets::GetBudget( $budget_id );
33
34 my $csv_profile = Koha::CsvProfile->new({
35     profile => 'my user profile',
36     type => 'export_basket',
37     csv_separator => ',',
38     content => 'autor=biblio.author|title=biblio.title|quantity=aqorders.quantity',
39 })->store;
40
41 my $csv_profile2 = Koha::CsvProfile->new({
42     profile => 'my user profile',
43     type => 'export_basket',
44     csv_separator => ',',
45     content => 'biblio.author | title = biblio.title|quantity=aqorders.quantity',
46 })->store;
47
48 my $basketno;
49 $basketno = NewBasket($vendor->id, 1);
50
51 my $biblio = MARC::Record->new();
52 $biblio->append_fields(
53     MARC::Field->new( '100', ' ', ' ', a => 'King, Stephen' ),
54     MARC::Field->new( '245', ' ', ' ', a => 'Test Record' ),
55 );
56 my ($biblionumber, $biblioitemnumber) = AddBiblio($biblio, '');
57
58 my $order = Koha::Acquisition::Order->new({
59     basketno => $basketno,
60     quantity => 3,
61     biblionumber => $biblionumber,
62     budget_id => $budget_id,
63     entrydate => '2016-01-02',
64 })->insert;
65
66 # Use user CSV profile
67 my $basket_csv1 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile->export_format_id);
68 is($basket_csv1, 'autor,title,quantity
69 "King, Stephen","Test Record",3
70 ', 'CSV should be generated with user profile');
71
72 # Use default template
73 my $basket_csv2 = C4::Acquisition::GetBasketAsCSV($basketno, $query);
74 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
75
76 "",' . $order->{ordernumber}  . ',2016-01-02,,"King, Stephen","Test Record",,"","","",3,,"",""
77
78 ', 'CSV should be generated with default template');
79
80 my $basket_csv3 = C4::Acquisition::GetBasketAsCSV($basketno, $query, $csv_profile2->export_format_id);
81 is($basket_csv3, 'biblio.author,title,quantity
82 "King, Stephen","Test Record",3
83 ', 'CSV should be generated with user profile which does not have all headers defined');
84
85 $schema->storage->txn_rollback();