Skip to content

Commit b1c30c5

Browse files
author
Mike.lifeguard
committed
Rewrite get_image test using better data
Previously, we used a file that was deleted without anybody noticing. Now, we use the same image as is used in the API docs, which is very unlikely to disappear from beneath our feet.
1 parent 5c2a6f1 commit b1c30c5

File tree

2 files changed

+54
-46
lines changed

2 files changed

+54
-46
lines changed

lib/MediaWiki/Bot.pm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -977,14 +977,14 @@ sub get_pages {
977977
$buffer = $bot->get_image('File::Foo.jpg', {width=>256, height=>256});
978978
979979
Download an image from a wiki. This is derived from a similar function in
980-
MediaWiki::API. This one allows the image to be scaled down by passing a hashref
980+
L<MediaWiki::API>. This one allows the image to be scaled down by passing a hashref
981981
with height & width parameters.
982982
983983
It returns raw data in the original format. You may simply spew it to a file, or
984984
process it directly with a library such as L<Imager>.
985985
986+
use File::Slurp qw(write_file);
986987
my $img_data = $bot->get_image('File::Foo.jpg');
987-
use File::Slurp;
988988
write_file( 'Foo.jpg', {binmode => ':raw'}, \$img_data );
989989
990990
Images are scaled proportionally. (height/width) will remain
@@ -1011,8 +1011,8 @@ sub get_image{
10111011
prop => 'imageinfo',
10121012
iiprop => 'url|size',
10131013
%sizeparams
1014-
} ) or return $self->_handle_api_error();
1015-
1014+
});
1015+
return $self->_handle_api_error() unless $ref;
10161016
my ($pageref) = values %{ $ref->{query}->{pages} };
10171017
return unless defined $pageref->{imageinfo}; # if the image is missing
10181018

t/48-get_image.t

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,71 @@
11
use strict;
22
use warnings;
3-
use Test::More 0.88;
3+
use Test::More 0.96;
44

55
use MediaWiki::Bot;
66
my $t = __FILE__;
77

8-
unless (eval q{use Imager; 1 }) {
9-
plan skip_all => q{Imager required};
10-
exit;
8+
plan eval q{use Imager; 1 }
9+
? (tests => 3)
10+
: (skip_all => q{Imager required});
11+
12+
my $username = $ENV{'PWPUsername'};
13+
my $password = $ENV{'PWPPassword'};
14+
my $login_data;
15+
if (defined($username) and defined($password)) {
16+
$login_data = { username => $username, password => $password };
1117
}
1218

13-
#my $username = $ENV{'PWPUsername'};
14-
#my $password = $ENV{'PWPPassword'};
1519
my $bot = MediaWiki::Bot->new({
1620
agent => "MediaWiki::Bot tests ($t)",
1721
host => 'test.wikipedia.org',
18-
# login_data => { username => $username, password => $password },
22+
login_data => $login_data,
1923
});
2024
if(defined($ENV{'PWPMakeTestSetWikiHost'})) {
2125
$bot->set_wiki($ENV{'PWPMakeTestSetWikiHost'}, $ENV{'PWPMakeTestSetWikiDir'});
2226
}
2327

24-
{ #no width, no height
25-
my $data = $bot->get_image('File:Test-favicon.png');
26-
ok($data, 'nonscaled image retrieved');
28+
my $image_name = 'File:Albert_Einstein_Head.jpg';
29+
subtest 'no width, no height' => sub {
30+
plan tests => 4;
31+
my $data = $bot->get_image($image_name);
32+
ok $data, 'nonscaled image retrieved';
2733

28-
my $img = Imager->new;
29-
my $did_read = $img->read(data=>$data);
30-
diag $img->errstr unless $did_read;
31-
ok($did_read, 'retrieved nonscaled data is an image.');
34+
my $img = Imager->new;
35+
my $did_read = $img->read(data => $data);
36+
ok $did_read, 'retrieved nonscaled data is an image'
37+
or diag $img->errstr;
3238

33-
is($img->getwidth(),16, 'nonscaled img has w 16');
34-
is($img->getheight(),16, 'nonscaled img has h 16');
35-
}
36-
{ #supply a width
37-
my $data = $bot->get_image('File:Test-favicon.png',{width=>12});
38-
ok($data, 'wscaled image retrieved');
39+
is $img->getwidth(), 924, 'nonscaled img has w 924';
40+
is $img->getheight(), 1203, 'nonscaled img has h 1203';
41+
};
3942

40-
my $img = Imager->new;
41-
my $did_read = $img->read(data=>$data);
42-
diag $img->errstr unless $did_read;
43-
ok($did_read, 'retrieved wscaled data is an image.');
43+
subtest 'supply a width' => sub {
44+
plan tests => 3;
45+
my $data = $bot->get_image($image_name, {width => 12});
46+
ok $data, 'wscaled image retrieved';
4447

45-
is($img->getwidth(),12, 'wscaled img has w 12');
46-
is($img->getheight(),12, 'wscaled img has h 12');
47-
}
48-
{ #supply a width & a not-to-scale height. These
49-
# should both be considered maximum dimensions,
50-
# and scale should be proportional.
51-
my $data = $bot->get_image('File:Test-favicon.png',{width=>4,height=>8});
52-
ok($data, 'whscaled image retrieved');
53-
54-
my $img = Imager->new;
55-
my $did_read = $img->read(data=>$data);
56-
diag $img->errstr unless $did_read;
57-
ok($did_read, 'retrieved whscaled data is an image.');
58-
59-
is($img->getwidth(),4, 'whscaled img has w 4');
60-
is($img->getheight(),4, 'whscaled img has h 4');
61-
}
48+
my $img = Imager->new;
49+
my $did_read = $img->read(data => $data);
50+
ok $did_read, 'retrieved wscaled data is an image.'
51+
or diag $img->errstr;
52+
53+
is $img->getwidth(), 12, 'wscaled img has w 12';
54+
};
55+
56+
#supply a width & a not-to-scale height. These
57+
# should both be considered maximum dimensions,
58+
# and scale should be proportional.
59+
subtest 'supply a width and a not-to-scale height' => sub {
60+
plan tests => 4;
61+
my $data = $bot->get_image($image_name, {width => 200, height => 200});
62+
ok $data, 'whscaled image retrieved';
63+
64+
my $img = Imager->new;
65+
my $did_read = $img->read(data => $data);
66+
ok $did_read, 'retrieved whscaled data is an image.'
67+
or diag $img->errstr;
6268

63-
done_testing;
69+
cmp_ok $img->getwidth(), '<=', 200, '200 height is max';
70+
cmp_ok $img->getheight(), '<=', 200, '200 width is max';
71+
};

0 commit comments

Comments
 (0)