Bug 18448: Fix a few db_dependent tests
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Sun, 16 Apr 2017 16:50:58 +0000 (18:50 +0200)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 21 Apr 2017 00:09:43 +0000 (00:09 +0000)
Tests in db_dependent may expect a Koha database, but should not rely on
hardcoded categories, currencies, branch codes, etc.

This patch fixes a bunch of those. But this is a continuous project. We also
need QA to closely watch new edits.

Accounts.t: hardcoded category PT replaced
Acquisition/OrderFromSubscription.t: hardcoded USD
Acquisition/StandingOrders.t: same
ArticleRequests.t: create itemtype, branch and category for testing
AuthorisedValues.t: remove $dbh, add two test branches
AuthoritiesMarc.t: add hardcoded GEOGR_NAME authtype
Bookseller.t: add test currency
Koha.t: add test itemtype instead of hardcoded BK
UsageStats.t: add test branch and category

Test plan:
Run the adjusted tests.

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
All tests successful (see comment #9)
Signed-off-by: Marc VĂ©ron <veron@veron.ch>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
t/db_dependent/Accounts.t
t/db_dependent/Acquisition/OrderFromSubscription.t
t/db_dependent/Acquisition/StandingOrders.t
t/db_dependent/ArticleRequests.t
t/db_dependent/AuthorisedValues.t
t/db_dependent/AuthoritiesMarc.t
t/db_dependent/Bookseller.t
t/db_dependent/Koha.t
t/db_dependent/UsageStats.t

index 505ce2f..fcc63d3 100644 (file)
@@ -103,8 +103,8 @@ my @test_data = (
     { amount => -5    , days_ago => $days     , description =>'purge_zero_balance_fees should not delete fees with negative amout owed on threshold day'     , delete => 0 } ,
     { amount => -5    , days_ago => $days + 1 , description =>'purge_zero_balance_fees should not delete fees with negative amout owed after threshold day'  , delete => 0 }
 );
-
-my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => 'PT', branchcode => $branchcode } )->store();
+my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+my $borrower = Koha::Patron->new( { firstname => 'Test', surname => 'Patron', categorycode => $categorycode, branchcode => $branchcode } )->store();
 
 for my $data ( @test_data ) {
     $sth->execute($borrower->borrowernumber, $data->{amount}, $data->{days_ago}, $data->{description});
index 4641894..4f318b2 100644 (file)
@@ -2,6 +2,8 @@ use Modern::Perl;
 
 use Test::More tests => 12;
 
+use t::lib::TestBuilder;
+
 use_ok('C4::Acquisition');
 use_ok('C4::Biblio');
 use_ok('C4::Budgets');
@@ -13,9 +15,12 @@ use Koha::Database;
 # Start transaction
 my $schema = Koha::Database->new()->schema();
 $schema->storage->txn_begin();
+my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
 $dbh->{RaiseError} = 1;
 
+my $curcode = $builder->build({ source => 'Currency' })->{currencycode};
+
 my $bookseller = Koha::Acquisition::Bookseller->new(
     {
         name => "my vendor",
@@ -61,7 +66,7 @@ my $order = Koha::Acquisition::Order->new({
     biblionumber => $subscription->{biblionumber},
     entrydate => '01-01-2013',
     quantity => 1,
-    currency => 'USD',
+    currency => $curcode,
     listprice => $cost,
     notes => "This is a note",
     basketno => $basketno,
index f0e7dc8..9750bae 100644 (file)
@@ -22,6 +22,7 @@ my $branch = $builder->build( { source => 'Branch' } );
 my $bookseller = $builder->build( { source => 'Aqbookseller' } );
 my $budget = $builder->build( { source => 'Aqbudget' } );
 my $staffmember = $builder->build( { source => 'Borrower' } );
+my $curcode = $builder->build( { source => 'Currency' })->{currencycode};
 
 # Create baskets and orders
 
@@ -61,7 +62,7 @@ my $ordernumber = Koha::Acquisition::Order->new(
         basketno                 => $basketno,
         biblionumber             => $biblionumber,
         budget_id                => $budget->{budget_id},
-        currency                 => 'USD',
+        currency                 => $curcode,
         quantity                 => 0,
         rrp                      => 42,
         rrp_tax_included         => 42,
index af9e1c8..482cce2 100755 (executable)
@@ -20,8 +20,10 @@ use Modern::Perl;
 use POSIX qw(strftime);
 
 use Test::More tests => 49;
-use Koha::Database;
 
+use t::lib::TestBuilder;
+
+use Koha::Database;
 use Koha::Biblio;
 use Koha::Patron;
 use Koha::Library;
@@ -34,6 +36,7 @@ BEGIN {
 
 my $schema = Koha::Database->new()->schema();
 $schema->storage->txn_begin();
+my $builder = t::lib::TestBuilder->new;
 
 my $dbh = C4::Context->dbh;
 $dbh->{RaiseError} = 1;
@@ -50,21 +53,22 @@ my $biblioitem = $schema->resultset('Biblioitem')->new(
 )->insert();
 ok( $biblioitem->id, 'biblioitem created' );
 
+my $itype = $builder->build({ source => 'Itemtype' });
 my $item = Koha::Item->new(
     {
         biblionumber     => $biblio->id,
         biblioitemnumber => $biblioitem->id,
-        itype => $schema->resultset('Itemtype')->search()->next()->itemtype(),
+        itype => $itype->{itype},
     }
 )->store();
 ok( $item->id, 'Koha::Item created' );
 
-my $branch   = Koha::Libraries->search()->next();
-my $category = $schema->resultset('Category')->next();
+my $branch   = $builder->build({ source => 'Branch' });
+my $category = $builder->build({ source => 'Category' });
 my $patron   = Koha::Patron->new(
     {
-        categorycode => $category->id,
-        branchcode   => $branch->id,
+        categorycode => $category->{categorycode},
+        branchcode   => $branch->{branchcode},
     }
 )->store();
 ok( $patron->id, 'Koha::Patron created' );
index 6d44a72..ad5ec60 100644 (file)
@@ -3,18 +3,21 @@
 use Modern::Perl;
 use Test::More tests => 15;
 
+use t::lib::TestBuilder;
+
+use Koha::Database;
 use C4::Context;
 use Koha::AuthorisedValue;
 use Koha::AuthorisedValues;
 use Koha::AuthorisedValueCategories;
 use Koha::MarcSubfieldStructures;
 
-my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
 
-$dbh->do("DELETE FROM authorised_values");
-$dbh->do("DELETE FROM authorised_value_categories");
+Koha::AuthorisedValues->delete;
+Koha::AuthorisedValueCategories->delete;
 
 # insert
 Koha::AuthorisedValueCategory->new({ category_name => 'av_for_testing' })->store;
@@ -89,11 +92,8 @@ my @authorised_values =
   Koha::AuthorisedValues->new()->search( { category => 'av_for_testing' } );
 is( @authorised_values, 3, "Get correct number of values" );
 
-my $branches_rs = Koha::Database->new()->schema()->resultset('Branch')->search();
-my $branch1 = $branches_rs->next();
-my $branchcode1 = $branch1->branchcode();
-my $branch2 = $branches_rs->next();
-my $branchcode2 = $branch2->branchcode();
+my $branchcode1 = $builder->build({ source => 'Branch' })->{branchcode};
+my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode};
 
 $av1->add_branch_limitation( $branchcode1 );
 
@@ -225,3 +225,5 @@ subtest 'search_by_*_field + find_by_koha_field + get_description' => sub {
         );
     };
 };
+
+$schema->storage->txn_rollback;
index c9b44b4..a24d917 100755 (executable)
@@ -11,7 +11,9 @@ use Test::Warn;
 use MARC::Record;
 
 use t::lib::Mocks;
+use t::lib::TestBuilder;
 use Koha::Database;
+use Koha::Authority::Types;
 
 BEGIN {
         use_ok('C4::AuthoritiesMarc');
@@ -61,9 +63,15 @@ $module->mock('GetAuthority', sub {
 my $schema  = Koha::Database->new->schema;
 $schema->storage->txn_begin;
 my $dbh = C4::Context->dbh;
+my $builder = t::lib::TestBuilder->new;
 
 t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
 
+# Authority type GEOGR_NAME is hardcoded here
+if( ! Koha::Authority::Types->find('GEOGR_NAME') ) {
+    $builder->build({ source => 'AuthType', value => { authtypecode => 'GEOGR_NAME' }});
+};
+
 is(BuildAuthHierarchies(3, 1), '1,2,3', "Built linked authtrees hierarchy string");
 
 my $expectedhierarchy = [ [ {
index 77cf3ef..96e0837 100644 (file)
@@ -6,6 +6,8 @@ use Test::More tests => 86;
 use Test::MockModule;
 use Test::Warn;
 
+use t::lib::TestBuilder;
+
 use C4::Context;
 use Koha::DateUtils;
 use DateTime::Duration;
@@ -35,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|);
@@ -44,6 +46,9 @@ $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            = Koha::Acquisition::Booksellers->search()->count();
 my $sample_supplier1 = {
@@ -350,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,
@@ -370,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,
@@ -388,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,
@@ -406,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,
index b3f51e0..258289d 100644 (file)
@@ -4,22 +4,28 @@
 # It requires a working Koha database with the sample data
 
 use Modern::Perl;
+use DateTime::Format::MySQL;
+use Test::More tests => 7;
+
+use t::lib::TestBuilder;
+
 use C4::Context;
+use Koha::Database;
 use Koha::DateUtils qw(dt_from_string);
 use Koha::AuthorisedValue;
 use Koha::AuthorisedValueCategories;
 
-use Test::More tests => 7;
-use DateTime::Format::MySQL;
-
 BEGIN {
     use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesCategorized));
     use_ok('C4::Members');
 }
 
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+
+our $itype_1 = $builder->build({ source => 'Itemtype' });
 
 subtest 'Authorized Values Tests' => sub {
     plan tests => 3;
@@ -152,9 +158,9 @@ subtest 'Authorized Values Tests' => sub {
 };
 
 subtest 'Itemtype info Tests' => sub {
-    like ( getitemtypeinfo('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
-    like ( getitemtypeinfo('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
-    like ( getitemtypeinfo('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
+    like ( getitemtypeinfo( $itype_1->{itemtype} )->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
+    like ( getitemtypeinfo( $itype_1->{itemtype}, 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
+    like ( getitemtypeinfo( $itype_1->{itemtype}, 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
 };
 
 ### test for C4::Koha->GetDailyQuote()
@@ -294,4 +300,4 @@ subtest 'GetItemTypesCategorized test' => sub{
     is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
 };
 
-$dbh->rollback();
+$schema->storage->txn_rollback;
index d11cbd4..5f4dfad 100644 (file)
@@ -17,6 +17,7 @@
 use Modern::Perl;
 use Test::More tests => 57;
 use t::lib::Mocks qw(mock_preference);
+use t::lib::TestBuilder;
 use POSIX qw(strftime);
 use Data::Dumper;
 use Koha::Biblios;
@@ -41,9 +42,10 @@ can_ok(
       _count )
 );
 
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
 
 $dbh->do('DELETE FROM issues');
 $dbh->do('DELETE FROM biblio');
@@ -213,8 +215,8 @@ sub construct_objects_needed {
     my $cardnumber1  = 'test_card1';
     my $cardnumber2  = 'test_card2';
     my $cardnumber3  = 'test_card3';
-    my $categorycode = Koha::Database->new()->schema()->resultset('Category')->first()->categorycode();
-    my $branchcode   = Koha::Database->new()->schema()->resultset('Branch')->first()->branchcode();
+    my $categorycode = $builder->build({ source => 'Category' })->{categorycode};
+    my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
 
     my $query = '
     INSERT INTO borrowers
@@ -613,4 +615,4 @@ sub verif_systempreferences_values {
     }
 }
 
-$dbh->rollback;
+$schema->storage->txn_rollback;