View | Details | Raw Unified | Return to bug 33870
Collapse All | Expand All

(-)a/t/db_dependent/Sitemapper.t (-66 / +49 lines)
Lines 18-76 Link Here
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
use Test::More tests => 1;
22
use Carp qw/croak/;
21
use File::Basename;
23
use File::Basename;
22
use File::Path;
24
use File::Path;
23
use DateTime;
25
use File::Slurp qw( read_file );
24
use Koha::DateUtils qw( dt_from_string );
25
use Test::MockModule;
26
use Test::MockModule;
26
use Test::More tests => 16;
27
use Carp qw/croak carp/;
28
29
27
30
BEGIN {
28
use t::lib::TestBuilder;
31
    use_ok('Koha::Sitemapper');
29
use t::lib::Mocks;
32
    use_ok('Koha::Sitemapper::Writer');
33
}
34
30
35
my $now_value       = dt_from_string();
31
use Koha::Database;
36
my $mocked_datetime = Test::MockModule->new('DateTime');
32
use Koha::DateUtils qw( dt_from_string );
37
$mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
33
use Koha::Biblios;
38
34
use Koha::Checkouts;
39
sub slurp {
35
use Koha::Sitemapper;
40
    my $file = shift;
36
use Koha::Sitemapper::Writer;
41
    open my $fh, '<', $file or croak;
37
42
    local $/ = undef;
38
my $schema  = Koha::Database->new->schema;
43
    my $cont = <$fh>;
39
my $builder = t::lib::TestBuilder->new;
44
    close $fh;
40
$schema->storage->txn_begin;
45
    return $cont;
41
46
}
42
subtest 'Sitemapper' => sub {
47
43
    plan tests => 12;
48
use Test::DBIx::Class;
44
49
45
my $now = dt_from_string()->ymd;
50
sub fixtures {
46
51
    my ($data) = @_;
47
# FIXME Would be nice to remove both deletes again
52
    fixtures_ok [
48
Koha::Checkouts->delete;
53
        Biblio => [ [qw/ biblionumber datecreated timestamp  /], @{$data}, ],
49
Koha::Biblios->delete;
54
    ], 'add fixtures';
50
my $biblio1 = $builder->build_sample_biblio;
55
    return;
51
$biblio1->set({ datecreated => '2013-11-15', timestamp => '2013-11-15' })->store;
56
}
52
my $id1 = $biblio1->id;
57
53
my $biblio2 = $builder->build_sample_biblio;
58
# Make the code in the module use our mocked Koha::Schema/Koha::Database
54
$biblio2->set({ datecreated => '2015-08-31', timestamp => '2015-08-31' })->store;
59
my $db = Test::MockModule->new('Koha::Database');
55
my $id2 = $biblio2->id;
60
$db->mock(
61
62
    # Schema() gives us the DB connection set up by Test::DBIx::Class
63
    _new_schema => sub { return Schema(); }
64
);
65
56
66
my $dir = C4::Context::temporary_directory;
57
my $dir = C4::Context::temporary_directory;
67
58
68
my $data = [
69
    [qw/ 1         2013-11-15 2013-11-15/],
70
    [qw/ 2         2015-08-31 2015-08-31/],
71
];
72
fixtures($data);
73
74
# Create a sitemap for a catalog containg 2 biblios, with option 'long url'
59
# Create a sitemap for a catalog containg 2 biblios, with option 'long url'
75
my $sitemapper = Koha::Sitemapper->new(
60
my $sitemapper = Koha::Sitemapper->new(
76
    verbose => 0,
61
    verbose => 0,
Lines 82-89 $sitemapper->run(); Link Here
82
67
83
my $file = "$dir/sitemapindex.xml";
68
my $file = "$dir/sitemapindex.xml";
84
ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml created' );
69
ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml created' );
85
my $file_content     = slurp($file);
70
my $file_content     = read_file($file);
86
my $now              = DateTime->now->ymd;
87
my $expected_content = <<"EOS";
71
my $expected_content = <<"EOS";
88
<?xml version="1.0" encoding="UTF-8"?>
72
<?xml version="1.0" encoding="UTF-8"?>
89
73
Lines 99-115 is( $file_content, $expected_content, 'Its content is valid' ); Link Here
99
83
100
$file = "$dir/sitemap0001.xml";
84
$file = "$dir/sitemap0001.xml";
101
ok( -e $file, 'File sitemap0001.xml created' );
85
ok( -e $file, 'File sitemap0001.xml created' );
102
$file_content     = slurp($file);
86
$file_content     = read_file($file);
103
$expected_content = <<"EOS";
87
$expected_content = <<"EOS";
104
<?xml version="1.0" encoding="UTF-8"?>
88
<?xml version="1.0" encoding="UTF-8"?>
105
89
106
<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">
90
<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
  <url>
91
  <url>
108
    <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=1</loc>
92
    <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=$id1</loc>
109
    <lastmod>2013-11-15</lastmod>
93
    <lastmod>2013-11-15</lastmod>
110
  </url>
94
  </url>
111
  <url>
95
  <url>
112
    <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=2</loc>
96
    <loc>http://www.mylibrary.org/cgi-bin/koha/opac-detail.pl?biblionumber=$id2</loc>
113
    <lastmod>2015-08-31</lastmod>
97
    <lastmod>2015-08-31</lastmod>
114
  </url>
98
  </url>
115
</urlset>
99
</urlset>
Lines 128-158 $sitemapper->run(); Link Here
128
112
129
$file = "$dir/sitemap0001.xml";
113
$file = "$dir/sitemap0001.xml";
130
ok( -e $file, 'File sitemap0001.xml with short URLs created' );
114
ok( -e $file, 'File sitemap0001.xml with short URLs created' );
131
$file_content     = slurp($file);
115
$file_content     = read_file($file);
132
$expected_content = <<"EOS";
116
$expected_content = <<"EOS";
133
<?xml version="1.0" encoding="UTF-8"?>
117
<?xml version="1.0" encoding="UTF-8"?>
134
118
135
<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">
119
<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">
136
  <url>
120
  <url>
137
    <loc>http://www.mylibrary.org/bib/1</loc>
121
    <loc>http://www.mylibrary.org/bib/$id1</loc>
138
    <lastmod>2013-11-15</lastmod>
122
    <lastmod>2013-11-15</lastmod>
139
  </url>
123
  </url>
140
  <url>
124
  <url>
141
    <loc>http://www.mylibrary.org/bib/2</loc>
125
    <loc>http://www.mylibrary.org/bib/$id2</loc>
142
    <lastmod>2015-08-31</lastmod>
126
    <lastmod>2015-08-31</lastmod>
143
  </url>
127
  </url>
144
</urlset>
128
</urlset>
145
EOS
129
EOS
146
is( $file_content, $expected_content, 'Its content is valid' );
130
is( $file_content, $expected_content, 'Its content is valid' );
147
131
148
# Create a sitemap for a catalog containing 75000 biblios, with option 'short
132
# No need to create 75000 biblios here. Let's create 10 with $MAX == 6.
149
# url'. Test that 3 files are created: index file + 2 urls file with
133
# Expecting 3 files: index plus 2 url files with 6 and 4 urls.
150
# respectively 50000 et 25000 urls.
134
$Koha::Sitemapper::Writer::MAX = 6;
151
$data = [];
135
for my $count ( 3..10 ) {
152
for my $count ( 3 .. 75_000 ) {
136
    my $biblio2 = $builder->build_sample_biblio->set({ datecreated => '2015-08-31', timestamp => '2015-08-31' })->store;
153
    push @{$data}, [ $count, '2015-08-31', '2015-08-31' ];
154
}
137
}
155
fixtures($data);
138
156
$sitemapper = Koha::Sitemapper->new(
139
$sitemapper = Koha::Sitemapper->new(
157
    verbose => 0,
140
    verbose => 0,
158
    url     => 'http://www.mylibrary.org',
141
    url     => 'http://www.mylibrary.org',
Lines 162-170 $sitemapper = Koha::Sitemapper->new( Link Here
162
$sitemapper->run();
145
$sitemapper->run();
163
146
164
$file = "$dir/sitemapindex.xml";
147
$file = "$dir/sitemapindex.xml";
165
ok( -e "$dir/sitemapindex.xml",
148
ok( -e "$dir/sitemapindex.xml", 'File sitemapindex.xml for 10 bibs created' );
166
    'File sitemapindex.xml for 75000 bibs created' );
149
$file_content     = read_file($file);
167
$file_content     = slurp($file);
168
$expected_content = <<"EOS";
150
$expected_content = <<"EOS";
169
<?xml version="1.0" encoding="UTF-8"?>
151
<?xml version="1.0" encoding="UTF-8"?>
170
152
Lines 191-197 while (<$fh>) { Link Here
191
    if ( $_ =~ /<loc>/xsm ) { $count++; }
173
    if ( $_ =~ /<loc>/xsm ) { $count++; }
192
}
174
}
193
close $fh;
175
close $fh;
194
is( $count, 50_000, 'It contains 50000 URLs' );
176
is( $count, 6, 'It contains 6 URLs' );
195
177
196
$file = "$dir/sitemap0002.xml";
178
$file = "$dir/sitemap0002.xml";
197
ok( -e $file, 'File sitemap0002.xml created' );
179
ok( -e $file, 'File sitemap0002.xml created' );
Lines 202-210 while (<$fh>) { Link Here
202
    if ( $_ =~ /<loc>/xsm ) { $count++; }
184
    if ( $_ =~ /<loc>/xsm ) { $count++; }
203
}
185
}
204
close $fh;
186
close $fh;
205
is( $count, 25_000, 'It contains 25000 URLs' );
187
is( $count, 4, 'It contains 4 URLs' );
206
188
207
# Cleanup
189
# Cleanup
208
for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
190
for my $file (qw/sitemapindex.xml sitemap0001.xml sitemap0002.xml/) {
209
    unlink "$dir/$file";
191
    unlink "$dir/$file";
210
}
192
}
211
- 
193
};
194
$schema->storage->txn_rollback;

Return to bug 33870