Lines 18-28
Link Here
|
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
19 |
|
19 |
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
use Test::MockModule; |
|
|
22 |
use File::Basename; |
21 |
use File::Basename; |
23 |
use File::Path; |
22 |
use File::Path; |
24 |
use DateTime; |
23 |
use DateTime; |
25 |
use Test::More tests => 14; |
24 |
use Test::MockModule; |
|
|
25 |
use Test::More tests => 16; |
26 |
use Koha::Schema; |
26 |
|
27 |
|
27 |
|
28 |
|
28 |
BEGIN { |
29 |
BEGIN { |
Lines 30-36
BEGIN {
Link Here
|
30 |
use_ok('Koha::Sitemapper::Writer'); |
31 |
use_ok('Koha::Sitemapper::Writer'); |
31 |
} |
32 |
} |
32 |
|
33 |
|
33 |
|
|
|
34 |
sub slurp { |
34 |
sub slurp { |
35 |
my $file = shift; |
35 |
my $file = shift; |
36 |
open my $fh, '<', $file or die; |
36 |
open my $fh, '<', $file or die; |
Lines 40-67
sub slurp {
Link Here
|
40 |
return $cont; |
40 |
return $cont; |
41 |
} |
41 |
} |
42 |
|
42 |
|
|
|
43 |
use Test::DBIx::Class { |
44 |
schema_class => 'Koha::Schema', |
45 |
connect_info => ['dbi:SQLite:dbname=:memory:','',''], |
46 |
connect_opts => { name_sep => '.', quote_char => '`', }, |
47 |
fixture_class => '::Populate', |
48 |
}, 'Biblio' ; |
49 |
|
50 |
sub fixtures { |
51 |
my ( $data ) = @_; |
52 |
fixtures_ok [ |
53 |
Biblio => [ |
54 |
[ qw/ biblionumber datecreated timestamp / ], |
55 |
@$data, |
56 |
], |
57 |
], 'add fixtures'; |
58 |
} |
43 |
|
59 |
|
44 |
# Create 3 mocked dataset to be used by Koha::Sitemaper in place of DB content |
60 |
# Make the code in the module use our mocked Koha::Schema/Koha::Database |
45 |
my $module_context = new Test::MockModule('C4::Context'); |
61 |
my $db = Test::MockModule->new('Koha::Database'); |
46 |
$module_context->mock('_new_dbh', sub { |
62 |
$db->mock( |
47 |
my $dbh = DBI->connect( 'DBI:Mock:', '', '' ) |
63 |
# Schema() gives us the DB connection set up by Test::DBIx::Class |
48 |
|| die "Cannot create handle: $DBI::errstr\n"; |
64 |
_new_schema => sub { return Schema(); } |
49 |
return $dbh |
65 |
); |
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 |
|
66 |
|
63 |
my $dir = File::Spec->rel2abs( dirname(__FILE__) ); |
67 |
my $dir = File::Spec->rel2abs( dirname(__FILE__) ); |
64 |
|
68 |
|
|
|
69 |
|
70 |
my $data = [ |
71 |
[ qw/ 1 2013-11-15 2013-11-15/ ], |
72 |
[ qw/ 2 2015-08-31 2015-08-31/ ], |
73 |
]; |
74 |
fixtures($data); |
65 |
# Create a sitemap for a catalog containg 2 biblios, with option 'long url' |
75 |
# Create a sitemap for a catalog containg 2 biblios, with option 'long url' |
66 |
my $sitemaper = Koha::Sitemapper->new( |
76 |
my $sitemaper = Koha::Sitemapper->new( |
67 |
verbose => 0, |
77 |
verbose => 0, |
Lines 86-92
my $expected_content = <<EOS;
Link Here
|
86 |
</sitemapindex> |
96 |
</sitemapindex> |
87 |
EOS |
97 |
EOS |
88 |
chop $expected_content; |
98 |
chop $expected_content; |
89 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
99 |
is( $file_content, $expected_content, "Its content is valid" ); |
90 |
|
100 |
|
91 |
$file = "$dir/sitemap0001.xml"; |
101 |
$file = "$dir/sitemap0001.xml"; |
92 |
ok( -e $file, "File sitemap0001.xml created"); |
102 |
ok( -e $file, "File sitemap0001.xml created"); |
Lines 96-111
$expected_content = <<EOS;
Link Here
|
96 |
|
106 |
|
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"> |
107 |
<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> |
108 |
<url> |
99 |
<loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1234</loc> |
109 |
<loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc> |
100 |
<lastmod>2013-11-15</lastmod> |
110 |
<lastmod>2013-11-15</lastmod> |
101 |
</url> |
111 |
</url> |
102 |
<url> |
112 |
<url> |
103 |
<loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=9875</loc> |
113 |
<loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc> |
104 |
<lastmod>2015-08-31</lastmod> |
114 |
<lastmod>2015-08-31</lastmod> |
105 |
</url> |
115 |
</url> |
106 |
</urlset> |
116 |
</urlset> |
107 |
EOS |
117 |
EOS |
108 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
118 |
is( $file_content, $expected_content, "Its content is valid" ); |
109 |
|
119 |
|
110 |
|
120 |
|
111 |
# Create a sitemap for a catalog containg 2 biblios, with option 'short url'. |
121 |
# Create a sitemap for a catalog containg 2 biblios, with option 'short url'. |
Lines 126-146
$expected_content = <<EOS;
Link Here
|
126 |
|
136 |
|
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"> |
137 |
<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> |
138 |
<url> |
129 |
<loc>http://www.mylibrary.org/bib/1234</loc> |
139 |
<loc>http://www.mylibrary.org/bib/1</loc> |
130 |
<lastmod>2013-11-15</lastmod> |
140 |
<lastmod>2013-11-15</lastmod> |
131 |
</url> |
141 |
</url> |
132 |
<url> |
142 |
<url> |
133 |
<loc>http://www.mylibrary.org/bib/9875</loc> |
143 |
<loc>http://www.mylibrary.org/bib/2</loc> |
134 |
<lastmod>2015-08-31</lastmod> |
144 |
<lastmod>2015-08-31</lastmod> |
135 |
</url> |
145 |
</url> |
136 |
</urlset> |
146 |
</urlset> |
137 |
EOS |
147 |
EOS |
138 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
148 |
is( $file_content, $expected_content, "Its content is valid" ); |
139 |
|
149 |
|
140 |
|
150 |
|
141 |
# Create a sitemap for a catalog containing 75000 biblios, with option 'short |
151 |
# 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 |
152 |
# url'. Test that 3 files are created: index file + 2 urls file with |
143 |
# respectively 50000 et 25000 urls. |
153 |
# respectively 50000 et 25000 urls. |
|
|
154 |
$data = []; |
155 |
push @$data, [ $_, '2015-08-31', '2015-08-31'] for 3..75000; |
156 |
fixtures($data); |
144 |
$sitemaper = Koha::Sitemapper->new( |
157 |
$sitemaper = Koha::Sitemapper->new( |
145 |
verbose => 0, |
158 |
verbose => 0, |
146 |
url => 'http://www.mylibrary.org', |
159 |
url => 'http://www.mylibrary.org', |
Lines 167-173
$expected_content = <<EOS;
Link Here
|
167 |
</sitemapindex> |
180 |
</sitemapindex> |
168 |
EOS |
181 |
EOS |
169 |
chop $expected_content; |
182 |
chop $expected_content; |
170 |
ok( $file_content eq $expected_content, "Its content is valid" ); |
183 |
is( $file_content, $expected_content, "Its content is valid" ); |
171 |
|
184 |
|
172 |
$file = "$dir/sitemap0001.xml"; |
185 |
$file = "$dir/sitemap0001.xml"; |
173 |
ok( -e $file, "File sitemap0001.xml created"); |
186 |
ok( -e $file, "File sitemap0001.xml created"); |
Lines 177-183
my $count = 0;
Link Here
|
177 |
while (<$fh>) { |
190 |
while (<$fh>) { |
178 |
$count++ if /<loc>/; |
191 |
$count++ if /<loc>/; |
179 |
} |
192 |
} |
180 |
ok ( $count == 50000, "It contains 50000 URLs"); |
193 |
is( $count, 50000, "It contains 50000 URLs"); |
181 |
|
194 |
|
182 |
$file = "$dir/sitemap0002.xml"; |
195 |
$file = "$dir/sitemap0002.xml"; |
183 |
ok( -e $file, "File sitemap0002.xml created"); |
196 |
ok( -e $file, "File sitemap0002.xml created"); |
Lines 187-193
$count = 0;
Link Here
|
187 |
while (<$fh>) { |
200 |
while (<$fh>) { |
188 |
$count++ if /<loc>/; |
201 |
$count++ if /<loc>/; |
189 |
} |
202 |
} |
190 |
ok ( $count == 25000, "It contains 25000 URLs"); |
203 |
is( $count, 25000, "It contains 25000 URLs"); |
191 |
|
204 |
|
192 |
# Cleanup |
205 |
# Cleanup |
193 |
unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /; |
206 |
unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /; |
194 |
- |
|
|