Bug 20727: Move temporary_directory() to C4::Context
[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 use Carp qw/croak carp/;
28
29 use Koha::UploadedFile;
30
31 BEGIN {
32     use_ok('Koha::Sitemapper');
33     use_ok('Koha::Sitemapper::Writer');
34 }
35
36 my $now_value       = DateTime->now();
37 my $mocked_datetime = Test::MockModule->new('DateTime');
38 $mocked_datetime->mock( 'now', sub { return $now_value; } );
39
40 sub slurp {
41     my $file = shift;
42     open my $fh, '<', $file or croak;
43     local $/ = undef;
44     my $cont = <$fh>;
45     close $fh;
46     return $cont;
47 }
48
49 use Test::DBIx::Class;
50
51 sub fixtures {
52     my ($data) = @_;
53     fixtures_ok [
54         Biblio => [ [qw/ biblionumber datecreated timestamp  /], @{$data}, ],
55     ], 'add fixtures';
56     return;
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
63     # Schema() gives us the DB connection set up by Test::DBIx::Class
64     _new_schema => sub { return Schema(); }
65 );
66
67 my $dir = C4::Context::temporary_directory;
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
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 # 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 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
150 # url'. Test that 3 files are created: index file + 2 urls file with
151 # respectively 50000 et 25000 urls.
152 $data = [];
153 for my $count ( 3 .. 75_000 ) {
154     push @{$data}, [ $count, '2015-08-31', '2015-08-31' ];
155 }
156 fixtures($data);
157 $sitemapper = Koha::Sitemapper->new(
158     verbose => 0,
159     url     => 'http://www.mylibrary.org',
160     dir     => $dir,
161     short   => 1,
162 );
163 $sitemapper->run();
164
165 $file = "$dir/sitemapindex.xml";
166 ok( -e "$dir/sitemapindex.xml",
167     'File sitemapindex.xml for 75000 bibs created' );
168 $file_content     = slurp($file);
169 $expected_content = <<"EOS";
170 <?xml version="1.0" encoding="UTF-8"?>
171
172 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
173   <sitemap>
174     <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
175     <lastmod>$now</lastmod>
176   </sitemap>
177   <sitemap>
178     <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
179     <lastmod>$now</lastmod>
180   </sitemap>
181 </sitemapindex>
182 EOS
183 chop $expected_content;
184 is( $file_content, $expected_content, 'Its content is valid' );
185
186 $file = "$dir/sitemap0001.xml";
187 ok( -e $file, 'File sitemap0001.xml created' );
188
189 open my $fh, '<', $file or croak;
190 my $count = 0;
191 while (<$fh>) {
192     if ( $_ =~ /<loc>/xsm ) { $count++; }
193 }
194 close $fh;
195 is( $count, 50_000, 'It contains 50000 URLs' );
196
197 $file = "$dir/sitemap0002.xml";
198 ok( -e $file, 'File sitemap0002.xml created' );
199
200 open $fh, '<', $file or croak;
201 $count = 0;
202 while (<$fh>) {
203     if ( $_ =~ /<loc>/xsm ) { $count++; }
204 }
205 close $fh;
206 is( $count, 25_000, 'It contains 25000 URLs' );
207
208 # Cleanup
209 for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
210     unlink "$dir/$file";
211 }