Bug 3050 - Add an option to upload scanned invoices #1/3
[koha.git] / t / db_dependent / Koha_Misc_Files.t
1 #!/usr/bin/perl
2
3 # Unit tests for Koha::Misc::Files
4 # Author: Jacek Ablewicz, abl@biblos.pk.edu.pl
5
6 use Modern::Perl;
7 use C4::Context;
8 use Test::More tests => 27;
9
10 BEGIN {
11     use_ok('Koha::Misc::Files');
12 }
13
14 my $dbh = C4::Context->dbh;
15 $dbh->{AutoCommit} = 0;
16 $dbh->{RaiseError} = 1;
17
18 ## new() parameter handling check
19 is(Koha::Misc::Files->new(recordid => 12), undef, "new() param check test/1");
20 is(Koha::Misc::Files->new(recordid => 'aa123', tabletag => 'ttag_a'), undef, "new() param check test/2");
21
22 ## create some test objects with arbitrary (tabletag, recordid) pairs
23 my $mf_a_123 = Koha::Misc::Files->new(recordid => '123', tabletag => 'tst_table_a');
24 my $mf_a_124 = Koha::Misc::Files->new(recordid => '124', tabletag => 'tst_table_a');
25 my $mf_b_221 = Koha::Misc::Files->new(recordid => '221', tabletag => 'tst_table_b');
26 is(ref($mf_a_123), "Koha::Misc::Files", "new() returned object type");
27
28 ## GetFilesInfo() initial tests (dummy AddFile() / parameter handling checks)
29 is(ref($mf_a_123->GetFilesInfo()), 'ARRAY', "GetFilesInfo() return type");
30 is(scalar @{$mf_a_123->GetFilesInfo()}, 0, "GetFilesInfo() empty/non-empty result/1");
31 $mf_a_123->AddFile(name => '', type => 'text/plain', content => "aaabbcc");
32 is(scalar @{$mf_a_123->GetFilesInfo()}, 0, "GetFilesInfo() empty/non-empty result/2");
33
34 ## AddFile(); add 5 sample file records for 3 test objects
35 $mf_a_123->AddFile(name => 'File_name_1.txt', type => 'text/plain',
36   content => "file contents\n1111\n", description => "File #1 sample description");
37 $mf_a_123->AddFile(name => 'File_name_2.txt', type => 'text/plain',
38   content => "file contents\n2222\n", description => "File #2 sample description");
39 $mf_a_124->AddFile(name => 'File_name_3.txt', content => "file contents\n3333\n", type => 'text/whatever');
40 $mf_a_124->AddFile(name => 'File_name_4.txt', content => "file contents\n4444\n");
41 $mf_b_221->AddFile(name => 'File_name_5.txt', content => "file contents\n5555\n");
42
43 ## check GetFilesInfo() results for added files
44 my $files_a_123_infos = $mf_a_123->GetFilesInfo();
45 is(scalar @$files_a_123_infos, 2, "GetFilesInfo() result count/1");
46 is(scalar @{$mf_b_221->GetFilesInfo()}, 1, "GetFilesInfo() result count/2");
47 is(ref($files_a_123_infos->[0]), 'HASH', "GetFilesInfo() item file result type");
48 is($files_a_123_infos->[0]->{file_name}, 'File_name_1.txt', "GetFilesInfo() result check/1");
49 is($files_a_123_infos->[1]->{file_name}, 'File_name_2.txt', "GetFilesInfo() result check/2");
50 is($files_a_123_infos->[1]->{file_type}, 'text/plain', "GetFilesInfo() result check/3");
51 is($files_a_123_infos->[1]->{file_size}, 19, "GetFilesInfo() result check/4");
52 is($files_a_123_infos->[1]->{file_description}, 'File #2 sample description', "GetFilesInfo() result check/5");
53
54 ## GetFile() result checks
55 is($mf_a_123->GetFile(), undef, "GetFile() result check/1");
56 is($mf_a_123->GetFile(id => 0), undef, "GetFile() result check/2");
57
58 my $a123_file_1 = $mf_a_123->GetFile(id => $files_a_123_infos->[0]->{file_id});
59 is(ref($a123_file_1), 'HASH', "GetFile() result check/3");
60 is($a123_file_1->{file_id}, $files_a_123_infos->[0]->{file_id}, "GetFile() result check/4");
61 is($a123_file_1->{file_content}, "file contents\n1111\n", "GetFile() result check/5");
62
63 ## MergeFileRecIds() tests
64 $mf_a_123->MergeFileRecIds(123,221);
65 $files_a_123_infos = $mf_a_123->GetFilesInfo();
66 is(scalar @$files_a_123_infos, 2, "GetFilesInfo() result count after dummy MergeFileRecIds()");
67 $mf_a_123->MergeFileRecIds(124);
68 $files_a_123_infos = $mf_a_123->GetFilesInfo();
69 is(scalar @$files_a_123_infos, 4, "GetFilesInfo() result count after MergeFileRecIds()/1");
70 is(scalar @{$mf_a_124->GetFilesInfo()}, 0, "GetFilesInfo() result count after MergeFileRecIds()/2");
71 is($files_a_123_infos->[-1]->{file_name}, 'File_name_4.txt', "GetFilesInfo() result check after MergeFileRecIds()");
72
73 ## DelFile() test
74 $mf_a_123->DelFile(id => $files_a_123_infos->[-1]->{file_id});
75 $files_a_123_infos = $mf_a_123->GetFilesInfo();
76 is(scalar @$files_a_123_infos, 3, "GetFilesInfo() result count after DelFile()");
77
78 ## DelAllFiles() tests
79 $mf_a_123->DelAllFiles();
80 $files_a_123_infos = $mf_a_123->GetFilesInfo();
81 is(scalar @$files_a_123_infos, 0, "GetFilesInfo() result count after DelAllFiles()/1");
82 $mf_b_221->DelAllFiles();
83 is(scalar @{$mf_b_221->GetFilesInfo()}, 0, "GetFilesInfo() result count after DelAllFiles()/2");
84
85 $dbh->rollback;
86
87 1;