Bug 9031: (QA follow-up) Pass the same timezone in Calendar.t
[koha.git] / t / db_dependent / Bookseller.t
index 54765bd..416974c 100644 (file)
@@ -2,10 +2,12 @@
 
 use Modern::Perl;
 
-use Test::More tests => 88;
+use Test::More tests => 86;
 use Test::MockModule;
 use Test::Warn;
 
+use t::lib::TestBuilder;
+
 use C4::Context;
 use Koha::DateUtils;
 use DateTime::Duration;
@@ -15,21 +17,19 @@ use C4::Serials;
 use C4::Budgets;
 use C4::Biblio;
 
-use Koha::Acquisition::Order;
+use Koha::Acquisition::Booksellers;
+use Koha::Acquisition::Orders;
 use Koha::Database;
 
 BEGIN {
     use_ok('C4::Bookseller');
-    use_ok('Koha::Acquisition::Bookseller');
 }
 
 can_ok(
 
     'C4::Bookseller', qw(
-      AddBookseller
-      DelBookseller
       GetBooksellersWithLateOrders
-      ModBookseller )
+    )
 );
 
 #Start transaction
@@ -37,8 +37,8 @@ my $dbh = C4::Context->dbh;
 my $database = Koha::Database->new();
 my $schema = $database->schema();
 $schema->storage->txn_begin();
-
 $dbh->{RaiseError} = 1;
+my $builder = t::lib::TestBuilder->new;
 
 #Start tests
 $dbh->do(q|DELETE FROM aqorders|);
@@ -46,8 +46,11 @@ $dbh->do(q|DELETE FROM aqbasket|);
 $dbh->do(q|DELETE FROM aqbooksellers|);
 $dbh->do(q|DELETE FROM subscription|);
 
+# Add currency
+my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
+
 #Test AddBookseller
-my $count            = scalar( Koha::Acquisition::Bookseller->search() );
+my $count            = Koha::Acquisition::Booksellers->search()->count();
 my $sample_supplier1 = {
     name          => 'Name1',
     address1      => 'address1_1',
@@ -89,69 +92,70 @@ my $sample_supplier2 = {
     deliverytime  => 2
 };
 
-my $id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
-my $id_supplier2 = C4::Bookseller::AddBookseller($sample_supplier2);
-
-#my $id_bookseller3 = C4::Bookseller::AddBookseller();# NOTE : Doesn't work because the field name cannot be null
+my $supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
+my $id_supplier1 = $supplier1->id;
+my $supplier2 = Koha::Acquisition::Bookseller->new($sample_supplier2)->store;
+my $id_supplier2 = $supplier2->id;
 
 like( $id_supplier1, '/^\d+$/', "AddBookseller for supplier1 return an id" );
 like( $id_supplier2, '/^\d+$/', "AddBookseller for supplier2 return an id" );
-my @b = Koha::Acquisition::Bookseller->search();
-is ( scalar(@b),
+is( Koha::Acquisition::Booksellers->search()->count,
     $count + 2, "Supplier1 and Supplier2 have been added" );
 
 #Test DelBookseller
-my $del = C4::Bookseller::DelBookseller($id_supplier1);
+my $del = $supplier1->delete;
 is( $del, 1, "DelBookseller returns 1 - 1 supplier has been deleted " );
-my $b = Koha::Acquisition::Bookseller->fetch({id => $id_supplier1});
+my $b = Koha::Acquisition::Booksellers->find( $id_supplier1 );
 is( $b,
     undef, "Supplier1  has been deleted - id_supplier1 $id_supplier1 doesnt exist anymore" );
 
 #Test get bookseller
-my @bookseller2 = Koha::Acquisition::Bookseller->search({name => $sample_supplier2->{name} });
+my @bookseller2 = Koha::Acquisition::Booksellers->search({name => $sample_supplier2->{name} });
 is( scalar(@bookseller2), 1, "Get only  Supplier2" );
-$bookseller2[0] = field_filter( $bookseller2[0] );
+for my $bookseller ( @bookseller2 ) {
+    $bookseller = field_filter( $bookseller->unblessed );
+}
 
 $sample_supplier2->{id} = $id_supplier2;
 is_deeply( $bookseller2[0], $sample_supplier2,
-    "Koha::Acquisition::Bookseller->search returns the right informations about $sample_supplier2" );
+    "Koha::Acquisition::Booksellers->search returns the right informations about supplier $sample_supplier2->{name}" );
 
-$id_supplier1 = C4::Bookseller::AddBookseller($sample_supplier1);
-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] );
+$supplier1 = Koha::Acquisition::Bookseller->new($sample_supplier1)->store;
+$id_supplier1 = $supplier1->id;
+my @booksellers = Koha::Acquisition::Booksellers->search();
+for my $bookseller ( @booksellers ) {
+    $bookseller = field_filter( $bookseller->unblessed );
 }
 
 $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" );
 
-#Test basket_count
-my @bookseller1 = Koha::Acquisition::Bookseller->search({name => $sample_supplier1->{name} });
-is( $bookseller1[0]->basket_count, 0, 'Supplier1 has 0 basket' );
+#Test baskets
+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' );
 my $basketno2 =
   C4::Acquisition::NewBasket( $id_supplier1, 'authorisedby2', 'basketname2' );
-@bookseller1 = Koha::Acquisition::Bookseller::search({ name => $sample_supplier1->{name} });
-is( $bookseller1[0]->basket_count, 2, 'Supplier1 has 2 baskets' );
+@bookseller1 = Koha::Acquisition::Booksellers->search({ name => $sample_supplier1->{name} });
+is( $bookseller1[0]->baskets->count, 2, 'Supplier1 has 2 baskets' );
 
 #Test Koha::Acquisition::Bookseller->new using id
-my $bookseller1fromid = Koha::Acquisition::Bookseller->fetch;
+my $bookseller1fromid = Koha::Acquisition::Booksellers->find;
 is( $bookseller1fromid, undef,
-    "fetch returns undef if no id given" );
-$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
-$bookseller1fromid = field_filter($bookseller1fromid);
+    "find returns undef if no id given" );
+$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
+$bookseller1fromid = field_filter($bookseller1fromid->unblessed);
 is_deeply( $bookseller1fromid, $sample_supplier1,
-    "Get Supplier1 (fetch a bookseller by id)" );
+    "Get Supplier1 (find a bookseller by id)" );
 
-#Test basket_count
-$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1});
-is( $bookseller1fromid->basket_count, 2, 'Supplier1 has 2 baskets' );
+$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
+is( $bookseller1fromid->baskets->count, 2, 'Supplier1 has 2 baskets' );
 
-#Test subscription_count
+#Test subscriptions
 my $dt_today    = dt_from_string;
 my $today       = output_pref({ dt => $dt_today, dateformat => 'iso', timeformat => '24hr', dateonly => 1 });
 
@@ -178,8 +182,8 @@ $bib->append_fields(
     MARC::Field->new('500', ' ', ' ', a => 'bib notes'),
 );
 my ($biblionumber, $biblioitemnumber) = AddBiblio($bib, '');
-$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
-is( $bookseller1fromid->subscription_count,
+$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
+is( $bookseller1fromid->subscriptions->count,
     0, 'Supplier1 has 0 subscription' );
 
 my $id_subscription1 = NewSubscription(
@@ -203,8 +207,8 @@ my $id_subscription2 = NewSubscription(
     undef, undef, 0,          undef,         '2013-07-31', 0
 );
 
-$bookseller1fromid = Koha::Acquisition::Bookseller->fetch({ id => $id_supplier1 });
-is( $bookseller1fromid->subscription_count,
+$bookseller1fromid = Koha::Acquisition::Booksellers->find( $id_supplier1 );
+is( $bookseller1fromid->subscriptions->count,
     2, 'Supplier1 has 2 subscriptions' );
 
 #Test ModBookseller
@@ -230,22 +234,12 @@ $sample_supplier2 = {
     deliverytime  => 2,
 };
 
-my $modif1 = C4::Bookseller::ModBookseller();
-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( Koha::Acquisition::Bookseller->search ),
+my $modif1 = Koha::Acquisition::Booksellers->find($id_supplier2)->set($sample_supplier2)->store;
+is( ref $modif1, 'Koha::Acquisition::Bookseller', "ModBookseller has updated the bookseller" );
+is( Koha::Acquisition::Booksellers->search->count,
     $count + 2, "Supplier2 has been modified - Nothing added" );
-
-$modif1 = C4::Bookseller::ModBookseller(
-    {
-        id   => -1,
-        name => 'name3'
-    }
-);
-#is( $modif1, '0E0',
-#    "ModBookseller returns OEO if the id doesnt exist - Nothing modified" );
+$supplier2 = Koha::Acquisition::Booksellers->find($id_supplier2);
+is( $supplier2->name, 'Name2 modified', "supplier's name should have been modified" );
 
 #Test GetBooksellersWithLateOrders
 #Add 2 suppliers
@@ -288,8 +282,10 @@ my $sample_supplier4 = {
     discount      => '3.0000',
     notes         => 'notes3',
 };
-my $id_supplier3 = C4::Bookseller::AddBookseller($sample_supplier3);
-my $id_supplier4 = C4::Bookseller::AddBookseller($sample_supplier4);
+my $supplier3 = Koha::Acquisition::Bookseller->new($sample_supplier3)->store;
+my $id_supplier3 = $supplier3->id;
+my $supplier4 = Koha::Acquisition::Bookseller->new($sample_supplier4)->store;
+my $id_supplier4 = $supplier4->id;
 
 #Add 2 baskets
 my $basketno3 =
@@ -359,7 +355,7 @@ my $order1 = Koha::Acquisition::Order->new(
         biblionumber     => $biblionumber,
         budget_id        => $id_budget,
         entrydate        => '01-01-2013',
-        currency         => 'EUR',
+        currency         => $curcode,
         notes            => "This is a note1",
         tax_rate          => 0.0500,
         orderstatus      => 1,
@@ -369,8 +365,8 @@ my $order1 = Koha::Acquisition::Order->new(
         ecost            => 10,
         datereceived     => '01-06-2013'
     }
-)->insert;
-my $ordernumber1 = $order1->{ordernumber};
+)->store;
+my $ordernumber1 = $order1->ordernumber;
 
 my $order2 = Koha::Acquisition::Order->new(
     {
@@ -379,7 +375,7 @@ my $order2 = Koha::Acquisition::Order->new(
         biblionumber   => $biblionumber,
         budget_id      => $id_budget,
         entrydate      => '01-01-2013',
-        currency       => 'EUR',
+        currency       => $curcode,
         notes          => "This is a note2",
         tax_rate        => 0.0500,
         orderstatus    => 1,
@@ -387,8 +383,8 @@ my $order2 = Koha::Acquisition::Order->new(
         rrp            => 10,
         ecost          => 10,
     }
-)->insert;
-my $ordernumber2 = $order2->{ordernumber};
+)->store;
+my $ordernumber2 = $order2->ordernumber;
 
 my $order3 = Koha::Acquisition::Order->new(
     {
@@ -397,7 +393,7 @@ my $order3 = Koha::Acquisition::Order->new(
         biblionumber   => $biblionumber,
         budget_id      => $id_budget,
         entrydate      => '02-02-2013',
-        currency       => 'EUR',
+        currency       => $curcode,
         notes          => "This is a note3",
         tax_rate        => 0.0500,
         orderstatus    => 2,
@@ -405,8 +401,8 @@ my $order3 = Koha::Acquisition::Order->new(
         rrp            => 11,
         ecost          => 11,
     }
-)->insert;
-my $ordernumber3 = $order3->{ordernumber};
+)->store;
+my $ordernumber3 = $order3->ordernumber;
 
 my $order4 = Koha::Acquisition::Order->new(
     {
@@ -415,7 +411,7 @@ my $order4 = Koha::Acquisition::Order->new(
         biblionumber     => $biblionumber,
         budget_id        => $id_budget,
         entrydate        => '02-02-2013',
-        currency         => 'EUR',
+        currency         => $curcode,
         notes            => "This is a note3",
         tax_rate          => 0.0500,
         orderstatus      => 2,
@@ -424,8 +420,8 @@ my $order4 = Koha::Acquisition::Order->new(
         ecost            => 11,
         quantityreceived => 20
     }
-)->insert;
-my $ordernumber4 = $order4->{ordernumber};
+)->store;
+my $ordernumber4 = $order4->ordernumber;
 
 #Test cases:
 # Sample datas :
@@ -681,69 +677,83 @@ is(
     3,
     'superlibrarian can see all subscriptions with IndependentBranches on (bug 12048)'
 );
+
 #Test contact editing
-my $booksellerid = C4::Bookseller::AddBookseller(
-    {
+my $sample_supplier = {
         name     => "my vendor",
         address1 => "bookseller's address",
         phone    => "0123456",
         active   => 1
-    },
-    [
-        { name => 'John Smith',  phone => '0123456x1' },
-        { name => 'Leo Tolstoy', phone => '0123456x2' },
-    ]
-);
-
-@booksellers = Koha::Acquisition::Bookseller->search({ name => 'my vendor' });
+    };
+my $supplier = Koha::Acquisition::Bookseller->new($sample_supplier)->store;
+my $booksellerid = $supplier->id;
+my $contact1 = Koha::Acquisition::Bookseller::Contact->new({
+        name => 'John Smith',
+        phone => '0123456x1',
+        booksellerid => $booksellerid,
+})->store;
+my $contact2 = Koha::Acquisition::Bookseller::Contact->new({
+        name => 'Leo Tolstoy',
+        phone => '0123456x2',
+        booksellerid => $booksellerid,
+})->store;
+
+@booksellers = Koha::Acquisition::Booksellers->search({ name => 'my vendor' });
 ok(
-    ( grep { $_->{'id'} == $booksellerid } @booksellers ),
-    'Koha::Acquisition::Bookseller->search returns correct record when passed a name'
+    ( grep { $_->id == $booksellerid } @booksellers ),
+    'Koha::Acquisition::Booksellers->search returns correct record when passed a name'
 );
 
-my $bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
-is( $bookseller->{'id'}, $booksellerid, 'Retrieved desired record' );
-is( $bookseller->{'phone'}, '0123456', 'New bookseller has expected phone' );
+my $bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
+is( $bookseller->id, $booksellerid, 'Retrieved desired record' );
+is( $bookseller->phone, '0123456', 'New bookseller has expected phone' );
 my $contacts = $bookseller->contacts;
-is( ref $bookseller->contacts,
-    'ARRAY', 'Koha::Acquisition::Bookseller->fetch returns arrayref of contacts' );
+is( $contacts->count,
+    2, 'bookseller should have 2 contacts' );
+my $first_contact = $contacts->next;
 is(
-    ref $bookseller->{'contacts'}->[0],
-    'C4::Bookseller::Contact',
-    'First contact is a contact object'
+    ref $first_contact,
+    'Koha::Acquisition::Bookseller::Contact',
+    'First contact is a AqContact'
 );
-is( $bookseller->{'contacts'}->[0]->phone,
+is( $first_contact->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 = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+my $second_contact = $contacts->next;
+$second_contact->delete;
+$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
+$bookseller->name('your vendor')->store;
 $contacts = $bookseller->contacts;
-is( $bookseller->{'name'}, 'your vendor',
+$first_contact = $contacts->next;
+$first_contact->phone('654321');
+$first_contact->store;
+
+$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
+is( $bookseller->name, 'your vendor',
     'Successfully changed name of vendor' );
-is( $contacts->[0]->phone,
+$contacts = $bookseller->contacts;
+is( $contacts->count,
+    1, 'Only one contact after modification' );
+$first_contact = $contacts->next;
+is( $first_contact->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' } ] );
+$first_contact->name( 'John Jacob Jingleheimer Schmidt' );
+$first_contact->phone(undef);
+$first_contact->store;
 
-$bookseller = Koha::Acquisition::Bookseller->fetch({ id => $booksellerid });
+$bookseller = Koha::Acquisition::Booksellers->find( $booksellerid );
 $contacts = $bookseller->contacts;
+$first_contact = $contacts->next;
 is(
-    $contacts->[0]->name,
+    $first_contact->name,
     'John Jacob Jingleheimer Schmidt',
     'Changed name of contact'
 );
-is( $contacts->[0]->phone,
+is( $first_contact->phone,
     undef, 'Removed phone number from contact' );
-is( scalar @$contacts,
+is( $contacts->count,
     1, 'Only one contact after modification' );
 
 #End transaction