Filesystem tests nun ohne anderen Output
[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;
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 mkdir($storage_dir) || die $!;
18
19 my $db = SL::DB::Object->new->db;
20 $db->dbh->do("UPDATE defaults SET doc_files = 't'");
21 $db->dbh->do("UPDATE defaults SET doc_files_rootpath = ?", undef, $storage_dir);
22
23 my $scannerfile = "${temp_dir}/f2";
24
25 clear_up();
26 reset_state();
27
28 my $file1 = SL::Dev::File::create_uploaded( file_name => 'file1', file_contents => 'inhalt1 uploaded' );
29 my $file2 = SL::Dev::File::create_scanned(  file_name => 'file2', file_contents => 'inhalt2 scanned', file_path => $scannerfile );
30 my $file3 = SL::Dev::File::create_created(  file_name => 'file3', file_contents => 'inhalt3 created'    );
31 my $file4 = SL::Dev::File::create_created(  file_name => 'file3', file_contents => 'inhalt3 new version');
32
33 is( SL::Dev::File->get_all_count(),                    3,"total number of files created is 3");
34 ok( $file1->file_name                        eq 'file1' ,"file has right name");
35 my $content1 = $file1->get_content;
36 ok( $$content1 eq 'inhalt1 uploaded'                    ,"file has right content");
37
38 is( -f $scannerfile ? 1 : 0,                           0,"scanned document is moved from scanner");
39
40 $file2->delete;
41 is( -f $scannerfile ? 1 : 0,                           1,"scanned document is moved back to scanner");
42 my $content2 = File::Slurp::read_file($scannerfile);
43 ok( $content2 eq 'inhalt2 scanned'                      ,"scanned file has right content");
44
45 my @file5 = SL::Dev::File->get_all(file_name => 'file3');
46 is(   scalar( @file5),                                 1, "one actual file found");
47 my $content5 = $file5[0]->get_content();
48 ok( $$content5 eq 'inhalt3 new version'                 ,"file has right actual content");
49
50 my @file6 = SL::Dev::File->get_all_versions(file_name => 'file3');
51 is(   scalar( @file6),                                 2,"two file versions found");
52 $content5 = $file6[0]->get_content;
53 ok( $$content5 eq 'inhalt3 new version'                 ,"file has right actual content");
54 $content5 = $file6[1]->get_content;
55 ok( $$content5 eq 'inhalt3 created'                     ,"file has right old content");
56
57 #print "\n\nController Test:\n";
58 # now test controller
59 #$::form->{object_id}  = 1;
60 #$::form->{object_type}= 'sales_order';
61 #$::form->{file_type}  = 'document';
62
63 my $output;
64 open(my $outputFH, '>', \$output) or die; # This shouldn't fail
65 my $oldFH = select $outputFH;
66
67 $::form->{id}  = $file1->id;
68 use SL::Controller::File;
69 SL::Controller::File->action_download();
70
71 select $oldFH;
72 close $outputFH;
73 my @lines = split "\n" , $output;
74 ok($lines[4] eq 'inhalt1 uploaded'                 ,"controller download has correct content");
75
76 #some controller checks
77 $::form->{object_id}   = 12345678;
78 $::form->{object_type} = undef;
79 my $result='xx1';
80 eval {
81   SL::Controller::File->check_object_params();
82   $result='yy1';
83   1;
84 } or do {
85   $result=$@;
86 };
87 ok($result eq "No object type at SL/Controller/File.pm line 327.\n","correct error 'No object type'");
88
89 $::form->{object_type} ='sales_order';
90 $::form->{file_type} ='';
91 $result='xx2';
92 eval {
93   SL::Controller::File->check_object_params();
94   $result='yy2';
95   1;
96 } or do {
97   $result=$@;
98 };
99 ok($result eq "No file type at SL/Controller/File.pm line 328.\n","correct error 'No file type'");
100
101 clear_up();
102 done_testing;
103
104 sub clear_up {
105   # Cleaning up may fail.
106   eval {
107     SL::Dev::File->delete_all();
108     unlink($scannerfile);
109   };
110 }
111
112 sub reset_state {
113   my %params = @_;
114
115 };
116
117 1;