Bug 35922: Fix www/batch.t
[koha.git] / t / db_dependent / www / batch.t
1 #!/usr/bin/perl
2
3 # Copyright 2012 C & P Bibliography Services
4 # Copyright 2017 Koha Development Team
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use utf8;
22 use Test::More; #See plan tests => \d+ below
23 use Test::WWW::Mechanize;
24 use XML::Simple;
25 use JSON;
26 use File::Basename;
27 use File::Spec;
28 use POSIX;
29 use t::lib::Mocks::Zebra;
30 use Koha::BackgroundJobs;
31
32 my $testdir = File::Spec->rel2abs( dirname(__FILE__) );
33
34 my $koha_conf = $ENV{KOHA_CONF};
35 my $xml       = XMLin($koha_conf);
36
37 use C4::Context;
38 my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
39
40 my $file =
41   $marcflavour eq 'UNIMARC'
42   ? "$testdir/data/unimarcrecord.mrc"
43   : "$testdir/data/marc21record.mrc";
44
45 my $user     = $ENV{KOHA_USER} || $xml->{config}->{user};
46 my $password = $ENV{KOHA_PASS} || $xml->{config}->{pass};
47 my $intranet = $ENV{KOHA_INTRANET_URL};
48
49 if (not defined $intranet) {
50     plan skip_all =>
51          "You must set the environment variable KOHA_INTRANET_URL to ".
52          "point this test to your staff interface. If you do not have ".
53          "KOHA_CONF set, you must also set KOHA_USER and KOHA_PASS for ".
54          "your username and password";
55 }
56 else {
57     plan tests => 24;
58 }
59
60 $intranet =~ s#/$##;
61
62 my $mock_zebra = t::lib::Mocks::Zebra->new(
63     {
64         intranet  => $intranet,
65         koha_conf => $ENV{KOHA_CONF},
66     }
67 );
68
69 my $import_batch_id = $mock_zebra->load_records_ui($file);
70
71 my $bookdescription;
72 if ( $marcflavour eq 'UNIMARC' ) {
73     $bookdescription = 'Jeffrey Esakov et Tom Weiss';
74 }
75 else {
76     $bookdescription = 'Data structures';
77 }
78
79 my $agent = Test::WWW::Mechanize->new( autocheck => 1 );
80 $agent->get_ok( "$intranet/cgi-bin/koha/mainpage.pl", 'connect to intranet' );
81 $agent->form_name('loginform');
82 $agent->field( 'password', $password );
83 $agent->field( 'userid',   $user );
84 $agent->field( 'branch',   '' );
85 $agent->click_ok( '', 'login to staff interface' );
86
87 # Get datatable for the batch id
88 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
89 my $jsonresponse = decode_json $agent->content;
90 like( $jsonresponse->{data}[0]->{citation}, qr/$bookdescription/, 'found book' );
91 is( $jsonresponse->{data}[0]->{status},         'imported', 'record marked as staged' );
92 is( $jsonresponse->{data}[0]->{overlay_status}, 'no_match', 'record has no matches' );
93
94 my $biblionumber = $jsonresponse->{data}[0]->{matched};
95
96 $agent->get_ok(
97     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
98     'getting imported bib' );
99 $agent->content_contains( 'Details for ' . $bookdescription,
100     'bib is imported' );
101
102 $agent->get("$intranet/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=$import_batch_id");
103 $agent->form_number(5);
104 $agent->click_ok( 'mainformsubmit', "revert import" );
105
106 sleep(1);
107 # FIXME - This if fragile and can fail if there is a race condition
108 my $job = Koha::BackgroundJobs->search({ type => 'marc_import_revert_batch' })->last;
109 my $i;
110 while ( $job->discard_changes->status ne 'finished' ) {
111     sleep(1);
112     last if ++$i > 10;
113 }
114 is ( $job->status, 'finished', 'job is finished' );
115
116 $agent->get_ok(
117     "$intranet/cgi-bin/koha/catalogue/detail.pl?biblionumber=$biblionumber",
118     'getting reverted bib' );
119 $agent->content_contains( 'The record you requested does not exist',
120     'bib is gone' );
121
122 $agent->get("$intranet/cgi-bin/koha/tools/batch_records_ajax.pl?import_batch_id=$import_batch_id");
123 $jsonresponse = decode_json $agent->content;
124 is( $jsonresponse->{data}[0]->{status}, 'reverted', 'record marked as reverted' );
125