kivitendo/t/file/filesystem.t @ e14e91e1
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";
|
||||
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 | |||
c0b53018 | Moritz Bunkus | my $scannerfile = "${temp_dir}/f2";
|
||
8c7f25bc | Martin Helmling | |||
clear_up();
|
||||
reset_state();
|
||||
63410aa6 | Sven Schöling | my $file1 = create_uploaded( file_name => 'file1', file_contents => 'inhalt1 uploaded' );
|
||
my $file2 = create_scanned( file_name => 'file2', file_contents => 'inhalt2 scanned', file_path => $scannerfile );
|
||||
my $file3 = create_created( file_name => 'file3', file_contents => 'inhalt3 created' );
|
||||
my $file4 = create_created( file_name => 'file3', file_contents => 'inhalt3 new version');
|
||||
8c7f25bc | Martin Helmling | |||
63410aa6 | Sven Schöling | is( SL::Dev::File::get_all_count(), 3,"total number of files created is 3");
|
||
8c7f25bc | Martin Helmling | ok( $file1->file_name eq 'file1' ,"file has right name");
|
||
my $content1 = $file1->get_content;
|
||||
ok( $$content1 eq 'inhalt1 uploaded' ,"file has right content");
|
||||
is( -f $scannerfile ? 1 : 0, 0,"scanned document is moved from scanner");
|
||||
$file2->delete;
|
||||
is( -f $scannerfile ? 1 : 0, 1,"scanned document is moved back to scanner");
|
||||
my $content2 = File::Slurp::read_file($scannerfile);
|
||||
ok( $content2 eq 'inhalt2 scanned' ,"scanned file has right content");
|
||||
63410aa6 | Sven Schöling | my @file5 = SL::Dev::File::get_all(file_name => 'file3');
|
||
8c7f25bc | Martin Helmling | is( scalar( @file5), 1, "one actual file found");
|
||
my $content5 = $file5[0]->get_content();
|
||||
ok( $$content5 eq 'inhalt3 new version' ,"file has right actual content");
|
||||
63410aa6 | Sven Schöling | my @file6 = SL::Dev::File::get_all_versions(file_name => 'file3');
|
||
8c7f25bc | Martin Helmling | is( scalar( @file6), 2,"two file versions found");
|
||
$content5 = $file6[0]->get_content;
|
||||
ok( $$content5 eq 'inhalt3 new version' ,"file has right actual content");
|
||||
$content5 = $file6[1]->get_content;
|
||||
ok( $$content5 eq 'inhalt3 created' ,"file has right old content");
|
||||
a4684ad1 | Martin Helmling | #print "\n\nController Test:\n";
|
||
8c7f25bc | Martin Helmling | # now test controller
|
||
#$::form->{object_id} = 1;
|
||||
#$::form->{object_type}= 'sales_order';
|
||||
#$::form->{file_type} = 'document';
|
||||
a4684ad1 | Martin Helmling | |||
my $output;
|
||||
open(my $outputFH, '>', \$output) or die; # This shouldn't fail
|
||||
my $oldFH = select $outputFH;
|
||||
8c7f25bc | Martin Helmling | $::form->{id} = $file1->id;
|
||
use SL::Controller::File;
|
||||
SL::Controller::File->action_download();
|
||||
a4684ad1 | Martin Helmling | |||
select $oldFH;
|
||||
close $outputFH;
|
||||
my @lines = split "\n" , $output;
|
||||
ok($lines[4] eq 'inhalt1 uploaded' ,"controller download has correct content");
|
||||
#some controller checks
|
||||
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();
|
||||
a4684ad1 | Martin Helmling | $result='yy1';
|
||
8c7f25bc | Martin Helmling | 1;
|
||
} or do {
|
||||
a4684ad1 | Martin Helmling | $result=$@;
|
||
8c7f25bc | Martin Helmling | };
|
||
83c97773 | Martin Helmling | $result = substr($result,0,14);
|
||
cbf8ed02 | Martin Helmling | #print $result."\n";
|
||
83c97773 | Martin Helmling | ok($result eq "No object type","correct error 'No object type'");
|
||
a4684ad1 | Martin Helmling | |||
$::form->{object_type} ='sales_order';
|
||||
$::form->{file_type} ='';
|
||||
$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 | };
|
||
83c97773 | Martin Helmling | $result = substr($result,0,12);
|
||
cbf8ed02 | Martin Helmling | #print $result."\n";
|
||
83c97773 | Martin Helmling | ok($result eq "No file type","correct error 'No file type'");
|
||
8c7f25bc | Martin Helmling | |||
clear_up();
|
||||
done_testing;
|
||||
sub clear_up {
|
||||
c0b53018 | Moritz Bunkus | # Cleaning up may fail.
|
||
eval {
|
||||
63410aa6 | Sven Schöling | SL::Dev::File::delete_all();
|
||
c0b53018 | Moritz Bunkus | unlink($scannerfile);
|
||
};
|
||||
}
|
||||
8c7f25bc | Martin Helmling | |||
d3856535 | Sven Schöling | }
|
||
8c7f25bc | Martin Helmling | sub reset_state {
|
||
my %params = @_;
|
||||
};
|
||||
1;
|