Bugzilla – Attachment 41560 Details for
Bug 14321
Merge UploadedFile and UploadedFiles into Koha::Upload
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 14321: Unit test for Koha::Upload
Bug-14321-Unit-test-for-KohaUpload.patch (text/plain), 5.67 KB, created by
Marcel de Rooy
on 2015-08-17 13:18:46 UTC
(
hide
)
Description:
Bug 14321: Unit test for Koha::Upload
Filename:
MIME Type:
Creator:
Marcel de Rooy
Created:
2015-08-17 13:18:46 UTC
Size:
5.67 KB
patch
obsolete
>From 3cf5c55d231d48e21e55a239d99461a1e29268b8 Mon Sep 17 00:00:00 2001 >From: Marcel de Rooy <m.de.rooy@rijksmuseum.nl> >Date: Thu, 13 Aug 2015 15:35:55 +0200 >Subject: [PATCH] Bug 14321: Unit test for Koha::Upload >Content-Type: text/plain; charset=utf-8 > >This unit test replaces the test for UploadedFiles.pm. That one used >dependency Test::CGI::Multipart. >We are now mocking CGI and its hook to achieve the same result. >Some tests are added to cover additional routines in Upload.pm. > >Test plan: >Run the test! >--- > t/db_dependent/Upload.t | 158 +++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 158 insertions(+) > create mode 100644 t/db_dependent/Upload.t > >diff --git a/t/db_dependent/Upload.t b/t/db_dependent/Upload.t >new file mode 100644 >index 0000000..c97b39c >--- /dev/null >+++ b/t/db_dependent/Upload.t >@@ -0,0 +1,158 @@ >+#!/usr/bin/perl >+ >+use Modern::Perl; >+use File::Temp qw/ tempdir /; >+use Test::More tests => 6; >+ >+use Test::MockModule; >+use t::lib::Mocks; >+ >+use C4::Context; >+use Koha::Upload; >+ >+my $dbh = C4::Context->dbh; >+$dbh->{AutoCommit} = 0; >+$dbh->{RaiseError} = 1; >+ >+our $current_upload = 0; >+our $uploads = [ >+ [ >+ { name => 'file1', cat => 'A', size => 6000 }, >+ { name => 'file2', cat => 'A', size => 8000 }, >+ ], >+ [ >+ { name => 'file3', cat => 'B', size => 1000 }, >+ ], >+ [ >+ { name => 'file4', cat => undef, size => 5000 }, # temporary >+ ], >+ [ >+ { name => 'file2', cat => 'A', size => 8000 }, >+ # uploading a duplicate in cat A should fail >+ ], >+ [ >+ { name => 'file4', cat => undef, size => 5000 }, # temp duplicate >+ ], >+]; >+ >+# Redirect upload dir structure and mock File::Spec and CGI >+my $tempdir = tempdir( CLEANUP => 1 ); >+t::lib::Mocks::mock_config('upload_path', $tempdir); >+my $specmod = Test::MockModule->new( 'File::Spec' ); >+$specmod->mock( 'tmpdir' => sub { return $tempdir; } ); >+my $cgimod = Test::MockModule->new( 'CGI' ); >+$cgimod->mock( 'new' => \&newCGI ); >+ >+# Start testing >+subtest 'Test01' => sub { >+ plan tests => 7; >+ test01(); >+}; >+subtest 'Test02' => sub { >+ plan tests => 4; >+ test02(); >+}; >+subtest 'Test03' => sub { >+ plan tests => 2; >+ test03(); >+}; >+subtest 'Test04' => sub { >+ plan tests => 3; >+ test04(); >+}; >+subtest 'Test05' => sub { >+ plan tests => 5; >+ test05(); >+}; >+subtest 'Test06' => sub { >+ plan tests => 2; >+ test06(); >+}; >+$dbh->rollback; >+ >+sub test01 { >+ my $upl = Koha::Upload->new({ >+ category => $uploads->[$current_upload]->[0]->{cat}, >+ }); >+ my $cgi= $upl->cgi; >+ my $res= $upl->result; >+ is( $res =~ /^\d+,\d+$/, 1, 'Upload 1 includes two files' ); >+ is( $upl->count, 2, 'Count returns 2 also' ); >+ foreach my $r ( $upl->get({ id => $res }) ) { >+ if( $r->{name} eq 'file1' ) { >+ is( $r->{categorycode}, 'A', 'Check category A' ); >+ is( $r->{filesize}, 6000, 'Check size of file1' ); >+ } elsif( $r->{name} eq 'file2' ) { >+ is( $r->{filesize}, 8000, 'Check size of file2' ); >+ is( $r->{public}, undef, 'Check public undefined' ); >+ } >+ } >+ is( $upl->err, undef, 'No errors reported' ); >+} >+ >+sub test02 { >+ my $upl = Koha::Upload->new({ >+ category => $uploads->[$current_upload]->[0]->{cat}, >+ public => 1, >+ }); >+ my $cgi= $upl->cgi; >+ is( $upl->count, 1, 'Upload 2 includes one file' ); >+ my $res= $upl->result; >+ my $r = $upl->get({ id => $res, filehandle => 1 }); >+ is( $r->{categorycode}, 'B', 'Check category B' ); >+ is( $r->{public}, 1, 'Check public == 1' ); >+ is( ref($r->{fh}) eq 'IO::File' && $r->{fh}->opened, 1, 'Get returns a file handle' ); >+} >+ >+sub test03 { >+ my $upl = Koha::Upload->new; #temporary >+ my $cgi= $upl->cgi; >+ is( $upl->count, 1, 'Upload 3 includes one temporary file' ); >+ my $r = $upl->get({ id => $upl->result }); >+ is( $r->{categorycode}, undef, 'Category NULL for temp file' ); >+} >+ >+sub test04 { # Fail on a file already there >+ my $upl = Koha::Upload->new({ >+ category => $uploads->[$current_upload]->[0]->{cat}, >+ }); >+ my $cgi= $upl->cgi; >+ is( $upl->count, 0, 'Upload 4 failed as expected' ); >+ is( $upl->result, undef, 'Result is undefined' ); >+ my $e = $upl->err; >+ is( $e->{file2}, 1, "Errcode 1 [already exists] reported" ); >+} >+ >+sub test05 { # add temporary file with same name and contents, delete it >+ my $upl = Koha::Upload->new({ tmp => 1 }); >+ my $cgi= $upl->cgi; >+ is( $upl->count, 1, 'Upload 5 adds duplicate temporary file' ); >+ my $id = $upl->result; >+ my $r = $upl->get({ id => $id }); >+ my @d = $upl->delete({ id => $id }); >+ is( $d[0], $r->{name}, 'Delete successful' ); >+ is( -e $r->{path}? 1: 0, 0, 'File no longer found after delete' ); >+ is( scalar $upl->get({ id => $id }), undef, 'Record also gone' ); >+ is( $upl->delete({ id => $id }), undef, 'Repeated delete failed' ); >+} >+ >+sub test06 { #simple test for httpheaders and getCategories >+ my @hdrs = Koha::Upload->httpheaders('does_not_matter_yet'); >+ is( @hdrs == 4 && $hdrs[1] =~ /application\/octet-stream/, 1, 'Simple test for httpheaders'); >+ $dbh->do("INSERT INTO authorised_values (category, authorised_value, lib) VALUES (?,?,?) ", undef, ( 'UPLOAD', 'HAVE_AT_LEAST_ONE', 'Hi there' )); >+ my $cat = Koha::Upload->getCategories; >+ is( @$cat >= 1, 1, 'getCategories returned at least one category' ); >+} >+ >+sub newCGI { >+ my ( $class, $hook ) = @_; >+ my $read = 0; >+ foreach my $uh ( @{$uploads->[ $current_upload ]} ) { >+ for( my $i=0; $i< $uh->{size}; $i+=1000 ) { >+ $read+= 1000; >+ &$hook( $uh->{name}, 'a'x1000, $read ); >+ } >+ } >+ $current_upload++; >+ return $class; >+} >-- >1.7.10.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 14321
:
41498
|
41499
|
41500
|
41501
|
41555
|
41556
|
41557
|
41558
|
41559
|
41560
|
41561
|
41562
|
41665
|
41666
|
41667
|
41668
|
41669
|
41677
|
41873
|
41874
|
41875
|
41876
|
41877
|
41878
|
41879
|
41880
|
41881
|
42411
|
42412
|
42413
|
42414
|
42558
|
42559
|
42560
|
42561
|
42584
|
42587
|
42588
|
42589
|
42590
|
42591
|
42660
|
42661
|
42662
|
42663
|
42664
|
42665
|
42666
|
42707
|
42726
|
42727
|
42728
|
42729
|
42730
|
42731
|
42732
|
42889
|
42890