Bug 17770: Fix Sitemapper.t if date changes during test run
[koha.git] / 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
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 die;
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 }
58
59 # Make the code in the module use our mocked Koha::Schema/Koha::Database
60 my $db = Test::MockModule->new('Koha::Database');
61 $db->mock(
62     # Schema() gives us the DB connection set up by Test::DBIx::Class
63     _new_schema => sub { return Schema(); }
64 );
65
66 my $dir = File::Spec->tmpdir();
67
68
69 my $data = [
70     [ qw/ 1         2013-11-15 2013-11-15/ ],
71     [ qw/ 2         2015-08-31 2015-08-31/ ],
72 ];
73 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,
80 );
81 $sitemapper->run();
82
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"?>
89
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" );
99
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"?>
105
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>
116 EOS
117 is( $file_content, $expected_content, "Its content is valid" );
118
119
120 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
121 # Test that 2 files are created.
122 $sitemapper = Koha::Sitemapper->new(
123     verbose => 0,
124     url     => 'http://www.mylibrary.org',
125     dir     => $dir,
126     short   => 1,
127 );
128 $sitemapper->run();
129
130 $file = "$dir/sitemap0001.xml";
131 ok( -e $file, "File sitemap0001.xml with short URLs created");
132 $file_content = slurp($file);
133 $expected_content = <<EOS;
134 <?xml version="1.0" encoding="UTF-8"?>
135
136 <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">
137   <url>
138     <loc>http://www.mylibrary.org/bib/1</loc>
139     <lastmod>2013-11-15</lastmod>
140   </url>
141   <url>
142     <loc>http://www.mylibrary.org/bib/2</loc>
143     <lastmod>2015-08-31</lastmod>
144   </url>
145 </urlset>
146 EOS
147 is( $file_content, $expected_content, "Its content is valid" );
148
149
150 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
151 # url'. Test that 3 files are created: index file + 2 urls file with
152 # respectively 50000 et 25000 urls.
153 $data = [];
154 push @$data, [ $_, '2015-08-31', '2015-08-31'] for 3..75000;
155 fixtures($data);
156 $sitemapper = Koha::Sitemapper->new(
157     verbose => 0,
158     url     => 'http://www.mylibrary.org',
159     dir     => $dir,
160     short   => 1,
161 );
162 $sitemapper->run();
163
164 $file = "$dir/sitemapindex.xml";
165 ok( -e "$dir/sitemapindex.xml", "File sitemapindex.xml for 75000 bibs created");
166 $file_content = slurp($file);
167 $expected_content = <<EOS;
168 <?xml version="1.0" encoding="UTF-8"?>
169
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>
180 EOS
181 chop $expected_content;
182 is( $file_content, $expected_content, "Its content is valid" );
183
184 $file = "$dir/sitemap0001.xml";
185 ok( -e $file, "File sitemap0001.xml created");
186
187 open my $fh, "<", $file;
188 my $count = 0;
189 while (<$fh>) {
190         $count++ if /<loc>/;
191 }
192 is( $count, 50000, "It contains 50000 URLs");
193
194 $file = "$dir/sitemap0002.xml";
195 ok( -e $file, "File sitemap0002.xml created");
196
197 open $fh, "<", $file;
198 $count = 0;
199 while (<$fh>) {
200         $count++ if /<loc>/;
201 }
202 is( $count, 25000, "It contains 25000 URLs");
203
204 # Cleanup
205 unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /;