| 
      
            Lines 1-49
          
      
      
        Link Here
      
     | 
  
        
          | 1 | 
          #!/usr/bin/perl  | 
          1 | 
          #!/usr/bin/perl  | 
        
            
              | 2 | 
              #  | 
               | 
               | 
            
            
              | 3 | 
              # This Koha test module is a stub!    | 
            
            
              | 4 | 
              # Add more tests here!!!  | 
            
        
          | 5 | 
           | 
          2 | 
           | 
        
          
            
              | 6 | 
              use strict;  | 
              3 | 
              use Modern::Perl;  | 
            
            
              | 7 | 
              use warnings;  | 
               | 
               | 
            
        
          | 8 | 
          use C4::Auth;  | 
          4 | 
          use C4::Auth;  | 
        
        
          | 9 | 
          use CGI qw ( -utf8 );  | 
          5 | 
          use CGI qw ( -utf8 );  | 
        
          
            
              | 10 | 
              use Test::More tests => 14;  | 
              6 | 
              use Test::More tests => 18;  | 
            
        
          | 11 | 
           | 
          7 | 
           | 
        
        
          | 12 | 
          BEGIN { | 
          8 | 
          BEGIN { | 
        
          
            
              | 13 | 
                      use_ok('C4::BackgroundJob'); | 
              9 | 
                  use_ok('C4::BackgroundJob'); | 
            
        
          | 14 | 
          }  | 
          10 | 
          }  | 
        
        
          | 15 | 
          my $query = new CGI;  | 
          11 | 
          my $query = new CGI;  | 
        
            
              | 16 | 
              my ($userid, $cookie, $sessionID) = &checkauth($query, 1);  | 
               | 
               | 
            
            
              | 17 | 
              #my ($sessionID, $job_name, $job_invoker, $num_work_units) = @_;  | 
            
            
              | 18 | 
              my $background;  | 
            
            
              | 19 | 
              diag $sessionID;  | 
            
            
              | 20 | 
              ok ($background=C4::BackgroundJob->new($sessionID), "making job");  | 
            
            
              | 21 | 
              ok ($background->id, "fetching id number");  | 
            
        
          | 22 | 
           | 
          12 | 
           | 
        
          
            
              | 23 | 
              $background->name("George"); | 
              13 | 
              # Generate a session id  | 
            
            
              | 24 | 
              is ($background->name, "George", "testing name");  | 
              14 | 
              my $dbh     = C4::Context->dbh;  | 
            
            
               | 
               | 
              15 | 
              $dbh->{AutoCommit} = 1; | 
            
            
              | 16 | 
              $dbh->{RaiseError} = 1; | 
            
        
          | 25 | 
           | 
          17 | 
           | 
        
          
            
              | 26 | 
              $background->invoker("enjoys"); | 
              18 | 
              my $session = C4::Auth::get_session;  | 
            
            
              | 27 | 
              is ($background->invoker, "enjoys", "testing invoker");  | 
              19 | 
              $session->flush;  | 
            
            
               | 
               | 
              20 | 
              my $sessionID = $session->id;  | 
            
            
              | 21 | 
              my $job;  | 
            
            
              | 22 | 
              ok( $job = C4::BackgroundJob->new($sessionID), "making job" );  | 
            
            
              | 23 | 
              ok( $job->id, "fetching id number" );  | 
            
        
          | 28 | 
           | 
          24 | 
           | 
        
          
            
              | 29 | 
              $background->progress("testing"); | 
              25 | 
              $job->name("George"); | 
            
            
              | 30 | 
              is ($background->progress, "testing", "testing progress");  | 
              26 | 
              is( $job->name, "George", "testing name" );  | 
            
        
          | 31 | 
           | 
          27 | 
           | 
        
          
            
              | 32 | 
              ok ($background->status, "testing status");  | 
              28 | 
              $job->invoker("enjoys"); | 
            
            
               | 
               | 
              29 | 
              is( $job->invoker, "enjoys", "testing invoker" );  | 
            
        
          | 33 | 
           | 
          30 | 
           | 
        
          
            
              | 34 | 
              $background->size("56"); | 
              31 | 
              $job->progress("testing"); | 
            
            
              | 35 | 
              is ($background->size, "56", "testing size");  | 
              32 | 
              is( $job->progress, "testing", "testing progress" );  | 
            
        
          | 36 | 
           | 
          33 | 
           | 
        
          
            
              | 37 | 
              ok (!$background->fetch($sessionID, $background->id), "testing fetch");  | 
              34 | 
              ok( $job->status, "testing status" );  | 
            
        
          | 38 | 
           | 
          35 | 
           | 
        
          
            
              | 39 | 
              $background->set({ key1 => 'value1', key2 => 'value2' }); | 
              36 | 
              $job->size("56"); | 
            
            
              | 40 | 
              is ($background->get('key1'), 'value1', 'fetched extra value for key key1'); | 
              37 | 
              is( $job->size, "56", "testing size" );  | 
            
            
              | 41 | 
              is ($background->get('key2'), 'value2', 'fetched extra value for key key2'); | 
               | 
               | 
            
        
          | 42 | 
           | 
          38 | 
           | 
        
          
            
              | 43 | 
              $background->set({ size => 666 }); | 
              39 | 
              ok( C4::BackgroundJob->fetch( $sessionID, $job->id ), "testing fetch" );  | 
            
            
              | 44 | 
              is ($background->size, "56", '->set() does not scribble over private object data');  | 
              40 | 
              $job->set( { key1 => 'value1', key2 => 'value2' } ); | 
            
            
               | 
               | 
              41 | 
              is( $job->get('key1'), 'value1', 'fetched extra value for key key1' ); | 
            
            
              | 42 | 
              is( $job->get('key2'), 'value2', 'fetched extra value for key key2' ); | 
            
        
          | 45 | 
           | 
          43 | 
           | 
        
          
            
              | 46 | 
              $background->finish("finished"); | 
              44 | 
              $job->set( { size => 666 } ); | 
            
            
              | 47 | 
              is ($background->status,'completed', "testing finished");  | 
              45 | 
              is( $job->size, "56", '->set() does not scribble over private object data' );  | 
            
        
          | 48 | 
           | 
          46 | 
           | 
        
          
            
              | 49 | 
              ok ($background->results); #Will return undef unless finished  | 
              47 | 
              $job->finish("finished"); | 
            
            
               | 
               | 
              48 | 
              is( $job->status, 'completed', "testing finished" );  | 
            
            
              | 49 | 
               | 
            
            
              | 50 | 
              ok( $job->results );    #Will return undef unless finished  | 
            
            
              | 51 | 
               | 
            
            
              | 52 | 
              my $second_job = C4::BackgroundJob->new( $sessionID, "making new job" );  | 
            
            
              | 53 | 
              $session = C4::Auth::get_session( $job->{sessionID} ); | 
            
            
              | 54 | 
              is( ref( $session->param( 'job_' . $job->id ) ),        "C4::BackgroundJob", 'job_$jobid should be a C4::BackgroundJob for uncleared job 1' );  | 
            
            
              | 55 | 
              is( ref( $session->param( 'job_' . $second_job->id ) ), "C4::BackgroundJob", 'job_$jobid should be a C4::BackgroundJob for uncleared job 2' );  | 
            
            
              | 56 | 
              $job->clear;  | 
            
            
              | 57 | 
              $session = C4::Auth::get_session( $job->{sessionID} ); | 
            
            
              | 58 | 
              is( $session->param( 'job_' . $job->id ), undef, 'After clearing it, job 1 should not exist anymore in the session' );  | 
            
            
              | 59 | 
              is( ref( $session->param( 'job_' . $second_job->id ) ), "C4::BackgroundJob", 'After clear on job 1, job 2 should still be a C4::BackgroundJob' );  | 
            
            
              | 50 | 
              -   | 
               | 
               |