File Coverage

blib/lib/App/Prove/Plugin/KohaBootstrap.pm
Criterion Covered Total %
statement 17 65 26.1
branch 0 4 0.0
condition 0 2 0.0
subroutine 6 7 85.7
pod 1 1 100.0
total 24 79 30.3


line stmt bran cond sub pod time code
1             package App::Prove::Plugin::KohaBootstrap;
2              
3 1     1   103315 use 5.010;
  1         4  
4 1     1   10 use strict;
  1         3  
  1         20  
5 1     1   4 use warnings;
  1         3  
  1         35  
6              
7 1     1   1829 use DBI;
  1         18031  
  1         61  
8 1     1   838 use File::Temp qw( tempfile );
  1         21859  
  1         63  
9 1     1   699 use XML::LibXML;
  1         43930  
  1         7  
10              
11             =head1 NAME
12              
13             App::Prove::Plugin::KohaBootstrap - prove plugin to run Koha tests on a separate database
14              
15             =head1 VERSION
16              
17             Version 0.01
18              
19             =cut
20              
21             our $VERSION = '0.01';
22              
23              
24             =head1 SYNOPSIS
25              
26             prove -P KohaBootstrap=database,koha_test,marcflavour=MARC21
27              
28             =head1 SUBROUTINES/METHODS
29              
30             =head2 load
31              
32             Drop and recreate a database, and run the necessary installation steps to fill
33             the database
34              
35             =cut
36              
37             sub load {
38 0     0 1   my ($class, $p) = @_;
39              
40 0           my $app = $p->{app_prove};
41 0           my %args = @{ $p->{args} };
  0            
42              
43 0 0         unless (defined $args{database}) {
44 0           die "Test database is not defined";
45             }
46              
47 0   0       $args{marcflavour} //= 'MARC21';
48              
49 0           my $xml = XML::LibXML->load_xml(location => $ENV{KOHA_CONF});
50 0           my $root = $xml->documentElement();
51 0           my ($databaseElement) = $root->findnodes('//config/database');
52 0           my $currentDatabase = $databaseElement->textContent();
53              
54 0 0         if ($currentDatabase eq $args{database}) {
55 0           die "Test database is the same as database in KOHA_CONF, abort!";
56             }
57              
58 0           $databaseElement->firstChild()->setData($args{database});
59              
60 0           my ($fh, $filename) = tempfile('koha-conf.XXXXXX', TMPDIR => 1, UNLINK => 1);
61 0           $xml->toFH($fh);
62 0           close $fh;
63              
64 0           $ENV{KOHA_CONF} = $filename;
65              
66 0           require C4::Context;
67 0           C4::Context->import;
68              
69 0           require C4::Installer;
70 0           C4::Installer->import;
71              
72 0           require C4::Languages;
73              
74 0           my $host = C4::Context->config('hostname');
75 0           my $port = C4::Context->config('port');
76 0           my $database = C4::Context->config('database');
77 0           my $user = C4::Context->config('user');
78 0           my $pass = C4::Context->config('pass');
79              
80 0           say "Create test database $database...";
81              
82 0           my $dbh = DBI->connect("dbi:mysql:;host=$host;port=$port", $user, $pass, {
83             RaiseError => 1,
84             PrintError => 0,
85             });
86              
87 0           $dbh->do("DROP DATABASE IF EXISTS $database");
88 0           $dbh->do("CREATE DATABASE $database");
89              
90 0           my $installer = C4::Installer->new();
91 0           $installer->load_db_schema();
92 0           $installer->set_marcflavour_syspref($args{marcflavour});
93 0           my (undef, $fwklist) = $installer->marc_framework_sql_list('en', $args{marcflavour});
94 0           my (undef, $list) = $installer->sample_data_sql_list('en');
95 0           my @frameworks;
96 0           foreach my $fwk (@$fwklist, @$list) {
97 0           foreach my $framework (@{ $fwk->{frameworks} }) {
  0            
98 0           push @frameworks, $framework->{fwkfile};
99             }
100             }
101 0           my $all_languages = C4::Languages::getAllLanguages();
102 0           $installer->load_sql_in_order('en', $all_languages, @frameworks);
103 0           require Koha::SearchEngine::Elasticsearch;
104 0           Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings;
105 0           $installer->set_version_syspref();
106              
107 0           return 1;
108             }
109              
110              
111             =head1 AUTHOR
112              
113             Julian Maurice, C<< >>
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to C, or through
118             the web interface at L. I will be notified, and then you'll
119             automatically be notified of progress on your bug as I make changes.
120              
121              
122              
123              
124             =head1 SUPPORT
125              
126             You can find documentation for this module with the perldoc command.
127              
128             perldoc App::Prove::Plugin::KohaBootstrap
129              
130              
131             You can also look for information at:
132              
133             =over 4
134              
135             =item * RT: CPAN's request tracker (report bugs here)
136              
137             L
138              
139             =item * CPAN Ratings
140              
141             L
142              
143             =item * Search CPAN
144              
145             L
146              
147             =back
148              
149              
150             =head1 ACKNOWLEDGEMENTS
151              
152              
153             =head1 LICENSE AND COPYRIGHT
154              
155             This software is Copyright (c) 2023 by Julian Maurice.
156              
157             This is free software, licensed under:
158              
159             The Artistic License 2.0 (GPL Compatible)
160              
161              
162             =cut
163              
164             1;