Bug 26157: Hide expected DBI warnings from tests
[koha.git] / t / db_dependent / Sitemapper.t
blobfe559c1308ae7e6aafc37288d2ba4577ae4b4ebf
1 #!/usr/bin/perl
3 # Copyright 2015 Tamil s.a.r.l.
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
21 use File::Basename;
22 use File::Path;
23 use DateTime;
24 use Koha::DateUtils;
25 use Test::MockModule;
26 use Test::More tests => 16;
27 use Carp qw/croak carp/;
30 BEGIN {
31 use_ok('Koha::Sitemapper');
32 use_ok('Koha::Sitemapper::Writer');
35 my $now_value = dt_from_string();
36 my $mocked_datetime = Test::MockModule->new('DateTime');
37 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
39 sub slurp {
40 my $file = shift;
41 open my $fh, '<', $file or croak;
42 local $/ = undef;
43 my $cont = <$fh>;
44 close $fh;
45 return $cont;
48 use Test::DBIx::Class;
50 sub fixtures {
51 my ($data) = @_;
52 fixtures_ok [
53 Biblio => [ [qw/ biblionumber datecreated timestamp /], @{$data}, ],
54 ], 'add fixtures';
55 return;
58 # Make the code in the module use our mocked Koha::Schema/Koha::Database
59 my $db = Test::MockModule->new('Koha::Database');
60 $db->mock(
62 # Schema() gives us the DB connection set up by Test::DBIx::Class
63 _new_schema => sub { return Schema(); }
66 my $dir = C4::Context::temporary_directory;
68 my $data = [
69 [qw/ 1 2013-11-15 2013-11-15/],
70 [qw/ 2 2015-08-31 2015-08-31/],
72 fixtures($data);
74 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
75 my $sitemapper = Koha::Sitemapper->new(
76 verbose => 0,
77 url => 'http://www.mylibrary.org',
78 dir => $dir,
79 short => 0,
81 $sitemapper->run();
83 my $file = "$dir/sitemapindex.xml";
84 ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml created' );
85 my $file_content = slurp($file);
86 my $now = DateTime->now->ymd;
87 my $expected_content = <<"EOS";
88 <?xml version="1.0" encoding="UTF-8"?>
90 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
91 <sitemap>
92 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
93 <lastmod>$now</lastmod>
94 </sitemap>
95 </sitemapindex>
96 EOS
97 chop $expected_content;
98 is( $file_content, $expected_content, 'Its content is valid' );
100 $file = "$dir/sitemap0001.xml";
101 ok( -e $file, 'File sitemap0001.xml created' );
102 $file_content = slurp($file);
103 $expected_content = <<"EOS";
104 <?xml version="1.0" encoding="UTF-8"?>
106 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
107 <url>
108 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
109 <lastmod>2013-11-15</lastmod>
110 </url>
111 <url>
112 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
113 <lastmod>2015-08-31</lastmod>
114 </url>
115 </urlset>
117 is( $file_content, $expected_content, 'Its content is valid' );
119 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
120 # Test that 2 files are created.
121 $sitemapper = Koha::Sitemapper->new(
122 verbose => 0,
123 url => 'http://www.mylibrary.org',
124 dir => $dir,
125 short => 1,
127 $sitemapper->run();
129 $file = "$dir/sitemap0001.xml";
130 ok( -e $file, 'File sitemap0001.xml with short URLs created' );
131 $file_content = slurp($file);
132 $expected_content = <<"EOS";
133 <?xml version="1.0" encoding="UTF-8"?>
135 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
136 <url>
137 <loc>http://www.mylibrary.org/bib/1</loc>
138 <lastmod>2013-11-15</lastmod>
139 </url>
140 <url>
141 <loc>http://www.mylibrary.org/bib/2</loc>
142 <lastmod>2015-08-31</lastmod>
143 </url>
144 </urlset>
146 is( $file_content, $expected_content, 'Its content is valid' );
148 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
149 # url'. Test that 3 files are created: index file + 2 urls file with
150 # respectively 50000 et 25000 urls.
151 $data = [];
152 for my $count ( 3 .. 75_000 ) {
153 push @{$data}, [ $count, '2015-08-31', '2015-08-31' ];
155 fixtures($data);
156 $sitemapper = Koha::Sitemapper->new(
157 verbose => 0,
158 url => 'http://www.mylibrary.org',
159 dir => $dir,
160 short => 1,
162 $sitemapper->run();
164 $file = "$dir/sitemapindex.xml";
165 ok( -e "$dir/sitemapindex.xml",
166 'File sitemapindex.xml for 75000 bibs created' );
167 $file_content = slurp($file);
168 $expected_content = <<"EOS";
169 <?xml version="1.0" encoding="UTF-8"?>
171 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
172 <sitemap>
173 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
174 <lastmod>$now</lastmod>
175 </sitemap>
176 <sitemap>
177 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
178 <lastmod>$now</lastmod>
179 </sitemap>
180 </sitemapindex>
182 chop $expected_content;
183 is( $file_content, $expected_content, 'Its content is valid' );
185 $file = "$dir/sitemap0001.xml";
186 ok( -e $file, 'File sitemap0001.xml created' );
188 open my $fh, '<', $file or croak;
189 my $count = 0;
190 while (<$fh>) {
191 if ( $_ =~ /<loc>/xsm ) { $count++; }
193 close $fh;
194 is( $count, 50_000, 'It contains 50000 URLs' );
196 $file = "$dir/sitemap0002.xml";
197 ok( -e $file, 'File sitemap0002.xml created' );
199 open $fh, '<', $file or croak;
200 $count = 0;
201 while (<$fh>) {
202 if ( $_ =~ /<loc>/xsm ) { $count++; }
204 close $fh;
205 is( $count, 25_000, 'It contains 25000 URLs' );
207 # Cleanup
208 for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
209 unlink "$dir/$file";