Bug 26635: Add tests
authorAgustin Moyano <agustinmoyano@theke.io>
Fri, 9 Oct 2020 12:59:46 +0000 (09:59 -0300)
committerTomas Cohen Arazi <tomascohen@theke.io>
Wed, 9 Nov 2022 17:00:31 +0000 (14:00 -0300)
Add tests in t/db_dependent/Koha/Object.t and
t/db_dependent/Koha/REST/Plugin/Objects.t

Sponsored-by: Virginia Polytechnic Institute and State University
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
t/db_dependent/Koha/Object.t
t/db_dependent/Koha/REST/Plugin/Objects.t

index 4adafcb..d1495d3 100755 (executable)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 21;
+use Test::More tests => 22;
 use Test::Exception;
 use Test::Warn;
 use DateTime;
@@ -29,6 +29,9 @@ use C4::Biblio qw( AddBiblio );
 use Koha::Database;
 
 use Koha::Acquisition::Orders;
+use Koha::ApiKeys;
+use Koha::AuthorisedValueCategories;
+use Koha::AuthorisedValues;
 use Koha::DateUtils qw( dt_from_string );
 use Koha::Libraries;
 use Koha::Patrons;
@@ -996,3 +999,42 @@ subtest 'messages() and add_message() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'Authorised values expansion' => sub {
+    plan tests => 4;
+
+    $schema->storage->txn_begin;
+
+    Koha::AuthorisedValues->search({category => 'Countries'})->delete;
+    Koha::AuthorisedValueCategories->search({category_name =>'Countries'})->delete;
+
+    my $cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories', value => {category_name =>'Countries'} });
+    my $fr = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'FR', lib=>'France', category=>$cat->category_name} });
+    my $us = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'US', lib=>'United States of America', category=>$cat->category_name} });
+    my $ar = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'AR', lib=>'Argentina', category=>$cat->category_name} });
+
+    my $city_class = Test::MockModule->new('Koha::City');
+    $city_class->mock( '_fetch_authorised_values',
+        sub {
+            my ($self) = @_;
+            use Koha::AuthorisedValues;
+            my $av = Koha::AuthorisedValues->find({authorised_value => $self->city_country, category => 'Countries'});
+            return {country => $av->unblessed};
+        }
+    );
+
+    my $marseille = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'FR', city_name => 'Marseille'} });
+    my $cordoba = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'AR', city_name => 'Córdoba'} });
+
+    my $mobj = $marseille->to_api({av_expand => 1});
+    my $cobj = $cordoba->to_api({av_expand => 1});
+
+    isnt($mobj->{_authorised_values}, undef, '_authorised_values exists for Marseille');
+    isnt($cobj->{_authorised_values}, undef, '_authorised_values exists for Córdoba');
+
+    is($mobj->{_authorised_values}->{country}->{lib}, $fr->lib, 'Authorised value for country expanded');
+    is($cobj->{_authorised_values}->{country}->{lib}, $ar->lib, 'Authorised value for country expanded');
+
+
+    $schema->storage->txn_rollback;
+};
index 8deee9d..60163ee 100755 (executable)
@@ -18,6 +18,8 @@
 use Modern::Perl;
 
 use Koha::Acquisition::Orders;
+use Koha::AuthorisedValueCategories;
+use Koha::AuthorisedValues;
 use Koha::Cities;
 use Koha::Biblios;
 use Koha::Patrons;
@@ -117,7 +119,7 @@ get '/my_patrons' => sub {
 };
 
 # The tests
-use Test::More tests => 14;
+use Test::More tests => 16;
 use Test::Mojo;
 
 use t::lib::Mocks;
@@ -385,7 +387,7 @@ subtest 'objects.search helper, embed' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'object.search helper with query parameter' => sub {
+subtest 'objects.search helper with query parameter' => sub {
     plan tests => 4;
 
     $schema->storage->txn_begin;
@@ -409,7 +411,7 @@ subtest 'object.search helper with query parameter' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'object.search helper with q parameter' => sub {
+subtest 'objects.search helper with q parameter' => sub {
     plan tests => 4;
 
     $schema->storage->txn_begin;
@@ -433,7 +435,7 @@ subtest 'object.search helper with q parameter' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'object.search helper with x-koha-query header' => sub {
+subtest 'objects.search helper with x-koha-query header' => sub {
     plan tests => 4;
 
     $schema->storage->txn_begin;
@@ -457,7 +459,7 @@ subtest 'object.search helper with x-koha-query header' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'object.search helper with all query methods' => sub {
+subtest 'objects.search helper with all query methods' => sub {
     plan tests => 6;
 
     $schema->storage->txn_begin;
@@ -484,8 +486,7 @@ subtest 'object.search helper with all query methods' => sub {
     $schema->storage->txn_rollback;
 };
 
-subtest 'object.search helper order by embedded columns' => sub {
-
+subtest 'objects.search helper order by embedded columns' => sub {
     plan tests => 3;
 
     $schema->storage->txn_begin;
@@ -618,3 +619,194 @@ subtest 'objects.search helper, search_limited() tests' => sub {
 
     $schema->storage->txn_rollback;
 };
+
+subtest 'objects.find helper with expanded authorised values' => sub {
+    plan tests => 10;
+
+    $schema->storage->txn_begin;
+
+    my $t = Test::Mojo->new;
+
+    Koha::AuthorisedValues->search( { category => 'Countries' } )->delete;
+    Koha::AuthorisedValueCategories->search( { category_name => 'Countries' } )
+      ->delete;
+
+    my $cat = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValueCategories',
+            value => { category_name => 'Countries' }
+        }
+    );
+    my $fr = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 'FR',
+                lib              => 'France',
+                category         => $cat->category_name
+            }
+        }
+    );
+    my $us = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 'US',
+                lib              => 'United States of America',
+                category         => $cat->category_name
+            }
+        }
+    );
+    my $ar = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 'AR',
+                lib              => 'Argentina',
+                category         => $cat->category_name
+            }
+        }
+    );
+
+    my $city_class = Test::MockModule->new('Koha::City');
+    $city_class->mock(
+        '_fetch_authorised_values',
+        sub {
+            my ($self) = @_;
+            use Koha::AuthorisedValues;
+            my $av = Koha::AuthorisedValues->find(
+                {
+                    authorised_value => $self->city_country,
+                    category         => 'Countries'
+                }
+            );
+            return { country => $av->unblessed };
+        }
+    );
+
+    my $manuel = $builder->build_object(
+        {
+            class => 'Koha::Cities',
+            value => {
+                city_name    => 'Manuel',
+                city_country => 'AR'
+            }
+        }
+    );
+    my $manuela = $builder->build_object(
+        {
+            class => 'Koha::Cities',
+            value => {
+                city_name    => 'Manuela',
+                city_country => 'US'
+            }
+        }
+    );
+
+    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 1 } )
+      ->status_is(200)->json_is( '/name' => 'Manuel' )
+      ->json_has('/_authorised_values')
+      ->json_is( '/_authorised_values/country/lib' => $ar->lib );
+
+    $t->get_ok( '/cities/' . $manuela->cityid => { 'x-koha-av-expand' => 1 } )
+      ->status_is(200)->json_is( '/name' => 'Manuela' )
+      ->json_has('/_authorised_values')
+      ->json_is( '/_authorised_values/country/lib' => $us->lib );
+
+    $schema->storage->txn_rollback;
+};
+
+subtest 'objects.search helper with expanded authorised values' => sub {
+
+    plan tests => 11;
+
+    my $t = Test::Mojo->new;
+
+    $schema->storage->txn_begin;
+
+    Koha::AuthorisedValues->search( { category => 'Countries' } )->delete;
+    Koha::AuthorisedValueCategories->search( { category_name => 'Countries' } )
+      ->delete;
+
+    my $cat = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValueCategories',
+            value => { category_name => 'Countries' }
+        }
+    );
+    my $fr = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 'FR',
+                lib              => 'France',
+                category         => $cat->category_name
+            }
+        }
+    );
+    my $us = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 'US',
+                lib              => 'United States of America',
+                category         => $cat->category_name
+            }
+        }
+    );
+    my $ar = $builder->build_object(
+        {
+            class => 'Koha::AuthorisedValues',
+            value => {
+                authorised_value => 'AR',
+                lib              => 'Argentina',
+                category         => $cat->category_name
+            }
+        }
+    );
+
+    my $city_class = Test::MockModule->new('Koha::City');
+    $city_class->mock(
+        '_fetch_authorised_values',
+        sub {
+            my ($self) = @_;
+            use Koha::AuthorisedValues;
+            my $av = Koha::AuthorisedValues->find(
+                {
+                    authorised_value => $self->city_country,
+                    category         => 'Countries'
+                }
+            );
+            return { country => $av->unblessed };
+        }
+    );
+
+    $builder->build_object(
+        {
+            class => 'Koha::Cities',
+            value => {
+                city_name    => 'Manuel',
+                city_country => 'AR'
+            }
+        }
+    );
+    $builder->build_object(
+        {
+            class => 'Koha::Cities',
+            value => {
+                city_name    => 'Manuela',
+                city_country => 'US'
+            }
+        }
+    );
+
+    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
+          { 'x-koha-av-expand' => 1 } )->status_is(200)->json_has('/0')
+      ->json_has('/1')->json_hasnt('/2')->json_is( '/0/name' => 'Manuel' )
+      ->json_has('/0/_authorised_values')
+      ->json_is( '/0/_authorised_values/country/lib' => $ar->lib )
+      ->json_is( '/1/name' => 'Manuela' )->json_has('/1/_authorised_values')
+      ->json_is( '/1/_authorised_values/country/lib' => $us->lib );
+
+    $schema->storage->txn_rollback;
+};