05e3284af54aa0ecf730cfae5c5a974ab04926be
[kivitendo-erp.git] / t / file / filesystem.t
1 use strict;
2 use Test::More tests => 14;
3
4 use lib 't';
5
6 use File::Temp;
7 use Support::TestSetup;
8 use Test::Exception;
9 use SL::File;
10 use SL::Dev::File qw(create_uploaded create_scanned create_created);
11
12 Support::TestSetup::login();
13
14 my $temp_dir    = File::Temp::tempdir("kivi-t-file-filesystem.XXXXXX", TMPDIR => 1, CLEANUP => 1);
15 my $storage_dir = "$temp_dir/storage";
16
17 my %common_params = (
18   object_id   => 1,
19   object_type => 'sales_order',
20 );
21
22 mkdir($storage_dir) || die $!;
23 {
24 local $::lx_office_conf{paths}->{document_path} = $storage_dir;
25 $::instance_conf->data;
26 local $::instance_conf->{data}{doc_files} = 1;
27
28 my $scanner_file = "${temp_dir}/f2";
29
30 clear_up();
31
32 note('testing SL::File');
33
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');
38
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");
43
44 is( -f $scanner_file ? 1 : 0,                0, "scanned document was moved from scanner");
45
46 $file2->delete;
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");
50
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");
55
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");
62
63 note('testing controller');
64 my $output;
65 open(my $outputFH, '>', \$output) or die; # This shouldn't fail
66 my $oldFH = select $outputFH;
67
68 $::form->{id} = $file1->id;
69 use SL::Controller::File;
70 SL::Controller::File->action_download();
71
72 select $oldFH;
73 close $outputFH;
74 my @lines = split "\n" , $output;
75 ok($lines[4] eq 'content1 uploaded', "controller download has correct content");
76
77 #some controller checks
78 $::form = Support::TestSetup->create_new_form;
79 $::form->{object_id}   = 12345678;
80 $::form->{object_type} = undef;
81 my $result='xx1';
82 eval {
83   SL::Controller::File->check_object_params();
84   $result = 'yy1';
85   1;
86 } or do {
87   $result = $@;
88 };
89 is(substr($result,0,14), "No object type", "controller error response 'No object type' ok");
90
91 $::form = Support::TestSetup->create_new_form;
92 $::form->{object_type} = 'sales_order';
93 $::form->{file_type}   = '';
94
95 $result='xx2';
96 eval {
97   SL::Controller::File->check_object_params();
98   $result='yy2';
99   1;
100 } or do {
101   $result=$@;
102 };
103 is(substr($result,0,12), "No file type", "controller error response 'No file type' ok");
104
105 sub clear_up {
106   # Cleaning up may fail.
107   eval {
108     SL::File->delete_all(%common_params);
109     unlink($scanner_file);
110   };
111 }
112
113 }
114
115 clear_up();
116 done_testing;
117
118 1;