View | Details | Raw Unified | Return to bug 26635
Collapse All | Expand All

(-)a/t/db_dependent/Koha/Object.t (-1 / +43 lines)
Lines 17-23 Link Here
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Test::More tests => 21;
20
use Test::More tests => 22;
21
use Test::Exception;
21
use Test::Exception;
22
use Test::Warn;
22
use Test::Warn;
23
use DateTime;
23
use DateTime;
Lines 29-34 use C4::Biblio qw( AddBiblio ); Link Here
29
use Koha::Database;
29
use Koha::Database;
30
30
31
use Koha::Acquisition::Orders;
31
use Koha::Acquisition::Orders;
32
use Koha::ApiKeys;
33
use Koha::AuthorisedValueCategories;
34
use Koha::AuthorisedValues;
32
use Koha::DateUtils qw( dt_from_string );
35
use Koha::DateUtils qw( dt_from_string );
33
use Koha::Libraries;
36
use Koha::Libraries;
34
use Koha::Patrons;
37
use Koha::Patrons;
Lines 994-998 subtest 'messages() and add_message() tests' => sub { Link Here
994
    isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
997
    isnt( $patron->object_messages, undef, '->messages initializes the array if required' );
995
    is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
998
    is( scalar @{ $patron->object_messages }, 0, '->messages returns an empty arrayref' );
996
999
1000
    $schema->storage->txn_rollback;
1001
};
1002
1003
subtest 'Authorised values expansion' => sub {
1004
    plan tests => 4;
1005
1006
    $schema->storage->txn_begin;
1007
1008
    Koha::AuthorisedValues->search({category => 'Countries'})->delete;
1009
    Koha::AuthorisedValueCategories->search({category_name =>'Countries'})->delete;
1010
1011
    my $cat = $builder->build_object({ class => 'Koha::AuthorisedValueCategories', value => {category_name =>'Countries'} });
1012
    my $fr = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'FR', lib=>'France', category=>$cat->category_name} });
1013
    my $us = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'US', lib=>'United States of America', category=>$cat->category_name} });
1014
    my $ar = $builder->build_object({ class => 'Koha::AuthorisedValues', value => {authorised_value => 'AR', lib=>'Argentina', category=>$cat->category_name} });
1015
1016
    my $city_class = Test::MockModule->new('Koha::City');
1017
    $city_class->mock( '_fetch_authorised_values',
1018
        sub {
1019
            my ($self) = @_;
1020
            use Koha::AuthorisedValues;
1021
            my $av = Koha::AuthorisedValues->find({authorised_value => $self->city_country, category => 'Countries'});
1022
            return {country => $av->unblessed};
1023
        }
1024
    );
1025
1026
    my $marseille = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'FR', city_name => 'Marseille'} });
1027
    my $cordoba = $builder->build_object({ class => 'Koha::Cities', value => {city_country => 'AR', city_name => 'Córdoba'} });
1028
1029
    my $mobj = $marseille->to_api({av_expand => 1});
1030
    my $cobj = $cordoba->to_api({av_expand => 1});
1031
1032
    isnt($mobj->{_authorised_values}, undef, '_authorised_values exists for Marseille');
1033
    isnt($cobj->{_authorised_values}, undef, '_authorised_values exists for Córdoba');
1034
1035
    is($mobj->{_authorised_values}->{country}->{lib}, $fr->lib, 'Authorised value for country expanded');
1036
    is($cobj->{_authorised_values}->{country}->{lib}, $ar->lib, 'Authorised value for country expanded');
1037
1038
997
    $schema->storage->txn_rollback;
1039
    $schema->storage->txn_rollback;
998
};
1040
};
(-)a/t/db_dependent/Koha/REST/Plugin/Objects.t (-8 / +199 lines)
Lines 18-23 Link Here
18
use Modern::Perl;
18
use Modern::Perl;
19
19
20
use Koha::Acquisition::Orders;
20
use Koha::Acquisition::Orders;
21
use Koha::AuthorisedValueCategories;
22
use Koha::AuthorisedValues;
21
use Koha::Cities;
23
use Koha::Cities;
22
use Koha::Biblios;
24
use Koha::Biblios;
23
use Koha::Patrons;
25
use Koha::Patrons;
Lines 117-123 get '/my_patrons' => sub { Link Here
117
};
119
};
118
120
119
# The tests
121
# The tests
120
use Test::More tests => 14;
122
use Test::More tests => 16;
121
use Test::Mojo;
123
use Test::Mojo;
122
124
123
use t::lib::Mocks;
125
use t::lib::Mocks;
Lines 385-391 subtest 'objects.search helper, embed' => sub { Link Here
385
    $schema->storage->txn_rollback;
387
    $schema->storage->txn_rollback;
386
};
388
};
387
389
388
subtest 'object.search helper with query parameter' => sub {
390
subtest 'objects.search helper with query parameter' => sub {
389
    plan tests => 4;
391
    plan tests => 4;
390
392
391
    $schema->storage->txn_begin;
393
    $schema->storage->txn_begin;
Lines 409-415 subtest 'object.search helper with query parameter' => sub { Link Here
409
    $schema->storage->txn_rollback;
411
    $schema->storage->txn_rollback;
410
};
412
};
411
413
412
subtest 'object.search helper with q parameter' => sub {
414
subtest 'objects.search helper with q parameter' => sub {
413
    plan tests => 4;
415
    plan tests => 4;
414
416
415
    $schema->storage->txn_begin;
417
    $schema->storage->txn_begin;
Lines 433-439 subtest 'object.search helper with q parameter' => sub { Link Here
433
    $schema->storage->txn_rollback;
435
    $schema->storage->txn_rollback;
434
};
436
};
435
437
436
subtest 'object.search helper with x-koha-query header' => sub {
438
subtest 'objects.search helper with x-koha-query header' => sub {
437
    plan tests => 4;
439
    plan tests => 4;
438
440
439
    $schema->storage->txn_begin;
441
    $schema->storage->txn_begin;
Lines 457-463 subtest 'object.search helper with x-koha-query header' => sub { Link Here
457
    $schema->storage->txn_rollback;
459
    $schema->storage->txn_rollback;
458
};
460
};
459
461
460
subtest 'object.search helper with all query methods' => sub {
462
subtest 'objects.search helper with all query methods' => sub {
461
    plan tests => 6;
463
    plan tests => 6;
462
464
463
    $schema->storage->txn_begin;
465
    $schema->storage->txn_begin;
Lines 484-491 subtest 'object.search helper with all query methods' => sub { Link Here
484
    $schema->storage->txn_rollback;
486
    $schema->storage->txn_rollback;
485
};
487
};
486
488
487
subtest 'object.search helper order by embedded columns' => sub {
489
subtest 'objects.search helper order by embedded columns' => sub {
488
489
    plan tests => 3;
490
    plan tests => 3;
490
491
491
    $schema->storage->txn_begin;
492
    $schema->storage->txn_begin;
Lines 618-620 subtest 'objects.search helper, search_limited() tests' => sub { Link Here
618
619
619
    $schema->storage->txn_rollback;
620
    $schema->storage->txn_rollback;
620
};
621
};
621
- 
622
623
subtest 'objects.find helper with expanded authorised values' => sub {
624
    plan tests => 10;
625
626
    $schema->storage->txn_begin;
627
628
    my $t = Test::Mojo->new;
629
630
    Koha::AuthorisedValues->search( { category => 'Countries' } )->delete;
631
    Koha::AuthorisedValueCategories->search( { category_name => 'Countries' } )
632
      ->delete;
633
634
    my $cat = $builder->build_object(
635
        {
636
            class => 'Koha::AuthorisedValueCategories',
637
            value => { category_name => 'Countries' }
638
        }
639
    );
640
    my $fr = $builder->build_object(
641
        {
642
            class => 'Koha::AuthorisedValues',
643
            value => {
644
                authorised_value => 'FR',
645
                lib              => 'France',
646
                category         => $cat->category_name
647
            }
648
        }
649
    );
650
    my $us = $builder->build_object(
651
        {
652
            class => 'Koha::AuthorisedValues',
653
            value => {
654
                authorised_value => 'US',
655
                lib              => 'United States of America',
656
                category         => $cat->category_name
657
            }
658
        }
659
    );
660
    my $ar = $builder->build_object(
661
        {
662
            class => 'Koha::AuthorisedValues',
663
            value => {
664
                authorised_value => 'AR',
665
                lib              => 'Argentina',
666
                category         => $cat->category_name
667
            }
668
        }
669
    );
670
671
    my $city_class = Test::MockModule->new('Koha::City');
672
    $city_class->mock(
673
        '_fetch_authorised_values',
674
        sub {
675
            my ($self) = @_;
676
            use Koha::AuthorisedValues;
677
            my $av = Koha::AuthorisedValues->find(
678
                {
679
                    authorised_value => $self->city_country,
680
                    category         => 'Countries'
681
                }
682
            );
683
            return { country => $av->unblessed };
684
        }
685
    );
686
687
    my $manuel = $builder->build_object(
688
        {
689
            class => 'Koha::Cities',
690
            value => {
691
                city_name    => 'Manuel',
692
                city_country => 'AR'
693
            }
694
        }
695
    );
696
    my $manuela = $builder->build_object(
697
        {
698
            class => 'Koha::Cities',
699
            value => {
700
                city_name    => 'Manuela',
701
                city_country => 'US'
702
            }
703
        }
704
    );
705
706
    $t->get_ok( '/cities/' . $manuel->cityid => { 'x-koha-av-expand' => 1 } )
707
      ->status_is(200)->json_is( '/name' => 'Manuel' )
708
      ->json_has('/_authorised_values')
709
      ->json_is( '/_authorised_values/country/lib' => $ar->lib );
710
711
    $t->get_ok( '/cities/' . $manuela->cityid => { 'x-koha-av-expand' => 1 } )
712
      ->status_is(200)->json_is( '/name' => 'Manuela' )
713
      ->json_has('/_authorised_values')
714
      ->json_is( '/_authorised_values/country/lib' => $us->lib );
715
716
    $schema->storage->txn_rollback;
717
};
718
719
subtest 'objects.search helper with expanded authorised values' => sub {
720
721
    plan tests => 11;
722
723
    my $t = Test::Mojo->new;
724
725
    $schema->storage->txn_begin;
726
727
    Koha::AuthorisedValues->search( { category => 'Countries' } )->delete;
728
    Koha::AuthorisedValueCategories->search( { category_name => 'Countries' } )
729
      ->delete;
730
731
    my $cat = $builder->build_object(
732
        {
733
            class => 'Koha::AuthorisedValueCategories',
734
            value => { category_name => 'Countries' }
735
        }
736
    );
737
    my $fr = $builder->build_object(
738
        {
739
            class => 'Koha::AuthorisedValues',
740
            value => {
741
                authorised_value => 'FR',
742
                lib              => 'France',
743
                category         => $cat->category_name
744
            }
745
        }
746
    );
747
    my $us = $builder->build_object(
748
        {
749
            class => 'Koha::AuthorisedValues',
750
            value => {
751
                authorised_value => 'US',
752
                lib              => 'United States of America',
753
                category         => $cat->category_name
754
            }
755
        }
756
    );
757
    my $ar = $builder->build_object(
758
        {
759
            class => 'Koha::AuthorisedValues',
760
            value => {
761
                authorised_value => 'AR',
762
                lib              => 'Argentina',
763
                category         => $cat->category_name
764
            }
765
        }
766
    );
767
768
    my $city_class = Test::MockModule->new('Koha::City');
769
    $city_class->mock(
770
        '_fetch_authorised_values',
771
        sub {
772
            my ($self) = @_;
773
            use Koha::AuthorisedValues;
774
            my $av = Koha::AuthorisedValues->find(
775
                {
776
                    authorised_value => $self->city_country,
777
                    category         => 'Countries'
778
                }
779
            );
780
            return { country => $av->unblessed };
781
        }
782
    );
783
784
    $builder->build_object(
785
        {
786
            class => 'Koha::Cities',
787
            value => {
788
                city_name    => 'Manuel',
789
                city_country => 'AR'
790
            }
791
        }
792
    );
793
    $builder->build_object(
794
        {
795
            class => 'Koha::Cities',
796
            value => {
797
                city_name    => 'Manuela',
798
                city_country => 'US'
799
            }
800
        }
801
    );
802
803
    $t->get_ok( '/cities?name=manuel&_per_page=4&_page=1&_match=starts_with' =>
804
          { 'x-koha-av-expand' => 1 } )->status_is(200)->json_has('/0')
805
      ->json_has('/1')->json_hasnt('/2')->json_is( '/0/name' => 'Manuel' )
806
      ->json_has('/0/_authorised_values')
807
      ->json_is( '/0/_authorised_values/country/lib' => $ar->lib )
808
      ->json_is( '/1/name' => 'Manuela' )->json_has('/1/_authorised_values')
809
      ->json_is( '/1/_authorised_values/country/lib' => $us->lib );
810
811
    $schema->storage->txn_rollback;
812
};

Return to bug 26635