From d6ee5112a313a7a1806cef0b7e620f36baa299b4 Mon Sep 17 00:00:00 2001
From: Jared Camins-Esakov <jcamins@cpbibliography.com>
Date: Sat, 23 Jun 2012 16:55:43 -0400
Subject: [PATCH] Bug 8300: Add mechanized unit test for batch import
Content-Type: text/plain; charset="UTF-8"

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
---
 t/db_dependent/www/batch.t         |  127 ++++++++++++++++++++++++++++++++++++
 t/db_dependent/www/data/record.mrc |    1 +
 2 files changed, 128 insertions(+), 0 deletions(-)
 create mode 100644 t/db_dependent/www/batch.t
 create mode 100644 t/db_dependent/www/data/record.mrc

diff --git a/t/db_dependent/www/batch.t b/t/db_dependent/www/batch.t
new file mode 100644
index 0000000..3ff44c8
--- /dev/null
+++ b/t/db_dependent/www/batch.t
@@ -0,0 +1,127 @@
+#!/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 Test::More tests => 19;
+use Test::WWW::Mechanize;
+use Data::Dumper;
+use XML::Simple;
+use JSON;
+use File::Basename;
+use File::Spec;
+
+my $testdir = File::Spec->rel2abs(dirname(__FILE__));
+
+my $koha_conf = $ENV{KOHA_CONF};
+my $xml = XMLin( $koha_conf );
+
+
+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};
+
+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({ text => 'Tools' }, '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' => ["$testdir/data/record.mrc"],
+        ],
+        '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;
+    }
+    print ($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');
+$agent->content_contains('Data structures : Esakov, Jeffrey', '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/Data structures/, '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/record.mrc b/t/db_dependent/www/data/record.mrc
new file mode 100644
index 0000000..b93f6b6
--- /dev/null
+++ b/t/db_dependent/www/data/record.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
-- 
1.7.2.5