Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / lib / TestBuilder.pm
index cceacc1..e8b0dbf 100644 (file)
@@ -2,16 +2,15 @@ package t::lib::TestBuilder;
 
 use Modern::Perl;
 
-use Koha::Database;
-use C4::Biblio;
-use C4::Items;
-use Koha::Biblios;
-use Koha::Items;
+use Koha::Database qw( schema );
+use C4::Biblio qw( AddBiblio );
+use Koha::Biblios qw( _type );
+use Koha::Items qw( _type );
 use Koha::DateUtils qw( dt_from_string );
 
 use Bytes::Random::Secure;
-use Carp;
-use Module::Load;
+use Carp qw( carp );
+use Module::Load qw( load );
 use String::Random;
 
 use constant {
@@ -86,6 +85,8 @@ sub build_object {
     my $object;
     if ( $class eq 'Koha::Old::Patrons' ) {
         $object = $class->search({ borrowernumber => $hashref->{borrowernumber} })->next;
+    } elsif ( $class eq 'Koha::Statistics' ) {
+        $object = $class->search({ datetime => $hashref->{datetime} })->next;
     } else {
         my @ids;
         my @pks = $self->schema->source( $class->_type )->primary_columns;
@@ -153,6 +154,8 @@ sub build_sample_biblio {
     my $marcflavour = C4::Context->preference('marcflavour');
 
     my $record = MARC::Record->new();
+    $record->encoding( 'UTF-8' );
+
     my ( $tag, $subfield ) = $marcflavour eq 'UNIMARC' ? ( 200, 'a' ) : ( 245, 'a' );
     $record->append_fields(
         MARC::Field->new( $tag, ' ', ' ', $subfield => $title ),
@@ -180,23 +183,20 @@ sub build_sample_item {
     my $library = delete $args->{library}
       || $self->build_object( { class => 'Koha::Libraries' } )->branchcode;
 
-    my $itype = delete $args->{itype}
-      || $self->build_object( { class => 'Koha::ItemTypes' } )->itemtype;
+    # If itype is not passed it will be picked from the biblio (see Koha::Item->store)
 
     my $barcode = delete $args->{barcode}
       || $self->_gen_text( { info => { size => SIZE_BARCODE } } );
 
-    my ( undef, undef, $itemnumber ) = C4::Items::AddItem(
+    return Koha::Item->new(
         {
+            biblionumber  => $biblionumber,
             homebranch    => $library,
             holdingbranch => $library,
             barcode       => $barcode,
-            itype         => $itype,
             %$args,
-        },
-        $biblionumber
-    );
-    return Koha::Items->find($itemnumber);
+        }
+    )->store->get_from_storage;
 }
 
 # ------------------------------------------------------------------------------
@@ -230,8 +230,15 @@ sub _create_links {
     if( $cnt_scalar == @$keys ) {
         # if one or more fk cols are null, the FK constraint will not be forced
         return {} if $cnt_null > 0;
+
         # does the record exist already?
-        return {} if $self->schema->resultset($linked_tbl)->find( $fk_value );
+        my @pks = $self->schema->source( $linked_tbl )->primary_columns;
+        my %fk_pk_value;
+        for (@pks) {
+            $fk_pk_value{$_} = $fk_value->{$_} if defined $fk_value->{$_};
+        }
+        return {} if !(keys %fk_pk_value);
+        return {} if $self->schema->resultset($linked_tbl)->find( \%fk_pk_value );
     }
     # create record with a recursive build call
     my $row = $self->build({ source => $linked_tbl, value => $fk_value });
@@ -493,6 +500,7 @@ sub _gen_real {
     if( defined( $params->{info}->{size} ) ) {
         $max = 10 ** ($params->{info}->{size}->[0] - $params->{info}->{size}->[1]);
     }
+    $max = 10 ** 5 if $max > 10 ** 5;
     return sprintf("%.2f", rand($max-0.1));
 }
 
@@ -551,6 +559,8 @@ sub _gen_default_values {
             itemlost           => 0,
             withdrawn          => 0,
             restricted         => 0,
+            damaged            => 0,
+            materials          => undef,
             more_subfields_xml => undef,
         },
         Category => {
@@ -558,6 +568,14 @@ sub _gen_default_values {
             reservefee   => 0,
             # Not X, used for statistics
             category_type => sub { return [ qw( A C S I P ) ]->[int(rand(5))] },
+            min_password_length => undef,
+            require_strong_password => undef,
+        },
+        Branch => {
+            pickup_location => 0,
+        },
+        Reserve => {
+            non_priority => 0,
         },
         Itemtype => {
             rentalcharge => 0,
@@ -574,8 +592,8 @@ sub _gen_default_values {
         AuthHeader => {
             marcxml => '',
         },
-        RefundLostItemFeeRules => {
-            rule_name => 'refund',
+        BorrowerAttributeType => {
+            mandatory => 0,
         },
     };
 }