Bug 22132: (QA follow-up) set_password now expects a hashref
[koha.git] / t / db_dependent / Upload.t
index 6af3fce..802dc58 100644 (file)
@@ -2,7 +2,7 @@
 
 use Modern::Perl;
 use File::Temp qw/ tempdir /;
-use Test::More tests => 11;
+use Test::More tests => 13;
 use Test::Warn;
 
 use Test::MockModule;
@@ -18,7 +18,7 @@ use Koha::Uploader;
 
 my $schema  = Koha::Database->new->schema;
 $schema->storage->txn_begin;
-my $builder = t::lib::TestBuilder->new;
+our $builder = t::lib::TestBuilder->new;
 
 our $current_upload = 0;
 our $uploads = [
@@ -48,11 +48,11 @@ our $uploads = [
     ],
 ];
 
-# Redirect upload dir structure and mock File::Spec and CGI
+# Redirect upload dir structure and mock C4::Context 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 $specmod = Test::MockModule->new( 'C4::Context' );
+$specmod->mock( 'temporary_directory' => sub { return $tempdir; } );
 my $cgimod = Test::MockModule->new( 'CGI' );
 $cgimod->mock( 'new' => \&newCGI );
 
@@ -74,7 +74,7 @@ subtest 'permanent_directory and temporary_directory' => sub {
     # Check mocked directories
     is( Koha::UploadedFile->permanent_directory, $tempdir,
         'Check permanent directory' );
-    is( Koha::UploadedFile->temporary_directory, $tempdir,
+    is( C4::Context::temporary_directory, $tempdir,
         'Check temporary directory' );
 };
 
@@ -145,7 +145,7 @@ subtest 'Add same file in same category' => sub {
     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" );
+    is( $e->{file2}->{code}, Koha::Uploader::ERR_EXISTS, "Already exists error reported" );
 };
 
 subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
@@ -185,13 +185,33 @@ subtest 'Test delete via UploadedFile as well as UploadedFiles' => sub {
 
     # add another one with TestBuilder and delete twice (file does not exist)
     $upload01 = $builder->build({ source => 'UploadedFile' });
-    my $kohaobj = Koha::UploadedFiles->find( $upload01->{id} );
+    $kohaobj = Koha::UploadedFiles->find( $upload01->{id} );
     $delete = $kohaobj->delete({ keep_file => 1 });
     $delete = $kohaobj->delete({ keep_file => 1 });
     ok( $delete =~ /^(0E0|-1)$/, 'Repeated delete unsuccessful' );
     # NOTE: Koha::Object->delete does not return 0E0 (yet?)
 };
 
+subtest 'Test delete_missing' => sub {
+    plan tests => 5;
+
+    # If we add files via TestBuilder, they do not exist
+    my $upload01 = $builder->build({ source => 'UploadedFile' });
+    my $upload02 = $builder->build({ source => 'UploadedFile' });
+    # dry run first
+    my $deleted = Koha::UploadedFiles->delete_missing({ keep_record => 1 });
+    is( $deleted, 2, 'Expect two records with missing files' );
+    isnt( Koha::UploadedFiles->find( $upload01->{id} ), undef, 'Not deleted' );
+    $deleted = Koha::UploadedFiles->delete_missing;
+    ok( $deleted =~ /^(2|-1)$/, 'Deleted two records with missing files' );
+    is( Koha::UploadedFiles->search({
+        id => [ $upload01->{id}, $upload02->{id} ],
+    })->count, 0, 'Records are gone' );
+    # Repeat it
+    $deleted = Koha::UploadedFiles->delete_missing;
+    is( $deleted, "0E0", "Return value of 0E0 expected" );
+};
+
 subtest 'Call search_term with[out] private flag' => sub {
     plan tests => 3;
 
@@ -296,6 +316,17 @@ subtest 'Testing delete_temporary' => sub {
         'Still 3 permanent uploads' );
 };
 
+subtest 'Testing download headers' => sub {
+    plan tests => 2;
+    my $test_pdf = Koha::UploadedFile->new({ filename => 'pdf.pdf', uploadcategorycode => 'B', filesize => 1000 });
+    my $test_not = Koha::UploadedFile->new({ filename => 'pdf.not', uploadcategorycode => 'B', filesize => 1000 });
+    my @pdf_expect = ( '-type'=>'application/pdf','Content-Disposition'=>'inline; filename=pdf.pdf' );
+    my @not_expect = ( '-type'=>'application/octet-stream','-attachment'=>'pdf.not' );
+    my @pdf_head = $test_pdf->httpheaders;
+    my @not_head = $test_not->httpheaders;
+    is_deeply(\@pdf_head, \@pdf_expect,"Get inline pdf headers for pdf");
+    is_deeply(\@not_head, \@not_expect,"Get download headers for non pdf");
+};
 # The end
 $schema->storage->txn_rollback;