Bug 21971: TestBuilder::build_sample_item
[koha.git] / t / lib / TestBuilder.pm
index c66291c..be2a834 100644 (file)
@@ -4,13 +4,19 @@ use Modern::Perl;
 
 use Koha::Database;
 use C4::Biblio;
+use C4::Items;
 use Koha::Biblios;
+use Koha::Items;
 
 use Bytes::Random::Secure;
 use Carp;
 use Module::Load;
 use String::Random;
 
+use constant {
+    SIZE_BARCODE => 20, # Not perfect but avoid to fetch the value when creating a new item
+};
+
 sub new {
     my ($class) = @_;
     my $self = {};
@@ -158,6 +164,30 @@ sub build_sample_biblio {
     return Koha::Biblios->find($biblio_id);
 }
 
+sub build_sample_item {
+    my ( $self, $args ) = @_;
+
+    my $biblionumber =
+      delete $args->{biblionumber} || $self->build_sample_biblio->biblionumber;
+    my $library = delete $args->{library}
+      || $self->build_object( { class => 'Koha::Libraries' } )->branchcode;
+
+    my $itype = delete $args->{itype}
+      || $self->build_object( { class => 'Koha::ItemTypes' } )->itemtype;
+
+    my ( undef, undef, $itemnumber ) = C4::Items::AddItem(
+        {
+            homebranch    => $library,
+            holdingbranch => $library,
+            barcode       => $self->_gen_text( { info => { size => SIZE_BARCODE } } ),
+            itype         => $itype,
+            %$args,
+        },
+        $biblionumber
+    );
+    return Koha::Items->find($itemnumber);
+}
+
 # ------------------------------------------------------------------------------
 # Internal helper routines