Bug 21604: Add two trivial test cases
[koha.git] / t / db_dependent / Koha / SearchEngine / Search.t
1 #!/usr/bin/perl
2
3 # Tests for Koha/SearchEngine/Search
4
5 use Modern::Perl;
6
7 use Test::More tests => 1;
8
9 use MARC::Field;
10 use MARC::Record;
11 use Test::MockModule;
12 use Test::MockObject;
13
14 use t::lib::Mocks;
15
16 #use C4::Biblio qw//;
17 use Koha::Database;
18 use Koha::SearchEngine::Search;
19
20 BEGIN {
21     my $mock = Test::MockObject->new();
22     $mock->fake_module( 'Catmandu::Store::ElasticSearch' );
23 }
24
25 my $schema  = Koha::Database->new->schema;
26 $schema->storage->txn_begin;
27
28 subtest 'Test extract_biblionumber' => sub {
29     plan tests => 2;
30
31     t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' );
32     my $biblio_mod = Test::MockModule->new( 'C4::Biblio' );
33     my $search_mod = Test::MockModule->new( 'C4::Search' );
34     my $koha_fields = [ '001', '' ];
35     $biblio_mod->mock( 'GetMarcFromKohaField', sub { return @$koha_fields; });
36     $search_mod->mock( 'new_record_from_zebra', \&test_record );
37
38     # Extract using 001
39     my $searcher = Koha::SearchEngine::Search->new;
40     my $bibno = $searcher->extract_biblionumber( 'fake_result' );
41     is( $bibno, 3456, 'Extracted biblio number for Zebra' );
42
43     # Now use 999c with Elasticsearch
44     t::lib::Mocks::mock_preference( 'SearchEngine', 'Elasticsearch' );
45     $search_mod->unmock( 'new_record_from_zebra' );
46     $koha_fields = [ '999', 'c' ];
47     $searcher = Koha::SearchEngine::Search->new({ index => 'biblios' });
48     $bibno = $searcher->extract_biblionumber( test_record() );
49     is( $bibno, 4567, 'Extracted biblio number for Zebra' );
50 };
51
52 # -- Helper routine
53 sub test_record {
54     my $marc = MARC::Record->new;
55     $marc->append_fields(
56         MARC::Field->new( '001', '3456' ),
57         MARC::Field->new( '245', '', '', a => 'Some title' ),
58         MARC::Field->new( '999', '', '', c => '4567' ),
59     );
60     return $marc;
61 }
62
63 $schema->storage->txn_rollback;