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