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 => 14;
29 use_ok('Koha::Sitemapper');
30 use_ok('Koha::Sitemapper::Writer');
36 open my $fh, '<', $file or die;
44 # Create 3 mocked dataset to be used by Koha::Sitemaper in place of DB content
45 my $module_context = new Test::MockModule('C4::Context');
46 $module_context->mock('_new_dbh', sub {
47 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
48 || die "Cannot create handle: $DBI::errstr\n";
51 my $dbh = C4::Context->dbh();
53 [ qw/ biblionumber timestamp / ],
54 [ qw/ 1234 2013-11-15 / ],
55 [ qw/ 9875 2015-08-31 / ],
57 my $lotof_bibs = [ [ qw/ biblionumber timestamp / ] ];
58 push @$lotof_bibs, [ $_, '2015-08-31' ] for 1..75000;
59 $dbh->{mock_add_resultset} = $two_bibs;
60 $dbh->{mock_add_resultset} = $two_bibs;
61 $dbh->{mock_add_resultset} = $lotof_bibs;
63 my $dir = File::Spec->rel2abs( dirname(__FILE__) );
65 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
66 my $sitemaper = Koha::Sitemapper->new(
68 url => 'http://www.mylibrary.org',
74 my $file = "$dir/sitemapindex.xml";
75 ok( -e "$dir/sitemapindex.xml", "File sitemapindex.xml created");
76 my $file_content = slurp($file);
77 my $now = DateTime->now->ymd;
78 my $expected_content = <<EOS;
79 <?xml version="1.0" encoding="UTF-8"?>
81 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
83 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
84 <lastmod>$now</lastmod>
88 chop $expected_content;
89 ok( $file_content eq $expected_content, "Its content is valid" );
91 $file = "$dir/sitemap0001.xml";
92 ok( -e $file, "File sitemap0001.xml created");
93 $file_content = slurp($file);
94 $expected_content = <<EOS;
95 <?xml version="1.0" encoding="UTF-8"?>
97 <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">
99 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1234</loc>
100 <lastmod>2013-11-15</lastmod>
103 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=9875</loc>
104 <lastmod>2015-08-31</lastmod>
108 ok( $file_content eq $expected_content, "Its content is valid" );
111 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
112 # Test that 2 files are created.
113 $sitemaper = Koha::Sitemapper->new(
115 url => 'http://www.mylibrary.org',
121 $file = "$dir/sitemap0001.xml";
122 ok( -e $file, "File sitemap0001.xml with short URLs created");
123 $file_content = slurp($file);
124 $expected_content = <<EOS;
125 <?xml version="1.0" encoding="UTF-8"?>
127 <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">
129 <loc>http://www.mylibrary.org/bib/1234</loc>
130 <lastmod>2013-11-15</lastmod>
133 <loc>http://www.mylibrary.org/bib/9875</loc>
134 <lastmod>2015-08-31</lastmod>
138 ok( $file_content eq $expected_content, "Its content is valid" );
141 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
142 # url'. Test that 3 files are created: index file + 2 urls file with
143 # respectively 50000 et 25000 urls.
144 $sitemaper = Koha::Sitemapper->new(
146 url => 'http://www.mylibrary.org',
152 $file = "$dir/sitemapindex.xml";
153 ok( -e "$dir/sitemapindex.xml", "File sitemapindex.xml for 75000 bibs created");
154 $file_content = slurp($file);
155 $expected_content = <<EOS;
156 <?xml version="1.0" encoding="UTF-8"?>
158 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
160 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
161 <lastmod>$now</lastmod>
164 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
165 <lastmod>$now</lastmod>
169 chop $expected_content;
170 ok( $file_content eq $expected_content, "Its content is valid" );
172 $file = "$dir/sitemap0001.xml";
173 ok( -e $file, "File sitemap0001.xml created");
175 open my $fh, "<", $file;
180 ok ( $count == 50000, "It contains 50000 URLs");
182 $file = "$dir/sitemap0002.xml";
183 ok( -e $file, "File sitemap0002.xml created");
185 open $fh, "<", $file;
190 ok ( $count == 25000, "It contains 25000 URLs");
193 unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /;