|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha. |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
use Modern::Perl; |
| 18 |
use Test::More tests => 1; |
| 19 |
use Test::MockModule; |
| 20 |
use Test::Warn; |
| 21 |
use Test::WWW::Mechanize; |
| 22 |
require HTTP::Request; |
| 23 |
use MARC::Record; |
| 24 |
use XML::Simple; |
| 25 |
use C4::MarcModificationTemplates; |
| 26 |
use C4::Biblio; |
| 27 |
|
| 28 |
eval{ |
| 29 |
use C4::Context; |
| 30 |
}; |
| 31 |
if ($@) { |
| 32 |
plan skip_all => "Tests skip. You must have a working Context\n"; |
| 33 |
} |
| 34 |
|
| 35 |
my $koha_conf = $ENV{KOHA_CONF}; |
| 36 |
my $koha_conf_xml = XMLin($koha_conf); |
| 37 |
|
| 38 |
my $user = $ENV{KOHA_USER} || $koha_conf_xml->{config}->{user}; |
| 39 |
my $password = $ENV{KOHA_PASS} || $koha_conf_xml->{config}->{pass}; |
| 40 |
my $intranet = $ENV{KOHA_INTRANET_URL}; |
| 41 |
|
| 42 |
if (not defined $intranet) { |
| 43 |
plan skip_all => "Tests skipped. You must set environment variable KOHA_INTRANET_URL to run tests\n"; |
| 44 |
} |
| 45 |
|
| 46 |
subtest 'Templates applied using simple and advanced MARC Editor' => sub { |
| 47 |
plan tests => 13; |
| 48 |
|
| 49 |
# Create "Test" MARC modification template |
| 50 |
my $template_id = AddModificationTemplate('TEST'); |
| 51 |
ok($template_id, 'MARC modification template successfully created'); |
| 52 |
my $add_template_action_result = AddModificationTemplateAction( |
| 53 |
$template_id, |
| 54 |
'update_field', # Action |
| 55 |
0, |
| 56 |
250, # Field |
| 57 |
'a', # Subfield |
| 58 |
'251 bottles of beer on the wall', # Value |
| 59 |
'', |
| 60 |
'', |
| 61 |
'', |
| 62 |
'', |
| 63 |
'', |
| 64 |
'', |
| 65 |
'', |
| 66 |
'', |
| 67 |
'', |
| 68 |
'', |
| 69 |
0, |
| 70 |
'' |
| 71 |
); |
| 72 |
ok($add_template_action_result, 'MARC modification template action successfully created'); |
| 73 |
my $record = MARC::Record->new(); |
| 74 |
$record->leader(' nam a22 7a 4500'); |
| 75 |
$record->append_fields( |
| 76 |
MARC::Field->new('001', '12345'), |
| 77 |
MARC::Field->new('003', 'TEST'), |
| 78 |
MARC::Field->new('245', '1', '0', 'a' => 'TEST'), |
| 79 |
MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall'), |
| 80 |
# @FIXME: Create test item type? Not super safe to rely on existing 'BK' type |
| 81 |
MARC::Field->new('942', '','', 'c' => 'BK'), |
| 82 |
); |
| 83 |
my ($biblionumber) = AddBiblio($record, ''); |
| 84 |
|
| 85 |
my $saved_record = GetMarcBiblio($biblionumber, 0); |
| 86 |
my $saved_record_250_field = $saved_record->field('250'); |
| 87 |
isa_ok($saved_record_250_field, 'MARC::Field', 'Field with tag 250 has been saved'); |
| 88 |
is($saved_record_250_field->subfield('a'), '250 bottles of beer on the wall', 'Field 250a has the same value passed to AddBiblio'); |
| 89 |
|
| 90 |
my $old_template_preference = C4::Context->preference('EditBiblioMarcModificationTemplate'); |
| 91 |
C4::Context->set_preference('EditBiblioMarcModificationTemplate', 'TEST'); |
| 92 |
|
| 93 |
my $agent = Test::WWW::Mechanize->new(autocheck => 1); |
| 94 |
|
| 95 |
$agent->get_ok("$intranet/cgi-bin/koha/mainpage.pl", 'Connect to intranet'); |
| 96 |
$agent->form_name('loginform'); |
| 97 |
$agent->field('password', $password); |
| 98 |
$agent->field('userid', $user); |
| 99 |
$agent->field('branch', ''); |
| 100 |
$agent->click_ok('', 'Login to staff client'); |
| 101 |
|
| 102 |
$agent->get_ok("$intranet/cgi-bin/koha/mainpage.pl", 'Load main page'); #FIXME: Remove? |
| 103 |
$agent->get_ok("$intranet/cgi-bin/koha/cataloguing/addbiblio.pl?biblionumber=$biblionumber", 'Load bibliographic record in simple MARC editor'); |
| 104 |
$agent->submit_form_ok( |
| 105 |
{ |
| 106 |
form_id => 'f', |
| 107 |
button => '', |
| 108 |
}, |
| 109 |
'Save bibliographic record using simple MARC editor' |
| 110 |
); |
| 111 |
|
| 112 |
$saved_record = GetMarcBiblio($biblionumber, 0); |
| 113 |
$saved_record_250_field = $saved_record->field('250'); |
| 114 |
is($saved_record_250_field->subfield('a'), '251 bottles of beer on the wall', 'Field with tag 250 has been modified by MARC modification template'); |
| 115 |
|
| 116 |
# FIXME: Don't really need to to this, but could be useful so |
| 117 |
# test below does not succed by accident |
| 118 |
$saved_record->delete_fields($saved_record_250_field); |
| 119 |
$saved_record->insert_fields_ordered(MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall')); |
| 120 |
$biblionumber = ModBiblioMarc($saved_record, $biblionumber); |
| 121 |
ok($biblionumber, 'Field with tag 250 has been restored to original value'); |
| 122 |
|
| 123 |
# @FIXME: Leader record status n or c? |
| 124 |
my $record_xml = <<'EOF'; |
| 125 |
<?xml version="1.0" standalone='yes'?> |
| 126 |
<record xmlns="http://www.loc.gov/MARC21/slim"> |
| 127 |
<leader> cam a22 7a 4500</leader> |
| 128 |
<controlfield tag="001">12345</controlfield> |
| 129 |
<controlfield tag="003">TEST</controlfield> |
| 130 |
<datafield tag="245" ind1="1" ind2="0"> |
| 131 |
<subfield code="a">TEST</subfield> |
| 132 |
</datafield> |
| 133 |
<datafield tag="250" ind1=" " ind2=" "> |
| 134 |
<subfield code="a">250 bottles of beer on the wall</subfield> |
| 135 |
</datafield> |
| 136 |
<datafield tag="942" ind1=" " ind2=" "> |
| 137 |
<subfield code="c">BK</subfield> |
| 138 |
<subfield code="2">ddc</subfield> |
| 139 |
</datafield></record> |
| 140 |
EOF |
| 141 |
my $headers = HTTP::Headers->new( |
| 142 |
Content_Type => 'text/xml', |
| 143 |
); |
| 144 |
my $request = HTTP::Request->new('POST', "$intranet/cgi-bin/koha/svc/bib/$biblionumber", $headers, $record_xml); |
| 145 |
my $response = $agent->request($request); |
| 146 |
|
| 147 |
is($response->code, '200', 'Emulate save in advanced MARC editor, Koha REST API responded with 200 for update biblio request'); |
| 148 |
like($response->decoded_content, qr/251 bottles of beer on the wall/, 'Field with tag 250 has been modified by MARC modification template'); |
| 149 |
|
| 150 |
# Clean up |
| 151 |
DelBiblio($biblionumber); |
| 152 |
DelModificationTemplate($template_id); |
| 153 |
C4::Context->set_preference('EditBiblioMarcModificationTemplate', $old_template_preference); |
| 154 |
}; |