Bug 13457 - Followup for CPL and S codes
[koha.git] / t / db_dependent / Bookseller.t
index 2f73c81..0f1a3b6 100644 (file)
@@ -2,8 +2,10 @@
 
 use Modern::Perl;
 
-use Test::More tests => 72;
+use Test::More tests => 88;
 use Test::MockModule;
+use Test::Warn;
+
 use C4::Context;
 use Koha::DateUtils;
 use DateTime::Duration;
@@ -12,8 +14,11 @@ use C4::Serials;
 use C4::Budgets;
 use C4::Biblio;
 
+use Koha::Acquisition::Order;
+
 BEGIN {
     use_ok('C4::Bookseller');
+    use_ok('Koha::Acquisition::Bookseller');
 }
 
 can_ok(
@@ -21,8 +26,6 @@ can_ok(
     'C4::Bookseller', qw(
       AddBookseller
       DelBookseller
-      GetBookSeller
-      GetBookSellerFromId
       GetBooksellersWithLateOrders
       ModBookseller )
 );
@@ -39,7 +42,7 @@ $dbh->do(q|DELETE FROM aqbooksellers|);
 $dbh->do(q|DELETE FROM subscription|);
 
 #Test AddBookseller
-my $count            = scalar( C4::Bookseller::GetBookSeller('') );
+my $count            = scalar( Koha::Acquisition::Bookseller->search() );
 my $sample_supplier1 = {
     name          => 'Name1',
     address1      => 'address1_1',
@@ -51,13 +54,6 @@ my $sample_supplier1 = {
     accountnumber => 'accountnumber1',
     fax           => 'fax1',
     url           => 'url1',
-    contact       => 'contact1',
-    contpos       => 'contpos1',
-    contphone     => 'contphone1',
-    contfax       => 'contefax1',
-    contaltphone  => 'contaltphone1',
-    contemail     => 'contemail1',
-    contnotes     => 'contnotes1',
     active        => 1,
     gstreg        => 1,
     listincgst    => 1,
@@ -78,13 +74,6 @@ my $sample_supplier2 = {
     accountnumber => 'accountnumber2',
     fax           => 'fax2',
     url           => 'url2',
-    contact       => 'contact2',
-    contpos       => 'contpos2',
-    contphone     => 'contphone2',
-    contfax       => 'contefax2',
-    contaltphone  => 'contaltphone2',
-    contemail     => 'contemail2',
-    contnotes     => 'contnotes2',
     active        => 1,
     gstreg        => 1,
     listincgst    => 1,
@@ -92,7 +81,7 @@ my $sample_supplier2 = {
     gstrate       => '2.0000',
     discount      => '2.0000',
     notes         => 'notes2',
-    deliverytime  => 2,
+    deliverytime  => 2
 };
 
 my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
@@ -102,31 +91,30 @@ my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
 
 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
-is( scalar( C4::Bookseller::GetBookSeller('') ),
+my @b = Koha::Acquisition::Bookseller->search();
+is ( scalar(@b),
     $count + 2, "Supplier1 and Supplier2 have been added" );
 
 #Test DelBookseller
 my $del = C4::Bookseller::DelBookseller($id_supplier1);
 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
-is( C4::Bookseller::GetBookSellerFromId($id_supplier1),
-    undef, "Supplier1  has been deleted - id_supplier1 doesnt exist anymore" );
+my $b = Koha::Acquisition::Bookseller->fetch({id => $id_supplier1});
+is( $b,
+    undef, "Supplier1  has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
 
-#Test GetBookSeller
-my @bookseller2 = C4::Bookseller::GetBookSeller( $sample_supplier2->{name} );
+#Test get bookseller
+my @bookseller2 = Koha::Acquisition::Bookseller->search({name => $sample_supplier2->{name} });
 is( scalar(@bookseller2), 1, "Get only  Supplier2" );
 $bookseller2[0] = field_filter( $bookseller2[0] );
-delete $bookseller2[0]->{basketcount};
 
 $sample_supplier2->{id} = $id_supplier2;
 is_deeply( $bookseller2[0], $sample_supplier2,
-    "GetBookSeller returns the right informations about $sample_supplier2" );
+    "Koha::Acquisition::Bookseller->search returns the right informations about $sample_supplier2" );
 
 $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
-my @booksellers = C4::Bookseller::GetBookSeller('')
-  ;    #NOTE :without params, it returns all the booksellers
+my @booksellers = Koha::Acquisition::Bookseller->search(); #NOTE :without params, it returns all the booksellers
 for my $i ( 0 .. scalar(@booksellers) - 1 ) {
     $booksellers[$i] = field_filter( $booksellers[$i] );
-    delete $booksellers[$i]->{basketcount};
 }
 
 $sample_supplier1->{id} = $id_supplier1;
@@ -135,33 +123,30 @@ my @tab = ( $sample_supplier1, $sample_supplier2 );
 is_deeply( \@booksellers, \@tab,
     "Returns right fields of Supplier1 and Supplier2" );
 
-#Test basketcount
-my @bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
-#FIXME : if there is 0 basket, GetBookSeller returns 1 as basketcount
-#is( $bookseller1[0]->{basketcount}, 0, 'Supplier1 has 0 basket' );
-my $sample_basket1 =
+#Test basket_count
+my @bookseller1 = Koha::Acquisition::Bookseller->search({name => $sample_supplier1->{name} });
+is( $bookseller1[0]->basket_count, 0, 'Supplier1 has 0 basket' );
+my $basketno1 =
   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby1', 'basketname1' );
-my $sample_basket2 =
+my $basketno2 =
   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
-@bookseller1 = C4::Bookseller::GetBookSeller( $sample_supplier1->{name} );
-is( $bookseller1[0]->{basketcount}, 2, 'Supplier1 has 2 baskets' );
+@bookseller1 = Koha::Acquisition::Bookseller::search({ name => $sample_supplier1->{name} });
+is( $bookseller1[0]->basket_count, 2, 'Supplier1 has 2 baskets' );
 
-#Test GetBookSellerFromId
-my $bookseller1fromid = C4::Bookseller::GetBookSellerFromId();
+#Test Koha::Acquisition::Bookseller->new using id
+my $bookseller1fromid = Koha::Acquisition::Bookseller->fetch;
 is( $bookseller1fromid, undef,
-    "GetBookSellerFromId returns undef if no id given" );
-$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
+    "fetch returns undef if no id given" );
+$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
 $bookseller1fromid = field_filter($bookseller1fromid);
-delete $bookseller1fromid->{basketcount};
-delete $bookseller1fromid->{subscriptioncount};
 is_deeply( $bookseller1fromid, $sample_supplier1,
-    "Get Supplier1 (GetBookSellerFromId)" );
+    "Get Supplier1 (fetch a bookseller by id)" );
 
-#Test basketcount
-$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
-is( $bookseller1fromid->{basketcount}, 2, 'Supplier1 has 2 baskets' );
+#Test basket_count
+$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
+is( $bookseller1fromid->basket_count, 2, 'Supplier1 has 2 baskets' );
 
-#Test subscriptioncount
+#Test subscription_count
 my $dt_today    = dt_from_string;
 my $today       = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
 
@@ -188,8 +173,8 @@ $bib->append_fields(
     MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
 );
 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
-$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
-is( $bookseller1fromid->{subscriptioncount},
+$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
+is( $bookseller1fromid->subscription_count,
     0, 'Supplier1 has 0 subscription' );
 
 my $id_subscription1 = NewSubscription(
@@ -213,8 +198,8 @@ my $id_subscription2 = NewSubscription(
     undef, undef, 0,          undef,         '2013-07-31', 0
 );
 
-$bookseller1fromid = C4::Bookseller::GetBookSellerFromId($id_supplier1);
-is( $bookseller1fromid->{subscriptioncount},
+$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
+is( $bookseller1fromid->subscription_count,
     2, 'Supplier1 has 2 subscriptions' );
 
 #Test ModBookseller
@@ -230,13 +215,6 @@ $sample_supplier2 = {
     accountnumber => 'accountnumber2 modified',
     fax           => 'fax2 modified',
     url           => 'url2 modified',
-    contact       => 'contact2 modified',
-    contpos       => 'contpos2 modified',
-    contphone     => 'contphone2 modified',
-    contfax       => 'contefax2 modified',
-    contaltphone  => 'contaltphone2 modified',
-    contemail     => 'contemail2 modified',
-    contnotes     => 'contnotes2 modified',
     active        => 1,
     gstreg        => 1,
     listincgst    => 1,
@@ -252,7 +230,7 @@ is( $modif1, undef,
     "ModBookseller returns undef if no params given - Nothing happened" );
 $modif1 = C4::Bookseller::ModBookseller($sample_supplier2);
 is( $modif1, 1, "ModBookseller modifies only the supplier2" );
-is( scalar( C4::Bookseller::GetBookSeller('') ),
+is( scalar( Koha::Acquisition::Bookseller->search ),
     $count + 2, "Supplier2 has been modified - Nothing added" );
 
 $modif1 = C4::Bookseller::ModBookseller(
@@ -277,13 +255,6 @@ my $sample_supplier3 = {
     accountnumber => 'accountnumber3',
     fax           => 'fax3',
     url           => 'url3',
-    contact       => 'contact3',
-    contpos       => 'contpos3',
-    contphone     => 'contphone3',
-    contfax       => 'contefax3',
-    contaltphone  => 'contaltphone3',
-    contemail     => 'contemail3',
-    contnotes     => 'contnotes3',
     active        => 1,
     gstreg        => 1,
     listincgst    => 1,
@@ -304,13 +275,6 @@ my $sample_supplier4 = {
     accountnumber => 'accountnumber4',
     fax           => 'fax4',
     url           => 'url4',
-    contact       => 'contact4',
-    contpos       => 'contpos4',
-    contphone     => 'contphone4',
-    contfax       => 'contefax4',
-    contaltphone  => 'contaltphone4',
-    contemail     => 'contemail4',
-    contnotes     => 'contnotes4',
     active        => 1,
     gstreg        => 1,
     listincgst    => 1,
@@ -323,22 +287,22 @@ my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
 my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
 
 #Add 2 baskets
-my $sample_basket3 =
+my $basketno3 =
   C4::Acquisition::NewBasket( $id_supplier3, 'authorisedby3', 'basketname3',
     'basketnote3' );
-my $sample_basket4 =
+my $basketno4 =
   C4::Acquisition::NewBasket( $id_supplier4, 'authorisedby4', 'basketname4',
     'basketnote4' );
 
 #Modify the basket to add a close date
 my $basket1info = {
-    basketno     => $sample_basket1,
+    basketno     => $basketno1,
     closedate    => $today,
     booksellerid => $id_supplier1
 };
 
 my $basket2info = {
-    basketno     => $sample_basket2,
+    basketno     => $basketno2,
     closedate    => $daysago5,
     booksellerid => $id_supplier2
 };
@@ -348,12 +312,12 @@ my $dur10 = DateTime::Duration->new( days => -10 );
 $dt_today2->add_duration($dur10);
 my $daysago10 = output_pref({ dt => $dt_today2, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
 my $basket3info = {
-    basketno  => $sample_basket3,
+    basketno  => $basketno3,
     closedate => $daysago10,
 };
 
 my $basket4info = {
-    basketno  => $sample_basket4,
+    basketno  => $basketno4,
     closedate => $today,
 };
 ModBasket($basket1info);
@@ -383,11 +347,9 @@ is(scalar(@subscriptions), 1, 'search for subscriptions by call number');
 is(scalar(@subscriptions), 1, 'search for subscriptions by location');
 
 #Add 4 orders
-my ( $ordernumber1, $ordernumber2, $ordernumber3, $ordernumber4 );
-my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
-( $basketno1, $ordernumber1 ) = C4::Acquisition::NewOrder(
+my $order1 = Koha::Acquisition::Order->new(
     {
-        basketno         => $sample_basket1,
+        basketno         => $basketno1,
         quantity         => 24,
         biblionumber     => $biblionumber,
         budget_id        => $id_budget,
@@ -402,10 +364,12 @@ my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
         ecost            => 10,
         datereceived     => '01-06-2013'
     }
-);
-( $basketno2, $ordernumber2 ) = C4::Acquisition::NewOrder(
+)->insert;
+my $ordernumber1 = $order1->{ordernumber};
+
+my $order2 = Koha::Acquisition::Order->new(
     {
-        basketno       => $sample_basket2,
+        basketno       => $basketno2,
         quantity       => 20,
         biblionumber   => $biblionumber,
         budget_id      => $id_budget,
@@ -418,10 +382,12 @@ my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
         rrp            => 10,
         ecost          => 10,
     }
-);
-( $basketno3, $ordernumber3 ) = C4::Acquisition::NewOrder(
+)->insert;
+my $ordernumber2 = $order2->{ordernumber};
+
+my $order3 = Koha::Acquisition::Order->new(
     {
-        basketno       => $sample_basket3,
+        basketno       => $basketno3,
         quantity       => 20,
         biblionumber   => $biblionumber,
         budget_id      => $id_budget,
@@ -434,10 +400,12 @@ my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
         rrp            => 11,
         ecost          => 11,
     }
-);
-( $basketno4, $ordernumber4 ) = C4::Acquisition::NewOrder(
+)->insert;
+my $ordernumber3 = $order3->{ordernumber};
+
+my $order4 = Koha::Acquisition::Order->new(
     {
-        basketno         => $sample_basket4,
+        basketno         => $basketno4,
         quantity         => 20,
         biblionumber     => $biblionumber,
         budget_id        => $id_budget,
@@ -451,7 +419,8 @@ my ( $basketno1,    $basketno2,    $basketno3,    $basketno4 );
         ecost            => 11,
         quantityreceived => 20
     }
-);
+)->insert;
+my $ordernumber4 = $order4->{ordernumber};
 
 #Test cases:
 # Sample datas :
@@ -485,8 +454,13 @@ ok( exists( $suppliers{$id_supplier3} ),
 isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
 
 #Case 3: With $delay = -1
-is( C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ),
-    undef, "-1 is a wrong value for a delay" );
+my $bslo;
+warning_like
+  { $bslo = C4::Bookseller::GetBooksellersWithLateOrders( -1, undef, undef ) }
+    qr/^WARNING: GetBooksellerWithLateOrders is called with a negative value/,
+    "GetBooksellerWithLateOrders prints a warning on negative values";
+
+is( $bslo, undef, "-1 is a wrong value for a delay" );
 
 #Case 4: With $delay = 0
 #    today  == now-0 -LATE- (if no deliverytime or deliverytime == 0)
@@ -632,7 +606,7 @@ isnt( exists( $suppliers{$id_supplier4} ), 1, "Supplier4 hasnt late orders" );
 
 #Basket1 closedate -> $daysago10
 $basket1info = {
-    basketno  => $sample_basket1,
+    basketno  => $basketno1,
     closedate => $daysago10,
 };
 ModBasket($basket1info);
@@ -710,20 +684,24 @@ my $booksellerid = C4::Bookseller::AddBookseller(
         phone    => "0123456",
         active   => 1
     },
-    [ { name => 'John Smith', phone => '0123456x1' } ]
+    [
+        { name => 'John Smith',  phone => '0123456x1' },
+        { name => 'Leo Tolstoy', phone => '0123456x2' },
+    ]
 );
 
-my @booksellers = C4::Bookseller::GetBookSeller('my vendor');
+@booksellers = Koha::Acquisition::Bookseller->search({ name => 'my vendor' });
 ok(
-    (grep { $_->{'id'} == $booksellerid } @booksellers),
-    'GetBookSeller returns correct record when passed a name'
+    ( grep { $_->{'id'} == $booksellerid } @booksellers ),
+    'Koha::Acquisition::Bookseller->search returns correct record when passed a name'
 );
 
-my $bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
+my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
 is( $bookseller->{'id'}, $booksellerid, 'Retrieved desired record' );
 is( $bookseller->{'phone'}, '0123456', 'New bookseller has expected phone' );
-is( ref $bookseller->{'contacts'},
-    'ARRAY', 'GetBookSellerFromId returns arrayref of contacts' );
+my $contacts = $bookseller->contacts;
+is( ref $bookseller->contacts,
+    'ARRAY', 'Koha::Acquisition::Bookseller->fetch returns arrayref of contacts' );
 is(
     ref $bookseller->{'contacts'}->[0],
     'C4::Bookseller::Contact',
@@ -731,29 +709,37 @@ is(
 );
 is( $bookseller->{'contacts'}->[0]->phone,
     '0123456x1', 'Contact has expected phone number' );
+is( scalar @{ $bookseller->{'contacts'} }, 2, 'Saved two contacts' );
 
+pop @{ $bookseller->{'contacts'} };
 $bookseller->{'name'} = 'your vendor';
 $bookseller->{'contacts'}->[0]->phone('654321');
 C4::Bookseller::ModBookseller($bookseller);
 
-$bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
+$bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+$contacts = $bookseller->contacts;
 is( $bookseller->{'name'}, 'your vendor',
     'Successfully changed name of vendor' );
-is( $bookseller->{'contacts'}->[0]->phone,
+is( $contacts->[0]->phone,
     '654321',
     'Successfully changed contact phone number by modifying bookseller hash' );
+is( scalar @$contacts,
+    1, 'Only one contact after modification' );
 
 C4::Bookseller::ModBookseller( $bookseller,
     [ { name => 'John Jacob Jingleheimer Schmidt' } ] );
 
-$bookseller = C4::Bookseller::GetBookSellerFromId($booksellerid);
+$bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+$contacts = $bookseller->contacts;
 is(
-    $bookseller->{'contacts'}->[0]->name,
+    $contacts->[0]->name,
     'John Jacob Jingleheimer Schmidt',
     'Changed name of contact'
 );
-is( $bookseller->{'contacts'}->[0]->phone,
+is( $contacts->[0]->phone,
     undef, 'Removed phone number from contact' );
+is( scalar @$contacts,
+    1, 'Only one contact after modification' );
 
 #End transaction
 $dbh->rollback;
@@ -768,7 +754,7 @@ sub field_filter {
         'bookselleremail', 'booksellerfax',
         'booksellerurl',   'othersupplier',
         'currency',        'invoiceprice',
-        'listprice'
+        'listprice',       'contacts'
       )
     {