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