From 201dab65a54c63243bd49af0fc6925eecb135eec Mon Sep 17 00:00:00 2001 From: Jared Camins-Esakov Date: Sat, 23 Jun 2012 16:55:43 -0400 Subject: [PATCH] Bug 8300: Add mechanized unit test for batch import This new unit test confirms that importing records via the Stage MARC import tool still works by connecting to a Koha instance and importing a record then reverting the import, checking at each step of the way that everything is as it should be. To test: 1. Install Test::WWW::Mechanize > sudo apt-get install libtest-www-mechanize-perl 2. Set environment variables to reflect your Koha instance: > export KOHA_USER=kohaadmin > export KOHA_PASS=katikoan > export KOHA_INTRANET_URL=http://localhost:8080 > export KOHA_OPAC_URL=http://localhost 3. Run the test: > prove t/db_dependent/www/batch.t This updated patch now handles both MARC21 and UNIMARC installations. Signed-off-by: Magnus Enger All tests pass with marcflavour = MARC21 and NORMARC. Test #15 fails when marcflavour = UNIMARC, but from what I can see, that is because I'm testing on a MARC21 setup, missing the UNIMARC frameworks etc. I'm signing off - QA folks, please ask for a proper UNIMARC signoff if you feel it is needed. Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart Amended patch: Load the POSIX module (for the floor routine). Signed-off-by: Jared Camins-Esakov (cherry picked from commit bc2ab3b5882e664d91ce086ce96dd32c7452dee4) Signed-off-by: Jared Camins-Esakov --- t/db_dependent/www/batch.t | 186 ++++++++++++++++++++++ t/db_dependent/www/data/marc21record.mrc | 1 + t/db_dependent/www/data/unimarcrecord.mrc | 1 + 3 files changed, 188 insertions(+) create mode 100644 t/db_dependent/www/batch.t create mode 100644 t/db_dependent/www/data/marc21record.mrc create mode 100644 t/db_dependent/www/data/unimarcrecord.mrc diff --git a/t/db_dependent/www/batch.t b/t/db_dependent/www/batch.t new file mode 100644 index 0000000000..13e5010ac4 --- /dev/null +++ b/t/db_dependent/www/batch.t @@ -0,0 +1,186 @@ +#!/usr/bin/perl + +# Copyright 2012 C & P Bibliography Services +# +# This 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. +# +# This 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., 59 Temple Place, +# Suite 330, Boston, MA 02111-1307 USA +# + +use Modern::Perl; +use utf8; +use Test::More tests => 20; +use Test::WWW::Mechanize; +use Data::Dumper; +use XML::Simple; +use JSON; +use File::Basename; +use File::Spec; +use POSIX; + +my $testdir = File::Spec->rel2abs( dirname(__FILE__) ); + +my $koha_conf = $ENV{KOHA_CONF}; +my $xml = XMLin($koha_conf); + +use C4::Context; +my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21'; + +# For the purpose of this test, we can reasonably take MARC21 and NORMARC to be the same +my $file = + $marcflavour eq 'UNIMARC' + ? "$testdir/data/unimarcrecord.mrc" + : "$testdir/data/marc21record.mrc"; + +my $user = $ENV{KOHA_USER} || $xml->{config}->{user}; +my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass}; +my $intranet = $ENV{KOHA_INTRANET_URL}; +my $opac = $ENV{KOHA_OPAC_URL}; + +BAIL_OUT("You must set the environment variable KOHA_INTRANET_URL to ". + "point this test to your staff client. If you do not have ". + "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ". + "your username and password") unless $intranet; + +$intranet =~ s#/$##; +$opac =~ s#/$##; + +my $agent = Test::WWW::Mechanize->new( autocheck => 1 ); +my $jsonresponse; + +$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' ); +$agent->form_name('loginform'); +$agent->field( 'password', $password ); +$agent->field( 'userid', $user ); +$agent->field( 'branch', '' ); +$agent->click_ok( '', 'login to staff client' ); + +$agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'load main page' ); + +$agent->follow_link_ok( { url_regex => qr/tools-home/i }, 'open tools module' ); +$agent->follow_link_ok( { text => 'Stage MARC records for import' }, + 'go to stage MARC' ); + +$agent->post( + "$intranet/cgi-bin/koha/tools/upload-file.pl", + [ 'fileToUpload' => [$file], ], + 'Content_Type' => 'form-data', +); +ok( $agent->success, 'uploaded file' ); + +$jsonresponse = decode_json $agent->content(); +is( $jsonresponse->{'status'}, 'done', 'upload succeeded' ); +my $fileid = $jsonresponse->{'fileid'}; + +$agent->get_ok( "$intranet/cgi-bin/koha/tools/stage-marc-import.pl", + 'reopen stage MARC page' ); +$agent->submit_form_ok( + { + form_number => 5, + fields => { + 'uploadedfileid' => $fileid, + 'nomatch_action' => 'create_new', + 'overlay_action' => 'replace', + 'item_action' => 'always_add', + 'matcher' => '', + 'comments' => '', + 'encoding' => 'utf8', + 'parse_items' => '1', + 'runinbackground' => '1', + } + }, + 'stage MARC' +); + +$jsonresponse = decode_json $agent->content(); +my $jobID = $jsonresponse->{'jobID'}; +ok( $jobID, 'have job ID' ); + +my $completed = 0; + +# if we haven't completed the batch in two minutes, it's not happening +for my $counter ( 1 .. 24 ) { + $agent->get( + "$intranet/cgi-bin/koha/tools/background-job-progress.pl?jobID=$jobID", + "get job progress" + ); + $jsonresponse = decode_json $agent->content(); + if ( $jsonresponse->{'job_status'} eq 'completed' ) { + $completed = 1; + last; + } + warn( + ( + $jsonresponse->{'job_size'} + ? floor( + 100 * $jsonresponse->{'progress'} / $jsonresponse->{'job_size'} + ) + : '100' + ) + . "% completed" + ); + sleep 5; +} +is( $jsonresponse->{'job_status'}, 'completed', 'job was completed' ); + +$agent->get_ok( + "$intranet/cgi-bin/koha/tools/stage-marc-import.pl", + 'reopen stage MARC page at end of upload' +); +$agent->submit_form_ok( + { + form_number => 5, + fields => { + 'uploadedfileid' => $fileid, + 'nomatch_action' => 'create_new', + 'overlay_action' => 'replace', + 'item_action' => 'always_add', + 'matcher' => '1', + 'comments' => '', + 'encoding' => 'utf8', + 'parse_items' => '1', + 'runinbackground' => '1', + 'completedJobID' => $jobID, + } + }, + 'stage MARC' +); + +$agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' ); +my $bookdescription; +if ( $marcflavour eq 'UNIMARC' ) { + $bookdescription = 'Jeffrey Esakov et Tom Weiss'; +} +else { + $bookdescription = 'Data structures'; +} +$agent->content_contains( $bookdescription, 'found book' ); +$agent->form_number(5); +$agent->field( 'framework', '' ); +$agent->click_ok( 'mainformsubmit', "imported records into catalog" ); +my $newbib; +foreach my $link ( $agent->links() ) { + if ( $link->url() =~ m#/cgi-bin/koha/catalogue/detail.pl\?biblionumber=# ) { + $newbib = $link->text(); + $agent->link_content_like( [$link], qr/$bookdescription/, + 'successfully imported record' ); + last; + } +} + +$agent->form_number(4); +$agent->click_ok( 'mainformsubmit', "revert import" ); +$agent->get_ok( + "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$newbib", + 'getting reverted bib' ); +$agent->content_contains( 'The record you requested does not exist', + 'bib is gone' ); diff --git a/t/db_dependent/www/data/marc21record.mrc b/t/db_dependent/www/data/marc21record.mrc new file mode 100644 index 0000000000..b93f6b691c --- /dev/null +++ b/t/db_dependent/www/data/marc21record.mrc @@ -0,0 +1 @@ +01002pam a2200289 a 4500001000800000005001700008008004100025035002100066906004500087010001700132020002500149040001800174050002600192082001700218100002100235245008200256260005300338300003300391440003400424504003000458500002000488650003400508650003900542700001600581991005200597991006300649338389719890317122103.8880830s1989 njua b 001 0 eng  9(DLC) 88028856 a7bcbccorignewd1eocipf19gy-gencatlg a 88028856  a0131988476 :c$27.00 aDLCcDLCdDLC00aQA76.73.C15bE83 198900a005.13/32191 aEsakov, Jeffrey.10aData structures :ban advanced approach using C /cJeffrey Esakov, Tom Weiss. aEnglewood Cliffs, N.J. :bPrentice Hall,cc1989. axi, 372 p. :bill. ;c25 cm. 0aPrentice Hall software series aBibliography: p. 361-366. aIncludes index. 0aC (Computer program language) 0aData structures (Computer science)1 aWeiss, Tom. bc-GenCollhQA76.73.C15iE83 1989tCopy 1wBOOKS bc-GenCollhQA76.73.C15iE83 1989p00009695734tCopy 2wCCF \ No newline at end of file diff --git a/t/db_dependent/www/data/unimarcrecord.mrc b/t/db_dependent/www/data/unimarcrecord.mrc new file mode 100644 index 0000000000..7e32632a3f --- /dev/null +++ b/t/db_dependent/www/data/unimarcrecord.mrc @@ -0,0 +1 @@ +00413 2200121 45000010005000000100023000050900009000281000041000372000166000782100016002442150023002601010008002835001 a2-12-486514-5bbr. a5001 a19951005d1994 m y0frey50 ba aStructures de donnéesbLIVReune approche avancée utilisant CfJeffrey Esakov et Tom Weissgtrad. française, Clotilde Fermautgrévision, Emmanuel Fermaut cAFNORd1994 aXIII-382 p.d24 cm afre \ No newline at end of file -- 2.39.5