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