2 use Test::More tests => 14;
 
   7 use Support::TestSetup;
 
  10 use SL::Dev::File qw(create_uploaded create_scanned create_created);
 
  12 Support::TestSetup::login();
 
  14 my $temp_dir    = File::Temp::tempdir("kivi-t-file-filesystem.XXXXXX", TMPDIR => 1, CLEANUP => 1);
 
  15 my $storage_dir = "$temp_dir/storage";
 
  19   object_type => 'sales_order',
 
  22 mkdir($storage_dir) || die $!;
 
  24 local $::lx_office_conf{paths}->{document_path} = $storage_dir;
 
  25 $::instance_conf->data;
 
  26 local $::instance_conf->{data}{doc_files} = 1;
 
  28 my $scanner_file = "${temp_dir}/f2";
 
  32 note('testing SL::File');
 
  34 my $file1 = create_uploaded( %common_params, file_name => 'file1', file_contents => 'content1 uploaded' );
 
  35 my $file2 = create_scanned(  %common_params, file_name => 'file2', file_contents => 'content2 scanned', file_path => $scanner_file );
 
  36 my $file3 = create_created(  %common_params, file_name => 'file3', file_contents => 'content3 created'    );
 
  37 my $file4 = create_created(  %common_params, file_name => 'file3', file_contents => 'content3 new version');
 
  39 is( SL::File->get_all_count(%common_params), 3, "3 files were created");
 
  40 ok( $file1->file_name              eq 'file1' , "file1 has correct name");
 
  41 my $content1 = $file1->get_content;
 
  42 ok( $$content1 eq 'content1 uploaded'         , "file1 has correct content");
 
  44 is( -f $scanner_file ? 1 : 0,                0, "scanned document was moved from scanner");
 
  47 is( -f $scanner_file ? 1 : 0,                1, "scanned document was moved back to scanner");
 
  48 my $content2 = File::Slurp::read_file($scanner_file);
 
  49 ok( $content2 eq 'content2 scanned'           , "scanned file has correct content");
 
  51 my @file5 = SL::File->get_all(%common_params, file_name => 'file3');
 
  52 is( scalar @file5,                           1, "get_all file3: one currnt file found");
 
  53 my $content5 = $file5[0]->get_content();
 
  54 ok( $$content5 eq 'content3 new version'      , "file has correct current content");
 
  56 my @file6 = SL::File->get_all_versions(%common_params, file_name => 'file3');
 
  57 is( scalar @file6 ,                           2, "file3: two file versions found");
 
  58 my $content6 = $file6[0]->get_content;
 
  59 ok( $$content6 eq 'content3 new version'      , "file has correct current content");
 
  60 $content6 = $file6[1]->get_content;
 
  61 ok( $$content6 eq 'content3 created'          , "file has correct old content");
 
  63 note('testing controller');
 
  65 open(my $outputFH, '>', \$output) or die; # This shouldn't fail
 
  66 my $oldFH = select $outputFH;
 
  68 $::form->{id} = $file1->id;
 
  69 use SL::Controller::File;
 
  70 SL::Controller::File->action_download();
 
  74 my @lines = split "\n" , $output;
 
  75 ok($lines[4] eq 'content1 uploaded', "controller download has correct content");
 
  77 #some controller checks
 
  78 $::form = Support::TestSetup->create_new_form;
 
  79 $::form->{object_id}   = 12345678;
 
  80 $::form->{object_type} = undef;
 
  83   SL::Controller::File->check_object_params();
 
  89 is(substr($result,0,14), "No object type", "controller error response 'No object type' ok");
 
  91 $::form = Support::TestSetup->create_new_form;
 
  92 $::form->{object_type} = 'sales_order';
 
  93 $::form->{file_type}   = '';
 
  97   SL::Controller::File->check_object_params();
 
 103 is(substr($result,0,12), "No file type", "controller error response 'No file type' ok");
 
 106   # Cleaning up may fail.
 
 108     SL::File->delete_all(%common_params);
 
 109     unlink($scanner_file);