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 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
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.
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.
25 use Test
::More tests
=> 16;
26 use Carp qw
/croak carp/;
30 use_ok
('Koha::Sitemapper');
31 use_ok
('Koha::Sitemapper::Writer');
34 my $now_value = DateTime
->now();
35 my $mocked_datetime = Test
::MockModule
->new('DateTime');
36 $mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
40 open my $fh, '<', $file or croak
;
47 use Test
::DBIx
::Class
;
52 Biblio
=> [ [qw
/ biblionumber datecreated timestamp /], @
{$data}, ],
57 # Make the code in the module use our mocked Koha::Schema/Koha::Database
58 my $db = Test
::MockModule
->new('Koha::Database');
61 # Schema() gives us the DB connection set up by Test::DBIx::Class
62 _new_schema
=> sub { return Schema
(); }
65 my $dir = C4
::Context
::temporary_directory
;
68 [qw
/ 1 2013-11-15 2013-11-15/],
69 [qw
/ 2 2015-08-31 2015-08-31/],
73 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
74 my $sitemapper = Koha
::Sitemapper
->new(
76 url
=> 'http://www.mylibrary.org',
82 my $file = "$dir/sitemapindex.xml";
83 ok
( -e
"$dir/sitemapindex.xml", 'File sitemapindex.xml created' );
84 my $file_content = slurp
($file);
85 my $now = DateTime
->now->ymd;
86 my $expected_content = <<"EOS";
87 <?xml version="1.0" encoding="UTF-8"?>
89 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
91 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
92 <lastmod>$now</lastmod>
96 chop $expected_content;
97 is
( $file_content, $expected_content, 'Its content is valid' );
99 $file = "$dir/sitemap0001.xml";
100 ok
( -e
$file, 'File sitemap0001.xml created' );
101 $file_content = slurp
($file);
102 $expected_content = <<"EOS";
103 <?xml version="1.0" encoding="UTF-8"?>
105 <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 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
108 <lastmod>2013-11-15</lastmod>
111 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
112 <lastmod>2015-08-31</lastmod>
116 is
( $file_content, $expected_content, 'Its content is valid' );
118 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
119 # Test that 2 files are created.
120 $sitemapper = Koha
::Sitemapper
->new(
122 url
=> 'http://www.mylibrary.org',
128 $file = "$dir/sitemap0001.xml";
129 ok
( -e
$file, 'File sitemap0001.xml with short URLs created' );
130 $file_content = slurp
($file);
131 $expected_content = <<"EOS";
132 <?xml version="1.0" encoding="UTF-8"?>
134 <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">
136 <loc>http://www.mylibrary.org/bib/1</loc>
137 <lastmod>2013-11-15</lastmod>
140 <loc>http://www.mylibrary.org/bib/2</loc>
141 <lastmod>2015-08-31</lastmod>
145 is
( $file_content, $expected_content, 'Its content is valid' );
147 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
148 # url'. Test that 3 files are created: index file + 2 urls file with
149 # respectively 50000 et 25000 urls.
151 for my $count ( 3 .. 75_000
) {
152 push @
{$data}, [ $count, '2015-08-31', '2015-08-31' ];
155 $sitemapper = Koha
::Sitemapper
->new(
157 url
=> 'http://www.mylibrary.org',
163 $file = "$dir/sitemapindex.xml";
164 ok
( -e
"$dir/sitemapindex.xml",
165 'File sitemapindex.xml for 75000 bibs created' );
166 $file_content = slurp
($file);
167 $expected_content = <<"EOS";
168 <?xml version="1.0" encoding="UTF-8"?>
170 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
172 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
173 <lastmod>$now</lastmod>
176 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
177 <lastmod>$now</lastmod>
181 chop $expected_content;
182 is
( $file_content, $expected_content, 'Its content is valid' );
184 $file = "$dir/sitemap0001.xml";
185 ok
( -e
$file, 'File sitemap0001.xml created' );
187 open my $fh, '<', $file or croak
;
190 if ( $_ =~ /<loc>/xsm ) { $count++; }
193 is
( $count, 50_000
, 'It contains 50000 URLs' );
195 $file = "$dir/sitemap0002.xml";
196 ok
( -e
$file, 'File sitemap0002.xml created' );
198 open $fh, '<', $file or croak
;
201 if ( $_ =~ /<loc>/xsm ) { $count++; }
204 is
( $count, 25_000
, 'It contains 25000 URLs' );
207 for my $file (qw
/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {