1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
use Test::More;
Test::More->builder->no_ending(1);
use Config;
use Carp::Assert;
use lib 't/lib';
use strict;
use CGI::Uploader;
use DBI;
use CGI;
use HTTP::Request::Common;
use CGI::Uploader::Test;
$| = 1;
if (! $Config{d_fork} ) {
plan skip_all => "fork not available on this platform";
}
else {
plan tests => 12;
}
my ($DBH, $drv) = setup();
my $req = &HTTP::Request::Common::POST(
'/dummy_location',
Content_Type => 'form-data',
Content => [
test_file => ["t/test_file.txt"],
]
);
# Useful in simulating an upload.
$ENV{REQUEST_METHOD} = 'POST';
$ENV{CONTENT_TYPE} = 'multipart/form-data';
$ENV{CONTENT_LENGTH} = $req->content_length;
if ( open( CHILD, "|-" ) ) {
print CHILD $req->content;
close CHILD;
exit 0;
}
my $q = new CGI;
$DBH->do("ALTER TABLE uploads ADD COLUMN custom char(64)");
my %imgs = (
'test_file' => {
gen_files => {
test_file_gen => {
transform_method => \&test_gen_transform
},
},
},
);
my $u = CGI::Uploader->new(
updir_path=>'t/uploads',
updir_url=>'http://localhost/test',
dbh => $DBH,
query => $q,
spec => \%imgs,
up_table_map => {
upload_id => 'upload_id',
mime_type => 'mime_type',
extension => 'extension',
width => 'width',
height => 'height',
custom => undef,
}
);
ok($u, 'Uploader object creation');
eval {
my %entity_upload_extra = $u->store_upload(
file_field => 'test_file',
src_file => 't/test_file.txt',
uploaded_mt => 'test/plain',
file_name => 'test_file.txt',
shared_meta => { custom => 'custom_value' },
);
};
is($@,'', 'store_upload() survives');
my $imgs_with_custom_value =$DBH->selectrow_array(
"SELECT count(*)
FROM uploads
WHERE custom = 'custom_value'");
is($imgs_with_custom_value,2, 'both parent and generated file have shared_meta');
# testing transform_meta
my $img_href = $DBH->selectrow_hashref("SELECT * FROM uploads WHERE upload_id = 1");
my %meta = $u->transform_meta(
meta => $img_href,
prefix => 'test',
prevent_browser_caching => 1,
fields => [qw/id url width height/],
);
is($meta{test_id}, 1, 'meta_hashref id');
ok((not exists $meta{test_extension}), 'meta_hashref extension');
like($meta{test_url}, qr!http://localhost/test/1.txt\?!, 'meta_hashref url');
|