Bug 22048: Use set_password in tests
[koha.git] / t / db_dependent / Bookseller.t
index 96e0837..c6f4c7f 100644 (file)
@@ -18,7 +18,7 @@ use C4::Budgets;
 use C4::Biblio;
 
 use Koha::Acquisition::Booksellers;
-use Koha::Acquisition::Order;
+use Koha::Acquisition::Orders;
 use Koha::Database;
 
 BEGIN {
@@ -46,6 +46,7 @@ $dbh->do(q|DELETE FROM aqbasket|);
 $dbh->do(q|DELETE FROM aqbooksellers|);
 $dbh->do(q|DELETE FROM subscription|);
 
+my $patron = $builder->build_object({ class => 'Koha::Patrons' });
 # Add currency
 my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
 
@@ -66,8 +67,8 @@ my $sample_supplier1 = {
     gstreg        => 1,
     listincgst    => 1,
     invoiceincgst => 1,
-    tax_rate       => '1.0000',
-    discount      => '1.0000',
+    tax_rate      => '1.0000',
+    discount      => 1.0000,
     notes         => 'notes1',
     deliverytime  => undef
 };
@@ -86,8 +87,8 @@ my $sample_supplier2 = {
     gstreg        => 1,
     listincgst    => 1,
     invoiceincgst => 1,
-    tax_rate       => '2.0000',
-    discount      => '2.0000',
+    tax_rate      => '2.0000',
+    discount      => 2.0000,
     notes         => 'notes2',
     deliverytime  => 2
 };
@@ -117,7 +118,7 @@ for my $bookseller ( @bookseller2 ) {
 }
 
 $sample_supplier2->{id} = $id_supplier2;
-is_deeply( $bookseller2[0], $sample_supplier2,
+is_deeply( cast_precision($bookseller2[0]), $sample_supplier2,
     "Koha::Acquisition::Booksellers->search returns the right informations about supplier $sample_supplier2->{name}" );
 
 $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
@@ -125,11 +126,12 @@ $id_supplier1 = $supplier1->id;
 my @booksellers = Koha::Acquisition::Booksellers->search();
 for my $bookseller ( @booksellers ) {
     $bookseller = field_filter( $bookseller->unblessed );
+    $bookseller = cast_precision($bookseller);
 }
 
 $sample_supplier1->{id} = $id_supplier1;
 is( scalar(@booksellers), $count + 2, "Get  Supplier1 and Supplier2" );
-my @tab = ( $sample_supplier1, $sample_supplier2 );
+my @tab = ( $sample_supplier2, $sample_supplier1 );
 is_deeply( \@booksellers, \@tab,
     "Returns right fields of Supplier1 and Supplier2" );
 
@@ -137,9 +139,9 @@ is_deeply( \@booksellers, \@tab,
 my @bookseller1 = Koha::Acquisition::Booksellers->search({name => $sample_supplier1->{name} });
 is( $bookseller1[0]->baskets->count, 0, 'Supplier1 has 0 basket' );
 my $basketno1 =
-  C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
+  C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname1' );
 my $basketno2 =
-  C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
+  C4::Acquisition::NewBasket( $id_supplier1, $patron->borrowernumber, 'basketname2' );
 @bookseller1 = Koha::Acquisition::Booksellers->search({ name => $sample_supplier1->{name} });
 is( $bookseller1[0]->baskets->count, 2, 'Supplier1 has 2 baskets' );
 
@@ -149,7 +151,7 @@ is( $bookseller1fromid, undef,
     "find returns undef if no id given" );
 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
 $bookseller1fromid = field_filter($bookseller1fromid->unblessed);
-is_deeply( $bookseller1fromid, $sample_supplier1,
+is_deeply( cast_precision($bookseller1fromid), $sample_supplier1,
     "Get Supplier1 (find a bookseller by id)" );
 
 $bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
@@ -188,9 +190,9 @@ is( $bookseller1fromid->subscriptions->count,
 
 my $id_subscription1 = NewSubscription(
     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
-    '01-01-2013',undef, undef, undef,  undef,
+    '2013-01-01',undef, undef, undef,  undef,
     undef,      undef,  undef, undef, undef, undef,
-    1,          "subscription notes",undef, '01-01-2013', undef, undef,
+    1,          "subscription notes",undef, '2013-01-01', undef, undef,
     undef, 'CALL ABC',  0,    "intnotes",  0,
     undef, undef, 0,          undef,         '2013-11-30', 0
 );
@@ -200,9 +202,9 @@ is($subscriptions[0]->{publicnotes}, 'subscription notes', 'subscription search
 
 my $id_subscription2 = NewSubscription(
     undef,      'BRANCH2',     $id_supplier1, undef, $id_budget, $biblionumber,
-    '01-01-2013',undef, undef, undef,  undef,
+    '2013-01-01',undef, undef, undef,  undef,
     undef,      undef,  undef, undef, undef, undef,
-    1,          "subscription notes",undef, '01-01-2013', undef, undef,
+    1,          "subscription notes",undef, '2013-01-01', undef, undef,
     undef, 'CALL DEF',  0,    "intnotes",  0,
     undef, undef, 0,          undef,         '2013-07-31', 0
 );
@@ -228,8 +230,8 @@ $sample_supplier2 = {
     gstreg        => 1,
     listincgst    => 1,
     invoiceincgst => 1,
-    tax_rate       => '2.0000 ',
-    discount      => '2.0000',
+    tax_rate      => '2.0000',
+    discount      => 2.0000,
     notes         => 'notes2 modified',
     deliverytime  => 2,
 };
@@ -258,8 +260,8 @@ my $sample_supplier3 = {
     gstreg        => 1,
     listincgst    => 1,
     invoiceincgst => 1,
-    tax_rate       => '3.0000',
-    discount      => '3.0000',
+    tax_rate      => '3.0000',
+    discount      => 3.0000,
     notes         => 'notes3',
     deliverytime  => 3
 };
@@ -278,8 +280,8 @@ my $sample_supplier4 = {
     gstreg        => 1,
     listincgst    => 1,
     invoiceincgst => 1,
-    tax_rate       => '3.0000',
-    discount      => '3.0000',
+    tax_rate      => '3.0000',
+    discount      => 3.0000,
     notes         => 'notes3',
 };
 my $supplier3 = Koha::Acquisition::Bookseller->new($sample_supplier3)->store;
@@ -289,10 +291,10 @@ my $id_supplier4 = $supplier4->id;
 
 #Add 2 baskets
 my $basketno3 =
-  C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
+  C4::Acquisition::NewBasket( $id_supplier3, $patron->borrowernumber, 'basketname3',
     'basketnote3' );
 my $basketno4 =
-  C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
+  C4::Acquisition::NewBasket( $id_supplier4, $patron->borrowernumber, 'basketname4',
     'basketnote4' );
 
 #Modify the basket to add a close date
@@ -329,9 +331,9 @@ ModBasket($basket4info);
 #Add 1 subscription
 my $id_subscription3 = NewSubscription(
     undef,      "BRANCH1",     $id_supplier1, undef, $id_budget, $biblionumber,
-    '01-01-2013',undef, undef, undef,  undef,
+    '2013-01-01',undef, undef, undef,  undef,
     undef,      undef,  undef, undef, undef, undef,
-    1,          "subscription notes",undef, '01-01-2013', undef, undef,
+    1,          "subscription notes",undef, '2013-01-01', undef, undef,
     undef,       undef,  0,    "intnotes",  0,
     undef, undef, 0,          'LOCA',         '2013-12-31', 0
 );
@@ -354,19 +356,19 @@ my $order1 = Koha::Acquisition::Order->new(
         quantity         => 24,
         biblionumber     => $biblionumber,
         budget_id        => $id_budget,
-        entrydate        => '01-01-2013',
+        entrydate        => '2013-01-01',
         currency         => $curcode,
         notes            => "This is a note1",
-        tax_rate          => 0.0500,
+        tax_rate         => 0.0500,
         orderstatus      => 1,
         subscriptionid   => $id_subscription1,
         quantityreceived => 2,
         rrp              => 10,
         ecost            => 10,
-        datereceived     => '01-06-2013'
+        datereceived     => '2013-06-01'
     }
-)->insert;
-my $ordernumber1 = $order1->{ordernumber};
+)->store;
+my $ordernumber1 = $order1->ordernumber;
 
 my $order2 = Koha::Acquisition::Order->new(
     {
@@ -374,17 +376,17 @@ my $order2 = Koha::Acquisition::Order->new(
         quantity       => 20,
         biblionumber   => $biblionumber,
         budget_id      => $id_budget,
-        entrydate      => '01-01-2013',
+        entrydate      => '2013-01-01',
         currency       => $curcode,
         notes          => "This is a note2",
-        tax_rate        => 0.0500,
+        tax_rate       => 0.0500,
         orderstatus    => 1,
         subscriptionid => $id_subscription2,
         rrp            => 10,
         ecost          => 10,
     }
-)->insert;
-my $ordernumber2 = $order2->{ordernumber};
+)->store;
+my $ordernumber2 = $order2->ordernumber;
 
 my $order3 = Koha::Acquisition::Order->new(
     {
@@ -392,17 +394,17 @@ my $order3 = Koha::Acquisition::Order->new(
         quantity       => 20,
         biblionumber   => $biblionumber,
         budget_id      => $id_budget,
-        entrydate      => '02-02-2013',
+        entrydate      => '2013-02-02',
         currency       => $curcode,
         notes          => "This is a note3",
-        tax_rate        => 0.0500,
+        tax_rate       => 0.0500,
         orderstatus    => 2,
         subscriptionid => $id_subscription3,
         rrp            => 11,
         ecost          => 11,
     }
-)->insert;
-my $ordernumber3 = $order3->{ordernumber};
+)->store;
+my $ordernumber3 = $order3->ordernumber;
 
 my $order4 = Koha::Acquisition::Order->new(
     {
@@ -410,18 +412,18 @@ my $order4 = Koha::Acquisition::Order->new(
         quantity         => 20,
         biblionumber     => $biblionumber,
         budget_id        => $id_budget,
-        entrydate        => '02-02-2013',
+        entrydate        => '2013-02-02',
         currency         => $curcode,
         notes            => "This is a note3",
-        tax_rate          => 0.0500,
+        tax_rate         => 0.0500,
         orderstatus      => 2,
         subscriptionid   => $id_subscription3,
         rrp              => 11,
         ecost            => 11,
         quantityreceived => 20
     }
-)->insert;
-my $ordernumber4 = $order4->{ordernumber};
+)->store;
+my $ordernumber4 = $order4->ordernumber;
 
 #Test cases:
 # Sample datas :
@@ -637,9 +639,7 @@ ok( exists( $suppliers{$id_supplier1} ),
     "Supplier1 has late orders and $daysago10==$daysago10 " )
   ;
 
-C4::Context->_new_userenv('DUMMY SESSION');
-C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 0, '', '');
-my $userenv = C4::Context->userenv;
+t::lib::Mocks::mock_userenv({ flags => 0, branchcode => 'BRANCH1' });
 
 my $module = Test::MockModule->new('C4::Auth');
 $module->mock(
@@ -670,7 +670,7 @@ is(
 );
 
 # don the cape and turn into Superlibrarian!
-C4::Context->set_userenv(0,0,0,'firstname','surname', 'BRANCH1', 'Library 1', 1, '', '');
+t::lib::Mocks::mock_userenv({ flags => 1, branchcode => 'BRANCH1' });
 @subscriptions = SearchSubscriptions({expiration_date => '2013-12-31'});
 is(
     scalar(grep { !$_->{cannotdisplay} } @subscriptions ),
@@ -779,3 +779,14 @@ sub field_filter {
     }
     return $struct;
 }
+
+# ensure numbers are actually tested as numbers to prevent
+# precision changes causing test failures (D8->D9 Upgrades)
+sub cast_precision {
+    my ($struct) = @_;
+    my @cast = ('discount');
+    for my $cast (@cast) {
+        $struct->{$cast} = $struct->{$cast}+0;
+    }
+    return $struct;
+}