View | Details | Raw Unified | Return to bug 25265
Collapse All | Expand All

(-)a/t/db_dependent/Koha/SearchEngine/Indexer.t (-1 / +139 lines)
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
#    t::lib::Mocks::mock_preference('IndependentBranches', 1);
30
#        my $module = Test::MockModule->new('C4::Items');
31
#        $module->mock( GetAnalyticsCount => sub { return 1 } );
32
#    my $biblio = $builder->build_sample_biblio;
33
34
$schema->storage->txn_begin;
35
36
subtest 'Test indexer object creation' => sub {
37
    plan tests => 6;
38
39
    t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' );
40
    my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
41
    is( ref $indexer, 'Koha::SearchEngine::Zebra::Indexer', 'We get the correct class for Zebra biblios');
42
    $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
43
    is( ref $indexer, 'Koha::SearchEngine::Zebra::Indexer', 'We get the correct class for Zebra authorities');
44
45
    t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' );
46
    $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
47
    is( ref $indexer, 'Koha::SearchEngine::Elasticsearch::Indexer', 'We get the correct class for Elasticsearch biblios');
48
    ok( $indexer->index_name =~ /biblios$/, "The index is set correctly for biblios");
49
    $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
50
    is( ref $indexer, 'Koha::SearchEngine::Elasticsearch::Indexer', 'We get the correct class for Elasticsearch authorities');
51
    ok( $indexer->index_name =~ /authorities$/, "The index is set correctly for authorities");
52
};
53
54
subtest 'Test indexer calls' => sub {
55
    plan tests => 22;
56
57
    for my $engine ( ('Zebra','Elasticsearch') ){
58
        t::lib::Mocks::mock_preference( 'SearchEngine', $engine );
59
        my $mock_index = Test::MockModule->new("Koha::SearchEngine::".$engine."::Indexer");
60
61
        my $biblionumber1 = $builder->build_sample_biblio()->biblionumber;
62
        my $biblionumber2 = $builder->build_sample_biblio()->biblionumber;
63
64
        my $mock_zebra = Test::MockModule->new("Koha::SearchEngine::Zebra::Indexer");
65
        $mock_zebra->mock( ModZebra => sub { warn "ModZebra"; } );
66
        my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX });
67
        warnings_are{
68
            $indexer->index_records([$biblionumber1,$biblionumber1],"specialUpdate","biblioserver",undef);
69
        } ["ModZebra","ModZebra"],"ModZebra called for each record being indexed for $engine";
70
71
        $mock_index->mock( index_records => sub {
72
            warn $engine;
73
            my ($package, undef, undef) = caller;
74
            warn $package;
75
        });
76
77
        my $auth = MARC::Record->new;
78
        my $authid;
79
        warnings_are{
80
            $authid = AddAuthority( $auth, undef, 'TOPIC_TERM' );
81
        } [$engine,"C4::AuthoritiesMarc"], "index_records is called for $engine is called when adding authority";
82
83
        warnings_are{
84
            $authid = DelAuthority({ authid => $authid });
85
        } [$engine,"C4::AuthoritiesMarc"], "index_records is called for $engine is called when adding authority";
86
87
        my $biblio;
88
        my $biblio2;
89
        warnings_are{
90
            $biblio = $builder->build_sample_biblio();
91
            $biblio2 = $builder->build_sample_biblio();
92
        } [$engine,'C4::Biblio',$engine,'C4::Biblio'], "index_records is called for $engine when adding a biblio (ModBiblioMarc)";
93
94
95
        my $item;
96
        my $item2;
97
        warnings_are{
98
            $item = $builder->build_sample_item({biblionumber => $biblio->biblionumber});
99
            $item2 = $builder->build_sample_item({biblionumber => $biblio->biblionumber});
100
        } [$engine,"Koha::Item",$engine,"Koha::Item"], "index_records is called for $engine when adding an item (Item->store)";
101
        warnings_are{
102
            $item->store({ skip_modzebra_update => 1 });
103
        } undef, "index_records is not called for $engine when adding an item (Item->store) if skip_modzebra_update passed";
104
105
        $builder->build({
106
            source => 'Branchtransfer',
107
            value => {
108
                itemnumber => $item->itemnumber,
109
                datearrived => undef}
110
        });
111
        warnings_are{
112
            LostItem( $item->itemnumber, "tests");
113
        } [$engine,"Koha::Item"], "index_records is called for $engine when calling LostItem and transfer exists";
114
        $builder->build({
115
            source => 'Branchtransfer',
116
            value => {
117
                itemnumber => $item2->itemnumber,
118
                datearrived => undef}
119
        });
120
        warnings_are{
121
            LostItem( $item->itemnumber, "tests", undef, { skip_modzebra_update => 1 });
122
        } undef, "index_records is not called for $engine when calling LostItem and transfer exists if skip_modzebra_update";
123
124
        warnings_are{
125
            $item->delete();
126
        } [$engine,"Koha::Item"], "index_records is called for $engine when deleting an item (Item->delete)";
127
        warnings_are{
128
            $item2->delete({ skip_modzebra_update => 1 });
129
        } undef, "index_records is not called for $engine when adding an item (Item->store) if skip_modzebra_update passed";
130
131
        warnings_are{
132
            DelBiblio( $biblio->biblionumber );
133
        } [$engine, "C4::Biblio"], "index_records is called for $engine when callind DelBiblio";
134
135
    }
136
137
};
138
139
$schema->storage->txn_rollback;

Return to bug 25265