Bug 19926: Add tests for Koha::Object->unblessed_all_relateds
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 8 Jan 2018 16:48:28 +0000 (13:48 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 3 Apr 2018 14:43:06 +0000 (11:43 -0300)
Signed-off-by: Benjamin Rokseth <benjamin.rokseth@deichman.no>
Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
t/db_dependent/Koha/Object.t

index 542b1e2..125394c 100755 (executable)
 
 use Modern::Perl;
 
 
 use Modern::Perl;
 
-use Test::More tests => 10;
+use Test::More tests => 11;
 use Test::Exception;
 use Test::Warn;
 use Test::Exception;
 use Test::Warn;
+use DateTime;
 
 use C4::Context;
 
 use C4::Context;
+use C4::Biblio; # AddBiblio
+use C4::Circulation; # AddIssue
+use C4::Members;# AddMember
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Libraries;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Libraries;
@@ -309,3 +313,55 @@ subtest 'store() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'unblessed_all_relateds' => sub {
+    plan tests => 3;
+
+    $schema->storage->txn_begin;
+
+    # FIXME It's very painful to create an issue in tests!
+    my $library = $builder->build_object( { class => 'Koha::Libraries' } );
+    C4::Context->_new_userenv('xxx');
+    C4::Context->set_userenv(0,0,0,'firstname','surname', $library->branchcode, 'Midway Public Library', '', '', '');
+    my $patron_category = $builder->build(
+        {
+            source => 'Category',
+            value  => {
+                category_type                 => 'P',
+                enrolmentfee                  => 0,
+                BlockExpiredPatronOpacActions => -1, # Pick the pref value
+            }
+        }
+    );
+    my $patron_data = {
+        firstname =>  'firstname',
+        surname => 'surname',
+        categorycode => $patron_category->{categorycode},
+        branchcode => $library->branchcode,
+    };
+    my $borrowernumber = C4::Members::AddMember(%$patron_data);
+    my $patron = Koha::Patrons->find( $borrowernumber );
+    my ($biblionumber) = AddBiblio( MARC::Record->new, '' );
+    my $biblio = Koha::Biblios->find( $biblionumber );
+    my $item = $builder->build_object(
+        {
+            class => 'Koha::Items',
+            value => {
+                homebranch    => $library->branchcode,
+                holdingbranch => $library->branchcode,
+                biblionumber  => $biblio->biblionumber,
+                itemlost      => 0,
+                withdrawn     => 0,
+            }
+        }
+    );
+
+    my $issue = AddIssue( $patron->unblessed, $item->barcode, DateTime->now->subtract( days => 1 ) );
+    my $overdues = Koha::Patrons->find( $patron->id )->get_overdues; # Koha::Patron->get_overdue prefetches
+    my $overdue = $overdues->next->unblessed_all_relateds;
+    is( $overdue->{issue_id}, $issue->issue_id, 'unblessed_all_relateds has field from the original table (issues)' );
+    is( $overdue->{title}, $biblio->title, 'unblessed_all_relateds has field from other tables (biblio)' );
+    is( $overdue->{homebranch}, $item->homebranch, 'unblessed_all_relateds has field from other tables (items)' );
+
+    $schema->storage->txn_rollback;
+};