Bug 7317: (followup) Make tests independent of data on the DB
[koha.git] / t / db_dependent / Illrequests.t
index c778bad..4259a70 100644 (file)
@@ -103,6 +103,8 @@ subtest 'Working with related objects' => sub {
 
     $schema->storage->txn_begin;
 
+    Koha::Illrequests->search->delete;
+
     my $patron = $builder->build({ source => 'Borrower' });
     my $illrq = $builder->build({
         source => 'Illrequest',
@@ -220,20 +222,20 @@ subtest 'Backend testing (mocks)' => sub {
     # the Dummy plugin installed.  load_backend & available_backends don't
     # currently have tests as a result.
 
+    t::lib::Mocks->mock_config('interlibrary_loans', { backend_dir => 'a_dir' }  );
     my $backend = Test::MockObject->new;
     $backend->set_isa('Koha::Illbackends::Mock');
     $backend->set_always('name', 'Mock');
 
     my $patron = $builder->build({ source => 'Borrower' });
-    my $illrq = $builder->build({
-        source => 'Illrequest',
+    my $illrq = $builder->build_object({
+        class => 'Koha::Illrequests',
         value => { borrowernumber => $patron->{borrowernumber} }
     });
-    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
 
-    $illrq_obj->_backend($backend);
+    $illrq->_backend($backend);
 
-    isa_ok($illrq_obj->_backend, 'Koha::Illbackends::Mock',
+    isa_ok($illrq->_backend, 'Koha::Illbackends::Mock',
            "OK accessing mocked backend.");
 
     # _backend_capability tests:
@@ -245,15 +247,15 @@ subtest 'Backend testing (mocks)' => sub {
     # functionality, such as unmediated in the BLDSS backend (also see
     # bugzilla 18837).
     $backend->set_always('capabilities', undef);
-    is($illrq_obj->_backend_capability('Test'), 0,
+    is($illrq->_backend_capability('Test'), 0,
        "0 returned on Mock not implementing capabilities.");
 
     $backend->set_always('capabilities', 0);
-    is($illrq_obj->_backend_capability('Test'), 0,
+    is($illrq->_backend_capability('Test'), 0,
        "0 returned on Mock not implementing Test capability.");
 
     $backend->set_always('capabilities', sub { return 'bar'; } );
-    is($illrq_obj->_backend_capability('Test'), 'bar',
+    is($illrq->_backend_capability('Test'), 'bar',
        "'bar' returned on Mock implementing Test capability.");
 
     # metadata test: we need to be sure that we return the arbitrary values
@@ -270,10 +272,10 @@ subtest 'Backend testing (mocks)' => sub {
     );
 
     is_deeply(
-        $illrq_obj->metadata,
+        $illrq->metadata,
         {
-            ID => $illrq_obj->illrequest_id,
-            Title => $illrq_obj->patron->borrowernumber
+            ID => $illrq->illrequest_id,
+            Title => $illrq->patron->borrowernumber
         },
         "Test metadata."
     );
@@ -282,7 +284,7 @@ subtest 'Backend testing (mocks)' => sub {
 
     # No backend graph extension
     $backend->set_always('status_graph', {});
-    is_deeply($illrq_obj->capabilities('COMP'),
+    is_deeply($illrq->capabilities('COMP'),
               {
                   prev_actions   => [ 'REQ' ],
                   id             => 'COMP',
@@ -293,10 +295,10 @@ subtest 'Backend testing (mocks)' => sub {
                   ui_method_icon => 'fa-check',
               },
               "Dummy status graph for COMP.");
-    is($illrq_obj->capabilities('UNKNOWN'), undef,
+    is($illrq->capabilities('UNKNOWN'), undef,
        "Dummy status graph for UNKNOWN.");
-    is_deeply($illrq_obj->capabilities(),
-              $illrq_obj->_core_status_graph,
+    is_deeply($illrq->capabilities(),
+              $illrq->_core_status_graph,
               "Dummy full status graph.");
     # Simple backend graph extension
     $backend->set_always('status_graph',
@@ -307,18 +309,18 @@ subtest 'Backend testing (mocks)' => sub {
                                  next_actions   => [ 'REQ' ],
                              },
                          });
-    is_deeply($illrq_obj->capabilities('QER'),
+    is_deeply($illrq->capabilities('QER'),
               {
                   prev_actions   => [ 'REQ' ],
                   id             => 'QER',
                   next_actions   => [ 'REQ' ],
               },
               "Simple status graph for QER.");
-    is($illrq_obj->capabilities('UNKNOWN'), undef,
+    is($illrq->capabilities('UNKNOWN'), undef,
        "Simple status graph for UNKNOWN.");
-    is_deeply($illrq_obj->capabilities(),
-              $illrq_obj->_status_graph_union(
-                  $illrq_obj->_core_status_graph,
+    is_deeply($illrq->capabilities(),
+              $illrq->_status_graph_union(
+                  $illrq->_core_status_graph,
                   {
                       QER => {
                           prev_actions   => [ 'REQ' ],
@@ -333,7 +335,7 @@ subtest 'Backend testing (mocks)' => sub {
 
     # No backend graph extension
     $backend->set_always('status_graph', {});
-    is($illrq_obj->custom_capability('unknown', {}), 0,
+    is($illrq->custom_capability('unknown', {}), 0,
        "Unknown candidate.");
 
     # Simple backend graph extension
@@ -348,7 +350,7 @@ subtest 'Backend testing (mocks)' => sub {
                          });
     $backend->mock('identity',
                    sub { my ( $self, $params ) = @_; return $params->{other}; });
-    is($illrq_obj->custom_capability('identity', { test => 1 })->{test}, 1,
+    is($illrq->custom_capability('identity', { test => 1, method => 'blah' })->{test}, 1,
        "Resolve identity custom_capability");
 
     $schema->storage->txn_rollback;
@@ -357,7 +359,7 @@ subtest 'Backend testing (mocks)' => sub {
 
 subtest 'Backend core methods' => sub {
 
-    plan tests => 16;
+    plan tests => 18;
 
     $schema->storage->txn_begin;
 
@@ -371,13 +373,26 @@ subtest 'Backend core methods' => sub {
     $config->set_always('getLimitRules',
                         { default => { count => 0, method => 'active' } });
 
-    my $illrq = $builder->build({source => 'Illrequest'});
-    my $illrq_obj = Koha::Illrequests->find($illrq->{illrequest_id});
-    $illrq_obj->_config($config);
-    $illrq_obj->_backend($backend);
+    my $illrq = $builder->build_object({
+        class => 'Koha::Illrequests',
+        value => { backend => undef }
+    });
+    $illrq->_config($config);
+
+    # Test error conditions (no backend)
+    throws_ok { $illrq->load_backend; }
+        'Koha::Exceptions::Ill::InvalidBackendId',
+        'Exception raised correctly';
+
+    throws_ok { $illrq->load_backend(''); }
+        'Koha::Exceptions::Ill::InvalidBackendId',
+        'Exception raised correctly';
+
+    # Now load the mocked backend
+    $illrq->_backend($backend);
 
     # expandTemplate:
-    is_deeply($illrq_obj->expandTemplate({ test => 1, method => "bar" }),
+    is_deeply($illrq->expandTemplate({ test => 1, method => "bar" }),
               {
                   test => 1,
                   method => "bar",
@@ -394,7 +409,7 @@ subtest 'Backend core methods' => sub {
                          { stage => 'commit', method => 'create' });
     # Test Copyright Clearance
     t::lib::Mocks::mock_preference("ILLModuleCopyrightClearance", "Test Copyright Clearance.");
-    is_deeply($illrq_obj->backend_create({test => 1}),
+    is_deeply($illrq->backend_create({test => 1}),
               {
                   error   => 0,
                   status  => '',
@@ -408,7 +423,7 @@ subtest 'Backend core methods' => sub {
               "Backend create: copyright clearance.");
     t::lib::Mocks::mock_preference("ILLModuleCopyrightClearance", "");
     # Test non-commit
-    is_deeply($illrq_obj->backend_create({test => 1}),
+    is_deeply($illrq->backend_create({test => 1}),
               {
                   stage => 'bar', method => 'create',
                   template => "/tmp/Mock/intra-includes/create.inc",
@@ -416,28 +431,28 @@ subtest 'Backend core methods' => sub {
               },
               "Backend create: arbitrary stage.");
     # Test commit
-    is_deeply($illrq_obj->backend_create({test => 1}),
+    is_deeply($illrq->backend_create({test => 1}),
               {
                   stage => 'commit', method => 'create', permitted => 0,
                   template => "/tmp/Mock/intra-includes/create.inc",
                   opac_template => "/tmp/Mock/opac-includes/create.inc",
               },
               "Backend create: arbitrary stage, not permitted.");
-    is($illrq_obj->status, "QUEUED", "Backend create: queued if restricted.");
+    is($illrq->status, "QUEUED", "Backend create: queued if restricted.");
     $config->set_always('getLimitRules', {});
-    $illrq_obj->status('NEW');
-    is_deeply($illrq_obj->backend_create({test => 1}),
+    $illrq->status('NEW');
+    is_deeply($illrq->backend_create({test => 1}),
               {
                   stage => 'commit', method => 'create', permitted => 1,
                   template => "/tmp/Mock/intra-includes/create.inc",
                   opac_template => "/tmp/Mock/opac-includes/create.inc",
               },
               "Backend create: arbitrary stage, permitted.");
-    is($illrq_obj->status, "NEW", "Backend create: not-queued.");
+    is($illrq->status, "NEW", "Backend create: not-queued.");
 
     # backend_renew
     $backend->set_series('renew', { stage => 'bar', method => 'renew' });
-    is_deeply($illrq_obj->backend_renew({test => 1}),
+    is_deeply($illrq->backend_renew({test => 1}),
               {
                   stage => 'bar', method => 'renew',
                   template => "/tmp/Mock/intra-includes/renew.inc",
@@ -447,7 +462,7 @@ subtest 'Backend core methods' => sub {
 
     # backend_cancel
     $backend->set_series('cancel', { stage => 'bar', method => 'cancel' });
-    is_deeply($illrq_obj->backend_cancel({test => 1}),
+    is_deeply($illrq->backend_cancel({test => 1}),
               {
                   stage => 'bar', method => 'cancel',
                   template => "/tmp/Mock/intra-includes/cancel.inc",
@@ -457,7 +472,7 @@ subtest 'Backend core methods' => sub {
 
     # backend_update_status
     $backend->set_series('update_status', { stage => 'bar', method => 'update_status' });
-    is_deeply($illrq_obj->backend_update_status({test => 1}),
+    is_deeply($illrq->backend_update_status({test => 1}),
               {
                   stage => 'bar', method => 'update_status',
                   template => "/tmp/Mock/intra-includes/update_status.inc",
@@ -467,7 +482,7 @@ subtest 'Backend core methods' => sub {
 
     # backend_confirm
     $backend->set_series('confirm', { stage => 'bar', method => 'confirm' });
-    is_deeply($illrq_obj->backend_confirm({test => 1}),
+    is_deeply($illrq->backend_confirm({test => 1}),
               {
                   stage => 'bar', method => 'confirm',
                   template => "/tmp/Mock/intra-includes/confirm.inc",
@@ -489,7 +504,7 @@ subtest 'Backend core methods' => sub {
         source => 'Borrower',
         value => { categorycode => "ILLTSTLIB" },
     });
-    my $gen_conf = $illrq_obj->generic_confirm({
+    my $gen_conf = $illrq->generic_confirm({
         current_branchcode => $illbrn->{branchcode}
     });
     isnt(index($gen_conf->{value}->{draft}->{body}, $backend->metadata->{Test}), -1,
@@ -502,13 +517,13 @@ subtest 'Backend core methods' => sub {
        "Generic confirm: partner 2 is correct."
     );
 
-    dies_ok { $illrq_obj->generic_confirm({
+    dies_ok { $illrq->generic_confirm({
         current_branchcode => $illbrn->{branchcode},
         stage => 'draft'
     }) }
         "Generic confirm: missing to dies OK.";
 
-    dies_ok { $illrq_obj->generic_confirm({
+    dies_ok { $illrq->generic_confirm({
         current_branchcode => $illbrn->{branchcode},
         partners => $partner1->{email},
         stage => 'draft'