From: G. Richardson Date: Thu, 24 Aug 2017 09:01:49 +0000 (+0200) Subject: SL::Dev::File und t/file/filesystem.t überarbeitet X-Git-Tag: release-3.5.4~863 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=1bed9601d931c64f2f3d29bd3d7622ad4283fce4;p=kivitendo-erp.git SL::Dev::File und t/file/filesystem.t überarbeitet * Einrückungen / Syntax überarbeitet * Englisch leicht verbessert SL/Dev/File.pm: * die %common_params aus SL/Dev/File sind nur für den speziellen Test filesystem.t, daher in den Test verlagert. * ebenfalls die get_all, get_all_count entfernt, SL/Dev/ soll nicht nur die Anzahl der zu tippenden Zeichen in Tests verringern, sondern allgemeine Funktionen für verschiedene Tests bereitstellen oder sinnvolle Defaults vorbelegen --- diff --git a/SL/Dev/File.pm b/SL/Dev/File.pm index 62228ea9e..9fa1b962f 100644 --- a/SL/Dev/File.pm +++ b/SL/Dev/File.pm @@ -7,11 +7,6 @@ our %EXPORT_TAGS = (ALL => \@EXPORT_OK); use SL::DB::File; -my %common_params = ( - object_id => 1, - object_type => 'sales_order', -); - sub create_scanned { my (%params) = @_; $params{source} = 'scanner1'; @@ -44,23 +39,18 @@ sub _create_file { my (%params) = @_; my $fileobj = SL::File->save( - %common_params, - mime_type => 'text/plain', - description => 'Test File', - file_type => $params{file_type}, - source => $params{source}, - file_name => $params{file_name}, - file_contents => $params{file_contents}, - file_path => $params{file_path} + mime_type => 'text/plain', + description => 'Test File', + %params, + # file_type => $params{file_type}, + # source => $params{source}, + # file_name => $params{file_name}, + # file_contents => $params{file_contents}, + # file_path => $params{file_path} ); return $fileobj; } -sub get_all { SL::File->get_all (%common_params, @_) } -sub get_all_count { SL::File->get_all_count (%common_params, @_) } -sub get_all_versions { SL::File->get_all_versions(%common_params, @_) } -sub delete_all { SL::File->delete_all (%common_params, @_) } - 1; __END__ diff --git a/t/file/filesystem.t b/t/file/filesystem.t index 8797a1cda..05e3284af 100644 --- a/t/file/filesystem.t +++ b/t/file/filesystem.t @@ -9,88 +9,89 @@ use Test::Exception; use SL::File; use SL::Dev::File qw(create_uploaded create_scanned create_created); - Support::TestSetup::login(); my $temp_dir = File::Temp::tempdir("kivi-t-file-filesystem.XXXXXX", TMPDIR => 1, CLEANUP => 1); my $storage_dir = "$temp_dir/storage"; +my %common_params = ( + object_id => 1, + object_type => 'sales_order', +); + mkdir($storage_dir) || die $!; { local $::lx_office_conf{paths}->{document_path} = $storage_dir; $::instance_conf->data; local $::instance_conf->{data}{doc_files} = 1; -my $scannerfile = "${temp_dir}/f2"; +my $scanner_file = "${temp_dir}/f2"; clear_up(); -reset_state(); -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'); +note('testing SL::File'); -is( SL::Dev::File::get_all_count(), 3,"total number of files created is 3"); -ok( $file1->file_name eq 'file1' ,"file has right name"); +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"); my $content1 = $file1->get_content; -ok( $$content1 eq 'inhalt1 uploaded' ,"file has right content"); +ok( $$content1 eq 'content1 uploaded' , "file1 has correct content"); -is( -f $scannerfile ? 1 : 0, 0,"scanned document is moved from scanner"); +is( -f $scanner_file ? 1 : 0, 0, "scanned document was 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"); +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"); -my @file5 = SL::Dev::File::get_all(file_name => 'file3'); -is( scalar( @file5), 1, "one actual file found"); +my @file5 = SL::File->get_all(%common_params, file_name => 'file3'); +is( scalar @file5, 1, "get_all file3: one currnt file found"); my $content5 = $file5[0]->get_content(); -ok( $$content5 eq 'inhalt3 new version' ,"file has right actual content"); - -my @file6 = SL::Dev::File::get_all_versions(file_name => 'file3'); -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"); +ok( $$content5 eq 'content3 new version' , "file has correct current content"); -#print "\n\nController Test:\n"; -# now test controller -#$::form->{object_id} = 1; -#$::form->{object_type}= 'sales_order'; -#$::form->{file_type} = 'document'; +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"); +note('testing controller'); my $output; open(my $outputFH, '>', \$output) or die; # This shouldn't fail my $oldFH = select $outputFH; -$::form->{id} = $file1->id; +$::form->{id} = $file1->id; use SL::Controller::File; SL::Controller::File->action_download(); select $oldFH; close $outputFH; my @lines = split "\n" , $output; -ok($lines[4] eq 'inhalt1 uploaded' ,"controller download has correct content"); +ok($lines[4] eq 'content1 uploaded', "controller download has correct content"); #some controller checks +$::form = Support::TestSetup->create_new_form; $::form->{object_id} = 12345678; $::form->{object_type} = undef; my $result='xx1'; eval { SL::Controller::File->check_object_params(); - $result='yy1'; + $result = 'yy1'; 1; } or do { - $result=$@; + $result = $@; }; -$result = substr($result,0,14); -#print $result."\n"; -ok($result eq "No object type","correct error 'No object type'"); +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} = ''; -$::form->{object_type} ='sales_order'; -$::form->{file_type} =''; $result='xx2'; eval { SL::Controller::File->check_object_params(); @@ -99,26 +100,19 @@ eval { } or do { $result=$@; }; -$result = substr($result,0,12); -#print $result."\n"; -ok($result eq "No file type","correct error 'No file type'"); - -clear_up(); -done_testing; +is(substr($result,0,12), "No file type", "controller error response 'No file type' ok"); sub clear_up { # Cleaning up may fail. eval { - SL::Dev::File::delete_all(); - unlink($scannerfile); + SL::File->delete_all(%common_params); + unlink($scanner_file); }; } } -sub reset_state { - my %params = @_; - -}; +clear_up(); +done_testing; 1;