Bug 24545: Fix license statements
[srvgit] / t / db_dependent / www / batch.t
index 13e5010..b2b48ed 100644 (file)
@@ -1,26 +1,26 @@
 #!/usr/bin/perl
 
 # Copyright 2012 C & P Bibliography Services
+# Copyright 2017 Koha Development Team
 #
-# 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.
+# 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.
 #
-# 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
+# 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 <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
+
 use utf8;
-use Test::More tests => 20;
+use Test::More; #See plan tests => \d+ below
 use Test::WWW::Mechanize;
-use Data::Dumper;
 use XML::Simple;
 use JSON;
 use File::Basename;
@@ -44,15 +44,19 @@ my $file =
 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 ".
+if (not defined $intranet) {
+    plan skip_all =>
+         "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;
+         "your username and password";
+}
+else {
+    plan tests => 26;
+}
 
 $intranet =~ s#/$##;
-$opac     =~ s#/$##;
 
 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
 my $jsonresponse;
@@ -71,7 +75,7 @@ $agent->follow_link_ok( { text => 'Stage MARC records for import' },
     'go to stage MARC' );
 
 $agent->post(
-    "$intranet/cgi-bin/koha/tools/upload-file.pl",
+    "$intranet/cgi-bin/koha/tools/upload-file.pl?temp=1",
     [ 'fileToUpload' => [$file], ],
     'Content_Type' => 'form-data',
 );
@@ -93,9 +97,10 @@ $agent->submit_form_ok(
             'item_action'     => 'always_add',
             'matcher'         => '',
             'comments'        => '',
-            'encoding'        => 'utf8',
+            'encoding'        => 'UTF-8',
             'parse_items'     => '1',
             'runinbackground' => '1',
+            'record_type'     => 'biblio'
         }
     },
     'stage MARC'
@@ -110,9 +115,8 @@ 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"
-    );
+        "$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;
@@ -146,16 +150,18 @@ $agent->submit_form_ok(
             'item_action'     => 'always_add',
             'matcher'         => '1',
             'comments'        => '',
-            'encoding'        => 'utf8',
+            'encoding'        => 'UTF-8',
             'parse_items'     => '1',
             'runinbackground' => '1',
             'completedJobID'  => $jobID,
+            'record_type'     => 'biblio'
         }
     },
     'stage MARC'
 );
 
 $agent->follow_link_ok( { text => 'Manage staged records' }, 'view batch' );
+
 my $bookdescription;
 if ( $marcflavour eq 'UNIMARC' ) {
     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
@@ -163,24 +169,49 @@ if ( $marcflavour eq 'UNIMARC' ) {
 else {
     $bookdescription = 'Data structures';
 }
-$agent->content_contains( $bookdescription, 'found book' );
-$agent->form_number(5);
+
+# Save the staged records URI for later use
+my $staged_records_uri = $agent->uri;
+
+my $import_batch_id = ( split( '=', $staged_records_uri->as_string ) )[-1];
+# Get datatable for the batch id
+$agent->get_ok(
+    "$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id",
+    'get the datatable for the new batch id'
+);
+$jsonresponse = decode_json $agent->content;
+like( $jsonresponse->{ aaData }[0]->{ citation }, qr/$bookdescription/, 'found book' );
+is( $jsonresponse->{ aaData }[0]->{ status }, 'staged', 'record marked as staged' );
+is( $jsonresponse->{ aaData }[0]->{ overlay_status }, 'no_match', 'record has no matches' );
+
+# Back to the manage staged records page
+$agent->get($staged_records_uri);
+$agent->form_number(6);
 $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->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
+$jsonresponse = decode_json $agent->content;
+is( $jsonresponse->{ aaData }[0]->{ status }, 'imported', 'record marked as imported' );
+
+my $biblionumber = $jsonresponse->{aaData}[0]->{matched};
+
+$agent->get_ok(
+    "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
+    'getting imported bib' );
+$agent->content_contains( 'Details for ' . $bookdescription,
+    'bib is imported' );
+
+$agent->get($staged_records_uri);
+$agent->form_number(5);
 $agent->click_ok( 'mainformsubmit', "revert import" );
 $agent->get_ok(
-    "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$newbib",
+    "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
     'getting reverted bib' );
 $agent->content_contains( 'The record you requested does not exist',
     'bib is gone' );
+
+$agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
+$jsonresponse = decode_json $agent->content;
+is( $jsonresponse->{ aaData }[0]->{ status }, 'reverted', 'record marked as reverted' );
+