Projekt

Allgemein

Profil

Herunterladen (3,58 KB) Statistiken
| Zweig: | Markierung: | Revision:
8c7f25bc Martin Helmling
use strict;
a4684ad1 Martin Helmling
use Test::More tests => 14;
8c7f25bc Martin Helmling
use lib 't';
c0b53018 Moritz Bunkus
use File::Temp;
8c7f25bc Martin Helmling
use Support::TestSetup;
use Test::Exception;
use SL::File;
63410aa6 Sven Schöling
use SL::Dev::File qw(create_uploaded create_scanned create_created);

8c7f25bc Martin Helmling
Support::TestSetup::login();

c0b53018 Moritz Bunkus
my $temp_dir = File::Temp::tempdir("kivi-t-file-filesystem.XXXXXX", TMPDIR => 1, CLEANUP => 1);
my $storage_dir = "$temp_dir/storage";

1bed9601 Geoffrey Richardson
my %common_params = (
object_id => 1,
object_type => 'sales_order',
);

c0b53018 Moritz Bunkus
mkdir($storage_dir) || die $!;
d3856535 Sven Schöling
{
local $::lx_office_conf{paths}->{document_path} = $storage_dir;
$::instance_conf->data;
local $::instance_conf->{data}{doc_files} = 1;
8c7f25bc Martin Helmling
1bed9601 Geoffrey Richardson
my $scanner_file = "${temp_dir}/f2";
8c7f25bc Martin Helmling
clear_up();

1bed9601 Geoffrey Richardson
note('testing SL::File');
8c7f25bc Martin Helmling
1bed9601 Geoffrey Richardson
my $file1 = create_uploaded( %common_params, file_name => 'file1', file_contents => 'content1 uploaded' );
my $file2 = create_scanned( %common_params, file_name => 'file2', file_contents => 'content2 scanned', file_path => $scanner_file );
my $file3 = create_created( %common_params, file_name => 'file3', file_contents => 'content3 created' );
my $file4 = create_created( %common_params, file_name => 'file3', file_contents => 'content3 new version');

is( SL::File->get_all_count(%common_params), 3, "3 files were created");
ok( $file1->file_name eq 'file1' , "file1 has correct name");
8c7f25bc Martin Helmling
my $content1 = $file1->get_content;
1bed9601 Geoffrey Richardson
ok( $$content1 eq 'content1 uploaded' , "file1 has correct content");
8c7f25bc Martin Helmling
1bed9601 Geoffrey Richardson
is( -f $scanner_file ? 1 : 0, 0, "scanned document was moved from scanner");
8c7f25bc Martin Helmling
$file2->delete;
1bed9601 Geoffrey Richardson
is( -f $scanner_file ? 1 : 0, 1, "scanned document was moved back to scanner");
my $content2 = File::Slurp::read_file($scanner_file);
ok( $content2 eq 'content2 scanned' , "scanned file has correct content");
8c7f25bc Martin Helmling
1bed9601 Geoffrey Richardson
my @file5 = SL::File->get_all(%common_params, file_name => 'file3');
is( scalar @file5, 1, "get_all file3: one currnt file found");
8c7f25bc Martin Helmling
my $content5 = $file5[0]->get_content();
1bed9601 Geoffrey Richardson
ok( $$content5 eq 'content3 new version' , "file has correct current content");
8c7f25bc Martin Helmling
1bed9601 Geoffrey Richardson
my @file6 = SL::File->get_all_versions(%common_params, file_name => 'file3');
is( scalar @file6 , 2, "file3: two file versions found");
my $content6 = $file6[0]->get_content;
ok( $$content6 eq 'content3 new version' , "file has correct current content");
$content6 = $file6[1]->get_content;
ok( $$content6 eq 'content3 created' , "file has correct old content");
a4684ad1 Martin Helmling
1bed9601 Geoffrey Richardson
note('testing controller');
a4684ad1 Martin Helmling
my $output;
open(my $outputFH, '>', \$output) or die; # This shouldn't fail
my $oldFH = select $outputFH;

1bed9601 Geoffrey Richardson
$::form->{id} = $file1->id;
8c7f25bc Martin Helmling
use SL::Controller::File;
SL::Controller::File->action_download();
a4684ad1 Martin Helmling
select $oldFH;
close $outputFH;
my @lines = split "\n" , $output;
1bed9601 Geoffrey Richardson
ok($lines[4] eq 'content1 uploaded', "controller download has correct content");
a4684ad1 Martin Helmling
#some controller checks
1bed9601 Geoffrey Richardson
$::form = Support::TestSetup->create_new_form;
8c7f25bc Martin Helmling
$::form->{object_id} = 12345678;
$::form->{object_type} = undef;
a4684ad1 Martin Helmling
my $result='xx1';
8c7f25bc Martin Helmling
eval {
SL::Controller::File->check_object_params();
1bed9601 Geoffrey Richardson
$result = 'yy1';
8c7f25bc Martin Helmling
1;
} or do {
1bed9601 Geoffrey Richardson
$result = $@;
8c7f25bc Martin Helmling
};
1bed9601 Geoffrey Richardson
is(substr($result,0,14), "No object type", "controller error response 'No object type' ok");

$::form = Support::TestSetup->create_new_form;
$::form->{object_type} = 'sales_order';
$::form->{file_type} = '';
a4684ad1 Martin Helmling
$result='xx2';
8c7f25bc Martin Helmling
eval {
SL::Controller::File->check_object_params();
a4684ad1 Martin Helmling
$result='yy2';
8c7f25bc Martin Helmling
1;
} or do {
a4684ad1 Martin Helmling
$result=$@;
8c7f25bc Martin Helmling
};
1bed9601 Geoffrey Richardson
is(substr($result,0,12), "No file type", "controller error response 'No file type' ok");
8c7f25bc Martin Helmling
sub clear_up {
c0b53018 Moritz Bunkus
# Cleaning up may fail.
eval {
1bed9601 Geoffrey Richardson
SL::File->delete_all(%common_params);
unlink($scanner_file);
c0b53018 Moritz Bunkus
};
}
8c7f25bc Martin Helmling
d3856535 Sven Schöling
}

1bed9601 Geoffrey Richardson
clear_up();
done_testing;
8c7f25bc Martin Helmling
1;