Bug 14778: Install fixtures for t/Images.t
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Tue, 20 Oct 2015 11:44:55 +0000 (12:44 +0100)
committerTomas Cohen Arazi <tomascohen@theke.io>
Fri, 23 Oct 2015 15:01:18 +0000 (12:01 -0300)
Note that this tests file were completely buggy before.

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
C4/Images.pm
t/Images.t

index 74b7068..936c4df 100644 (file)
@@ -102,7 +102,7 @@ sub RetrieveImage {
 
     my $dbh = C4::Context->dbh;
     my $query =
-'SELECT mimetype, imagefile, thumbnail FROM biblioimages WHERE imagenumber = ?';
+'SELECT imagenumber, mimetype, imagefile, thumbnail FROM biblioimages WHERE imagenumber = ?';
     my $sth = $dbh->prepare($query);
     $sth->execute($imagenumber);
     my $imagedata = $sth->fetchrow_hashref;
index c35d5bd..274766b 100644 (file)
@@ -2,53 +2,50 @@
 #
 #Testing C4 Images
 
-use strict;
-use warnings;
-use Test::More tests => 7;
+use Modern::Perl;
+use Test::More tests => 8;
 use Test::MockModule;
 
-BEGIN {
-    use_ok('C4::Images');
-}
-
-my $module = new Test::MockModule('C4::Context');
-$module->mock(
-    '_new_dbh',
-    sub {
-        my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
-          || die "Cannot create handle: $DBI::errstr\n";
-        return $dbh;
-    }
+use_ok('C4::Images');
+
+use Test::DBIx::Class {
+    schema_class => 'Koha::Schema',
+    connect_info => ['dbi:SQLite:dbname=:memory:','',''],
+    connect_opts => { name_sep => '.', quote_char => '`', },
+    fixture_class => '::Populate',
+}, 'Biblioimage' ;
+
+# Make the code in the module use our mocked Koha::Schema/Koha::Database
+my $db = Test::MockModule->new('Koha::Database');
+$db->mock(
+    # Schema() gives us the DB connection set up by Test::DBIx::Class
+    _new_schema => sub { return Schema(); }
 );
+
+my $biblionumber = 2;
 my $images = [
-    [ 'imagenumber', 'biblionumber', 'mimetype', 'imagefile', 'thumbnail' ],
-    [ 1, 2, 'gif',  'red',  001, 000 ],
-    [ 3, 2, 'jpeg', 'blue', 111, 110 ]
+    [ 1, $biblionumber, 'gif',  'imagefile1', 'thumbnail1' ],
+    [ 3, $biblionumber, 'jpeg', 'imagefile3', 'thumbnail3' ],
 ];
-my $dbh = C4::Context->dbh();
-
-$dbh->{mock_add_resultset} = $images;
+fixtures_ok [
+    Biblioimage => [
+        [ 'imagenumber', 'biblionumber', 'mimetype', 'imagefile', 'thumbnail' ],
+        @$images,
+    ],
+], 'add fixtures';
 
-my $image = C4::Images::RetrieveImage();
+my $image = C4::Images::RetrieveImage(1);
 
 is( $image->{'imagenumber'}, 1, 'First imagenumber is 1' );
 
-is( $image->{'mimetype'}, 'gif', 'First mimetype is red' );
+is( $image->{'mimetype'}, 'gif', 'First mimetype is gif' );
 
-is( $image->{'thumbnail'}, 001, 'First thumbnail is 001' );
+is( $image->{'thumbnail'}, 'thumbnail1', 'First thumbnail is correct' );
 
-$image = C4::Images::RetrieveImage();
-
-$image = C4::Images::RetrieveImage();
-
-$dbh->{mock_add_resultset} = $images;
-
-my @imagenumbers = C4::Images::ListImagesForBiblio();
+my @imagenumbers = C4::Images::ListImagesForBiblio($biblionumber);
 
 is( $imagenumbers[0], 1, 'imagenumber is 1' );
 
 is( $imagenumbers[1], 3, 'imagenumber is 3' );
 
-$dbh->{mock_add_resultset} = $images;
-
 is( $imagenumbers[4], undef, 'imagenumber undef' );