Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# Tests for Koha/SearchEngine/Search |
4 |
|
5 |
use Modern::Perl; |
6 |
|
7 |
use Test::More tests => 2; |
8 |
use Test::Warn; |
9 |
|
10 |
use MARC::Field; |
11 |
use MARC::Record; |
12 |
use Test::MockModule; |
13 |
use Test::MockObject; |
14 |
|
15 |
use t::lib::Mocks; |
16 |
|
17 |
#use C4::Biblio qw//; |
18 |
use C4::AuthoritiesMarc; |
19 |
use C4::Biblio; |
20 |
use C4::Circulation; |
21 |
use Koha::Database; |
22 |
use Koha::SearchEngine::Indexer; |
23 |
|
24 |
use t::lib::TestBuilder; |
25 |
use t::lib::Mocks; |
26 |
|
27 |
my $schema = Koha::Database->new->schema; |
28 |
my $builder = t::lib::TestBuilder->new; |
29 |
|
30 |
$schema->storage->txn_begin; |
31 |
|
32 |
subtest 'Test indexer object creation' => sub { |
33 |
plan tests => 6; |
34 |
|
35 |
t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' ); |
36 |
my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); |
37 |
is( ref $indexer, 'Koha::SearchEngine::Zebra::Indexer', 'We get the correct class for Zebra biblios'); |
38 |
$indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX }); |
39 |
is( ref $indexer, 'Koha::SearchEngine::Zebra::Indexer', 'We get the correct class for Zebra authorities'); |
40 |
|
41 |
t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' ); |
42 |
|
43 |
SKIP: { |
44 |
|
45 |
eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; }; |
46 |
|
47 |
skip 'Elasticsearch configuration not available', 4 |
48 |
if $@; |
49 |
|
50 |
$indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); |
51 |
is( ref $indexer, 'Koha::SearchEngine::Elasticsearch::Indexer', 'We get the correct class for Elasticsearch biblios'); |
52 |
ok( $indexer->index_name =~ /biblios$/, "The index is set correctly for biblios"); |
53 |
$indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX }); |
54 |
is( ref $indexer, 'Koha::SearchEngine::Elasticsearch::Indexer', 'We get the correct class for Elasticsearch authorities'); |
55 |
ok( $indexer->index_name =~ /authorities$/, "The index is set correctly for authorities"); |
56 |
|
57 |
} |
58 |
}; |
59 |
|
60 |
subtest 'Test indexer calls' => sub { |
61 |
plan tests => 24; |
62 |
|
63 |
my @engines = ('Zebra'); |
64 |
eval { Koha::SearchEngine::Elasticsearch->get_elasticsearch_params; }; |
65 |
push @engines, 'Elasticsearch' unless $@; |
66 |
SKIP: { |
67 |
skip 'Elasticsearch configuration not available', 12 |
68 |
if scalar @engines == 1; |
69 |
} |
70 |
|
71 |
for my $engine ( @engines ){ |
72 |
t::lib::Mocks::mock_preference( 'SearchEngine', $engine ); |
73 |
my $mock_index = Test::MockModule->new("Koha::SearchEngine::".$engine."::Indexer"); |
74 |
|
75 |
my $biblionumber1 = $builder->build_sample_biblio()->biblionumber; |
76 |
my $biblionumber2 = $builder->build_sample_biblio()->biblionumber; |
77 |
|
78 |
my $mock_zebra = Test::MockModule->new("Koha::SearchEngine::Zebra::Indexer"); |
79 |
$mock_zebra->mock( ModZebra => sub { warn "ModZebra"; } ); |
80 |
my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); |
81 |
warnings_are{ |
82 |
$indexer->index_records([$biblionumber1,$biblionumber1],"specialUpdate","biblioserver",undef); |
83 |
} ["ModZebra","ModZebra"],"ModZebra called for each record being indexed for $engine"; |
84 |
|
85 |
$mock_index->mock( index_records => sub { |
86 |
warn $engine; |
87 |
my ($package, undef, undef) = caller; |
88 |
warn $package; |
89 |
}); |
90 |
|
91 |
my $auth = MARC::Record->new; |
92 |
my $authid; |
93 |
warnings_are{ |
94 |
$authid = AddAuthority( $auth, undef, 'TOPIC_TERM' ); |
95 |
} [$engine,"C4::AuthoritiesMarc"], "index_records is called for $engine is called when adding authority"; |
96 |
|
97 |
warnings_are{ |
98 |
$authid = DelAuthority({ authid => $authid }); |
99 |
} [$engine,"C4::AuthoritiesMarc"], "index_records is called for $engine is called when adding authority"; |
100 |
|
101 |
my $biblio; |
102 |
my $biblio2; |
103 |
warnings_are{ |
104 |
$biblio = $builder->build_sample_biblio(); |
105 |
$biblio2 = $builder->build_sample_biblio(); |
106 |
} [$engine,'C4::Biblio',$engine,'C4::Biblio'], "index_records is called for $engine when adding a biblio (ModBiblioMarc)"; |
107 |
|
108 |
|
109 |
my $item; |
110 |
my $item2; |
111 |
warnings_are{ |
112 |
$item = $builder->build_sample_item({biblionumber => $biblio->biblionumber}); |
113 |
$item2 = $builder->build_sample_item({biblionumber => $biblio->biblionumber}); |
114 |
} [$engine,"Koha::Item",$engine,"Koha::Item"], "index_records is called for $engine when adding an item (Item->store)"; |
115 |
warnings_are{ |
116 |
$item->store({ skip_modzebra_update => 1 }); |
117 |
} undef, "index_records is not called for $engine when adding an item (Item->store) if skip_modzebra_update passed"; |
118 |
|
119 |
$builder->build({ |
120 |
source => 'Branchtransfer', |
121 |
value => { |
122 |
itemnumber => $item->itemnumber, |
123 |
datearrived => undef} |
124 |
}); |
125 |
warnings_are{ |
126 |
LostItem( $item->itemnumber, "tests"); |
127 |
} [$engine,"Koha::Item"], "index_records is called for $engine when calling LostItem and transfer exists"; |
128 |
$builder->build({ |
129 |
source => 'Branchtransfer', |
130 |
value => { |
131 |
itemnumber => $item2->itemnumber, |
132 |
datearrived => undef} |
133 |
}); |
134 |
warnings_are{ |
135 |
LostItem( $item->itemnumber, "tests", undef, { skip_modzebra_update => 1 }); |
136 |
} undef, "index_records is not called for $engine when calling LostItem and transfer exists if skip_modzebra_update"; |
137 |
|
138 |
warnings_are{ |
139 |
$item->delete(); |
140 |
} [$engine,"Koha::Item"], "index_records is called for $engine when deleting an item (Item->delete)"; |
141 |
warnings_are{ |
142 |
$item2->delete({ skip_modzebra_update => 1 }); |
143 |
} undef, "index_records is not called for $engine when adding an item (Item->store) if skip_modzebra_update passed"; |
144 |
|
145 |
warnings_are{ |
146 |
DelBiblio( $biblio->biblionumber ); |
147 |
} [$engine, "C4::Biblio"], "index_records is called for $engine when calling DelBiblio"; |
148 |
warnings_are{ |
149 |
DelBiblio( $biblio->biblionumber, { skip_record_index =>1 }); |
150 |
} undef, "index_records is not called for $engine when calling DelBiblio if skip_record_index passed"; |
151 |
|
152 |
} |
153 |
|
154 |
}; |
155 |
|
156 |
$schema->storage->txn_rollback; |