Bug 26145: Refactoring - Move C4::Images to Koha::CoverImages
[koha.git] / t / db_dependent / Koha / CoverImages.t
blob66067df9b76e472af10366ec9c6ad13e9659942d
1 #!/usr/bin/perl
3 # Copyright 2020 Koha Development team
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use Test::More tests => 13;
23 use Test::Exception;
25 use FindBin '$Bin';
27 use Koha::CoverImages;
28 use Koha::Database;
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
37 my $biblio = $builder->build_sample_biblio;
38 my $item = $builder->build_sample_item;
40 is( $biblio->cover_images->count, 0, 'No cover images yet' );
42 my $no_image = Koha::CoverImages->no_image;
43 is( ref($no_image), 'Koha::CoverImage',
44 'no_image returns a Koha::CoverImage object' );
45 is( $no_image->mimetype, 'image/gif', 'no_image is a gif image' );
46 ok( $no_image->imagefile, 'no_image has imagefile set' );
47 ok( $no_image->thumbnail, 'no_image has thumbnail set' );
49 my $logo_filepath =
50 "$Bin/../../../koha-tmpl/intranet-tmpl/prog/img/koha-logo.png";
51 my $image = Koha::CoverImage->new(
53 biblionumber => $biblio->biblionumber,
54 src_image => GD::Image->new($logo_filepath)
56 )->store;
58 is( $biblio->cover_images->count, 1, 'There is one cover image' );
59 my $cover_image = $biblio->cover_images->next;
60 ok( $cover_image->imagefile, 'image is stored in imagefile' );
61 ok( $cover_image->thumbnail, 'thumbnail has been generated' );
62 is( $cover_image->mimetype, 'image/png',
63 'mimetype has been correctly guessed' );
65 $image = Koha::CoverImage->new(
67 biblionumber => $biblio->biblionumber,
68 src_image => GD::Image->new($logo_filepath)
70 )->store;
71 is( $biblio->cover_images->count, 2, 'There are now two cover images' );
73 is( $item->cover_image, undef, 'No cover images yet' );
74 $image = Koha::CoverImage->new(
76 itemnumber => $item->itemnumber,
77 src_image => GD::Image->new($logo_filepath)
79 )->store;
80 is( ref( $item->cover_image ),
81 'Koha::CoverImage',
82 'Koha::Item->cover_image returns a Koha::CoverImage object' );
84 Koha::CoverImage->new(
86 biblionumber => $biblio->biblionumber,
87 itemnumber => $item->itemnumber,
88 src_image => GD::Image->new($logo_filepath)
90 )->store;
91 is( $biblio->cover_images->count, 3, );
93 $schema->storage->txn_rollback;