|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright 2015 Tamil s.a.r.l. |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 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 |
| 10 |
# version. |
| 11 |
# |
| 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. |
| 15 |
# |
| 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. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Test::MockModule; |
| 22 |
use File::Basename; |
| 23 |
use File::Path; |
| 24 |
use DateTime; |
| 25 |
use Test::More tests => 14; |
| 26 |
|
| 27 |
|
| 28 |
BEGIN { |
| 29 |
use_ok('Koha::Sitemapper'); |
| 30 |
use_ok('Koha::Sitemapper::Writer'); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
sub slurp { |
| 35 |
my $file = shift; |
| 36 |
open my $fh, '<', $file or die; |
| 37 |
local $/ = undef; |
| 38 |
my $cont = <$fh>; |
| 39 |
close $fh; |
| 40 |
return $cont; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 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"; |
| 49 |
return $dbh |
| 50 |
}); |
| 51 |
my $dbh = C4::Context->dbh(); |
| 52 |
my $two_bibs = [ |
| 53 |
[ qw/ biblionumber timestamp / ], |
| 54 |
[ qw/ 1234 2013-11-15 / ], |
| 55 |
[ qw/ 9875 2015-08-31 / ], |
| 56 |
]; |
| 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; |
| 62 |
|
| 63 |
my $dir = File::Spec->rel2abs( dirname(__FILE__) ); |
| 64 |
|
| 65 |
# Create a sitemap for a catalog containg 2 biblios, with option 'long url' |
| 66 |
my $sitemaper = Koha::Sitemapper->new( |
| 67 |
verbose => 0, |
| 68 |
url => 'http://www.mylibrary.org', |
| 69 |
dir => $dir, |
| 70 |
short => 0, |
| 71 |
); |
| 72 |
$sitemaper->run(); |
| 73 |
|
| 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"?> |
| 80 |
|
| 81 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 82 |
<sitemap> |
| 83 |
<loc>http://www.mylibrary.org/sitemap0001.xml</loc> |
| 84 |
<lastmod>$now</lastmod> |
| 85 |
</sitemap> |
| 86 |
</sitemapindex> |
| 87 |
EOS |
| 88 |
chop $expected_content; |
| 89 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
| 90 |
|
| 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"?> |
| 96 |
|
| 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"> |
| 98 |
<url> |
| 99 |
<loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1234</loc> |
| 100 |
<lastmod>2013-11-15</lastmod> |
| 101 |
</url> |
| 102 |
<url> |
| 103 |
<loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=9875</loc> |
| 104 |
<lastmod>2015-08-31</lastmod> |
| 105 |
</url> |
| 106 |
</urlset> |
| 107 |
EOS |
| 108 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
| 109 |
|
| 110 |
|
| 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( |
| 114 |
verbose => 0, |
| 115 |
url => 'http://www.mylibrary.org', |
| 116 |
dir => $dir, |
| 117 |
short => 1, |
| 118 |
); |
| 119 |
$sitemaper->run(); |
| 120 |
|
| 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"?> |
| 126 |
|
| 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"> |
| 128 |
<url> |
| 129 |
<loc>http://www.mylibrary.org/bib/1234</loc> |
| 130 |
<lastmod>2013-11-15</lastmod> |
| 131 |
</url> |
| 132 |
<url> |
| 133 |
<loc>http://www.mylibrary.org/bib/9875</loc> |
| 134 |
<lastmod>2015-08-31</lastmod> |
| 135 |
</url> |
| 136 |
</urlset> |
| 137 |
EOS |
| 138 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
| 139 |
|
| 140 |
|
| 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( |
| 145 |
verbose => 0, |
| 146 |
url => 'http://www.mylibrary.org', |
| 147 |
dir => $dir, |
| 148 |
short => 1, |
| 149 |
); |
| 150 |
$sitemaper->run(); |
| 151 |
|
| 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"?> |
| 157 |
|
| 158 |
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 159 |
<sitemap> |
| 160 |
<loc>http://www.mylibrary.org/sitemap0001.xml</loc> |
| 161 |
<lastmod>$now</lastmod> |
| 162 |
</sitemap> |
| 163 |
<sitemap> |
| 164 |
<loc>http://www.mylibrary.org/sitemap0002.xml</loc> |
| 165 |
<lastmod>$now</lastmod> |
| 166 |
</sitemap> |
| 167 |
</sitemapindex> |
| 168 |
EOS |
| 169 |
chop $expected_content; |
| 170 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
| 171 |
|
| 172 |
$file = "$dir/sitemap0001.xml"; |
| 173 |
ok( -e $file, "File sitemap0001.xml created"); |
| 174 |
|
| 175 |
open my $fh, "<", $file; |
| 176 |
my $count = 0; |
| 177 |
while (<$fh>) { |
| 178 |
$count++ if /<loc>/; |
| 179 |
} |
| 180 |
ok ( $count == 50000, "It contains 50000 URLs"); |
| 181 |
|
| 182 |
$file = "$dir/sitemap0002.xml"; |
| 183 |
ok( -e $file, "File sitemap0002.xml created"); |
| 184 |
|
| 185 |
open $fh, "<", $file; |
| 186 |
$count = 0; |
| 187 |
while (<$fh>) { |
| 188 |
$count++ if /<loc>/; |
| 189 |
} |
| 190 |
ok ( $count == 25000, "It contains 25000 URLs"); |
| 191 |
|
| 192 |
# Cleanup |
| 193 |
unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /; |