Bug 14899: schema changes to make the database better
[koha.git] / t / Images.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Test::More;
21 use Test::MockModule;
22
23 use Module::Load::Conditional qw/check_install/;
24
25 BEGIN {
26     if ( check_install( module => 'Test::DBIx::Class' ) ) {
27         plan tests => 8;
28     } else {
29         plan skip_all => "Need Test::DBIx::Class"
30     }
31 }
32
33 use_ok('C4::Images');
34
35 use Test::DBIx::Class {
36     schema_class => 'Koha::Schema',
37     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
38     connect_opts => { name_sep => '.', quote_char => '`', },
39     fixture_class => '::Populate',
40 }, 'Biblioimage' ;
41
42 # Make the code in the module use our mocked Koha::Schema/Koha::Database
43 my $db = Test::MockModule->new('Koha::Database');
44 $db->mock(
45     # Schema() gives us the DB connection set up by Test::DBIx::Class
46     _new_schema => sub { return Schema(); }
47 );
48
49 my $biblionumber = 2;
50 my $images = [
51     [ 1, $biblionumber, 'gif',  'imagefile1', 'thumbnail1' ],
52     [ 3, $biblionumber, 'jpeg', 'imagefile3', 'thumbnail3' ],
53 ];
54 fixtures_ok [
55     Biblioimage => [
56         [ 'imagenumber', 'biblionumber', 'mimetype', 'imagefile', 'thumbnail' ],
57         @$images,
58     ],
59 ], 'add fixtures';
60
61 my $image = C4::Images::RetrieveImage(1);
62
63 is( $image->{'imagenumber'}, 1, 'First imagenumber is 1' );
64
65 is( $image->{'mimetype'}, 'gif', 'First mimetype is gif' );
66
67 is( $image->{'thumbnail'}, 'thumbnail1', 'First thumbnail is correct' );
68
69 my @imagenumbers = C4::Images::ListImagesForBiblio($biblionumber);
70
71 is( $imagenumbers[0], 1, 'imagenumber is 1' );
72
73 is( $imagenumbers[1], 3, 'imagenumber is 3' );
74
75 is( $imagenumbers[4], undef, 'imagenumber undef' );
76
77 1;