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
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
26 use Test
::More tests
=> 16;
27 use Carp qw
/croak carp/;
31 use_ok
('Koha::Sitemapper');
32 use_ok
('Koha::Sitemapper::Writer');
35 my $now_value = dt_from_string
();
36 my $mocked_datetime = Test
::MockModule
->new('DateTime');
37 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
41 open my $fh, '<', $file or croak
;
48 use Test
::DBIx
::Class
;
53 Biblio
=> [ [qw
/ biblionumber datecreated timestamp /], @
{$data}, ],
58 # Make the code in the module use our mocked Koha::Schema/Koha::Database
59 my $db = Test
::MockModule
->new('Koha::Database');
62 # Schema() gives us the DB connection set up by Test::DBIx::Class
63 _new_schema
=> sub { return Schema
(); }
66 my $dir = C4
::Context
::temporary_directory
;
69 [qw
/ 1 2013-11-15 2013-11-15/],
70 [qw
/ 2 2015-08-31 2015-08-31/],
74 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
75 my $sitemapper = Koha
::Sitemapper
->new(
77 url
=> 'http://www.mylibrary.org',
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"?>
90 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
92 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
93 <lastmod>$now</lastmod>
97 chop $expected_content;
98 is
( $file_content, $expected_content, 'Its content is valid' );
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"?>
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">
108 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
109 <lastmod>2013-11-15</lastmod>
112 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
113 <lastmod>2015-08-31</lastmod>
117 is
( $file_content, $expected_content, 'Its content is valid' );
119 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
120 # Test that 2 files are created.
121 $sitemapper = Koha
::Sitemapper
->new(
123 url
=> 'http://www.mylibrary.org',
129 $file = "$dir/sitemap0001.xml";
130 ok
( -e
$file, 'File sitemap0001.xml with short URLs created' );
131 $file_content = slurp
($file);
132 $expected_content = <<"EOS";
133 <?xml version="1.0" encoding="UTF-8"?>
135 <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 <loc>http://www.mylibrary.org/bib/1</loc>
138 <lastmod>2013-11-15</lastmod>
141 <loc>http://www.mylibrary.org/bib/2</loc>
142 <lastmod>2015-08-31</lastmod>
146 is
( $file_content, $expected_content, 'Its content is valid' );
148 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
149 # url'. Test that 3 files are created: index file + 2 urls file with
150 # respectively 50000 et 25000 urls.
152 for my $count ( 3 .. 75_000
) {
153 push @
{$data}, [ $count, '2015-08-31', '2015-08-31' ];
156 $sitemapper = Koha
::Sitemapper
->new(
158 url
=> 'http://www.mylibrary.org',
164 $file = "$dir/sitemapindex.xml";
165 ok
( -e
"$dir/sitemapindex.xml",
166 'File sitemapindex.xml for 75000 bibs created' );
167 $file_content = slurp
($file);
168 $expected_content = <<"EOS";
169 <?xml version="1.0" encoding="UTF-8"?>
171 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
173 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
174 <lastmod>$now</lastmod>
177 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
178 <lastmod>$now</lastmod>
182 chop $expected_content;
183 is
( $file_content, $expected_content, 'Its content is valid' );
185 $file = "$dir/sitemap0001.xml";
186 ok
( -e
$file, 'File sitemap0001.xml created' );
188 open my $fh, '<', $file or croak
;
191 if ( $_ =~ /<loc>/xsm ) { $count++; }
194 is
( $count, 50_000
, 'It contains 50000 URLs' );
196 $file = "$dir/sitemap0002.xml";
197 ok
( -e
$file, 'File sitemap0002.xml created' );
199 open $fh, '<', $file or croak
;
202 if ( $_ =~ /<loc>/xsm ) { $count++; }
205 is
( $count, 25_000
, 'It contains 25000 URLs' );
208 for my $file (qw
/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {