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