Bug 20287: Use more simple object for Object.t
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 25 Jun 2018 15:53:19 +0000 (12:53 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 18 Jul 2018 15:49:56 +0000 (15:49 +0000)
We initially use Koha::Patron to test Object.t, but now it overwrites
->store and so it is better to use a more simple object.
ApiKey has foreign keys and unique key.
We lost one test: there is only one unique key whereas we had 2 on
borrowers (I did not find a better option)

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
t/db_dependent/Koha/Object.t

index d7e13cf..b377e4b 100755 (executable)
@@ -29,6 +29,7 @@ use Koha::Database;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Libraries;
 use Koha::Patrons;
+use Koha::ApiKeys;
 
 use Scalar::Util qw( isvstring );
 use Try::Tiny;
@@ -239,21 +240,24 @@ subtest "Test update method" => sub {
 
 subtest 'store() tests' => sub {
 
-    plan tests => 10;
+    plan tests => 7;
+
+    # Using Koha::ApiKey to test Koha::Object>-store
+    # Simple object with foreign keys and unique key
 
     $schema->storage->txn_begin;
 
-    # Create a category to make sure its ID doesn't exist on the DB
-    my $category = $builder->build_object({ class => 'Koha::Patron::Categories' });
-    my $category_id = $category->id;
-    $category->delete;
+    # Create a patron to make sure its ID doesn't exist on the DB
+    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    my $patron_id = $patron->id;
+    $patron->delete;
 
-    my $patron = Koha::Patron->new({ categorycode => $category_id });
+    my $api_key = Koha::ApiKey->new({ patron_id => $patron_id });
 
     my $print_error = $schema->storage->dbh->{PrintError};
     $schema->storage->dbh->{PrintError} = 0; # FIXME This does not longer work - because of the transaction in Koha::Patron->store?
     throws_ok
-        { $patron->store }
+        { $api_key->store }
         'Koha::Exceptions::Object::FKConstraint',
         'Exception is thrown correctly';
     is(
@@ -263,45 +267,20 @@ subtest 'store() tests' => sub {
     );
     is(
         $@->broken_fk,
-        'categorycode',
+        'patron_id',
         'Exception field is correct'
     );
 
-    my $library = $builder->build_object({ class => 'Koha::Libraries' });
-    $category   = $builder->build_object({ class => 'Koha::Patron::Categories' });
-    $patron     = $builder->build_object({ class => 'Koha::Patrons' });
-
-    my $new_patron = Koha::Patron->new({
-        branchcode   => $library->id,
-        cardnumber   => $patron->cardnumber,
-        categorycode => $category->id
-    });
-
-    throws_ok
-        { $new_patron->store }
-        'Koha::Exceptions::Object::DuplicateID',
-        'Exception is thrown correctly';
-
-    is(
-        $@->message,
-        'Duplicate ID',
-        'Exception message is correct'
-    );
-
-    is(
-       $@->duplicate_id,
-       'cardnumber',
-       'Exception field is correct'
-    );
+    $patron = $builder->build_object({ class => 'Koha::Patrons' });
+    $api_key = $builder->build_object({ class => 'Koha::ApiKeys' });
 
-    $new_patron = Koha::Patron->new({
-        branchcode   => $library->id,
-        userid       => $patron->userid,
-        categorycode => $category->id
+    my $new_api_key = Koha::ApiKey->new({
+        patron_id => $patron_id,
+        secret => $api_key->secret,
     });
 
     throws_ok
-        { $new_patron->store }
+        { $new_api_key->store }
         'Koha::Exceptions::Object::DuplicateID',
         'Exception is thrown correctly';
 
@@ -313,16 +292,16 @@ subtest 'store() tests' => sub {
 
     is(
        $@->duplicate_id,
-       'userid',
+       'secret',
        'Exception field is correct'
     );
 
     $schema->storage->dbh->{PrintError} = $print_error;
 
     # Successful test
-    $patron->set({ firstname => 'Manuel' });
-    my $ret = $patron->store;
-    is( ref($ret), 'Koha::Patron', 'store() returns the object on success' );
+    $api_key->set({ secret => 'Manuel' });
+    my $ret = $api_key->store;
+    is( ref($ret), 'Koha::ApiKey', 'store() returns the object on success' );
 
     $schema->storage->txn_rollback;
 };