Bug 6906: Tests - Do not assume CPL exists
[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 => 30;
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(), undef, "new() param check test/0");
20 is(Koha::Misc::Files->new(recordid => 12), undef, "new() param check test/1");
21 is(Koha::Misc::Files->new(recordid => 'aa123', tabletag => 'ttag_a'), undef, "new() param check test/2");
22
23 ## create some test objects with arbitrary (tabletag, recordid) pairs
24 my $mf_a_123 = Koha::Misc::Files->new(recordid => '123', tabletag => 'tst_table_a');
25 my $mf_a_124 = Koha::Misc::Files->new(recordid => '124', tabletag => 'tst_table_a');
26 my $mf_b_221 = Koha::Misc::Files->new(recordid => '221', tabletag => 'tst_table_b');
27 is(ref($mf_a_123), "Koha::Misc::Files", "new() returned object type");
28
29 ## GetFilesInfo() initial tests (dummy AddFile() / parameter handling checks)
30 is(ref($mf_a_123->GetFilesInfo()), 'ARRAY', "GetFilesInfo() return type");
31 is(scalar @{$mf_a_123->GetFilesInfo()}, 0, "GetFilesInfo() empty/non-empty result/1");
32 $mf_a_123->AddFile(name => '', type => 'text/plain', content => "aaabbcc");
33 is(scalar @{$mf_a_123->GetFilesInfo()}, 0, "GetFilesInfo() empty/non-empty result/2");
34
35 ## AddFile(); add 5 sample file records for 3 test objects
36 $mf_a_123->AddFile(name => 'File_name_1.txt', type => 'text/plain',
37   content => "file contents\n1111\n", description => "File #1 sample description");
38 $mf_a_123->AddFile(name => 'File_name_2.txt', type => 'text/plain',
39   content => "file contents\n2222\n", description => "File #2 sample description");
40 $mf_a_124->AddFile(name => 'File_name_3.txt', content => "file contents\n3333\n", type => 'text/whatever');
41 $mf_a_124->AddFile(name => 'File_name_4.txt', content => "file contents\n4444\n");
42 $mf_b_221->AddFile(name => 'File_name_5.txt', content => "file contents\n5555\n");
43
44 ## check GetFilesInfo() results for added files
45 my $files_a_123_infos = $mf_a_123->GetFilesInfo();
46 is(scalar @$files_a_123_infos, 2, "GetFilesInfo() result count/1");
47 is(scalar @{$mf_b_221->GetFilesInfo()}, 1, "GetFilesInfo() result count/2");
48 is(ref($files_a_123_infos->[0]), 'HASH', "GetFilesInfo() item file result type");
49 is($files_a_123_infos->[0]->{file_name}, 'File_name_1.txt', "GetFilesInfo() result check/1");
50 is($files_a_123_infos->[1]->{file_name}, 'File_name_2.txt', "GetFilesInfo() result check/2");
51 is($files_a_123_infos->[1]->{file_type}, 'text/plain', "GetFilesInfo() result check/3");
52 is($files_a_123_infos->[1]->{file_size}, 19, "GetFilesInfo() result check/4");
53 is($files_a_123_infos->[1]->{file_description}, 'File #2 sample description', "GetFilesInfo() result check/5");
54
55 ## GetFile() result checks
56 is($mf_a_123->GetFile(), undef, "GetFile() result check/1");
57 is($mf_a_123->GetFile(id => 0), undef, "GetFile() result check/2");
58
59 my $a123_file_1 = $mf_a_123->GetFile(id => $files_a_123_infos->[0]->{file_id});
60 is(ref($a123_file_1), 'HASH', "GetFile() result check/3");
61 is($a123_file_1->{file_id}, $files_a_123_infos->[0]->{file_id}, "GetFile() result check/4");
62 is($a123_file_1->{file_content}, "file contents\n1111\n", "GetFile() result check/5");
63
64 ## MergeFileRecIds() tests
65 $mf_a_123->MergeFileRecIds(123,221);
66 $files_a_123_infos = $mf_a_123->GetFilesInfo();
67 is(scalar @$files_a_123_infos, 2, "GetFilesInfo() result count after dummy MergeFileRecIds()");
68 $mf_a_123->MergeFileRecIds(124);
69 $files_a_123_infos = $mf_a_123->GetFilesInfo();
70 is(scalar @$files_a_123_infos, 4, "GetFilesInfo() result count after MergeFileRecIds()/1");
71 is(scalar @{$mf_a_124->GetFilesInfo()}, 0, "GetFilesInfo() result count after MergeFileRecIds()/2");
72 is($files_a_123_infos->[-1]->{file_name}, 'File_name_4.txt', "GetFilesInfo() result check after MergeFileRecIds()");
73
74 ## DelFile() test
75 $mf_a_123->DelFile(id => $files_a_123_infos->[-1]->{file_id});
76 $files_a_123_infos = $mf_a_123->GetFilesInfo();
77 is(scalar @$files_a_123_infos, 3, "GetFilesInfo() result count after DelFile()");
78
79 ## DelAllFiles() tests
80 my $number_of_deleted_files_a_123 = $mf_a_123->DelAllFiles();
81 is( $number_of_deleted_files_a_123, 3, "DelAllFiles returns the number of deleted files/1" );
82 $files_a_123_infos = $mf_a_123->GetFilesInfo();
83 is(scalar @$files_a_123_infos, 0, "GetFilesInfo() result count after DelAllFiles()/1");
84 my $number_of_deleted_files_b_221 = $mf_b_221->DelAllFiles();
85 is( $number_of_deleted_files_b_221, 1, "DelAllFiles returns the number of deleted files/2" );
86 is(scalar @{$mf_b_221->GetFilesInfo()}, 0, "GetFilesInfo() result count after DelAllFiles()/2");
87
88 $dbh->rollback;
89
90 1;