3 # This file is part of Koha.
5 # Copyright 2014 Biblibre SARL
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.
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.
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>.
26 use t
::lib
::TestBuilder
;
28 use Test
::More tests
=> 23;
30 use_ok
('Koha::Patron::Files');
32 my $schema = Koha
::Database
->schema;
33 $schema->storage->txn_begin;
34 my $builder = t
::lib
::TestBuilder
->new;
35 my $dbh = C4
::Context
->dbh;
37 $dbh->do(q
|DELETE FROM issues
|);
38 $dbh->do(q
|DELETE FROM borrowers
|);
39 $dbh->do(q
|DELETE FROM borrower_files
|);
41 my $library = $builder->build({
45 my $patron_category = $builder->build({ source
=> 'Category' });
46 my $borrowernumber = Koha
::Patron
->new({
47 firstname
=> 'my firstname',
48 surname
=> 'my surname',
49 categorycode
=> $patron_category->{categorycode
},
50 branchcode
=> $library->{branchcode
},
51 })->store->borrowernumber;
53 my $bf = Koha
::Patron
::Files
->new(
54 borrowernumber
=> $borrowernumber,
58 my $addFile = $bf->AddFile(
59 name
=> 'my filename',
62 is
( $addFile, undef, 'AddFile without the required parameter content returns undef' );
63 my $files = $bf->GetFilesInfo();
64 is
( @
$files, 0, 'AddFile does not add a file without the parameter content' );
66 $addFile = $bf->AddFile(
68 content
=> 'my filecontent',
70 is
( $addFile, undef, 'AddFile without the required parameter name returns undef' );
71 $files = $bf->GetFilesInfo();
72 is
( @
$files, 0, 'AddFile does not add a file without the parameter name' );
76 name
=> 'my filename1',
78 content
=> 'my filecontent1',
80 $addFile = $bf->AddFile(%$file1);
81 is
( $addFile, 1, 'AddFile with the required parameters returns 1' );
82 $files = $bf->GetFilesInfo();
83 is
( @
$files, 1, 'GetFilesInfo returns 1 file' );
84 is
( $files->[0]->{file_name
}, $file1->{name
}, 'Correctly stored name' );
85 is
( $files->[0]->{file_type
}, $file1->{type
}, 'Correctly stored type' );
89 name
=> 'my filename2',
91 description
=> 'my filedescription2',
92 content
=> 'my filecontent2',
94 $addFile = $bf->AddFile(%$file2);
95 is
( $addFile, 1, 'AddFile with the required parameters returns 1' );
96 $files = $bf->GetFilesInfo();
97 is
( @
$files, 2, "GetFilesInfo returns 2 files" );
98 is
( $files->[1]->{file_name
}, $file2->{name
}, 'Correctly stored name' );
99 is
( $files->[1]->{file_type
}, $file2->{type
}, 'Correctly stored type' );
100 is
( $files->[1]->{file_description
}, $file2->{description
}, 'Correctly stored description' );
102 my $file = $bf->GetFile();
103 is
( $file, undef, 'GetFile without parameters returns undef' );
105 $file = $bf->GetFile(
106 id
=> $files->[1]->{file_id
},
108 is
( $file->{file_name
}, $files->[1]->{file_name
}, 'GetFile returns the correct name' );
109 is
( $file->{file_type
}, $files->[1]->{file_type
}, 'GetFile returns the correct type' );
110 is
( $file->{file_description
}, $files->[1]->{file_description
}, 'GetFile returns the correct description' );
114 $files = $bf->GetFilesInfo();
115 is
( @
$files, 2, 'DelFile without parameters does not delete a file' );
118 id
=> $files->[1]->{file_id
},
120 $files = $bf->GetFilesInfo();
121 is
( @
$files, 1, 'DelFile delete a file' );
122 is
( $files->[0]->{file_name
}, $file1->{name
}, 'DelFile delete the correct entry' );
123 is
( $files->[0]->{file_type
}, $file1->{type
}, 'DelFile delete the correct entry' );
126 id
=> $files->[0]->{file_id
},
128 $files = $bf->GetFilesInfo();
129 is
( @
$files, 0, 'DelFile delete a file' );