3dc9b48bbe58f80cf39af7ca945fc1c4599fe41a
[koha_ffzg] / t / db_dependent / Sitemapper.t
1 #!/usr/bin/perl
2
3 # Copyright 2015 Tamil s.a.r.l.
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
15 #
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.
19
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 Koha::Schema;
27 use Carp qw/croak carp/;
28
29 BEGIN {
30     use_ok('Koha::Sitemapper');
31     use_ok('Koha::Sitemapper::Writer');
32 }
33
34 my $now_value = DateTime->now();
35 my $mocked_datetime = Test::MockModule->new('DateTime');
36 $mocked_datetime->mock('now', sub { return $now_value; } );
37
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;
45 }
46
47 use Test::DBIx::Class;
48
49 sub fixtures {
50     my ( $data ) = @_;
51     fixtures_ok [
52         Biblio => [
53             [ qw/ biblionumber datecreated timestamp  / ],
54             @{$data},
55         ],
56     ], 'add fixtures';
57     return;
58 }
59
60 # Make the code in the module use our mocked Koha::Schema/Koha::Database
61 my $db = Test::MockModule->new('Koha::Database');
62 $db->mock(
63     # Schema() gives us the DB connection set up by Test::DBIx::Class
64     _new_schema => sub { return Schema(); }
65 );
66
67 my $dir = File::Spec->tmpdir();
68
69
70 my $data = [
71     [ qw/ 1         2013-11-15 2013-11-15/ ],
72     [ qw/ 2         2015-08-31 2015-08-31/ ],
73 ];
74 fixtures($data);
75 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
76 my $sitemapper = Koha::Sitemapper->new(
77     verbose => 0,
78     url     => 'http://www.mylibrary.org',
79     dir     => $dir,
80     short   => 0,
81 );
82 $sitemapper->run();
83
84 my $file = "$dir/sitemapindex.xml";
85 ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml created');
86 my $file_content = slurp($file);
87 my $now = DateTime->now->ymd;
88 my $expected_content = <<"EOS";
89 <?xml version="1.0" encoding="UTF-8"?>
90
91 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
92   <sitemap>
93     <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
94     <lastmod>$now</lastmod>
95   </sitemap>
96 </sitemapindex>
97 EOS
98 chop $expected_content;
99 is( $file_content, $expected_content, 'Its content is valid' );
100
101 $file = "$dir/sitemap0001.xml";
102 ok( -e $file, 'File sitemap0001.xml created');
103 $file_content = slurp($file);
104 $expected_content = <<"EOS";
105 <?xml version="1.0" encoding="UTF-8"?>
106
107 <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">
108   <url>
109     <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
110     <lastmod>2013-11-15</lastmod>
111   </url>
112   <url>
113     <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
114     <lastmod>2015-08-31</lastmod>
115   </url>
116 </urlset>
117 EOS
118 is( $file_content, $expected_content, 'Its content is valid' );
119
120
121 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
122 # Test that 2 files are created.
123 $sitemapper = Koha::Sitemapper->new(
124     verbose => 0,
125     url     => 'http://www.mylibrary.org',
126     dir     => $dir,
127     short   => 1,
128 );
129 $sitemapper->run();
130
131 $file = "$dir/sitemap0001.xml";
132 ok( -e $file, 'File sitemap0001.xml with short URLs created');
133 $file_content = slurp($file);
134 $expected_content = <<"EOS";
135 <?xml version="1.0" encoding="UTF-8"?>
136
137 <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">
138   <url>
139     <loc>http://www.mylibrary.org/bib/1</loc>
140     <lastmod>2013-11-15</lastmod>
141   </url>
142   <url>
143     <loc>http://www.mylibrary.org/bib/2</loc>
144     <lastmod>2015-08-31</lastmod>
145   </url>
146 </urlset>
147 EOS
148 is( $file_content, $expected_content, 'Its content is valid' );
149
150
151 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
152 # url'. Test that 3 files are created: index file + 2 urls file with
153 # respectively 50000 et 25000 urls.
154 $data = [];
155 for my $count (3..75_000) {
156     push @{$data}, [ $count, '2015-08-31', '2015-08-31'];
157 }
158 fixtures($data);
159 $sitemapper = Koha::Sitemapper->new(
160     verbose => 0,
161     url     => 'http://www.mylibrary.org',
162     dir     => $dir,
163     short   => 1,
164 );
165 $sitemapper->run();
166
167 $file = "$dir/sitemapindex.xml";
168 ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml for 75000 bibs created');
169 $file_content = slurp($file);
170 $expected_content = <<"EOS";
171 <?xml version="1.0" encoding="UTF-8"?>
172
173 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
174   <sitemap>
175     <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
176     <lastmod>$now</lastmod>
177   </sitemap>
178   <sitemap>
179     <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
180     <lastmod>$now</lastmod>
181   </sitemap>
182 </sitemapindex>
183 EOS
184 chop $expected_content;
185 is( $file_content, $expected_content, 'Its content is valid' );
186
187 $file = "$dir/sitemap0001.xml";
188 ok( -e $file, 'File sitemap0001.xml created');
189
190 open my $fh, '<', $file or croak;
191 my $count = 0;
192 while (<$fh>) {
193     if ($_ =~ /<loc>/xsm) { $count++; }
194 }
195 close $fh;
196 is( $count, 50_000, 'It contains 50000 URLs');
197
198 $file = "$dir/sitemap0002.xml";
199 ok( -e $file, 'File sitemap0002.xml created');
200
201 open $fh, '<', $file or croak;
202 $count = 0;
203 while (<$fh>) {
204     if ($_ =~ /<loc>/xsm) { $count++; }
205 }
206 close $fh;
207 is( $count, 25_000, 'It contains 25000 URLs');
208
209 # Cleanup
210 for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
211     unlink "$dir/$file";
212 }