|
Line 0
Link Here
|
|
|
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 under the |
| 6 |
# terms of the GNU General Public License as published by the Free Software |
| 7 |
# Foundation; either version 3 of the License, or (at your option) any later |
| 8 |
# version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License along |
| 15 |
# with Koha; if not, see <http://www.gnu.org/licenses>. |
| 16 |
|
| 17 |
use Modern::Perl; |
| 18 |
|
| 19 |
use Test::NoWarnings; |
| 20 |
use Test::More tests => 10; |
| 21 |
use Test::Warn; |
| 22 |
use JSON qw( decode_json ); |
| 23 |
|
| 24 |
use File::Basename; |
| 25 |
|
| 26 |
use t::lib::Mocks; |
| 27 |
use t::lib::TestBuilder; |
| 28 |
|
| 29 |
BEGIN { |
| 30 |
# Mock pluginsdir before loading Plugins module |
| 31 |
my $path = dirname(__FILE__) . '/../../../lib/plugins'; |
| 32 |
t::lib::Mocks::mock_config( 'pluginsdir', $path ); |
| 33 |
|
| 34 |
use_ok('Koha::Plugins'); |
| 35 |
use_ok('Koha::Plugins::Handler'); |
| 36 |
use_ok('Koha::Plugin::Test'); |
| 37 |
|
| 38 |
use_ok('Koha::BackgroundJobs'); |
| 39 |
use_ok('Koha::BackgroundJob::MARCImportCommitBatch'); |
| 40 |
use_ok('Koha::BackgroundJob::StageMARCForImport'); |
| 41 |
} |
| 42 |
|
| 43 |
my $schema = Koha::Database->new->schema; |
| 44 |
my $builder = t::lib::TestBuilder->new; |
| 45 |
|
| 46 |
t::lib::Mocks::mock_config( 'enable_plugins', 1 ); |
| 47 |
|
| 48 |
|
| 49 |
subtest 'before_batch_action and after_batch_action hooks with after_biblio_action hook tests' => sub { |
| 50 |
|
| 51 |
plan tests => 1; |
| 52 |
|
| 53 |
$schema->storage->txn_begin; |
| 54 |
|
| 55 |
my $plugins = Koha::Plugins->new; |
| 56 |
$plugins->InstallPlugins; |
| 57 |
|
| 58 |
my $plugin = Koha::Plugin::Test->new->enable; |
| 59 |
|
| 60 |
my $test_plugin3 = Test::MockModule->new('Koha::Plugin::Test'); |
| 61 |
$test_plugin3->mock( 'after_item_action', undef ); |
| 62 |
$test_plugin3->mock( 'item_barcode_transform', undef ); |
| 63 |
|
| 64 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 65 |
my $job = Koha::BackgroundJob::StageMARCForImport->new( |
| 66 |
{ |
| 67 |
status => 'new', |
| 68 |
size => 1, |
| 69 |
borrowernumber => $patron->borrowernumber, |
| 70 |
type => 'stage_marc_for_import', |
| 71 |
} |
| 72 |
)->store; |
| 73 |
$job = Koha::BackgroundJobs->find( $job->id ); |
| 74 |
$job->process( |
| 75 |
{ |
| 76 |
job_id => $job->id, |
| 77 |
record_type => 'biblio', |
| 78 |
encoding => 'UTF-8', |
| 79 |
format => 'ISO2709', |
| 80 |
filepath => 't/db_dependent/data/marc21/zebraexport/biblio/exported_records', |
| 81 |
filename => 'some_records', |
| 82 |
parse_items => 1, |
| 83 |
} |
| 84 |
); |
| 85 |
|
| 86 |
my $report = decode_json($job->get_from_storage->data)->{report}; |
| 87 |
my $import_batch_id = $report->{import_batch_id}; |
| 88 |
|
| 89 |
my $job2 = Koha::BackgroundJob::MARCImportCommitBatch->new( |
| 90 |
{ |
| 91 |
status => 'new', |
| 92 |
size => 1, |
| 93 |
borrowernumber => $patron->borrowernumber, |
| 94 |
type => 'marc_import_commit_batch' |
| 95 |
} |
| 96 |
)->store; |
| 97 |
$job2 = Koha::BackgroundJobs->find( $job2->id ); |
| 98 |
|
| 99 |
warning_like { |
| 100 |
$job2->process( |
| 101 |
{ |
| 102 |
job_id => $job2->id, |
| 103 |
import_batch_id => $import_batch_id, |
| 104 |
frameworkcode => q{}, |
| 105 |
} |
| 106 |
); |
| 107 |
} qr/after_batch_action called with addBiblio count: 178/, |
| 108 |
'MARCImportCommitBatch calls the after_batch_action hook'; |
| 109 |
|
| 110 |
Koha::Plugins->RemovePlugins; |
| 111 |
$schema->storage->txn_rollback; |
| 112 |
|
| 113 |
}; |
| 114 |
|
| 115 |
subtest 'before_batch_action and after_batch_action hooks with after_item_action hook tests' => sub { |
| 116 |
|
| 117 |
plan tests => 1; |
| 118 |
|
| 119 |
$schema->storage->txn_begin; |
| 120 |
|
| 121 |
my $plugins = Koha::Plugins->new; |
| 122 |
$plugins->InstallPlugins; |
| 123 |
|
| 124 |
my $plugin = Koha::Plugin::Test->new->enable; |
| 125 |
|
| 126 |
my $test_plugin4 = Test::MockModule->new('Koha::Plugin::Test'); |
| 127 |
$test_plugin4->mock( 'after_biblio_action', undef ); |
| 128 |
$test_plugin4->mock( 'item_barcode_transform', undef ); |
| 129 |
|
| 130 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 131 |
my $job = Koha::BackgroundJob::StageMARCForImport->new( |
| 132 |
{ |
| 133 |
status => 'new', |
| 134 |
size => 1, |
| 135 |
borrowernumber => $patron->borrowernumber, |
| 136 |
type => 'stage_marc_for_import', |
| 137 |
} |
| 138 |
)->store; |
| 139 |
$job = Koha::BackgroundJobs->find( $job->id ); |
| 140 |
$job->process( |
| 141 |
{ |
| 142 |
job_id => $job->id, |
| 143 |
record_type => 'biblio', |
| 144 |
encoding => 'UTF-8', |
| 145 |
format => 'ISO2709', |
| 146 |
filepath => 't/db_dependent/data/marc21/zebraexport/biblio/exported_records', |
| 147 |
filename => 'some_records', |
| 148 |
parse_items => 1, |
| 149 |
} |
| 150 |
); |
| 151 |
|
| 152 |
my $report = decode_json($job->get_from_storage->data)->{report}; |
| 153 |
my $import_batch_id = $report->{import_batch_id}; |
| 154 |
|
| 155 |
my $job2 = Koha::BackgroundJob::MARCImportCommitBatch->new( |
| 156 |
{ |
| 157 |
status => 'new', |
| 158 |
size => 1, |
| 159 |
borrowernumber => $patron->borrowernumber, |
| 160 |
type => 'marc_import_commit_batch' |
| 161 |
} |
| 162 |
)->store; |
| 163 |
$job2 = Koha::BackgroundJobs->find( $job2->id ); |
| 164 |
|
| 165 |
warning_like { |
| 166 |
$job2->process( |
| 167 |
{ |
| 168 |
job_id => $job2->id, |
| 169 |
import_batch_id => $import_batch_id, |
| 170 |
frameworkcode => q{}, |
| 171 |
} |
| 172 |
); |
| 173 |
} qr/after_batch_action called with addItem count: 138/, |
| 174 |
'MARCImportCommitBatch calls the after_batch_action hook'; |
| 175 |
|
| 176 |
Koha::Plugins->RemovePlugins; |
| 177 |
$schema->storage->txn_rollback; |
| 178 |
|
| 179 |
}; |
| 180 |
|
| 181 |
subtest 'before_batch_action and after_batch_action hooks with after_authority_action hook tests' => sub { |
| 182 |
|
| 183 |
plan tests => 1; |
| 184 |
|
| 185 |
$schema->storage->txn_begin; |
| 186 |
|
| 187 |
my $plugins = Koha::Plugins->new; |
| 188 |
$plugins->InstallPlugins; |
| 189 |
|
| 190 |
my $plugin = Koha::Plugin::Test->new->enable; |
| 191 |
|
| 192 |
my $test_plugin5 = Test::MockModule->new('Koha::Plugin::Test'); |
| 193 |
$test_plugin5->mock( 'elasticsearch_to_document', undef ); |
| 194 |
|
| 195 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 196 |
my $job = Koha::BackgroundJob::StageMARCForImport->new( |
| 197 |
{ |
| 198 |
status => 'new', |
| 199 |
size => 1, |
| 200 |
borrowernumber => $patron->borrowernumber, |
| 201 |
type => 'stage_marc_for_import', |
| 202 |
} |
| 203 |
)->store; |
| 204 |
$job = Koha::BackgroundJobs->find( $job->id ); |
| 205 |
$job->process( |
| 206 |
{ |
| 207 |
job_id => $job->id, |
| 208 |
record_type => 'auth', |
| 209 |
encoding => 'UTF-8', |
| 210 |
format => 'ISO2709', |
| 211 |
filepath => 't/db_dependent/data/marc21/zebraexport/authority/exported_records', |
| 212 |
filename => 'some_auths', |
| 213 |
} |
| 214 |
); |
| 215 |
|
| 216 |
my $report = decode_json($job->get_from_storage->data)->{report}; |
| 217 |
my $import_batch_id = $report->{import_batch_id}; |
| 218 |
|
| 219 |
my $job2 = Koha::BackgroundJob::MARCImportCommitBatch->new( |
| 220 |
{ |
| 221 |
status => 'new', |
| 222 |
size => 1, |
| 223 |
borrowernumber => $patron->borrowernumber, |
| 224 |
type => 'marc_import_commit_batch' |
| 225 |
} |
| 226 |
)->store; |
| 227 |
$job2 = Koha::BackgroundJobs->find( $job2->id ); |
| 228 |
|
| 229 |
warning_like { |
| 230 |
$job2->process( |
| 231 |
{ |
| 232 |
job_id => $job2->id, |
| 233 |
import_batch_id => $import_batch_id, |
| 234 |
frameworkcode => q{}, |
| 235 |
} |
| 236 |
); |
| 237 |
} qr/after_batch_action called with addAuthority count: 2/, |
| 238 |
'MARCImportCommitBatch calls the after_batch_action hook'; |
| 239 |
|
| 240 |
Koha::Plugins->RemovePlugins; |
| 241 |
$schema->storage->txn_rollback; |
| 242 |
|
| 243 |
}; |