Bug 17781 - Improper branchcode set during renewal
[koha.git] / t / db_dependent / Sitemapper.t
bloba1ea4f8a56a30c6d885eef871fde24fd6e73237d
1 #!/usr/bin/perl
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
10 # version.
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.
20 use Modern::Perl;
21 use File::Basename;
22 use File::Path;
23 use DateTime;
24 use Test::MockModule;
25 use Test::More tests => 16;
26 use Koha::Schema;
29 BEGIN {
30 use_ok('Koha::Sitemapper');
31 use_ok('Koha::Sitemapper::Writer');
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;
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' ;
50 sub fixtures {
51 my ( $data ) = @_;
52 fixtures_ok [
53 Biblio => [
54 [ qw/ biblionumber datecreated timestamp / ],
55 @$data,
57 ], 'add fixtures';
60 # Make the code in the module use our mocked Koha::Schema/Koha::Database
61 my $db = Test::MockModule->new('Koha::Database');
62 $db->mock(
63 # Schema() gives us the DB connection set up by Test::DBIx::Class
64 _new_schema => sub { return Schema(); }
67 my $dir = File::Spec->rel2abs( dirname(__FILE__) );
70 my $data = [
71 [ qw/ 1 2013-11-15 2013-11-15/ ],
72 [ qw/ 2 2015-08-31 2015-08-31/ ],
74 fixtures($data);
75 # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
76 my $sitemapper = Koha::Sitemapper->new(
77 verbose => 0,
78 url => 'http://www.mylibrary.org',
79 dir => $dir,
80 short => 0,
82 $sitemapper->run();
84 my $file = "$dir/sitemapindex.xml";
85 ok( -e "$dir/sitemapindex.xml", "File sitemapindex.xml created");
86 my $file_content = slurp($file);
87 my $now = DateTime->now->ymd;
88 my $expected_content = <<EOS;
89 <?xml version="1.0" encoding="UTF-8"?>
91 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
92 <sitemap>
93 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
94 <lastmod>$now</lastmod>
95 </sitemap>
96 </sitemapindex>
97 EOS
98 chop $expected_content;
99 is( $file_content, $expected_content, "Its content is valid" );
101 $file = "$dir/sitemap0001.xml";
102 ok( -e $file, "File sitemap0001.xml created");
103 $file_content = slurp($file);
104 $expected_content = <<EOS;
105 <?xml version="1.0" encoding="UTF-8"?>
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">
108 <url>
109 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
110 <lastmod>2013-11-15</lastmod>
111 </url>
112 <url>
113 <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
114 <lastmod>2015-08-31</lastmod>
115 </url>
116 </urlset>
118 is( $file_content, $expected_content, "Its content is valid" );
121 # Create a sitemap for a catalog containg 2 biblios, with option 'short url'.
122 # Test that 2 files are created.
123 $sitemapper = Koha::Sitemapper->new(
124 verbose => 0,
125 url => 'http://www.mylibrary.org',
126 dir => $dir,
127 short => 1,
129 $sitemapper->run();
131 $file = "$dir/sitemap0001.xml";
132 ok( -e $file, "File sitemap0001.xml with short URLs created");
133 $file_content = slurp($file);
134 $expected_content = <<EOS;
135 <?xml version="1.0" encoding="UTF-8"?>
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">
138 <url>
139 <loc>http://www.mylibrary.org/bib/1</loc>
140 <lastmod>2013-11-15</lastmod>
141 </url>
142 <url>
143 <loc>http://www.mylibrary.org/bib/2</loc>
144 <lastmod>2015-08-31</lastmod>
145 </url>
146 </urlset>
148 is( $file_content, $expected_content, "Its content is valid" );
151 # Create a sitemap for a catalog containing 75000 biblios, with option 'short
152 # url'. Test that 3 files are created: index file + 2 urls file with
153 # respectively 50000 et 25000 urls.
154 $data = [];
155 push @$data, [ $_, '2015-08-31', '2015-08-31'] for 3..75000;
156 fixtures($data);
157 $sitemapper = Koha::Sitemapper->new(
158 verbose => 0,
159 url => 'http://www.mylibrary.org',
160 dir => $dir,
161 short => 1,
163 $sitemapper->run();
165 $file = "$dir/sitemapindex.xml";
166 ok( -e "$dir/sitemapindex.xml", "File sitemapindex.xml for 75000 bibs created");
167 $file_content = slurp($file);
168 $expected_content = <<EOS;
169 <?xml version="1.0" encoding="UTF-8"?>
171 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
172 <sitemap>
173 <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
174 <lastmod>$now</lastmod>
175 </sitemap>
176 <sitemap>
177 <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
178 <lastmod>$now</lastmod>
179 </sitemap>
180 </sitemapindex>
182 chop $expected_content;
183 is( $file_content, $expected_content, "Its content is valid" );
185 $file = "$dir/sitemap0001.xml";
186 ok( -e $file, "File sitemap0001.xml created");
188 open my $fh, "<", $file;
189 my $count = 0;
190 while (<$fh>) {
191 $count++ if /<loc>/;
193 is( $count, 50000, "It contains 50000 URLs");
195 $file = "$dir/sitemap0002.xml";
196 ok( -e $file, "File sitemap0002.xml created");
198 open $fh, "<", $file;
199 $count = 0;
200 while (<$fh>) {
201 $count++ if /<loc>/;
203 is( $count, 25000, "It contains 25000 URLs");
205 # Cleanup
206 unlink "$dir/$_" for qw / sitemapindex.xml sitemap0001.xml sitemap0002.xml /;