Bug 26984: Make tests pass if AnonymousPatron is set
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 12 Nov 2020 12:11:13 +0000 (13:11 +0100)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 20 Nov 2020 07:16:02 +0000 (08:16 +0100)
There is only 1 place where it can be tricky to not remove them as we
want exactly 10 patrons to be present in the DB

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
t/db_dependent/Accounts.t
t/db_dependent/Koha/Biblio.t
t/db_dependent/Koha/Objects.t
t/db_dependent/Template/Plugin/Categories.t

index da4d4c3..489ae7d 100755 (executable)
@@ -55,10 +55,6 @@ my $dbh = C4::Context->dbh;
 my $builder = t::lib::TestBuilder->new;
 my $library = $builder->build( { source => 'Branch' } );
 
-$dbh->do(q|DELETE FROM accountlines|);
-$dbh->do(q|DELETE FROM issues|);
-$dbh->do(q|DELETE FROM borrowers|);
-
 my $branchcode = $library->{branchcode};
 
 my $context = Test::MockModule->new('C4::Context');
@@ -1104,23 +1100,9 @@ subtest "Payment notice tests" => sub {
 
     plan tests => 8;
 
-    Koha::Account::Lines->delete();
-    Koha::Patrons->delete();
     Koha::Notice::Messages->delete();
-    # Create a borrower
-    my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
-    my $branchcode   = $builder->build({ source => 'Branch' })->{ branchcode };
-
-    my $borrower = Koha::Patron->new(
-        {
-            cardnumber   => 'chelseahall',
-            surname      => 'Hall',
-            firstname    => 'Chelsea',
-            email        => 'chelsea@example.com',
-            categorycode => $categorycode,
-            branchcode   => $branchcode,
-        }
-    )->store();
+    # Create a patron
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
 
     my $manager = $builder->build_object({ class => "Koha::Patrons" });
     my $context = Test::MockModule->new('C4::Context');
@@ -1130,11 +1112,11 @@ subtest "Payment notice tests" => sub {
             branch     => $manager->branchcode,
         };
     });
-    my $account = Koha::Account->new({ patron_id => $borrower->id });
+    my $account = Koha::Account->new({ patron_id => $patron->borrowernumber });
 
     my $line = Koha::Account::Line->new(
         {
-            borrowernumber    => $borrower->borrowernumber,
+            borrowernumber    => $patron->borrowernumber,
             amountoutstanding => 27,
             interface         => 'commandline',
             debit_type_code   => 'LOST'
index d4aac63..485b31a 100755 (executable)
@@ -185,19 +185,12 @@ subtest 'is_serial() tests' => sub {
 };
 
 subtest 'pickup_locations' => sub {
-    plan tests => 29;
+    plan tests => 8;
 
     $schema->storage->txn_begin;
 
-    my $dbh = C4::Context->dbh;
-
-    # Cleanup database
-    Koha::Holds->search->delete;
-    Koha::Patrons->search->delete;
-    Koha::Items->search->delete;
-    Koha::Libraries->search->delete;
+    my $nb_libraries = Koha::Libraries->count;
     Koha::CirculationRules->search->delete;
-    $dbh->do('DELETE FROM issues');
     Koha::CirculationRules->set_rules(
         {
             categorycode => undef,
@@ -222,6 +215,8 @@ subtest 'pickup_locations' => sub {
     my $library7 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } );
     my $library8 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 0 } } );
 
+    our @branchcodes = map { $_->branchcode } ($library1, $library2, $library3, $library4, $library5, $library6, $library7, $library8);
+
     Koha::CirculationRules->set_rules(
         {
             branchcode => $library1->branchcode,
@@ -373,11 +368,11 @@ subtest 'pickup_locations' => sub {
     my $patron8 = $builder->build_object( { class => 'Koha::Patrons', value => { firstname=>'8', branchcode => $library8->branchcode } } );
 
     my $results = {
-        "ItemHomeLibrary-1-1" => 6,
+        "ItemHomeLibrary-1-1" => 6  + $nb_libraries,
         "ItemHomeLibrary-1-8" => 1,
         "ItemHomeLibrary-2-1" => 2,
         "ItemHomeLibrary-2-8" => 0,
-        "PatronLibrary-1-1" => 6,
+        "PatronLibrary-1-1" => 6 + $nb_libraries,
         "PatronLibrary-1-8" => 3,
         "PatronLibrary-2-1" => 0,
         "PatronLibrary-2-8" => 3,
@@ -390,6 +385,9 @@ subtest 'pickup_locations' => sub {
         my @pl = $biblio->pickup_locations( { patron => $patron } );
 
         foreach my $pickup_location (@pl) {
+            next
+              unless grep { $pickup_location eq $_ } @branchcodes;
+
             is( ref($pickup_location), 'Koha::Library', 'Object type is correct' );
         }
 
index 265d327..19574f6 100755 (executable)
@@ -263,6 +263,7 @@ subtest '->is_paged and ->pager tests' => sub {
     $schema->storage->txn_begin;
 
     # Delete existing patrons
+    t::lib::Mocks::mock_preference('AnonymousPatron', '');
     Koha::Checkouts->delete;
     Koha::Patrons->delete;
     # Create 10 patrons
index b6080c7..0d99a85 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 5;
+use Test::More tests => 4;
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
@@ -30,24 +30,18 @@ use Koha::Template::Plugin::Categories;
 my $schema = Koha::Database->new->schema;
 $schema->storage->txn_begin;
 
-# Delete all categories
-Koha::Checkouts->search->delete;
-Koha::Patrons->search->delete;
-Koha::Patron::Categories->search->delete;
-
 my $builder = t::lib::TestBuilder->new;
 
-is( Koha::Template::Plugin::Categories->new->all->count,
-    0, '->all returns 0 results if no categories defined' );
+my $nb_categories = Koha::Patron::Categories->count;
 
 # Create sample categories
 my $category_1 = $builder->build( { source => 'Category' } );
 my @categories = Koha::Template::Plugin::Categories->new->all;
-is( scalar(@categories), 1, '->all returns all defined categories' );
+is( scalar(@categories), 1 + $nb_categories, '->all returns all defined categories' );
 
 my $category_2 = $builder->build( { source => 'Category' } );
 @categories = Koha::Template::Plugin::Categories->new->all;
-is( scalar(@categories), 2, '->all returns all defined categories' );
+is( scalar(@categories), 2 + $nb_categories, '->all returns all defined categories' );
 
 is( Koha::Template::Plugin::Categories->GetName(
         $category_1->{categorycode}