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

(-)a/t/db_dependent/Koha/Objects.t (-2 / +276 lines)
Lines 33-38 use Koha::Patrons; Link Here
33
use Koha::Database;
33
use Koha::Database;
34
34
35
use t::lib::TestBuilder;
35
use t::lib::TestBuilder;
36
use t::lib::Mocks;
36
37
37
use Try::Tiny;
38
use Try::Tiny;
38
39
Lines 398-404 subtest "TO_JSON() tests" => sub { Link Here
398
399
399
# Koha::Object[s] must behave the same as DBIx::Class
400
# Koha::Object[s] must behave the same as DBIx::Class
400
subtest 'Return same values as DBIx::Class' => sub {
401
subtest 'Return same values as DBIx::Class' => sub {
401
    plan tests => 1;
402
    plan tests => 2;
402
403
403
    subtest 'Delete' => sub {
404
    subtest 'Delete' => sub {
404
        plan tests => 2;
405
        plan tests => 2;
Lines 731-736 subtest 'Return same values as DBIx::Class' => sub { Link Here
731
        $schema->storage->txn_rollback;
732
        $schema->storage->txn_rollback;
732
733
733
    };
734
    };
735
736
    subtest 'Update (set/store)' => sub {
737
        plan tests => 2;
738
739
        $schema->storage->txn_begin;
740
741
        subtest 'Simple Koha::Objects - Koha::Cities' => sub {
742
            plan tests => 2;
743
744
            subtest 'Koha::Object->update' => sub {
745
746
                plan tests => 5;
747
748
                my ( $r_us, $e_us, $r_them, $e_them );
749
750
                # CASE 1 - Update an existing object
751
                my $c_us = Koha::City->new( { city_name => 'city4test' } )->store;
752
                try { $r_us = $c_us->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
753
                my $c_them = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert;
754
                try { $r_them = $c_them->update({ city_country => 'country4test_2' }); } catch { $e_them = $_ };
755
                ok( ref($r_us) && ref($r_them),
756
                    'Successful update should return the object ' );
757
                ok( !defined $e_us && !defined $e_them,
758
                    'Successful update should not raise an exception' );
759
                is( ref($r_us), 'Koha::City', 'Successful update should return our Koha::Obect based object' );
760
761
                # CASE 2 - Update an object that is not in storage
762
                $c_us->delete;
763
                $c_them->delete;
764
                try { $r_us   = $c_us->update({ city_country => 'another_country' });   } catch { $e_us   = $_ };
765
                try { $r_them = $c_them->update({ city_country => 'another_country' }); } catch { $e_them = $_ };
766
                ok(
767
                    defined $e_us && defined $e_them,
768
                    'Update an object that is not in storage should raise an exception'
769
                );
770
                is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
771
            };
772
773
            subtest 'Koha::Objects->update' => sub {
774
775
                plan tests => 4;
776
777
                my ( $r_us, $e_us, $r_them, $e_them );
778
779
                # CASE 1 - update existing objects
780
                my $city_1 = $builder->build_object({ class => 'Koha::Cities' });
781
                my $city_2 = $builder->build_object({ class => 'Koha::Cities' });
782
                my $city_3 = $builder->build_object({ class => 'Koha::Cities' });
783
                my $cities = Koha::Cities->search(
784
                    {
785
                        cityid => {
786
                            -in => [
787
                                $city_1->cityid,
788
                                $city_2->cityid,
789
                                $city_3->cityid,
790
                            ]
791
                        }
792
                    }
793
                );
794
795
                try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
796
797
                $city_1 = $builder->build_object({ class => 'Koha::Cities' });
798
                $city_2 = $builder->build_object({ class => 'Koha::Cities' });
799
                $city_3 = $builder->build_object({ class => 'Koha::Cities' });
800
                $cities = $schema->resultset('City')->search(
801
                    {
802
                        cityid => {
803
                            -in => [
804
                                $city_1->cityid,
805
                                $city_2->cityid,
806
                                $city_3->cityid,
807
                            ]
808
                        }
809
                    }
810
                );
811
812
                try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
813
814
                ok( $r_us == 3 && $r_them == 3, '->update should return the number of updated cities' );
815
                ok(!defined($e_us) && !defined($e_them));
816
817
                # CASE 2 - One of the object is not in storage
818
                $city_1 = $builder->build_object({ class => 'Koha::Cities' });
819
                $city_2 = $builder->build_object({ class => 'Koha::Cities' });
820
                $city_3 = $builder->build_object({ class => 'Koha::Cities' });
821
                $cities = Koha::Cities->search(
822
                    {
823
                        cityid => {
824
                            -in => [
825
                                $city_1->cityid,
826
                                $city_2->cityid,
827
                                $city_3->cityid,
828
                            ]
829
                        }
830
                    }
831
                );
832
833
                $city_2->delete; # We delete one of the object
834
                try { $r_us = $cities->update({ city_country => 'country4test' }); } catch { $e_us = $_ };
835
836
                $city_1 = $builder->build_object({ class => 'Koha::Cities' });
837
                $city_2 = $builder->build_object({ class => 'Koha::Cities' });
838
                $city_3 = $builder->build_object({ class => 'Koha::Cities' });
839
                $cities = $schema->resultset('City')->search(
840
                    {
841
                        cityid => {
842
                            -in => [
843
                                $city_1->cityid,
844
                                $city_2->cityid,
845
                                $city_3->cityid,
846
                            ]
847
                        }
848
                    }
849
                );
850
851
                $city_2->delete; # We delete one of the object
852
                try { $r_them = $cities->update({ city_country => 'country4test' }); } catch { $e_them = $_ };
853
854
                ok( $r_us == 2 && $r_them == 2, '->update should return the number of updated cities' );
855
                ok(!defined($e_us) && !defined($e_them));
856
            };
857
        };
858
859
        subtest 'Overwritten Koha::Objects->store|update - Koha::Patrons' => sub {
860
861
            plan tests => 2;
862
863
            subtest 'Koha::Object->update' => sub {
864
865
                plan tests => 5;
866
867
                my ( $r_us, $e_us, $r_them, $e_them );
868
869
                # CASE 1 - Update an existing patron
870
                my $patron_us = $builder->build_object({ class => 'Koha::Patrons' });
871
                try {$r_us = $patron_us->update({city => 'a_city'});} catch { $e_us = $_ };
872
873
                my $patron_data = $builder->build_object({ class => 'Koha::Patrons' })->delete->unblessed;
874
                my $patron_them = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert;
875
                try {$r_them = $patron_them->update({city => 'a_city'});} catch { $e_them = $_ };
876
                ok( ref($r_us) && ref($r_them),
877
                    'Successful update should return the patron object' );
878
                ok( !defined $e_us && !defined $e_them,
879
                    'Successful update should not raise an exception' );
880
                is( ref($r_us), 'Koha::Patron',
881
                    'Successful update should return our Koha::Obect based object' );
882
883
                # CASE 2 - Update a patron that is not in storage
884
                $patron_us->delete;
885
                $patron_them->delete;
886
                try { $r_us   = $patron_us->update({ city => 'another_city' });   } catch { $e_us   = $_ };
887
                try { $r_them = $patron_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
888
                ok(
889
                    defined $e_us && defined $e_them,
890
                    'Update a patron that is not in storage should raise an exception'
891
                );
892
                is( ref($e_us), 'Koha::Exceptions::Object::NotInStorage' );
893
894
            };
895
896
            subtest 'Koha::Objects->Update ' => sub {
897
898
                plan tests => 6;
899
900
                my ( $r_us, $e_us, $r_them, $e_them );
901
902
                # CASE 1 - Update existing objects
903
                my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
904
                my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
905
                my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
906
                my $patrons_us = Koha::Patrons->search(
907
                    {
908
                        borrowernumber => {
909
                            -in => [
910
                                $patron_1->borrowernumber,
911
                                $patron_2->borrowernumber,
912
                                $patron_3->borrowernumber
913
                            ]
914
                        }
915
                    }
916
                );
917
918
                try { $r_us = $patrons_us->update({ city => 'a_city' }); } catch { $e_us = $_ };
919
920
                $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
921
                $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
922
                $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
923
                my $patrons_them = $schema->resultset('Borrower')->search(
924
                    {
925
                        borrowernumber => {
926
                            -in => [
927
                                $patron_1->borrowernumber,
928
                                $patron_2->borrowernumber,
929
                                $patron_3->borrowernumber
930
                            ]
931
                        }
932
                    }
933
                );
934
935
                try { $r_them = $patrons_them->update({ city => 'a_city' }); } catch { $e_them = $_ };
936
937
                ok( $r_us == 3 && $r_them == 3, '->update should return the number of update patrons' );
938
                ok (!defined($e_us) && !defined($e_them), '->update should not raise exception if everything went well');
939
940
                # CASE 2 - One of the patrons is not in storage
941
                undef $_ for $r_us, $e_us, $r_them, $e_them;
942
                $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
943
                $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
944
                $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
945
                $patrons_us = Koha::Patrons->search(
946
                    {
947
                        borrowernumber => {
948
                            -in => [
949
                                $patron_1->borrowernumber,
950
                                $patron_2->borrowernumber,
951
                                $patron_3->borrowernumber
952
                            ]
953
                        }
954
                    }
955
                );
956
957
                $patron_2->delete; # We delete one of the patron
958
                try { $r_us = $patrons_us->update({ city => 'another_city' }); } catch { $e_us = $_ };
959
960
                $patron_1 = $builder->build_object({ class => 'Koha::Patrons' });
961
                $patron_2 = $builder->build_object({ class => 'Koha::Patrons' });
962
                $patron_3 = $builder->build_object({ class => 'Koha::Patrons' });
963
                $patrons_them = $schema->resultset('Borrower')->search(
964
                    {
965
                        borrowernumber => {
966
                            -in => [
967
                                $patron_1->borrowernumber,
968
                                $patron_2->borrowernumber,
969
                                $patron_3->borrowernumber
970
                            ]
971
                        }
972
                    }
973
                );
974
975
                $patron_2->delete; # We delete one of the patron
976
                try { $r_them = $patrons_them->update({ city => 'another_city' }); } catch { $e_them = $_ };
977
978
                ok( $r_us == 2 && $r_them == 2, 'Update patrons with one that was not in storage should update the patrons' );
979
                ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage');
980
981
982
                # Testing no_triggers
983
                t::lib::Mocks::mock_preference('uppercasesurnames', 1);
984
                $patrons_us = Koha::Patrons->search(
985
                    {
986
                        borrowernumber => {
987
                            -in => [
988
                                $patron_1->borrowernumber,
989
                                $patron_2->borrowernumber,
990
                                $patron_3->borrowernumber
991
                            ]
992
                        }
993
                    }
994
                );
995
                $patrons_us->update({ surname => 'foo' }); # Koha::Patron->store is supposed to uppercase the surnames
996
                is( $patrons_us->search({ surname => 'FOO' })->count, 2, 'Koha::Patron->store is hit' );
997
998
                $patrons_us->update({ surname => 'foo', no_triggers => 1 }); # The surnames won't be uppercase as we won't hit Koha::Patron->store
999
                is( $patrons_us->search({ surname => 'foo' })->count, 2, 'Koha::Patron->store is not hit');
1000
1001
            };
1002
1003
        };
1004
1005
        $schema->storage->txn_rollback;
1006
1007
    };
1008
734
};
1009
};
735
1010
736
subtest "attributes_from_api() tests" => sub {
1011
subtest "attributes_from_api() tests" => sub {
737
- 

Return to bug 23185