From a2ce10b91602097b42866568d81fe05123147bc7 Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Mon, 15 May 2017 10:39:39 +0000 Subject: [PATCH] Bug 18606: Rework tests for rotating collections --- t/db_dependent/Koha/RotatingCollections.t | 142 +++++++++++++ t/db_dependent/RotatingCollections.t | 337 ------------------------------ 2 files changed, 142 insertions(+), 337 deletions(-) create mode 100644 t/db_dependent/Koha/RotatingCollections.t delete mode 100644 t/db_dependent/RotatingCollections.t diff --git a/t/db_dependent/Koha/RotatingCollections.t b/t/db_dependent/Koha/RotatingCollections.t new file mode 100644 index 0000000..57f16ff --- /dev/null +++ b/t/db_dependent/Koha/RotatingCollections.t @@ -0,0 +1,142 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 2; +use C4::Context; +use Koha::Biblios; +use Koha::Database; +use Koha::Library; +use Koha::RotatingCollections; +use Koha::Items; +use Koha::Item::Transfers; +use Koha::Libraries; + +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +subtest 'remove_and_add_items' => sub { + plan tests => 8; + + $schema->storage->txn_begin; + + my $collection = Koha::RotatingCollection->new( { + colTitle => 'Test title 1', + colDesc => 'Test description 1', + } )->store; + + my $biblioitem = $builder->build( { source => 'Biblioitem' } ); + + my $library = $builder->build( { source => 'Branch' } ); + + my $item1 = Koha::Item->new( { + biblionumber => $biblioitem->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + barcode => 'barcode1', + } )->store; + + my $item2 = Koha::Item->new( { + biblionumber => $biblioitem->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + homebranch => $library->{branchcode}, + holdingbranch => $library->{branchcode}, + barcode => 'barode2', + } )->store; + + is( $collection->items->count, 0, 'In newly created collection there should not be any item'); + + $collection->add_item( $item1 ); + $collection->add_item( $item2 ); + + is( $collection->items->count, 2, 'Added two items, there should be two'); + + eval { $collection->add_item }; + is( ref($@), 'Koha::Exceptions::MissingParameter', 'Missing paramater exception'); + + eval { $collection->add_item( $item1 ) }; + is( ref($@), 'Koha::Exceptions::DuplicateObject', 'Duplicate Object Exception - you should not add the same item twice'); + + eval { $collection->add_item('bad_itemnumber') }; + is( ref($@), 'Koha::Exceptions::ObjectNotFound', 'Object Not Found Exception - you cannot add non existent item'); + + $collection->remove_item( $item1 ); + + is( $collection->items->count, 1, 'We removed first item, there should be one remaining'); + + eval { $collection->remove_item }; + is( ref($@), 'Koha::Exceptions::MissingParameter', 'Missing paramater exception'); + + eval { $collection->remove_item( $item1 ) }; + is( ref($@), 'Koha::Exceptions::ObjectNotFound', 'Object Not Found Exception - cannot remove the same item twice'); + + $schema->storage->txn_rollback; +}; + +subtest 'transfer' => sub { + plan tests => 4; + + $schema->storage->txn_begin; + + my $collection = Koha::RotatingCollection->new( { + colTitle => 'Test title 1', + colDesc => 'Test description 1', + } )->store; + my $biblioitem = $builder->build( { source => 'Biblioitem' } ); + + my $library1 = Koha::Library->new( { + branchcode => 'CODE1', + } )->store; + + my $library2 = Koha::Library->new( { + branchcode => 'CODE2', + } )->store; + + my $item = Koha::Item->new( { + biblionumber => $biblioitem->{biblionumber}, + biblioitemnumber => $biblioitem->{biblioitemnumber}, + homebranch => $library1->branchcode, + holdingbranch => $library1->branchcode, + itype => 'BK', + barcode => 'some_barcode', + } )->store; + + $collection->add_item( $item ); + + eval { $collection->transfer }; + is( ref($@), 'Koha::Exceptions::MissingParameter', 'Missing paramater exception'); + + $collection->transfer( $library2 ); + my $retrieved_item = Koha::Items->find( $item->itemnumber ); + + is( $collection->colBranchcode, $library2->branchcode, 'Collection should be transferred' ); + is( $retrieved_item->holdingbranch, $library2->branchcode, 'Items in collection should be transferred too' ); + + my $transfer = Koha::Item::Transfers->search( { + itemnumber => $item->itemnumber, + frombranch => $library1->branchcode, + tobranch => $library2->branchcode, + datearrived => undef, + } ); + is( $transfer->count, 1, 'There should be transfer started for item in collection'); + + $schema->storage->txn_rollback; +}; diff --git a/t/db_dependent/RotatingCollections.t b/t/db_dependent/RotatingCollections.t deleted file mode 100644 index a5bdb4a..0000000 --- a/t/db_dependent/RotatingCollections.t +++ /dev/null @@ -1,337 +0,0 @@ -#!/usr/bin/perl - -# This file is part of Koha. -# -# Koha is free software; you can redistribute it and/or modify it -# under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 3 of the License, or -# (at your option) any later version. -# -# Koha is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Koha; if not, see . - -use Modern::Perl; - -use Test::More tests => 52; -use C4::Context; -use C4::Biblio; -use Koha::Library; - -BEGIN { - use_ok('C4::RotatingCollections'); -} - -can_ok( - 'C4::RotatingCollections', - qw( - AddItemToCollection - CreateCollection - DeleteCollection - GetCollection - GetCollectionItemBranches - GetCollections - GetItemsInCollection - RemoveItemFromCollection - TransferCollection - UpdateCollection - isItemInAnyCollection - isItemInThisCollection - ) -); - -#Start transaction -my $dbh = C4::Context->dbh; -$dbh->{RaiseError} = 1; -$dbh->{AutoCommit} = 0; - -#Start Tests -$dbh->do(q|DELETE FROM issues |); -$dbh->do(q|DELETE FROM borrowers |); -$dbh->do(q|DELETE FROM items |); -$dbh->do(q|DELETE FROM collections_tracking |); -$dbh->do(q|DELETE FROM collections |); -$dbh->do(q|DELETE FROM branches |); -$dbh->do(q|DELETE FROM categories|); -$dbh->do(q|DELETE FROM branchcategories|); - -#Test CreateCollection -my $collections = GetCollections(); -my $countcollection = scalar(@$collections); - -my ($success,$errorCode,$errorMessage); - -($success,$errorCode,$errorMessage) = CreateCollection( 'Collection1', 'Description1' ); -is( $success, 1, "All parameters have been given - Collection 1 added" ); -ok( !defined $errorCode && !defined $errorMessage, - "Collection added, no error code or message"); -my $collection_id1 = $dbh->last_insert_id( undef, undef, 'collections', undef ); - -($success,$errorCode,$errorMessage) = CreateCollection( 'Collection2', 'Description2' ); -is( $success, 1, "All parameters have been given - Collection 2 added" ); -ok( !defined $errorCode && !defined $errorMessage, - "Collection added, no error code or message"); -my $collection_id2 = $dbh->last_insert_id( undef, undef, 'collections', undef ); - -$collections = GetCollections(); -is( scalar(@$collections), $countcollection + 2, - "Collection1 and Collection2 have been added" ); - -($success,$errorCode,$errorMessage) = CreateCollection('Collection3'); -is( $success, 1, "Collections can be created without description" ); -ok( !defined $errorCode && !defined $errorMessage, - "Collection added, no error code or message"); -my $collection_id3 = $dbh->last_insert_id( undef, undef, 'collections', undef ); - -($success,$errorCode,$errorMessage) = CreateCollection(); -is( $success, 0, "Title missing, fails to create collection" ); -is( $errorCode, 1, "Title missing, error code is 1" ); -is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE" ); - -$collections = GetCollections(); -is( scalar(@$collections), $countcollection + 3, "Only one collection added" ); - -#FIXME, as the id is auto incremented, two similar Collections (same title /same description) can be created -#$collection1 = CreateCollection('Collection1','Description1'); - -#Test GetCollections -my $collection = GetCollections(); -is_deeply( - $collections, - [ - { - colId => $collection_id1, - colTitle => 'Collection1', - colDesc => 'Description1', - colBranchcode => undef - }, - { - colId => $collection_id2, - colTitle => 'Collection2', - colDesc => 'Description2', - colBranchcode => undef - }, - { - colId => $collection_id3, - colTitle => 'Collection3', - colDesc => '', - colBranchcode => undef - } - - ], - 'All Collections' -); - -#Test UpdateCollection -($success,$errorCode,$errorMessage) = - UpdateCollection( $collection_id2, 'Collection2bis', undef ); -is( $success, 1, "UpdateCollection succeeds without description"); - -($success,$errorCode,$errorMessage) = - UpdateCollection( $collection_id2, 'Collection2 modified', 'Description2 modified' ); -is( $success, 1, "Collection2 has been modified" ); -ok( !defined $errorCode && !defined $errorMessage, - "Collection2 modified, no error code or message"); - -($success,$errorCode,$errorMessage) = - UpdateCollection( $collection_id2, undef, 'Description' ), -ok( !$success, "UpdateCollection fails without title" ); -is( $errorCode, 2, "Title missing, error code is 2"); -is( $errorMessage, 'NO_TITLE', "Title missing, error message is NO_TITLE"); - -is( UpdateCollection(), 'NO_ID', "UpdateCollection without params" ); - -#Test GetCollection -my @collection1 = GetCollection($collection_id1); -is_deeply( - \@collection1, - [ $collection_id1, 'Collection1', 'Description1', undef ], - "Collection1's informations" -); -my @collection2 = GetCollection($collection_id2); -is_deeply( - \@collection2, - [ $collection_id2, 'Collection2 modified', 'Description2 modified', undef ], - "Collection2's informations" -); -my @undef_collection = GetCollection(); -is_deeply( - \@undef_collection, - [ undef, undef, undef, undef ], - "GetCollection without id given" -); -@undef_collection = GetCollection(-1); -is_deeply( - \@undef_collection, - [ undef, undef, undef, undef ], - "GetCollection with a wrong id" -); - -#Test TransferCollection -my $samplebranch = { - branchcode => 'SAB', - branchname => 'Sample Branch', - branchaddress1 => 'sample adr1', - branchaddress2 => 'sample adr2', - branchaddress3 => 'sample adr3', - branchzip => 'sample zip', - branchcity => 'sample city', - branchstate => 'sample state', - branchcountry => 'sample country', - branchphone => 'sample phone', - branchfax => 'sample fax', - branchemail => 'sample email', - branchurl => 'sample url', - branchip => 'sample ip', - branchprinter => undef, - branchnotes => 'sample note', - opac_info => 'sample opac', -}; -Koha::Library->new($samplebranch)->store; -is( TransferCollection( $collection_id1, $samplebranch->{branchcode} ), - 1, "Collection1 has been transfered in the branch SAB" ); -@collection1 = GetCollection($collection_id1); -is_deeply( - \@collection1, - [ - $collection_id1, 'Collection1', - 'Description1', $samplebranch->{branchcode} - ], - "Collection1 belongs to the sample branch (SAB)" -); -is( TransferCollection, "NO_ID", "TransferCollection without ID" ); -is( - TransferCollection($collection_id1), - 'NO_BRANCHCODE', - "TransferCollection without branchcode" -); - -#Test AddItemToCollection -my $record = MARC::Record->new(); -$record->append_fields( - MARC::Field->new( - '952', '0', '0', - a => $samplebranch->{branchcode}, - b => $samplebranch->{branchcode} - ) -); -my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '', ); -my @sampleitem1 = C4::Items::AddItem( - { - barcode => 1, - itemcallnumber => 'callnumber1', - homebranch => $samplebranch->{branchcode}, - holdingbranch => $samplebranch->{branchcode} - }, - $biblionumber -); -my $item_id1 = $sampleitem1[2]; -my @sampleitem2 = C4::Items::AddItem( - { - barcode => 2, - itemcallnumber => 'callnumber2', - homebranch => $samplebranch->{branchcode}, - holdingbranch => $samplebranch->{branchcode} - }, - $biblionumber -); -my $item_id2 = $sampleitem2[2]; -is( AddItemToCollection( $collection_id1, $item_id1 ), - 1, "Sampleitem1 has been added to Collection1" ); -is( AddItemToCollection( $collection_id1, $item_id2 ), - 1, "Sampleitem2 has been added to Collection1" ); - -#Test GetItemsInCollection -my $itemsincollection1; -($itemsincollection1,$success,$errorCode,$errorMessage) = GetItemsInCollection($collection_id1); -is( scalar @$itemsincollection1, 2, "Collection1 has 2 items" ); -is_deeply( - $itemsincollection1, - [ - { - title => undef, - itemcallnumber => 'callnumber1', - biblionumber => $biblionumber, - barcode => 1 - }, - { - title => undef, - itemcallnumber => 'callnumber2', - biblionumber => $biblionumber, - barcode => 2 - } - ], - "Collection1 has Item1 and Item2" -); -($itemsincollection1,$success,$errorCode,$errorMessage) = GetItemsInCollection(); -ok( !$success, "GetItemsInCollection fails without a collection ID" ); -is( $errorCode, 1, "Title missing, error code is 2"); -is( $errorMessage, 'NO_ID', "Collection ID missing, error message is NO_ID"); - -#Test RemoveItemFromCollection -is( RemoveItemFromCollection( $collection_id1, $item_id2 ), - 1, "Item2 has been removed from collection 1" ); -$itemsincollection1 = GetItemsInCollection($collection_id1); -is( scalar @$itemsincollection1, 1, "Collection1 has 1 items" ); - -#Test isItemInAnyCollection -is( C4::RotatingCollections::isItemInAnyCollection($item_id1), - 1, "Item1 is in a collection" ); -is( C4::RotatingCollections::isItemInAnyCollection($item_id2), - 0, "Item2 is not in a collection" ); -is( C4::RotatingCollections::isItemInAnyCollection(), - 0, "isItemInAnyCollection returns 0 if no itemid given " ); -is( C4::RotatingCollections::isItemInAnyCollection(-1), - 0, "isItemInAnyCollection returns 0 if a wrong id is given" ); - -#Test isItemInThisCollection -is( - C4::RotatingCollections::isItemInThisCollection( - $item_id1, $collection_id1 - ), - 1, - "Item1 is in the Collection1" -); -is( - C4::RotatingCollections::isItemInThisCollection( - $item_id1, $collection_id2 - ), - 0, - "Item1 is not in the Collection2" -); -is( - C4::RotatingCollections::isItemInThisCollection( - $item_id2, $collection_id2 - ), - 0, - "Item2 is not in the Collection2" -); -is( C4::RotatingCollections::isItemInThisCollection($collection_id1), - 0, "isItemInThisCollection returns 0 is ItemId is missing" ); -is( C4::RotatingCollections::isItemInThisCollection($item_id1), - 0, "isItemInThisCollection returns 0 is Collectionid if missing" ); -is( C4::RotatingCollections::isItemInThisCollection(), - 0, "isItemInThisCollection returns 0 if no params given" ); - -#Test DeleteCollection -is( DeleteCollection($collection_id2), 1, "Collection2 deleted" ); -is( DeleteCollection($collection_id1), 1, "Collection1 deleted" ); -is( - DeleteCollection(), - 'NO_ID', - "DeleteCollection without id" -); -$collections = GetCollections(); -is( - scalar(@$collections), - $countcollection + 1, - "Two Collections have been deleted" -); - -#End transaction -$dbh->rollback; - -- 2.1.4