Lines 5-32
Link Here
|
5 |
# Larger modifications by Jonathan Druart and Marcel de Rooy |
5 |
# Larger modifications by Jonathan Druart and Marcel de Rooy |
6 |
|
6 |
|
7 |
use Modern::Perl; |
7 |
use Modern::Perl; |
8 |
use Test::More tests => 71; |
8 |
use Test::More tests => 96; |
9 |
use MARC::Record; |
9 |
use MARC::Record; |
10 |
|
10 |
|
11 |
use C4::Biblio qw( AddBiblio DelBiblio ); |
11 |
use C4::Biblio qw( AddBiblio DelBiblio ); |
12 |
use C4::Context; |
12 |
use C4::Context; |
|
|
13 |
use C4::Members qw( AddMember ); |
13 |
|
14 |
|
14 |
|
15 |
|
15 |
my $dbh = C4::Context->dbh; |
16 |
my $dbh = C4::Context->dbh; |
16 |
$dbh->{RaiseError} = 1; |
17 |
$dbh->{RaiseError} = 1; |
17 |
$dbh->{AutoCommit} = 0; |
18 |
$dbh->{AutoCommit} = 0; |
18 |
|
19 |
|
19 |
# Getting some borrowers from database. |
20 |
# Create some borrowers |
20 |
my $query = q{SELECT borrowernumber FROM borrowers LIMIT 10}; |
21 |
my @borrowernumbers; |
21 |
my $borr_ref=$dbh->selectall_arrayref($query); |
22 |
for my $i ( 1 .. 10 ) { |
22 |
if(@$borr_ref==0) { #no borrowers? should not occur of course |
23 |
my $borrowernumber = AddMember( |
23 |
$borr_ref->[0][0]=undef; |
24 |
firstname => 'my firstname', |
24 |
#but even then, we can run this robust test :) |
25 |
surname => 'my surname ' . $i, |
25 |
} |
26 |
categorycode => 'S', |
26 |
my @borrowers; |
27 |
branchcode => 'CPL', |
27 |
foreach(1..10) { |
28 |
); |
28 |
my $t= $_> @$borr_ref ? int(rand()*@$borr_ref): $_-1; #repeat if not enough |
29 |
push @borrowernumbers, $borrowernumber; |
29 |
push @borrowers, $borr_ref->[$t][0]; |
|
|
30 |
} |
30 |
} |
31 |
|
31 |
|
32 |
# Creating some biblios |
32 |
# Creating some biblios |
Lines 50-56
use_ok('C4::VirtualShelves');
Link Here
|
50 |
|
50 |
|
51 |
# creating shelves (could be <10 when names are not unique) |
51 |
# creating shelves (could be <10 when names are not unique) |
52 |
my @shelves; |
52 |
my @shelves; |
53 |
for my $i(0..9){ |
53 |
for my $i (0..9){ |
54 |
my $name= randomname(); |
54 |
my $name= randomname(); |
55 |
my $catg= int(rand(2))+1; |
55 |
my $catg= int(rand(2))+1; |
56 |
my $ShelfNumber= AddShelf( |
56 |
my $ShelfNumber= AddShelf( |
Lines 58-83
for my $i(0..9){
Link Here
|
58 |
shelfname => $name, |
58 |
shelfname => $name, |
59 |
category => $catg, |
59 |
category => $catg, |
60 |
}, |
60 |
}, |
61 |
$borrowers[$i]); |
61 |
$borrowernumbers[$i]); |
62 |
|
62 |
|
63 |
if($ShelfNumber>-1) { |
63 |
if($ShelfNumber>-1) { |
64 |
ok($ShelfNumber > -1, "created shelf"); # Shelf creation successful; |
64 |
ok($ShelfNumber > -1, "created shelf"); # Shelf creation successful; |
65 |
} |
65 |
} |
66 |
else { |
66 |
else { |
67 |
my $t= C4::VirtualShelves::_CheckShelfName( |
67 |
my $t= C4::VirtualShelves::_CheckShelfName( |
68 |
$name, $catg, $borrowers[$i], 0); |
68 |
$name, $catg, $borrowernumbers[$i], 0); |
69 |
ok($t==0, "Name clash expected on shelf creation"); |
69 |
is($t, 0, "Name clash expected on shelf creation"); |
70 |
} |
70 |
} |
71 |
push @shelves, { |
71 |
push @shelves, { |
72 |
number => $ShelfNumber, |
72 |
number => $ShelfNumber, |
73 |
name => $name, |
73 |
name => $name, |
74 |
catg => $catg, |
74 |
catg => $catg, |
75 |
owner => $borrowers[$i], |
75 |
owner => $borrowernumbers[$i], |
76 |
}; #also push the errors |
76 |
}; #also push the errors |
77 |
} |
77 |
} |
78 |
|
78 |
|
79 |
# try to create shelves with duplicate names |
79 |
# try to create shelves with duplicate names |
80 |
for my $i(0..9){ |
80 |
for my $i (0..9){ |
81 |
if($shelves[$i]->{number}<0) { |
81 |
if($shelves[$i]->{number}<0) { |
82 |
ok(1, 'skip duplicate test for earlier name clash'); |
82 |
ok(1, 'skip duplicate test for earlier name clash'); |
83 |
next; |
83 |
next; |
Lines 89-96
for my $i(0..9){
Link Here
|
89 |
my $badNumShelf= AddShelf( { |
89 |
my $badNumShelf= AddShelf( { |
90 |
shelfname=> $shelves[$i]->{name}, |
90 |
shelfname=> $shelves[$i]->{name}, |
91 |
category => 2 |
91 |
category => 2 |
92 |
}, $borrowers[$i]); |
92 |
}, $borrowernumbers[$i]); |
93 |
ok(-1==$badNumShelf, 'do not create public lists with duplicate names'); |
93 |
is($badNumShelf, -1, 'do not create public lists with duplicate names'); |
94 |
#AddShelf returns -1 if name already exist. |
94 |
#AddShelf returns -1 if name already exist. |
95 |
DelShelf($badNumShelf) if $badNumShelf>-1; #delete if went wrong.. |
95 |
DelShelf($badNumShelf) if $badNumShelf>-1; #delete if went wrong.. |
96 |
} |
96 |
} |
Lines 101-107
for my $i(0..9){
Link Here
|
101 |
category => 1, |
101 |
category => 1, |
102 |
}, |
102 |
}, |
103 |
$shlf[2]): -1; |
103 |
$shlf[2]): -1; |
104 |
ok(-1==$badNumShelf, 'do not create private lists with duplicate name for same user'); |
104 |
is($badNumShelf, -1, 'do not create private lists with duplicate name for same user'); |
105 |
DelShelf($badNumShelf) if $badNumShelf>-1; #delete if went wrong.. |
105 |
DelShelf($badNumShelf) if $badNumShelf>-1; #delete if went wrong.. |
106 |
} |
106 |
} |
107 |
} |
107 |
} |
Lines 112-118
for my $i(0..9){
Link Here
|
112 |
# usage : $biblist = GetShelfContents($shelfnumber); |
112 |
# usage : $biblist = GetShelfContents($shelfnumber); |
113 |
|
113 |
|
114 |
my %used = (); |
114 |
my %used = (); |
115 |
for my $i(0..9){ |
115 |
for my $i (0..9){ |
116 |
my $bib = $biblionumbers[int(rand(9))]; |
116 |
my $bib = $biblionumbers[int(rand(9))]; |
117 |
my $shelfnumber = $shelves[int(rand(9))]->{number}; |
117 |
my $shelfnumber = $shelves[int(rand(9))]->{number}; |
118 |
if($shelfnumber<0) { |
118 |
if($shelfnumber<0) { |
Lines 127-133
for my $i(0..9){
Link Here
|
127 |
#The different permissions could be tested too. |
127 |
#The different permissions could be tested too. |
128 |
|
128 |
|
129 |
my ($biblistBefore,$countbefore) = GetShelfContents($shelfnumber); |
129 |
my ($biblistBefore,$countbefore) = GetShelfContents($shelfnumber); |
130 |
my $status = AddToShelf($bib,$shelfnumber,$borrowers[$i]); |
130 |
my $status = AddToShelf($bib,$shelfnumber,$borrowernumbers[$i]); |
131 |
my ($biblistAfter,$countafter) = GetShelfContents($shelfnumber); |
131 |
my ($biblistAfter,$countafter) = GetShelfContents($shelfnumber); |
132 |
|
132 |
|
133 |
if ($should_fail) { |
133 |
if ($should_fail) { |
Lines 137-145
for my $i(0..9){
Link Here
|
137 |
} |
137 |
} |
138 |
|
138 |
|
139 |
if (defined $status) { |
139 |
if (defined $status) { |
140 |
ok($countbefore == $countafter - 1, 'added bib to list'); # the bib has been successfuly added. |
140 |
is($countbefore, $countafter - 1, 'added bib to list'); # the bib has been successfuly added. |
141 |
} else { |
141 |
} else { |
142 |
ok($countbefore == $countafter, 'did not add duplicate bib to list'); |
142 |
is($countbefore, $countafter, 'did not add duplicate bib to list'); |
143 |
} |
143 |
} |
144 |
|
144 |
|
145 |
$used{$key}++; |
145 |
$used{$key}++; |
Lines 149-155
for my $i(0..9){
Link Here
|
149 |
# usage : ModShelf($shelfnumber, $shelfname, $owner, $category ) |
149 |
# usage : ModShelf($shelfnumber, $shelfname, $owner, $category ) |
150 |
# usage : (shelfnumber,shelfname,owner,category) = GetShelf($shelfnumber); |
150 |
# usage : (shelfnumber,shelfname,owner,category) = GetShelf($shelfnumber); |
151 |
|
151 |
|
152 |
for my $i(0..9){ |
152 |
for my $i (0..9){ |
153 |
my $rand = int(rand(9)); |
153 |
my $rand = int(rand(9)); |
154 |
my $numA = $shelves[$rand]->{number}; |
154 |
my $numA = $shelves[$rand]->{number}; |
155 |
if($numA<0) { |
155 |
if($numA<0) { |
Lines 168-182
for my $i(0..9){
Link Here
|
168 |
$shelves[$rand]->{owner}, $numA)) { |
168 |
$shelves[$rand]->{owner}, $numA)) { |
169 |
ModShelf($numA,$shelf); |
169 |
ModShelf($numA,$shelf); |
170 |
my ($numB,$nameB,$ownerB,$categoryB) = GetShelf($numA); |
170 |
my ($numB,$nameB,$ownerB,$categoryB) = GetShelf($numA); |
171 |
ok($numA == $numB, 'modified shelf'); |
171 |
is($numA, $numB, 'modified shelf'); |
172 |
ok($shelf->{shelfname} eq $nameB, '... and name change took'); |
172 |
is($shelf->{shelfname}, $nameB, '... and name change took'); |
173 |
ok($shelf->{category} eq $categoryB, '... and category change took'); |
173 |
is($shelf->{category}, $categoryB, '... and category change took'); |
174 |
} |
174 |
} |
175 |
else { |
175 |
else { |
176 |
ok(1, "No ModShelf for $newname") for 1..3; |
176 |
ok(1, "No ModShelf for $newname") for 1..3; |
177 |
} |
177 |
} |
178 |
} |
178 |
} |
179 |
|
179 |
|
|
|
180 |
#----------------------- TEST AddShare ----------------------------------------# |
181 |
|
182 |
#first count the number of shares in the table; keep in mind that AddShare may |
183 |
#delete some expired records while housekeeping |
184 |
my $sql_sharecount="select count(*) from virtualshelfshares where DATEDIFF(sharedate, NOW())>0"; |
185 |
my $cnt1=$dbh->selectrow_array($sql_sharecount); |
186 |
|
187 |
#try to add a share without shelfnumber: should fail |
188 |
AddShare(0, 'abcdefghij'); |
189 |
my $cnt2=$dbh->selectrow_array($sql_sharecount); |
190 |
is($cnt1,$cnt2, "Did not add an invalid share record"); |
191 |
|
192 |
#add another share: should be okay |
193 |
#AddShare assumes that you tested if category==private (so we could actually |
194 |
#be doing something illegal here :) |
195 |
my $n=$shelves[0]->{number}; |
196 |
if($n<0) { |
197 |
ok(1, 'Skip AddShare for shelf -1'); |
198 |
} |
199 |
else { |
200 |
AddShare($n, 'abcdefghij'); |
201 |
my $cnt3=$dbh->selectrow_array($sql_sharecount); |
202 |
is(1+$cnt2, $cnt3, "Added one new share record with invitekey"); |
203 |
} |
204 |
|
205 |
#----------------------- TEST AcceptShare -------------------------------------# |
206 |
|
207 |
# test accepting a wrong key |
208 |
my $testkey= 'keyisgone9'; |
209 |
my $acctest="delete from virtualshelfshares where invitekey=?"; |
210 |
$dbh->do($acctest,undef,($testkey)); #just be sure it does not exist |
211 |
$acctest="select shelfnumber from virtualshelves"; |
212 |
my ($accshelf)= $dbh->selectrow_array($acctest); |
213 |
is(AcceptShare($accshelf,$testkey,$borrowernumbers[0]),undef,'Did not accept invalid key'); |
214 |
|
215 |
# test accepting a good key |
216 |
if( AddShare($accshelf,$testkey) && $borrowernumbers[0] ) { |
217 |
is(AcceptShare($accshelf, $testkey, $borrowernumbers[0]),1,'Accepted share'); |
218 |
} |
219 |
else { #cannot accept if addshared failed somehow |
220 |
ok(1, 'Skipped second AcceptShare test'); |
221 |
} |
222 |
|
223 |
#----------------------- TEST IsSharedList ------------------------------------# |
224 |
|
225 |
for my $i (0..9) { |
226 |
my $sql="select count(*) from virtualshelfshares where shelfnumber=? and borrowernumber is not null"; |
227 |
my $sh=$shelves[$i]->{number}; |
228 |
my ($n)=$dbh->selectrow_array($sql,undef,($sh)); |
229 |
is(IsSharedList($sh),$n? 1: '', "Checked IsSharedList for shelf $sh"); |
230 |
} |
231 |
|
232 |
#----------------TEST DelShelf & DelFromShelf functions------------------------# |
233 |
|
234 |
for my $i (0..9){ |
235 |
my $shelfnumber = $shelves[$i]->{number}; |
236 |
if($shelfnumber<0) { |
237 |
ok(1, 'Skip DelShelf for shelf -1'); |
238 |
next; |
239 |
} |
240 |
my $status = DelShelf($shelfnumber); |
241 |
is($status, 1, "deleted shelf $shelfnumber and its contents"); |
242 |
} |
243 |
|
244 |
#----------------------- TEST RemoveShare -------------------------------------# |
245 |
|
246 |
my $remshr_test="select borrowernumber, shelfnumber from virtualshelfshares where borrowernumber is not null"; |
247 |
my @remshr_shelf= $dbh->selectrow_array($remshr_test); |
248 |
if(@remshr_shelf) { |
249 |
is(RemoveShare(@remshr_shelf),1,'Removed a shelf share'); |
250 |
} |
251 |
else { |
252 |
ok(1,'Skipped RemoveShare test'); |
253 |
} |
254 |
|
180 |
#----------------------- SOME SUBS --------------------------------------------# |
255 |
#----------------------- SOME SUBS --------------------------------------------# |
181 |
|
256 |
|
182 |
sub randomname { |
257 |
sub randomname { |
183 |
- |
|
|