Lines 17-37
Link Here
|
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use Test::More tests => 12; |
20 |
use Test::More tests => 16; |
21 |
use t::lib::TestBuilder; |
21 |
use t::lib::TestBuilder; |
22 |
use t::lib::Mocks; |
22 |
use t::lib::Mocks; |
23 |
|
23 |
|
24 |
use Koha::Database; |
24 |
use Koha::Database; |
25 |
use Koha::List::Patron |
25 |
use Koha::List::Patron |
26 |
qw( AddPatronList AddPatronsToList DelPatronList DelPatronsFromList GetPatronLists ModPatronList ); |
26 |
qw( add_patron_list add_patrons_to_list del_patron_list del_patrons_from_list get_patron_list get_patron_lists mod_patron_list grant_patrons_access_to_list revoke_patrons_access_from_list ); |
27 |
use Koha::Patrons; |
27 |
use Koha::Patrons; |
28 |
|
28 |
|
29 |
my $schema = Koha::Database->schema; |
29 |
my $schema = Koha::Database->schema; |
30 |
$schema->storage->txn_begin; |
30 |
$schema->storage->txn_begin; |
31 |
|
31 |
|
32 |
my $builder = t::lib::TestBuilder->new; |
32 |
my $builder = t::lib::TestBuilder->new; |
|
|
33 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
33 |
|
34 |
|
34 |
t::lib::Mocks::mock_userenv(); |
35 |
t::lib::Mocks::mock_userenv({branchcode => $branchcode}); |
35 |
|
36 |
|
36 |
# Create 10 sample borrowers |
37 |
# Create 10 sample borrowers |
37 |
my @borrowers = (); |
38 |
my @borrowers = (); |
Lines 42-56
foreach (1..10) {
Link Here
|
42 |
my $owner = $borrowers[0]->{borrowernumber}; |
43 |
my $owner = $borrowers[0]->{borrowernumber}; |
43 |
my $owner2 = $borrowers[1]->{borrowernumber}; |
44 |
my $owner2 = $borrowers[1]->{borrowernumber}; |
44 |
|
45 |
|
45 |
my @lists = GetPatronLists( { owner => $owner } ); |
46 |
my $owner3 = Koha::Patron->new( |
|
|
47 |
{ |
48 |
surname => 'Test 1', |
49 |
branchcode => $branchcode, |
50 |
categorycode => $borrowers[0]->{categorycode} |
51 |
} |
52 |
); |
53 |
$owner3->store(); |
54 |
|
55 |
my @lists = get_patron_lists( { owner => $owner } ); |
46 |
my $list_count_original = @lists; |
56 |
my $list_count_original = @lists; |
47 |
|
57 |
|
48 |
my $list1 = AddPatronList( { name => 'Test List 1', owner => $owner } ); |
58 |
my $list1 = add_patron_list( { name => 'Test List 1', owner => $owner } ); |
49 |
is( $list1->name(), 'Test List 1', 'AddPatronList works' ); |
59 |
is( $list1->name(), 'Test List 1', 'add_patron_list works' ); |
50 |
|
60 |
|
51 |
my $list2 = AddPatronList( { name => 'Test List 2', owner => $owner } ); |
61 |
my $list2 = add_patron_list( { name => 'Test List 2', owner => $owner } ); |
52 |
|
62 |
|
53 |
ModPatronList( |
63 |
mod_patron_list( |
54 |
{ |
64 |
{ |
55 |
patron_list_id => $list2->patron_list_id(), |
65 |
patron_list_id => $list2->patron_list_id(), |
56 |
name => 'Test List 3', |
66 |
name => 'Test List 3', |
Lines 58-75
ModPatronList(
Link Here
|
58 |
} |
68 |
} |
59 |
); |
69 |
); |
60 |
$list2->discard_changes(); |
70 |
$list2->discard_changes(); |
61 |
is( $list2->name(), 'Test List 3', 'ModPatronList works' ); |
71 |
is( $list2->name(), 'Test List 3', 'mod_patron_list works' ); |
|
|
72 |
|
73 |
$list1 = |
74 |
get_patron_list( { patron_list_id => $list1->patron_list_id() } ); |
75 |
is( $list1->name(), 'Test List 1', 'get_patron_list works' ); |
62 |
|
76 |
|
63 |
AddPatronsToList( |
77 |
add_patrons_to_list( |
64 |
{ list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] } |
78 |
{ list => $list1, cardnumbers => [ map { $_->{cardnumber} } @borrowers ] } |
65 |
); |
79 |
); |
66 |
is( |
80 |
is( |
67 |
scalar @borrowers, |
81 |
scalar @borrowers, |
68 |
$list1->patron_list_patrons()->search_related('borrowernumber')->all(), |
82 |
$list1->patron_list_patrons()->search_related('borrowernumber')->all(), |
69 |
'AddPatronsToList works for cardnumbers' |
83 |
'add_patrons_to_list works for cardnumbers' |
70 |
); |
84 |
); |
71 |
|
85 |
|
72 |
AddPatronsToList( |
86 |
add_patrons_to_list( |
73 |
{ |
87 |
{ |
74 |
list => $list2, |
88 |
list => $list2, |
75 |
borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ] |
89 |
borrowernumbers => [ map { $_->{borrowernumber} } @borrowers ] |
Lines 78-126
AddPatronsToList(
Link Here
|
78 |
is( |
92 |
is( |
79 |
scalar @borrowers, |
93 |
scalar @borrowers, |
80 |
$list2->patron_list_patrons()->search_related('borrowernumber')->all(), |
94 |
$list2->patron_list_patrons()->search_related('borrowernumber')->all(), |
81 |
'AddPatronsToList works for borrowernumbers' |
95 |
'add_patrons_to_list works for borrowernumbers' |
82 |
); |
96 |
); |
83 |
|
97 |
|
84 |
my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' }); |
98 |
my $deleted_patron = $builder->build_object({ class => 'Koha::Patrons' }); |
85 |
$deleted_patron->delete; |
99 |
$deleted_patron->delete; |
86 |
my @result = AddPatronsToList({list => $list2,borrowernumbers => [ $deleted_patron->borrowernumber ]}); |
100 |
my @result = add_patrons_to_list({list => $list2,borrowernumbers => [ $deleted_patron->borrowernumber ]}); |
87 |
is( scalar @result, 0,'Invalid borrowernumber not added'); |
101 |
is( scalar @result, 0,'Invalid borrowernumber not added'); |
88 |
@result = AddPatronsToList({list => $list2,cardnumbers => [ $deleted_patron->cardnumber ]}); |
102 |
@result = add_patrons_to_list({list => $list2,cardnumbers => [ $deleted_patron->cardnumber ]}); |
89 |
is( scalar @result, 0,'Invalid cardnumber not added'); |
103 |
is( scalar @result, 0,'Invalid cardnumber not added'); |
90 |
|
104 |
|
91 |
my @ids = |
105 |
my @ids = |
92 |
$list1->patron_list_patrons()->get_column('patron_list_patron_id')->all(); |
106 |
$list1->patron_list_patrons()->get_column('patron_list_patron_id')->all(); |
93 |
DelPatronsFromList( |
107 |
del_patrons_from_list( |
94 |
{ |
108 |
{ |
95 |
list => $list1, |
109 |
list => $list1, |
96 |
patron_list_patrons => \@ids, |
110 |
patron_list_patrons => \@ids, |
97 |
} |
111 |
} |
98 |
); |
112 |
); |
99 |
$list1->discard_changes(); |
113 |
$list1->discard_changes(); |
100 |
is( $list1->patron_list_patrons()->count(), 0, 'DelPatronsFromList works.' ); |
114 |
is( $list1->patron_list_patrons()->count(), 0, 'del_patrons_from_list works.' ); |
101 |
|
115 |
|
102 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
116 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
103 |
AddPatronsToList({list => $list2,borrowernumbers => [ $patron->borrowernumber ]}); |
117 |
add_patrons_to_list({list => $list2,borrowernumbers => [ $patron->borrowernumber ]}); |
104 |
@lists = $patron->get_lists_with_patron; |
118 |
@lists = $patron->get_lists_with_patron; |
105 |
is( scalar @lists, 1, 'get_lists_with_patron works' ); |
119 |
is( scalar @lists, 1, 'get_lists_with_patron works' ); |
106 |
|
120 |
|
107 |
@lists = GetPatronLists( { owner => $owner } ); |
121 |
@lists = get_patron_lists( { owner => $owner } ); |
108 |
is( scalar @lists, $list_count_original + 2, 'GetPatronLists works' ); |
122 |
is( scalar @lists, $list_count_original + 2, 'get_patron_lists works' ); |
|
|
123 |
|
124 |
my $list3 = add_patron_list( { name => 'Test List 3', owner => $owner2, shared => 0 } ); |
125 |
@lists = get_patron_lists( { owner => $owner } ); |
126 |
is( scalar @lists, $list_count_original + 2, 'get_patron_lists does not return non-shared list' ); |
127 |
|
128 |
my $list4 = add_patron_list( { name => 'Test List 4', owner => $owner2, shared => 1 } ); |
129 |
@lists = get_patron_lists( { owner => $owner } ); |
130 |
is( scalar @lists, $list_count_original + 3, 'get_patron_lists does return shared list' ); |
131 |
|
132 |
del_patron_list( { patron_list_id => $list1->patron_list_id(), owner => $owner } ); |
133 |
del_patron_list( { patron_list_id => $list2->patron_list_id(), owner => $owner } ); |
134 |
|
135 |
$list1 = |
136 |
get_patron_list( { patron_list_id => $list1->patron_list_id() } ); |
137 |
is( $list1, undef, 'del_patron_list works' ); |
109 |
|
138 |
|
110 |
my $list3 = AddPatronList( { name => 'Test List 3', owner => $owner2, shared => 0 } ); |
139 |
@lists = get_patron_lists(); |
111 |
@lists = GetPatronLists( { owner => $owner } ); |
140 |
my $lib_list_count = @lists; |
112 |
is( scalar @lists, $list_count_original + 2, 'GetPatronLists does not return non-shared list' ); |
141 |
my $list5 = add_patron_list( { name => 'Test List 5', owner => $owner3->borrowernumber, shared => 2 } ); |
|
|
142 |
my $list6 = add_patron_list( { name => 'Test List 6', owner => $owner3->borrowernumber, shared => 3 } ); |
143 |
@lists = get_patron_lists(); |
144 |
is( scalar @lists, $lib_list_count + 1, 'get_patron_lists returns list shared with library' ); |
113 |
|
145 |
|
114 |
my $list4 = AddPatronList( { name => 'Test List 4', owner => $owner2, shared => 1 } ); |
146 |
grant_patrons_access_to_list( { list => $list6, borrowernumbers => [ $patron->borrowernumber ] } ); |
115 |
@lists = GetPatronLists( { owner => $owner } ); |
|
|
116 |
is( scalar @lists, $list_count_original + 3, 'GetPatronLists does return shared list' ); |
117 |
|
147 |
|
118 |
DelPatronList( { patron_list_id => $list1->patron_list_id(), owner => $owner } ); |
148 |
@lists = get_patron_lists({ owner => $patron->borrowernumber }); |
119 |
DelPatronList( { patron_list_id => $list2->patron_list_id(), owner => $owner } ); |
149 |
is( scalar @lists, $lib_list_count + 2, 'get_patron_lists returns list shared with individual, grant_patrons_access_to_list works' ); |
120 |
|
150 |
|
121 |
@lists = |
151 |
my @list_users = $list6->patron_list_users->get_column('patron_list_user_id')->all(); |
122 |
GetPatronLists( { patron_list_id => $list1->patron_list_id(), owner => $owner } ); |
152 |
revoke_patrons_access_from_list({ list => $list6, patron_list_users => \@list_users }); |
123 |
is( scalar @lists, 0, 'DelPatronList works' ); |
153 |
@lists = get_patron_lists(); |
|
|
154 |
is( scalar @lists, $lib_list_count + 1, 'revoke_patrons_access_from_list works' ); |
124 |
|
155 |
|
125 |
$schema->storage->txn_rollback; |
156 |
$schema->storage->txn_rollback; |
126 |
|
157 |
|