Bug 24146: Add test cases
[koha.git] / t / db_dependent / Sitemapper.t
blob30c64cfd1df15f9c023ff09e8dacc4def1f48881
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Modern::Perl;
21 use File::Basename;
22 use File::Path;
23 use DateTime;
24 use Test::MockModule;
25 use Test::More tests => 16;
26 use Carp qw/croak carp/;
29 BEGIN {
30 use_ok('Koha::Sitemapper');
31 use_ok('Koha::Sitemapper::Writer');
34 my $now_value = DateTime->now();
35 my $mocked_datetime = Test::MockModule->new('DateTime');
36 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
38 sub slurp {
39 my $file = shift;
40 open my $fh, '<', $file or croak;
41 local $/ = undef;
42 my $cont = <$fh>;
43 close $fh;
44 return $cont;
47 use Test::DBIx::Class;
49 sub fixtures {
50 my ($data) = @_;
51 fixtures_ok [
52 Biblio => [ [qw/ biblionumber datecreated timestamp /], @{$data}, ],
53 ], 'add fixtures';
54 return;
57 # Make the code in the module use our mocked Koha::Schema/Koha::Database
58 my $db = Test::MockModule->new('Koha::Database');
59 $db->mock(
61 # Schema() gives us the DB connection set up by Test::DBIx::Class
62 _new_schema => sub { return Schema(); }
65 my $dir = C4::Context::temporary_directory;
67 my $data = [
68 [qw/ 1 2013-11-15 2013-11-15/],
69 [qw/ 2 2015-08-31 2015-08-31/],
71 fixtures($data);
73 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
74 my $sitemapper = Koha::Sitemapper->new(
75 verbose => 0,
76 url => 'http://www.mylibrary.org',
77 dir => $dir,
78 short => 0,
80 $sitemapper->run();
82 my $file = "$dir/sitemapindex.xml";
83 ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml created' );
84 my $file_content = slurp($file);
85 my $now = DateTime->now->ymd;
86 my $expected_content = <<"EOS";
87 <?xml version="1.0" encoding="UTF-8"?>
89 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
90 <sitemap>
91 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
92 <lastmod>$now</lastmod>
93 </sitemap>
94 </sitemapindex>
95 EOS
96 chop $expected_content;
97 is( $file_content, $expected_content, 'Its content is valid' );
99 $file = "$dir/sitemap0001.xml";
100 ok( -e $file, 'File sitemap0001.xml created' );
101 $file_content = slurp($file);
102 $expected_content = <<"EOS";
103 <?xml version="1.0" encoding="UTF-8"?>
105 <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">
106 <url>
107 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
108 <lastmod>2013-11-15</lastmod>
109 </url>
110 <url>
111 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
112 <lastmod>2015-08-31</lastmod>
113 </url>
114 </urlset>
116 is( $file_content, $expected_content, 'Its content is valid' );
118 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
119 # Test that 2 files are created.
120 $sitemapper = Koha::Sitemapper->new(
121 verbose => 0,
122 url => 'http://www.mylibrary.org',
123 dir => $dir,
124 short => 1,
126 $sitemapper->run();
128 $file = "$dir/sitemap0001.xml";
129 ok( -e $file, 'File sitemap0001.xml with short URLs created' );
130 $file_content = slurp($file);
131 $expected_content = <<"EOS";
132 <?xml version="1.0" encoding="UTF-8"?>
134 <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">
135 <url>
136 <loc>http://www.mylibrary.org/bib/1</loc>
137 <lastmod>2013-11-15</lastmod>
138 </url>
139 <url>
140 <loc>http://www.mylibrary.org/bib/2</loc>
141 <lastmod>2015-08-31</lastmod>
142 </url>
143 </urlset>
145 is( $file_content, $expected_content, 'Its content is valid' );
147 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
148 # url'. Test that 3 files are created: index file + 2 urls file with
149 # respectively 50000 et 25000 urls.
150 $data = [];
151 for my $count ( 3 .. 75_000 ) {
152 push @{$data}, [ $count, '2015-08-31', '2015-08-31' ];
154 fixtures($data);
155 $sitemapper = Koha::Sitemapper->new(
156 verbose => 0,
157 url => 'http://www.mylibrary.org',
158 dir => $dir,
159 short => 1,
161 $sitemapper->run();
163 $file = "$dir/sitemapindex.xml";
164 ok( -e "$dir/sitemapindex.xml",
165 'File sitemapindex.xml for 75000 bibs created' );
166 $file_content = slurp($file);
167 $expected_content = <<"EOS";
168 <?xml version="1.0" encoding="UTF-8"?>
170 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
171 <sitemap>
172 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
173 <lastmod>$now</lastmod>
174 </sitemap>
175 <sitemap>
176 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
177 <lastmod>$now</lastmod>
178 </sitemap>
179 </sitemapindex>
181 chop $expected_content;
182 is( $file_content, $expected_content, 'Its content is valid' );
184 $file = "$dir/sitemap0001.xml";
185 ok( -e $file, 'File sitemap0001.xml created' );
187 open my $fh, '<', $file or croak;
188 my $count = 0;
189 while (<$fh>) {
190 if ( $_ =~ /<loc>/xsm ) { $count++; }
192 close $fh;
193 is( $count, 50_000, 'It contains 50000 URLs' );
195 $file = "$dir/sitemap0002.xml";
196 ok( -e $file, 'File sitemap0002.xml created' );
198 open $fh, '<', $file or croak;
199 $count = 0;
200 while (<$fh>) {
201 if ( $_ =~ /<loc>/xsm ) { $count++; }
203 close $fh;
204 is( $count, 25_000, 'It contains 25000 URLs' );
206 # Cleanup
207 for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
208 unlink "$dir/$file";