Koha/t/db_dependent/VirtualShelves.t
Marcel de Rooy 93e87ca0b6 Bug 10386: improvements to VirtualShelves.t
Most important: Does no longer delete all shelves!
Checks if there are ten borrowers for testing. But even works without them :)
When creating or modifying lists, takes name clashes into consideration.

Small change to _CheckShelfName in VirtualShelves module. Making it possible to
check a name for a list whose owner has been set to NULL. Note that a test
like field=? with undef for placeholder will not work in MySql.

Test plan:
How do you test a test? Well, you could run it on various databases..
But for real hacking, you could also add some debug lines.
I tested this by forcing 10 undefs in @borrowernumbers.
And by overwriting the return value of randomname with an existing name.

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
2013-06-28 05:19:49 -07:00

201 lines
6.4 KiB
Perl
Executable file

#!/usr/bin/perl
# This file is a test script for C4::VirtualShelves.pm
# Author : Antoine Farnault, antoine@koha-fr.org
# Larger modifications by Jonathan Druart and Marcel de Rooy
use Modern::Perl;
use Test::More tests => 81;
use MARC::Record;
use C4::Biblio qw( AddBiblio DelBiblio );
use C4::Context;
# Getting some borrowers from database.
my $dbh = C4::Context->dbh;
my $query = q{SELECT borrowernumber FROM borrowers LIMIT 10};
my $borr_ref=$dbh->selectall_arrayref($query);
if(@$borr_ref==0) { #no borrowers? should not occur of course
$borr_ref->[0][0]=undef;
#but even then, we can run this robust test :)
}
my @borrowers;
foreach(1..10) {
my $t= $_> @$borr_ref ? int(rand()*@$borr_ref): $_-1; #repeat if not enough
push @borrowers, $borr_ref->[$t][0];
}
# Creating some biblios
my @biblionumbers;
foreach(0..9) {
my ($biblionumber)= AddBiblio(MARC::Record->new, '');
#item number ignored
push @biblionumbers, $biblionumber;
}
#----------------------------------------------------------------------#
#
# TESTS START HERE
#
#----------------------------------------------------------------------#
use_ok('C4::VirtualShelves');
#-----------------------TEST AddShelf function------------------------#
# usage : $shelfnumber = &AddShelf( $shelfname, $owner, $category);
# creating shelves (could be <10 when names are not unique)
my @shelves;
for(my $i=0; $i<10;$i++) {
my $name= randomname();
my $catg= int(rand(2))+1;
my $ShelfNumber= AddShelf(
{
shelfname => $name,
category => $catg,
},
$borrowers[$i]);
if($ShelfNumber>-1) {
ok($ShelfNumber > -1, "created shelf"); # Shelf creation successful;
}
else {
my $t= C4::VirtualShelves::_CheckShelfName(
$name, $catg, $borrowers[$i], 0);
ok($t==0, "Name clash expected on shelf creation");
}
push @shelves, {
number => $ShelfNumber,
name => $name,
catg => $catg,
owner => $borrowers[$i],
}; #also push the errors
}
# try to create shelves with duplicate names
for(my $i=0;$i<10;$i++){
if($shelves[$i]->{number}<0) {
ok(1, 'skip duplicate test for earlier name clash');
next;
}
my @shlf=GetShelf($shelves[$i]->{number}); #number, name, owner, catg, ...
# A shelf name is not per se unique!
if( $shlf[3]==2 ) { #public list: try to create with same name
my $badNumShelf= AddShelf( {
shelfname=> $shelves[$i]->{name},
category => 2
}, $borrowers[$i]);
ok(-1==$badNumShelf, 'do not create public lists with duplicate names');
#AddShelf returns -1 if name already exist.
DelShelf($badNumShelf) if $badNumShelf>-1; #delete if went wrong..
}
else { #private list, try to add another one for SAME user (owner)
my $badNumShelf= defined($shlf[2])? AddShelf(
{
shelfname=> $shelves[$i]->{name},
category => 1,
},
$shlf[2]): -1;
ok(-1==$badNumShelf, 'do not create private lists with duplicate name for same user');
DelShelf($badNumShelf) if $badNumShelf>-1; #delete if went wrong..
}
}
#-----------TEST AddToShelf & GetShelfContents & DelFromShelf functions--------------#
# usage : &AddToShelf($biblionumber, $shelfnumber);
# usage : $biblist = &GetShelfContents($shelfnumber);
# usage : $biblist = GetShelfContents($shelfnumber);
my %used = ();
for(my $i=0; $i<10;$i++){
my $bib = $biblionumbers[int(rand(9))];
my $shelfnumber = $shelves[int(rand(9))]->{number};
if($shelfnumber<0) {
ok(1, 'skip add to list-test for shelf -1');
ok(1, 'skip counting list entries for shelf -1');
next;
}
my $key = "$bib\t$shelfnumber";
my $should_fail = exists($used{$key}) ? 1 : 0;
#FIXME We assume here that we have permission to add..
#The different permissions could be tested too.
my ($biblistBefore,$countbefore) = GetShelfContents($shelfnumber);
my $status = AddToShelf($bib,$shelfnumber,$borrowers[$i]);
my ($biblistAfter,$countafter) = GetShelfContents($shelfnumber);
if ($should_fail) {
ok(!defined($status), 'failed to add to list when we should');
} else {
ok(defined($status), 'added to list when we should');
}
if (defined $status) {
ok($countbefore == $countafter - 1, 'added bib to list'); # the bib has been successfuly added.
} else {
ok($countbefore == $countafter, 'did not add duplicate bib to list');
}
$used{$key}++;
}
#-----------------------TEST ModShelf & GetShelf functions------------------------#
# usage : ModShelf($shelfnumber, $shelfname, $owner, $category )
# usage : (shelfnumber,shelfname,owner,category) = GetShelf($shelfnumber);
for(my $i=0; $i<10;$i++){
my $rand = int(rand(9));
my $numA = $shelves[$rand]->{number};
if($numA<0) {
ok(1, 'Skip ModShelf test for shelf -1');
ok(1, 'Skip ModShelf test for shelf -1');
ok(1, 'Skip ModShelf test for shelf -1');
next;
}
my $newname= randomname();
my $shelf = {
shelfname => $newname,
category => 3-$shelves[$rand]->{catg}, # tric: 1->2 and 2->1
};
#check name change (with category change)
if(C4::VirtualShelves::_CheckShelfName($newname,$shelf->{category},
$shelves[$rand]->{owner}, $numA)) {
ModShelf($numA,$shelf);
my ($numB,$nameB,$ownerB,$categoryB) = GetShelf($numA);
ok($numA == $numB, 'modified shelf');
ok($shelf->{shelfname} eq $nameB, '... and name change took');
ok($shelf->{category} eq $categoryB, '... and category change took');
}
else {
ok(1, "No ModShelf for $newname") for 1..3;
}
}
#-----------------------TEST DelShelf & DelFromShelf functions------------------------#
# usage : ($status) = &DelShelf($shelfnumber);
for(my $i=0; $i<10;$i++){
my $shelfnumber = $shelves[$i]->{number};
if($shelfnumber<0) {
ok(1, 'Skip DelShelf for shelf -1');
next;
}
my $status = DelShelf($shelfnumber);
ok(1 == $status, "deleted shelf $shelfnumber and its contents");
}
#----------------------- CLEANUP ----------------------------------------------#
DelBiblio($_) for @biblionumbers;
#----------------------- SOME SUBS --------------------------------------------#
sub randomname {
my $rv='';
for(0..19) {
$rv.= ('a'..'z','A'..'Z',0..9) [int(rand()*62)];
}
return $rv;
}