Bug 35556: Fix random selenium failure if category on second page
[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
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21 use Test::More tests => 1;
22 use Carp qw/croak/;
23 use File::Basename;
24 use File::Path;
25 use File::Slurp qw( read_file );
26 use Test::MockModule;
27
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 use Koha::Database;
32 use Koha::DateUtils qw( dt_from_string );
33 use Koha::Sitemapper;
34 use Koha::Sitemapper::Writer;
35
36 my $schema  = Koha::Database->new->schema;
37 my $builder = t::lib::TestBuilder->new;
38 $schema->storage->txn_begin;
39
40 subtest 'Sitemapper' => sub {
41     plan tests => 12;
42
43     my $now = dt_from_string()->ymd;
44
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;
51
52     my $dir = C4::Context::temporary_directory;
53
54     # Create a sitemap for a catalog containg 2 biblios, with option 'long url'
55     my $sitemapper = Koha::Sitemapper->new(
56         verbose => 0,
57         url     => 'http://www.mylibrary.org',
58         dir     => $dir,
59         short   => 0,
60     );
61     $sitemapper->run( "biblionumber>=$id1" );
62
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"?>
68
69 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
70   <sitemap>
71     <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
72     <lastmod>$now</lastmod>
73   </sitemap>
74 </sitemapindex>
75 EOS
76     chop $expected_content;
77     is( $file_content, $expected_content, 'Its content is valid' );
78
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"?>
84
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">
86   <url>
87     <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=$id1</loc>
88     <lastmod>2013-11-15</lastmod>
89   </url>
90   <url>
91     <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=$id2</loc>
92     <lastmod>2015-08-31</lastmod>
93   </url>
94 </urlset>
95 EOS
96     is( $file_content, $expected_content, 'Its content is valid' );
97
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(
101         verbose => 0,
102         url     => 'http://www.mylibrary.org',
103         dir     => $dir,
104         short   => 1,
105     );
106     $sitemapper->run( "biblionumber>=$id1" );
107
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"?>
113
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">
115   <url>
116     <loc>http://www.mylibrary.org/bib/$id1</loc>
117     <lastmod>2013-11-15</lastmod>
118   </url>
119   <url>
120     <loc>http://www.mylibrary.org/bib/$id2</loc>
121     <lastmod>2015-08-31</lastmod>
122   </url>
123 </urlset>
124 EOS
125     is( $file_content, $expected_content, 'Its content is valid' );
126
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;
132     }
133
134     $sitemapper = Koha::Sitemapper->new(
135         verbose => 0,
136         url     => 'http://www.mylibrary.org',
137         dir     => $dir,
138         short   => 1,
139     );
140     $sitemapper->run( "biblionumber>$id2" ); # Note: new filter
141
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"?>
147
148 <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
149   <sitemap>
150     <loc>http://www.mylibrary.org/sitemap0001.xml</loc>
151     <lastmod>$now</lastmod>
152   </sitemap>
153   <sitemap>
154     <loc>http://www.mylibrary.org/sitemap0002.xml</loc>
155     <lastmod>$now</lastmod>
156   </sitemap>
157 </sitemapindex>
158 EOS
159     chop $expected_content;
160     is( $file_content, $expected_content, 'Its content is valid' );
161
162     $file = "$dir/sitemap0001.xml";
163     ok( -e $file, 'File sitemap0001.xml created' );
164
165     open my $fh, '<', $file or croak;
166     my $count = 0;
167     while (<$fh>) {
168         if ( $_ =~ /<loc>/xsm ) { $count++; }
169     }
170     close $fh;
171     is( $count, 6, 'It contains 6 URLs' );
172
173     $file = "$dir/sitemap0002.xml";
174     ok( -e $file, 'File sitemap0002.xml created' );
175
176     open $fh, '<', $file or croak;
177     $count = 0;
178     while (<$fh>) {
179         if ( $_ =~ /<loc>/xsm ) { $count++; }
180     }
181     close $fh;
182     is( $count, 4, 'It contains 4 URLs' );
183
184     # Cleanup
185     for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
186         unlink "$dir/$file";
187     }
188 };
189 $schema->storage->txn_rollback;