From 834588262d99849e169838717b276f5c9e758cad Mon Sep 17 00:00:00 2001 From: Olli-Antti Kivilahti Date: Wed, 27 May 2015 13:52:11 +0300 Subject: [PATCH] Bug 13906 - Cucumber tests. See Cucumber/testsStructuring.tutorial for more info --- t/Cucumber/SImpls/Accountlines.pm | 70 +++++++++ t/Cucumber/SImpls/Biblios.pm | 48 ++++++ t/Cucumber/SImpls/Borrowers.pm | 106 +++++++++++++ t/Cucumber/SImpls/BranchTransfers.pm | 87 +++++++++++ t/Cucumber/SImpls/Context.pm | 44 ++++++ t/Cucumber/SImpls/FileUtils.pm | 56 +++++++ t/Cucumber/SImpls/Issues.pm | 77 ++++++++++ t/Cucumber/SImpls/Items.pm | 55 +++++++ t/Cucumber/SImpls/LetterTemplates.pm | 68 +++++++++ t/Cucumber/SImpls/MessageQueues.pm | 217 +++++++++++++++++++++++++++ t/Cucumber/SImpls/ScriptRunning.pm | 40 +++++ t/Cucumber/SImpls/SystemPreferences.pm | 63 ++++++++ t/Cucumber/runCucumberTests.t | 104 +++++++++++++ t/Cucumber/steps/accountlines_steps.pl | 32 ++++ t/Cucumber/steps/biblios_steps.pl | 28 ++++ t/Cucumber/steps/borrowers_steps.pl | 32 ++++ t/Cucumber/steps/branchTransfers_steps.pl | 28 ++++ t/Cucumber/steps/common_steps.pl | 44 ++++++ t/Cucumber/steps/context_steps.pl | 30 ++++ t/Cucumber/steps/fileUtils_steps.pl | 32 ++++ t/Cucumber/steps/issues_steps.pl | 43 ++++++ t/Cucumber/steps/items_steps.pl | 28 ++++ t/Cucumber/steps/letterTemplates_steps.pl | 28 ++++ t/Cucumber/steps/messageQueue_steps.pl | 39 +++++ t/Cucumber/steps/scriptRunning_steps.pl | 28 ++++ t/Cucumber/steps/systempreferences_steps.pl | 28 ++++ t/Cucumber/testsStructuring.tutorial | 60 ++++++++ 27 files changed, 1515 insertions(+) create mode 100644 t/Cucumber/SImpls/Accountlines.pm create mode 100644 t/Cucumber/SImpls/Biblios.pm create mode 100644 t/Cucumber/SImpls/Borrowers.pm create mode 100644 t/Cucumber/SImpls/BranchTransfers.pm create mode 100644 t/Cucumber/SImpls/Context.pm create mode 100644 t/Cucumber/SImpls/FileUtils.pm create mode 100644 t/Cucumber/SImpls/Issues.pm create mode 100644 t/Cucumber/SImpls/Items.pm create mode 100644 t/Cucumber/SImpls/LetterTemplates.pm create mode 100644 t/Cucumber/SImpls/MessageQueues.pm create mode 100644 t/Cucumber/SImpls/ScriptRunning.pm create mode 100644 t/Cucumber/SImpls/SystemPreferences.pm create mode 100755 t/Cucumber/runCucumberTests.t create mode 100644 t/Cucumber/steps/accountlines_steps.pl create mode 100644 t/Cucumber/steps/biblios_steps.pl create mode 100644 t/Cucumber/steps/borrowers_steps.pl create mode 100644 t/Cucumber/steps/branchTransfers_steps.pl create mode 100644 t/Cucumber/steps/common_steps.pl create mode 100644 t/Cucumber/steps/context_steps.pl create mode 100644 t/Cucumber/steps/fileUtils_steps.pl create mode 100644 t/Cucumber/steps/issues_steps.pl create mode 100644 t/Cucumber/steps/items_steps.pl create mode 100644 t/Cucumber/steps/letterTemplates_steps.pl create mode 100644 t/Cucumber/steps/messageQueue_steps.pl create mode 100644 t/Cucumber/steps/scriptRunning_steps.pl create mode 100644 t/Cucumber/steps/systempreferences_steps.pl create mode 100644 t/Cucumber/testsStructuring.tutorial diff --git a/t/Cucumber/SImpls/Accountlines.pm b/t/Cucumber/SImpls/Accountlines.pm new file mode 100644 index 0000000..bb2624d --- /dev/null +++ b/t/Cucumber/SImpls/Accountlines.pm @@ -0,0 +1,70 @@ +package SImpls::Accountlines; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use Koha::Database; + + +sub deleteAllFines { + my $schema = Koha::Database->new()->schema(); + $schema->resultset('Accountline')->search({})->delete_all(); +} + +sub checkAccountlines { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + my $checks = $C->data(); #Get the checks, which might not have manifested itselves to the message_queue_items + ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); + + #See which checks are supposed to be in the accountlines-table, and which are just to clarify intent. + my @checksToDo; + foreach my $check (@$checks) { + my $status = $check->{fine}; + push @checksToDo, $check if ($status ne 'none'); + } + + ##Make sure that there are no trailing Accountlines. + my $schema = Koha::Database->new()->schema(); + my $allFinesCount = $schema->resultset('Accountline')->search({})->count(); + is($allFinesCount, scalar(@checksToDo), "We should have ".scalar(@checksToDo)." checks for $allFinesCount fines, with no trailing fines."); + + my $dbh = C4::Context->dbh(); + my $check_statement = $dbh->prepare( + "SELECT 1 FROM accountlines a ". + "LEFT JOIN borrowers b ON a.borrowernumber = b.borrowernumber ". + "WHERE b.cardnumber = ? AND a.amountoutstanding = ? ". + ""); + + ##Check that there is a matching Accountline for each given test. + foreach my $check (@checksToDo) { + my @params; + push @params, $check->{cardnumber}; + push @params, $check->{fine}; + $check_statement->execute( @params ); + my $ok = $check_statement->fetchrow(); + last unless ok(($ok && $ok == 1), "Check: For params @params"); + } +} + +1; diff --git a/t/Cucumber/SImpls/Biblios.pm b/t/Cucumber/SImpls/Biblios.pm new file mode 100644 index 0000000..eb9f7c4 --- /dev/null +++ b/t/Cucumber/SImpls/Biblios.pm @@ -0,0 +1,48 @@ +package SImpls::Biblios; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use t::db_dependent::TestObjects::Biblios::BiblioFactory; + +sub addBiblios { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + $S->{biblios} = {} unless $S->{biblios}; + $F->{biblios} = {} unless $F->{biblios}; + + my $records = t::db_dependent::TestObjects::Biblios::BiblioFactory::createTestGroup($C->data(),'biblioitems.isbn'); + + while( my ($key, $record) = each %$records) { + $S->{biblios}->{ $key } = $record; + $F->{biblios}->{ $key } = $record; + } +} + +sub deleteBiblios { + my $C = shift; + my $F = $C->{stash}->{feature}; + t::db_dependent::TestObjects::Biblios::BiblioFactory::deleteTestGroup( $F->{biblios} ); +} + +1; diff --git a/t/Cucumber/SImpls/Borrowers.pm b/t/Cucumber/SImpls/Borrowers.pm new file mode 100644 index 0000000..eea4f5a --- /dev/null +++ b/t/Cucumber/SImpls/Borrowers.pm @@ -0,0 +1,106 @@ +package SImpls::Borrowers; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use Koha::Borrowers; +use Koha::Borrower::Debarments; + +use t::db_dependent::TestObjects::Borrowers::BorrowerFactory; + +sub addBorrowers { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + $S->{borrowers} = {} unless $S->{borrowers}; + $F->{borrowers} = {} unless $F->{borrowers}; + + my $borrowers = t::db_dependent::TestObjects::Borrowers::BorrowerFactory::createTestGroup($C->data(),'cardnumber'); + + while( my ($key, $borrower) = each %$borrowers) { + $S->{borrowers}->{ $key } = $borrower; + $F->{borrowers}->{ $key } = $borrower; + } +} + +sub deleteBorrowers { + my $C = shift; + my $F = $C->{stash}->{feature}; + t::db_dependent::TestObjects::Borrowers::BorrowerFactory::deleteTestGroup( $F->{borrowers} ); +} + +sub checkDebarments { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + my $checks = $C->data(); #Get the checks we need to do. + ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); + + ##Make sure that there are no trailing Debarments. + my $schema = Koha::Database->new()->schema(); + my $allDebarmentsCount = $schema->resultset('BorrowerDebarment')->search({})->count(); + is($allDebarmentsCount, scalar(@$checks), "We should have ".scalar(@$checks)." checks for $allDebarmentsCount fines, with no trailing fines."); + + #We need to collect all checks for each borrower, because a single borrower can have multiple checks + my %checksByBorrower; + foreach my $check (@$checks) { + unless ($checksByBorrower{$check->{cardnumber}}) { + $checksByBorrower{$check->{cardnumber}} = []; + } + push( @{$checksByBorrower{$check->{cardnumber}}}, $check ); + #Remove keys not needed in the comparison ahead. Leaving keys not found in the debarment-object will ruin the check. + delete $check->{cardnumber}; + } + + ##Check that there is a matching Debarment for each given check. + while( my ($cardnumber, $checks) = each %checksByBorrower ) { + my $borrower = Koha::Borrowers->find({cardnumber => $cardnumber}); + die "checkDebarments():> No borrower for cardnumber '$cardnumber'" unless ($cardnumber); + my $debarments = Koha::Borrower::Debarments::GetDebarments({borrowernumber => $borrower->id()}); + + #Check every check against every debarment, if all checks are satisifed from the found debarments, all is fine! + foreach my $check (@$checks) { + my $checkOk = 0; + for (my $i=0 ; $i<@$debarments ; $i++) { + my $debarment = $debarments->[$i]; + + #Iterate all the keys in check, and see if they match the debarment + my $keysNeedingSuccessfulMatch = scalar(keys(%$check)); + while( my ($key, $value) = each %$check ) { + if ($debarment->{$key} eq $check->{$key}) { + $keysNeedingSuccessfulMatch--; + } + } + #This debarment matches this check + if ($keysNeedingSuccessfulMatch == 0) { + splice @$debarments, $i, 1; + $i--; + $checkOk = 1; + last(); + } + } + return unless ok($checkOk, "Debarment '".join(', ',join(' => ',each(%$check)))."' found."); + } + } +} + +1; diff --git a/t/Cucumber/SImpls/BranchTransfers.pm b/t/Cucumber/SImpls/BranchTransfers.pm new file mode 100644 index 0000000..176209c --- /dev/null +++ b/t/Cucumber/SImpls/BranchTransfers.pm @@ -0,0 +1,87 @@ +package SImpls::BranchTransfers; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; + +use Test::More; +use C4::Context; +use C4::Items; +use C4::Circulation; + +sub verifyItemsInTransit { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + my $checks = $C->data(); #Get the checks, which might not have manifested itselves to the branchtransfers + ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); + + #See which checks are supposed to be in the branchtransfers-table, and which are just to clarify intent. + my @checksInTable; + foreach my $check (@$checks) { + my $status = $check->{status}; + push @checksInTable, $check if (not($status) || ($status ne 'no_trfr')); + } + + ##Check that there is a matching branchtransfer-row for each given test. + my %testedBranchtransfers; #Collect the branchtransfers here so we can see that we won't accidentally test the same transfer twice, + # and can tell which branchtransfers are left untested and shouldn't exist according to the test plan. + foreach my $check (@checksInTable) { + my @params; #DEL THIS + my $itemnumber = C4::Items::GetItemnumberFromBarcode( $check->{barcode} ); + my @transfer = C4::Circulation::GetTransfers($itemnumber); + ##See if the branchtransfer_id is already tested and fail the test, or add the branchtransfer to the tested group. + # Throw an failed test only if the test should fail, don't clutter with meaningless tests. + ok((0), "Not testing fromBranch '".$check->{fromBranch}."', toBranch '".$check->{toBranch}."' twice. Awsum!") + if(defined($testedBranchtransfers{$transfer[3]})); + $testedBranchtransfers{$transfer[3]} = \@transfer; #Store the branchtransfer by id + + last unless ok(($transfer[1] eq $check->{fromBranch} && $transfer[2] eq $check->{toBranch}), "Check: fromBranch '".$check->{fromBranch}."', toBranch '".$check->{toBranch}."'"); + } + + ##!# Check for leaking branchtransfers, eg. unintended side-effects. #!## + #Remove all tested branchtransfers from all the transfers in DB, and see if we have some more in the DB. This means that our tests leak branchtransfers we didn't intend to have! + my $allBranchtransfers = C4::Circulation::GetAllTransfers(); + while (my ($branchtransfer_id, $branchtransferArr) = each(%testedBranchtransfers)) { + for (my $i=0 ; $i[$i]; + if ($branchTransfer->{branchtransfer_id} == $branchtransferArr->[3]) { #If id's match, this branchtransfer is tested. + splice(@$allBranchtransfers, $i, 1); #Remove array element at position $i + last; + } + } + } + #Throw a failed tests for each excess branchtransfer encountered. We never get into this loop if tests work ok. + foreach my $branchTransfer (@$allBranchtransfers) { + my $item = C4::Items::GetItem( $branchTransfer->{itemnumber} ); + ok((not($branchTransfer)), "Trailing branchtransfer for barcode '".$item->{barcode}."' fromBranch '".$branchTransfer->{frombranch}."' toBranch '".$branchTransfer->{tobranch}."'"); + } +} + +##We cannot use the Koha internal API, because then these would be preserved as arrived transfers +#Sometimes the test feature can fail and leave straggler Transfers, which prevent proper clean up. So it is better to just +#remove all Transfers. These tests should be transitive anyway. +sub deleteAllTransfers { + my $C = shift; + my $F = $C->{stash}->{feature}; + my $schema = Koha::Database->new()->schema(); + + $schema->resultset('Branchtransfer')->search({})->delete_all(); +} +1; diff --git a/t/Cucumber/SImpls/Context.pm b/t/Cucumber/SImpls/Context.pm new file mode 100644 index 0000000..b4a5ffd --- /dev/null +++ b/t/Cucumber/SImpls/Context.pm @@ -0,0 +1,44 @@ +package SImpls::Context; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; + +use C4::Context; + +sub setKohaContext { + my $C = shift; + + my $contextHash = $C->{_data}->[0]; + C4::Context->_new_userenv($contextHash->{userenvName} || 'DUMMY SESSION'); + C4::Context->set_userenv( + 0, + 0, + 0, + $contextHash->{firstName} || 'firstname', + $contextHash->{surname} || 'surname', + $contextHash->{branchCode} || 'CPL', + $contextHash->{branchName} || 'Centerville Public Library', + $contextHash->{userFlags} || 0, + $contextHash->{userEmail} || 'noemail@example.com', + $contextHash->{branchPrinter} || 'branchPrinter?', + ); +} + +1; diff --git a/t/Cucumber/SImpls/FileUtils.pm b/t/Cucumber/SImpls/FileUtils.pm new file mode 100644 index 0000000..5f6c4a0 --- /dev/null +++ b/t/Cucumber/SImpls/FileUtils.pm @@ -0,0 +1,56 @@ +package SImpls::FileUtils; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +sub checkRegexpInsideFiles { + my $C = shift; + my $data = $C->data; + + ##First cache files to avoid unnecessary file loading + my %files; + foreach my $d (@$data) { + unless ( $files{ $d->{fileName} } ) { + my $directory = $d->{directory}; + $directory =~ s/\$KOHA_PATH/$ENV{KOHA_PATH}/gsm; + my $fileContent = File::Slurp::read_file( $directory.$d->{fileName}, {binmode => ':utf8'} ) + or die "common_stepImpl::checkRegexpInsideFiles():> $!"; + $files{ $d->{fileName} } = $fileContent; + } + } + + foreach my $d (@$data) { + my $fileContent = $files{ $d->{fileName} }; + my $regexp = $d->{recordFindingRegexp}; + last unless ok(($fileContent =~ m/$regexp/u), "File ".$d->{fileName}." matches $regexp"); + } +} + +sub findFile { + my $C = shift; + my $file = $1; + $file =~ s/\$KOHA_PATH/$ENV{KOHA_PATH}/gsm; + + ok((-f $file && -r $file), "$file exists and is readable"); + $C->{stash}->{scenario}->{file} = $file; +} + +1; diff --git a/t/Cucumber/SImpls/Issues.pm b/t/Cucumber/SImpls/Issues.pm new file mode 100644 index 0000000..02670cf --- /dev/null +++ b/t/Cucumber/SImpls/Issues.pm @@ -0,0 +1,77 @@ +package SImpls::Issues; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use C4::Circulation; +use t::db_dependent::TestObjects::Issues::IssueFactory; + +sub addIssues { + my $C = shift; + my $homeOrHoldingbranch = shift; + die "addIssues():> \$homeOrHoldingbranch must be 'homebranch' or 'holdingbranch'" unless ($homeOrHoldingbranch eq 'homebranch' || $homeOrHoldingbranch eq 'holdingbranch'); + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + $S->{issues} = {} unless $S->{issues}; + $F->{issues} = {} unless $F->{issues}; + + my $issues = t::db_dependent::TestObjects::Issues::IssueFactory::createTestGroup( $C->data(), undef, $homeOrHoldingbranch ); + + while( my ($key, $issue) = each %$issues) { + $S->{issues}->{ $key } = $issue; + $F->{issues}->{ $key } = $issue; + } +} + +##We cannot use the Koha internal API, because then these would be put to the deleteditems, deletedbiblio, old_issues, ... -tables +#Sometimes the test feature can fail and leave straggler Issues, which prevent proper clean up. So it is better to just +#remove all Issues. These tests should be transitive anyway. +sub deleteAllIssues { + my $C = shift; + my $F = $C->{stash}->{feature}; + my $schema = Koha::Database->new()->schema(); + + $schema->resultset('Issue')->search({})->delete_all(); + $schema->resultset('OldIssue')->search({})->delete_all(); +} + +sub checkInIssues { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + my $homeOrHoldingbranch = shift; + die "checkInAllIssues():> \$homeOrHoldingbranch must be 'homebranch' or 'holdingbranch'" unless ($homeOrHoldingbranch eq 'homebranch' || $homeOrHoldingbranch eq 'holdingbranch'); + + if (ref $S->{issues} eq 'HASH') { + while( my ($key, $issue) = each %{$S->{issues}}) { + my $is = $S->{issues}->{ $key }; + my ($doreturn, $messages, $iteminformation, $borrower) = C4::Circulation::AddReturn( $is->{barcode}, $is->{branchcode} ); + my $debug; #Just here so I can inspect the AddReturn return values with the DBGP-protocol. + } + } + else { + die "checkInIssues():> No Scenario HASH for Issues!"; + } +} + +1; diff --git a/t/Cucumber/SImpls/Items.pm b/t/Cucumber/SImpls/Items.pm new file mode 100644 index 0000000..0e4f313 --- /dev/null +++ b/t/Cucumber/SImpls/Items.pm @@ -0,0 +1,55 @@ +package SImpls::Items; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use t::db_dependent::TestObjects::Items::ItemFactory; + +sub addItems { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + $S->{items} = {} unless $S->{items}; + $F->{items} = {} unless $F->{items}; + + #Find the biblionumber of the parent Biblios + my $data = $C->data(); + foreach my $itemHash (@$data) { + my $biblionumber = $S->{biblios}->{ $itemHash->{biblioisbn} }->{biblionumber}; + $itemHash->{biblionumber} = $biblionumber; + } + + my $items = t::db_dependent::TestObjects::Items::ItemFactory::createTestGroup($data, undef); + + while( my ($key, $item) = each %$items) { + $S->{items}->{ $key } = $item; + $F->{items}->{ $key } = $item; + } +} + +sub deleteItems { + my $C = shift; + my $F = $C->{stash}->{feature}; + t::db_dependent::TestObjects::Items::ItemFactory::deleteTestGroup( $F->{items} ); +} + +1; diff --git a/t/Cucumber/SImpls/LetterTemplates.pm b/t/Cucumber/SImpls/LetterTemplates.pm new file mode 100644 index 0000000..7ccb52a --- /dev/null +++ b/t/Cucumber/SImpls/LetterTemplates.pm @@ -0,0 +1,68 @@ +package SImpls::LetterTemplates; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory; + +sub addLetterTemplates { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + $S->{letterTemplates} = {} unless $S->{letterTemplates}; + $F->{letterTemplates} = {} unless $F->{letterTemplates}; + + #Split the given message_transport_types-column to separate mtts + #and create separate letterTemplates for each mtt. + my $data = $C->data(); + my @letterTemplatesSeparatedMTTs; + foreach my $lt (@$data) { + my @mtts = map {my $a = $_; $a =~ s/\s+//gsm; $a;} split(',', $lt->{message_transport_types}); + delete $lt->{message_transport_types}; + foreach my $mtt (@mtts) { + my %newLt = %$lt; #Clone the messageQueue. + $newLt{message_transport_type} = $mtt; + $newLt{content} =~ s/\\n/\n/gsm; + push @letterTemplatesSeparatedMTTs, \%newLt; + } + } + + my $letterTemplates = t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory::createTestGroup(\@letterTemplatesSeparatedMTTs, undef); + + while( my ($key, $letterTemplate) = each %$letterTemplates) { + $S->{letterTemplates}->{ $key } = $letterTemplate; + $F->{letterTemplates}->{ $key } = $letterTemplate; + } +} + +sub deleteLetterTemplates { + my $C = shift; + my $F = $C->{stash}->{feature}; + t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory::deleteTestGroup( $F->{letterTemplates} ); +} + +sub deleteAllLetterTemplates { + my $schema = Koha::Database->new()->schema(); + $schema->resultset('Letter')->search({})->delete_all(); +} + +1; diff --git a/t/Cucumber/SImpls/MessageQueues.pm b/t/Cucumber/SImpls/MessageQueues.pm new file mode 100644 index 0000000..b2b923c --- /dev/null +++ b/t/Cucumber/SImpls/MessageQueues.pm @@ -0,0 +1,217 @@ +package SImpls::MessageQueues; + +# Copyright 2015 Vaara-kirjastot +# +# 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 2 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use Koha::Database; +use C4::Context; + +sub addMessageQueues { + my $C = shift; + my $letterCode = shift; + my $messageTransportType = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + $S->{message_queues} = {} unless $S->{message_queues}; + $F->{message_queues} = {} unless $F->{message_queues}; + foreach my $cardumber (sort keys %{$S->{borrowers}}) { + my $borrower = $S->{borrowers}->{$cardumber}; + + my @repeat; + foreach my $i ( sort keys %{$S->{issues}} ) { + my $issue = $S->{issues}->{$i}; + if ( $issue->{borrowernumber} eq $borrower->{borrowernumber} ) { + my $itemForCurrentBorrower = $S->{items}->{ $issue->{itemnumber} }; + push @repeat, {items => $itemForCurrentBorrower}; + } + } + my $letter = C4::Letters::GetPreparedLetter ( + module => 'circulation', + letter_code => $letterCode, + branchcode => $borrower->{branchcode}, + tables => {borrowers => $borrower}, + #substitute => $substitute, + repeat => { item => \@repeat }, + message_transport_type => $messageTransportType, + ); + my $mqHash = { + letter => $letter, + borrowernumber => $borrower->{borrowernumber}, + message_transport_type => $messageTransportType, + from_address => C4::Context->preference('KohaAdminEmailAddress'), + to_address => 'dontsend@example.com', + }; + my $message_queue_id = C4::Letters::EnqueueLetter( $mqHash ); + like($message_queue_id, qr/^\d+$/, "Message enqueued."); + $S->{message_queues}->{$message_queue_id} = $mqHash; + $F->{message_queues}->{$message_queue_id} = $mqHash; + } +} + +sub deleteAllMessageQueues { + my $C = shift; + my $F = $C->{stash}->{feature}; + my $schema = Koha::Database->new()->schema(); + + $schema->resultset('MessageQueue')->search({})->delete_all(); +} + +sub verifyMessageQueueItems { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + my $checks = $C->data(); #Get the checks, which might not have manifested itselves to the message_queue_items + ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); + + #See which checks are supposed to be in the message_queue_item-table, and which are just to clarify intent. + my @checksInTable; + foreach my $check (@$checks) { + my $status = $check->{status}; + push @checksInTable, $check if ($status ne 'not_odue' && $status ne 'no_rule'); + } + + ##Make sure that there are no trailing MessageQueueItems. + my $schema = Koha::Database->new()->schema(); + my $allMessageQueueItemsCount = $schema->resultset('MessageQueueItem')->search({})->count(); + is($allMessageQueueItemsCount, scalar(@checksInTable), "We should have ".scalar(@checksInTable)." checks for $allMessageQueueItemsCount message_queue_items, with no trailing items."); + + #Expressing the following cannot be comfortably done with DBIx + #especially since message_queue_items-table should not have foreign key linkings to items and issues-tables. + my $dbh = C4::Context->dbh(); + my $check_statement = $dbh->prepare( + "SELECT 1 FROM message_queue_items mi ". + "LEFT JOIN message_queue mq ON mi.message_id = mq.message_id ". + "LEFT JOIN borrowers b ON mq.borrowernumber = b.borrowernumber ". + "LEFT JOIN items i ON mi.itemnumber = i.itemnumber ". + "LEFT JOIN issues iss ON iss.issue_id = mi.issue_id ". + "WHERE b.cardnumber = ? AND i.barcode = ? AND mi.branch = ? AND mq.letter_code = ? ". + "AND mi.letternumber = ? AND mq.status = ? AND mq.message_transport_type = ? ". + ""); + + ##Check that there is a matching MessageQueueItem for each given test. + foreach my $check (@checksInTable) { + my @params; + push @params, $check->{cardnumber}; + push @params, $check->{barcode}; + push @params, $check->{branch}; + push @params, $check->{lettercode}; + push @params, $check->{letternumber}; + push @params, $check->{status}; + push @params, $check->{transport_type}; + $check_statement->execute( @params ); + my $ok = $check_statement->fetchrow(); + last unless ok(($ok && $ok == 1), "Check: For params @params"); + } +} + +sub verifyMessageQueues { + my $C = shift; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + my $checks = $C->data(); #Get the checks, which might not have manifested itselves to the message_queues + ok(($checks && scalar(@$checks) > 0), "You must give checks as the data"); + + ##Make sure that there are no trailing MessageQueueItems. + my $schema = Koha::Database->new()->schema(); + my $allMessageQueuesCount = $schema->resultset('MessageQueue')->search({})->count(); + is($allMessageQueuesCount, scalar(@$checks), "We should have ".scalar(@$checks)." checks for $allMessageQueuesCount message_queue_items, with no trailing items."); + + my @all = $schema->resultset('MessageQueue')->search({}); + foreach my $a ( @all ) { + print $a->content()."\n"; + } + + if ($checks->[0]->{contentRegexp}) { + verifyMessageQueuesFromContentRegexp($checks); + } + elsif ($checks->[0]->{containedBarcodes}) { + verifyMessageQueuesFromBarcodes($checks); + } + else { + die "\nverifyMessageQueues():> The MessageQueues-data table must contain column 'barcodes', or 'contentRegexp'.\n". + "'barcodes' contains comma separated list of barcodes that must be found inside the message_queue.content.\n". + "'contentRegexp' is a regexp that must match with the message_queue.content.\n"; + } +} + +sub verifyMessageQueuesFromBarcodes { + my ($checks) = @_; + + my $dbh = C4::Context->dbh(); + my $check_statement = $dbh->prepare( + "SELECT 1 FROM message_queue mq ". + "LEFT JOIN message_queue_items mqi ON mqi.message_id = mq.message_id ". + "LEFT JOIN borrowers b ON mq.borrowernumber = b.borrowernumber ". + "LEFT JOIN items i ON mqi.itemnumber = i.itemnumber ". + "WHERE b.cardnumber = ? ". + "AND i.barcode = ? ". + "AND mq.letter_code = ? ". + "AND mq.status = ? ". + "AND mq.message_transport_type = ? ". + ""); + + ##Check that there is a matching MessageQueue for each given test. + foreach my $check (@$checks) { + my @barcodes = map {my $a = $_; $a =~ s/\s+//gsm; $a;} split(',',$check->{containedBarcodes}); + foreach my $bc (@barcodes) { + my @params; + push @params, $check->{cardnumber}; + push @params, $bc; + push @params, $check->{lettercode}; + push @params, $check->{status}; + push @params, $check->{transport_type}; + $check_statement->execute( @params ); + my $ok = $check_statement->fetchrow(); + last unless ok(($ok && $ok == 1), "Check: For params @params"); + } + } +} +sub verifyMessageQueuesFromContentRegexp { + my ($checks) = @_; + + my $dbh = C4::Context->dbh(); + my $check_statement = $dbh->prepare( + "SELECT 1 FROM message_queue mq ". + "LEFT JOIN borrowers b ON mq.borrowernumber = b.borrowernumber ". + "WHERE b.cardnumber = ? ". + "AND mq.letter_code = ? ". + "AND mq.status = ? ". + "AND mq.message_transport_type = ? ". + "AND mq.content REGEXP ? ". + ""); + + ##Check that there is a matching MessageQueue for each given test. + foreach my $check (@$checks) { + my @params; + push @params, $check->{cardnumber}; + push @params, $check->{lettercode}; + push @params, $check->{status}; + push @params, $check->{transport_type}; + push @params, $check->{contentRegexp}; + $check_statement->execute( @params ); + my $ok = $check_statement->fetchrow(); + last unless ok(($ok && $ok == 1), "Check: For params @params"); + } +} + +1; diff --git a/t/Cucumber/SImpls/ScriptRunning.pm b/t/Cucumber/SImpls/ScriptRunning.pm new file mode 100644 index 0000000..4e51490 --- /dev/null +++ b/t/Cucumber/SImpls/ScriptRunning.pm @@ -0,0 +1,40 @@ +package SImpls::ScriptRunning; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +sub runScript { + my $C = shift; + my $scriptPath = $C->matches()->[0]; + $scriptPath =~ s/\$KOHA_PATH/$ENV{KOHA_PATH}/gsm; + my $params = $C->data(); + + my @args = ($scriptPath); + while (my ($param, $value) = each(%{$params->[0]})) { + push @args, "$param $value"; + } + + my $retval = system("@args"); + + ok(($retval == 0), "System call\n@args\nsucceeded"); +} + +1; diff --git a/t/Cucumber/SImpls/SystemPreferences.pm b/t/Cucumber/SImpls/SystemPreferences.pm new file mode 100644 index 0000000..d4d8fd8 --- /dev/null +++ b/t/Cucumber/SImpls/SystemPreferences.pm @@ -0,0 +1,63 @@ +package SImpls::SystemPreferences; + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Carp; +use Test::More; + +use C4::Context; + +sub setSystemPreferences { + my $C = shift; + + my $data = $C->data(); + foreach my $syspref (@$data) { + setSystemPreference($C, $syspref->{systemPreference}, $syspref->{value}); + } +} +sub setSystemPreference { + my ($C, $key, $value) = @_; + my $S = $C->{stash}->{scenario}; + my $F = $C->{stash}->{feature}; + + $S->{originalSystempreferences} = {} unless $S->{originalSystempreferences}; + $F->{originalSystempreferences} = {} unless $F->{originalSystempreferences}; + + if ($S->{originalSystempreferences}->{$key} && $F->{originalSystempreferences}->{$key}) { + #This syspref has been set from somewhere already, so let's not remove the stored original value. + } + else { + my $oldSysprefValue = C4::Context->preference($key); + $S->{originalSystempreferences}->{$key} = $oldSysprefValue; + $F->{originalSystempreferences}->{$key} = $oldSysprefValue; + } + C4::Context->set_preference($key, $value); +} + +sub rollbackSystemPreferences { + my $C = shift; + my $F = $C->{stash}->{feature}; + if (ref $F->{originalSystempreferences} eq 'HASH') { + while (my ($syspref, $value) = each(%{$F->{originalSystempreferences}})) { + C4::Context->set_preference($syspref, $value); + } + } +} + +1; diff --git a/t/Cucumber/runCucumberTests.t b/t/Cucumber/runCucumberTests.t new file mode 100755 index 0000000..468187a --- /dev/null +++ b/t/Cucumber/runCucumberTests.t @@ -0,0 +1,104 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; +use Test::BDD::Cucumber::Loader; +use Test::BDD::Cucumber::Harness::TestBuilder; +use Test::BDD::Cucumber::Model::TagSpec; + +use Getopt::Long qw(:config no_ignore_case); + +my $help; +my $featureRegexp; +my $verbose; +my @tags; + +GetOptions( + 'h|help' => \$help, + 'f|featureRegexp:s' => \$featureRegexp, + 'v|verbose:i' => \$verbose, + 't|tags:s' => \@tags, +); + +my $usage = <_new_userenv('DUMMY SESSION'); +C4::Context->set_userenv(0,0,0,'firstname','surname', 'CPL', 'Library 1', 0, '', ''); + + +###### # # # # # # # ###### +### Run the Cucumber tests. ### +###### # # # # # # # ###### +my ( $executor, @features ) = Test::BDD::Cucumber::Loader->load( '.' ); + +if ($featureRegexp) { + #Pick only the needed feature + my @filteredFeatures; + for (my $i=0 ; $i<@features ; $i++) { + my $feature = $features[$i]; + push @filteredFeatures, ($features[$i]) if $feature->name() =~ m/$featureRegexp/i; + } + @features = @filteredFeatures; +} + +my $tagSet; +if (scalar(@tags)) { + #Load given tags. + for (my $i=0 ; $i<@tags ; $i++) { + $tags[$i] =~ s/[@]//; + } + $tagSet = Test::BDD::Cucumber::Model::TagSpec->new({ + tags => [ and => @tags ], + }); +} + +my $harness = Test::BDD::Cucumber::Harness::TestBuilder->new({}); +$executor->execute( $_, $harness, $tagSet ) for @features; +done_testing; \ No newline at end of file diff --git a/t/Cucumber/steps/accountlines_steps.pl b/t/Cucumber/steps/accountlines_steps.pl new file mode 100644 index 0000000..c530a24 --- /dev/null +++ b/t/Cucumber/steps/accountlines_steps.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::Accountlines; + +Given qr/there are no previous fines/, sub { + SImpls::Accountlines::deleteAllFines(@_); +}; + +Then qr/the following fines are encumbered on naughty borrowers/, sub { + SImpls::Accountlines::checkAccountlines( @_ ); +}; diff --git a/t/Cucumber/steps/biblios_steps.pl b/t/Cucumber/steps/biblios_steps.pl new file mode 100644 index 0000000..4a61396 --- /dev/null +++ b/t/Cucumber/steps/biblios_steps.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::Biblios; + +Given qr/a set of Biblios/, sub { + SImpls::Biblios::addBiblios(@_); +}; diff --git a/t/Cucumber/steps/borrowers_steps.pl b/t/Cucumber/steps/borrowers_steps.pl new file mode 100644 index 0000000..fde92f2 --- /dev/null +++ b/t/Cucumber/steps/borrowers_steps.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::Borrowers; + +Given qr/a set of Borrowers/, sub { + SImpls::Borrowers::addBorrowers(@_); +}; + +Then qr/the following borrowers are debarred/, sub { + SImpls::Borrowers::checkDebarments( @_ ); +}; diff --git a/t/Cucumber/steps/branchTransfers_steps.pl b/t/Cucumber/steps/branchTransfers_steps.pl new file mode 100644 index 0000000..4daf915 --- /dev/null +++ b/t/Cucumber/steps/branchTransfers_steps.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::BranchTransfers; + +Then qr/the following Items are in-transit/, sub { + SImpls::BranchTransfers::verifyItemsInTransit(@_); +}; diff --git a/t/Cucumber/steps/common_steps.pl b/t/Cucumber/steps/common_steps.pl new file mode 100644 index 0000000..b07c915 --- /dev/null +++ b/t/Cucumber/steps/common_steps.pl @@ -0,0 +1,44 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + + + +When qr/all scenarios are executed, tear down database changes./, sub { + my $C = shift; + #Forcibly make a new schema, because the existing schema might get timeout and cause this tear down step to fail. + #Koha::Database->new_schema() just wraps around the C4::Context->dbh() which has already died :( + #So we must get the new schema forcibly. + my $schema = Koha::Schema->connect( sub { C4::Context->_new_dbh() }, { unsafe => 1 } ); + my $db = Koha::Database->new(); + $db->set_schema($schema); + + SImpls::BranchTransfers::deleteAllTransfers($C); + SImpls::Issues::deleteAllIssues($C); + SImpls::Borrowers::deleteBorrowers($C); + SImpls::Items::deleteItems($C); + SImpls::Accountlines::deleteAllFines($C); + SImpls::Biblios::deleteBiblios($C); + SImpls::MessageQueues::deleteAllMessageQueues($C); + SImpls::LetterTemplates::deleteLetterTemplates($C); + SImpls::SystemPreferences::rollbackSystemPreferences($C); +}; diff --git a/t/Cucumber/steps/context_steps.pl b/t/Cucumber/steps/context_steps.pl new file mode 100644 index 0000000..b32167e --- /dev/null +++ b/t/Cucumber/steps/context_steps.pl @@ -0,0 +1,30 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + + +use SImpls::Context; + +Given qr/the Koha-context, we can proceed with other scenarios./, sub { + my $C = shift; + SImpls::Context::setKohaContext($C); +}; \ No newline at end of file diff --git a/t/Cucumber/steps/fileUtils_steps.pl b/t/Cucumber/steps/fileUtils_steps.pl new file mode 100644 index 0000000..4725e98 --- /dev/null +++ b/t/Cucumber/steps/fileUtils_steps.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::FileUtils; + +Given qr/a file at '(.*?)'/, sub { + SImpls::FileUtils::findFile( @_ ); +}; + +Then qr/I have the following text inside these files/, sub { + SImpls::FileUtils::checkRegexpInsideFiles( @_ ); +}; diff --git a/t/Cucumber/steps/issues_steps.pl b/t/Cucumber/steps/issues_steps.pl new file mode 100644 index 0000000..68fa482 --- /dev/null +++ b/t/Cucumber/steps/issues_steps.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::Issues; + +Given qr/there are no previous issues/, sub { + SImpls::Issues::deleteAllIssues(@_); +}; + +Given qr/a set of overdue Issues, checked out from the Items' current holdingbranch/, sub { + my $C = shift; + SImpls::Issues::addIssues($C, 'holdingbranch'); +}; + +Given qr/a set of Issues, checked out from the Items' current '(\w+)'/, sub { + my $C = shift; + SImpls::Issues::addIssues($C, $C->matches()->[0]); +}; + +When qr/checked-out Items are checked-in to their '(\w+)'/, sub { + my $C = shift; + SImpls::Issues::checkInIssues($C, $C->matches()->[0]); +}; diff --git a/t/Cucumber/steps/items_steps.pl b/t/Cucumber/steps/items_steps.pl new file mode 100644 index 0000000..ed1fe1c --- /dev/null +++ b/t/Cucumber/steps/items_steps.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::Items; + +Given qr/a set of Items/, sub { + SImpls::Items::addItems(@_); +}; diff --git a/t/Cucumber/steps/letterTemplates_steps.pl b/t/Cucumber/steps/letterTemplates_steps.pl new file mode 100644 index 0000000..4d28b2a --- /dev/null +++ b/t/Cucumber/steps/letterTemplates_steps.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::LetterTemplates; + +Given qr/a set of letter templates/, sub { + SImpls::LetterTemplates::addLetterTemplates(@_); +}; diff --git a/t/Cucumber/steps/messageQueue_steps.pl b/t/Cucumber/steps/messageQueue_steps.pl new file mode 100644 index 0000000..18e867f --- /dev/null +++ b/t/Cucumber/steps/messageQueue_steps.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::MessageQueues; + +Given qr/a bunch of message_queue-rows using letter code '(.*?)' and message_transport_type '(.*?)' based on the given Borrowers, Biblios, Items and Issues/, sub { + my $C = shift; + my $letterCode = $1; + my $messageTransportType = $2; + SImpls::MessageQueues::addMessageQueues($C, $letterCode, $messageTransportType); +}; + +Then qr/I have the following enqueued message queue items/, sub { + SImpls::MessageQueues::verifyMessageQueueItems( @_ ); +}; + +Then qr/I have the following message queue notices/, sub { + SImpls::MessageQueues::verifyMessageQueues( @_ ); +}; diff --git a/t/Cucumber/steps/scriptRunning_steps.pl b/t/Cucumber/steps/scriptRunning_steps.pl new file mode 100644 index 0000000..8c5ad75 --- /dev/null +++ b/t/Cucumber/steps/scriptRunning_steps.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::ScriptRunning; + +When qr/I run '(.*?)'-script with the following parameters/, sub { + SImpls::ScriptRunning::runScript( @_ ); +}; diff --git a/t/Cucumber/steps/systempreferences_steps.pl b/t/Cucumber/steps/systempreferences_steps.pl new file mode 100644 index 0000000..34feef8 --- /dev/null +++ b/t/Cucumber/steps/systempreferences_steps.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Copyright Vaara-kirjastot 2015 +# +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; +use Test::More; +use Test::BDD::Cucumber::StepFile; + +use SImpls::SystemPreferences; + +Given qr/the following system preferences/, sub { + SImpls::SystemPreferences::setSystemPreferences( @_ ); +}; diff --git a/t/Cucumber/testsStructuring.tutorial b/t/Cucumber/testsStructuring.tutorial new file mode 100644 index 0000000..74daf18 --- /dev/null +++ b/t/Cucumber/testsStructuring.tutorial @@ -0,0 +1,60 @@ + +--<>--<>--<>--<>-<>--<>--<>--<>--+ +<> Writing Cucumber tests for Koha <> + +--<>--<>--<>--<>-<>--<>--<>--<>--+ + ++--- +| Cucumber tests are split into 4 different types of files: )==> ++--- + +1. Feature definitions, features/*/*.feature + These are the user stories which trigger the Cucumber steps. + +2. step-definitions, steps/*_steps.pl + These files define the Cucumber steps and depend only on the step implementations to mimize coupling. + +3. step implementations, SImpls/*/*.pm + These files implement the steps defined in steps-folder, which are not using browser tests. This is all + back-end stuff, like cronjobs, basic CRUD for features, etc. + +4. Page Objects, POs/*/*.pm + These files implement the browser based steps which test interface functionality. + + ++--- +| Tests are structured into the following organization: )==> ++--- + +Step-files can be easily moved around so for now we can just put them in the steps-folder. + t/Cucumber/steps/borrower_steps.pl + t/Cucumber/steps/borrower_rest_steps.pl + t/Cucumber/steps/borrower_po_steps.pl + t/Cucumber/steps/issue_steps.pl + t/Cucumber/steps/biblio_rest_steps.pl + +A Cucumber best-practice is to separate code from the step definitions, and with the step +implementations we mimick the Koha module hierarchy. + t/Cucumber/SImpls/Borrowers/Borrowers.pm + t/Cucumber/SImpls/Borrowers/MessagePreferences.pm + t/Cucumber/SImpls/FloatingMatrix/FloatingMatrix.pm + +To test the web pages, we should use a Page Object Pattern, in which each page, like +members/memberentry.pl gets it's own matching object to abstract the HTML elements under +object methods. Thus writing tests is much easier and features are encapsulated better. So it +is easier to maintain interface tests when change happens. +Here we mimic the Koha-controllers hierarchy with the goal of each Page Object matching a Koha controller, +like members/memberentry.pl + t/Cucumber/POs/Borrowers/MemberEntry.pm + t/Cucumber/POs/Borrowers/Member.pm + t/Cucumber/POs/Borrowers/PrintSlip.pm + t/Cucumber/POs/Biblios/AddBiblio.pm + (we can put REST tests object first like "POs/Borrowers/RESTv1.pm" as an alternative) + t/Cucumber/POs/REST/v1/Biblios.pm + t/Cucumber/POs/REST/v1/Borrowers.pm + +Feature definitions are easily movable to another hierarchy, so we just for now gather them +under top-level modules. + t/Cucumber/features/Overdues/calendarCRUD.feature + t/Cucumber/features/Overdues/printProviderLimbo.feature + t/Cucumber/features/Borrowers/borrowersCRUD.feature + t/Cucumber/features/FloatingMatrix/floatingMatrixCRUD.feature + t/Cucumber/features/FloatingMatrix/floatingMatrix.feature -- 1.7.9.5