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;
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; } );
40 open my $fh, '<', $file or die;
47 use Test::DBIx::Class;
53 [ qw/ biblionumber datecreated timestamp / ],
59 # Make the code in the module use our mocked Koha::Schema/Koha::Database
60 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 = File::Spec->tmpdir();
70 [ qw/ 1 2013-11-15 2013-11-15/ ],
71 [ 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" );
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(
124 url => 'http://www.mylibrary.org',
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"?>
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">
138 <loc>http://www.mylibrary.org/bib/1</loc>
139 <lastmod>2013-11-15</lastmod>
142 <loc>http://www.mylibrary.org/bib/2</loc>
143 <lastmod>2015-08-31</lastmod>
147 is( $file_content, $expected_content, "Its content is valid" );
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.
154 push @$data, [ $_, '2015-08-31', '2015-08-31'] for 3..75000;
156 $sitemapper = Koha::Sitemapper->new(
158 url => 'http://www.mylibrary.org',
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"?>
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;
192 is( $count, 50000, "It contains 50000 URLs");
194 $file = "$dir/sitemap0002.xml";
195 ok( -e $file, "File sitemap0002.xml created");
197 open $fh, "<", $file;
202 is( $count, 25000, "It contains 25000 URLs");
205 unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /;