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>.
21 use Test::More tests => 1;
25 use File::Slurp qw( read_file );
28 use t::lib::TestBuilder;
32 use Koha::DateUtils qw( dt_from_string );
34 use Koha::Sitemapper::Writer;
36 my $schema = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
38 $schema->storage->txn_begin;
40 subtest 'Sitemapper' => sub {
43 my $now = dt_from_string()->ymd;
45 my $biblio1 = $builder->build_sample_biblio;
46 $biblio1->set( { datecreated => '2013-11-15', timestamp => '2013-11-15' } )->store;
47 my $id1 = $biblio1->id;
48 my $biblio2 = $builder->build_sample_biblio;
49 $biblio2->set( { datecreated => '2015-08-31', timestamp => '2015-08-31' } )->store;
50 my $id2 = $biblio2->id;
52 my $dir = C4::Context::temporary_directory;
54 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
55 my $sitemapper = Koha::Sitemapper->new(
57 url => 'http://www.mylibrary.org',
61 $sitemapper->run( "biblionumber>=$id1" );
63 my $file = "$dir/sitemapindex.xml";
64 ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml created' );
65 my $file_content = read_file($file);
66 my $expected_content = <<"EOS";
67 <?xml version="1.0" encoding="UTF-8"?>
69 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
71 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
72 <lastmod>$now</lastmod>
76 chop $expected_content;
77 is( $file_content, $expected_content, 'Its content is valid' );
79 $file = "$dir/sitemap0001.xml";
80 ok( -e $file, 'File sitemap0001.xml created' );
81 $file_content = read_file($file);
82 $expected_content = <<"EOS";
83 <?xml version="1.0" encoding="UTF-8"?>
85 <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">
87 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=$id1</loc>
88 <lastmod>2013-11-15</lastmod>
91 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=$id2</loc>
92 <lastmod>2015-08-31</lastmod>
96 is( $file_content, $expected_content, 'Its content is valid' );
98 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
99 # Test that 2 files are created.
100 $sitemapper = Koha::Sitemapper->new(
102 url => 'http://www.mylibrary.org',
106 $sitemapper->run( "biblionumber>=$id1" );
108 $file = "$dir/sitemap0001.xml";
109 ok( -e $file, 'File sitemap0001.xml with short URLs created' );
110 $file_content = read_file($file);
111 $expected_content = <<"EOS";
112 <?xml version="1.0" encoding="UTF-8"?>
114 <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">
116 <loc>http://www.mylibrary.org/bib/$id1</loc>
117 <lastmod>2013-11-15</lastmod>
120 <loc>http://www.mylibrary.org/bib/$id2</loc>
121 <lastmod>2015-08-31</lastmod>
125 is( $file_content, $expected_content, 'Its content is valid' );
127 # No need to create 75000 biblios here. Let's create 10 more with $MAX == 6.
128 # Expecting 3 files: index plus 2 url files with 6 and 4 urls (when we start after biblio2).
129 $Koha::Sitemapper::Writer::MAX = 6;
130 for my $count ( 0..9 ) {
131 my $biblio2 = $builder->build_sample_biblio->set({ datecreated => '2015-08-31', timestamp => '2015-08-31' })->store;
134 $sitemapper = Koha::Sitemapper->new(
136 url => 'http://www.mylibrary.org',
140 $sitemapper->run( "biblionumber>$id2" ); # Note: new filter
142 $file = "$dir/sitemapindex.xml";
143 ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml for 10 bibs created' );
144 $file_content = read_file($file);
145 $expected_content = <<"EOS";
146 <?xml version="1.0" encoding="UTF-8"?>
148 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
150 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
151 <lastmod>$now</lastmod>
154 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
155 <lastmod>$now</lastmod>
159 chop $expected_content;
160 is( $file_content, $expected_content, 'Its content is valid' );
162 $file = "$dir/sitemap0001.xml";
163 ok( -e $file, 'File sitemap0001.xml created' );
165 open my $fh, '<', $file or croak;
168 if ( $_ =~ /<loc>/xsm ) { $count++; }
171 is( $count, 6, 'It contains 6 URLs' );
173 $file = "$dir/sitemap0002.xml";
174 ok( -e $file, 'File sitemap0002.xml created' );
176 open $fh, '<', $file or croak;
179 if ( $_ =~ /<loc>/xsm ) { $count++; }
182 is( $count, 4, 'It contains 4 URLs' );
185 for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
189 $schema->storage->txn_rollback;