Bug 12803 - Add ability to skip closed libraries when generating the holds queue
[koha.git] / t / db_dependent / Borrower_Files.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014  Biblibre SARL
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use C4::Context;
23 use C4::Members;
24
25 use Koha::Database;
26 use t::lib::TestBuilder;
27
28 use Test::More tests => 23;
29
30 use_ok('Koha::Borrower::Files');
31
32 my $schema = Koha::Database->schema;
33 $schema->storage->txn_begin;
34 my $builder = t::lib::TestBuilder->new;
35 my $dbh = C4::Context->dbh;
36
37 $dbh->do(q|DELETE FROM issues|);
38 $dbh->do(q|DELETE FROM borrowers|);
39 $dbh->do(q|DELETE FROM borrower_files|);
40
41 my $library = $builder->build({
42     source => 'Branch',
43 });
44
45 my $borrowernumber = AddMember(
46     firstname =>  'my firstname',
47     surname => 'my surname',
48     categorycode => 'S',
49     branchcode => $library->{branchcode},
50 );
51
52 my $bf = Koha::Borrower::Files->new(
53     borrowernumber => $borrowernumber,
54 );
55
56
57 my $addFile = $bf->AddFile(
58     name => 'my filename',
59     type => 'text/plain',
60 );
61 is( $addFile, undef, 'AddFile without the required parameter content returns undef' );
62 my $files = $bf->GetFilesInfo();
63 is( @$files, 0, 'AddFile does not add a file without the parameter content' );
64
65 $addFile = $bf->AddFile(
66     type => 'text/plain',
67     content => 'my filecontent',
68 );
69 is( $addFile, undef, 'AddFile without the required parameter name returns undef' );
70 $files = $bf->GetFilesInfo();
71 is( @$files, 0, 'AddFile does not add a file without the parameter name' );
72
73
74 my $file1 = {
75     name => 'my filename1',
76     type => 'text/plain',
77     content => 'my filecontent1',
78 };
79 $addFile = $bf->AddFile(%$file1);
80 is( $addFile, 1, 'AddFile with the required parameters returns 1' );
81 $files = $bf->GetFilesInfo();
82 is( @$files, 1, 'GetFilesInfo returns 1 file' );
83 is( $files->[0]->{file_name}, $file1->{name}, 'Correctly stored name' );
84 is( $files->[0]->{file_type}, $file1->{type}, 'Correctly stored type' );
85
86
87 my $file2 = {
88     name => 'my filename2',
89     type => 'text/html',
90     description => 'my filedescription2',
91     content => 'my filecontent2',
92 };
93 $addFile = $bf->AddFile(%$file2);
94 is( $addFile, 1, 'AddFile with the required parameters returns 1' );
95 $files = $bf->GetFilesInfo();
96 is( @$files, 2, "GetFilesInfo returns 2 files" );
97 is( $files->[1]->{file_name}, $file2->{name}, 'Correctly stored name' );
98 is( $files->[1]->{file_type}, $file2->{type}, 'Correctly stored type' );
99 is( $files->[1]->{file_description}, $file2->{description}, 'Correctly stored description' );
100
101 my $file = $bf->GetFile();
102 is( $file, undef, 'GetFile without parameters returns undef' );
103
104 $file = $bf->GetFile(
105     id => $files->[1]->{file_id},
106 );
107 is( $file->{file_name}, $files->[1]->{file_name}, 'GetFile returns the correct name' );
108 is( $file->{file_type}, $files->[1]->{file_type}, 'GetFile returns the correct type' );
109 is( $file->{file_description}, $files->[1]->{file_description}, 'GetFile returns the correct description' );
110
111
112 $bf->DelFile();
113 $files = $bf->GetFilesInfo();
114 is( @$files, 2, 'DelFile without parameters does not delete a file' );
115
116 $bf->DelFile(
117     id => $files->[1]->{file_id},
118 );
119 $files = $bf->GetFilesInfo();
120 is( @$files, 1, 'DelFile delete a file' );
121 is( $files->[0]->{file_name}, $file1->{name}, 'DelFile delete the correct entry' );
122 is( $files->[0]->{file_type}, $file1->{type}, 'DelFile delete the correct entry' );
123
124 $bf->DelFile(
125     id => $files->[0]->{file_id},
126 );
127 $files = $bf->GetFilesInfo();
128 is( @$files, 0, 'DelFile delete a file' );