-
Notifications
You must be signed in to change notification settings - Fork 20
/
basic.t
314 lines (266 loc) · 8.74 KB
/
basic.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
use warnings;
use strict;
use Test::More;
use Path::Class::Dir;
use Path::Class::File;
use URI;
use DateTime;
use FindBin;
use lib "$FindBin::Bin/../lib";
use_ok( 'Plerd' );
# Prepare by making a fresh source directory, based on the source_model directory
# (and throw out said source dir if it's already there from e.g. a botched test)
my $source_dir = Path::Class::Dir->new( "$FindBin::Bin/source" );
$source_dir->rmtree;
$source_dir->mkpath;
my $now = DateTime->now( time_zone => 'local' );
my $ymd = $now->ymd;
my $model_dir = Path::Class::Dir->new( "$FindBin::Bin/source_model" );
foreach ( Path::Class::Dir->new( "$FindBin::Bin/source_model" )->children ) {
my $filename = $_->basename;
$filename =~ s/TODAY/$ymd/;
my $destination = Path::Class::File->new( $source_dir, $filename );
$_->copy_to( $destination );
}
# And then clean out the docroot.
my $docroot_dir = Path::Class::Dir->new( "$FindBin::Bin/docroot" );
$docroot_dir->rmtree;
$docroot_dir->mkpath;
# Now try to make a Plerd object, and send it through its paces.
my $plerd = Plerd->new(
path => $FindBin::Bin,
title => 'Test Blog',
author_name => 'Nobody',
author_email => 'nobody@example.com',
base_uri => URI->new ( 'http://blog.example.com/' ),
);
eval { $plerd->publish_all; };
like ( $@, qr/not in W3C format/, 'Rejected source file with invalid timestamp.' );
unlink "$FindBin::Bin/source/bad-date.md";
eval { $plerd->publish_all; };
like ( $@, qr/post title/, 'Rejected title-free source file.' );
unlink "$FindBin::Bin/source/no-title.md";
$plerd->publish_all;
# The "+3" below accounts for the generated recent, archive, and RSS files.
my $expected_docroot_count = scalar( $source_dir->children( no_hidden => 1 ) ) + 3;
is( scalar( $docroot_dir->children ),
$expected_docroot_count,
"Correct number of files generated in docroot."
);
### Test reading time
{
my $post = $plerd->posts->[-1];
is ( $post->reading_time, 4, 'Reading time is as expected.' );
}
### Test formatting in titles and filenames
{
my $post = Path::Class::File->new( $docroot_dir, '1999-01-01-backdated.html' )->slurp;
like ( $post,
qr{an <em>example</em> of a “backdated},
'Post title is formatted.'
);
}
### Test published-file naming
{
my $renamed_file =
Path::Class::File->new( $docroot_dir, $ymd . '-a-good-source-file.html' );
my $not_renamed_file =
Path::Class::File->new( $docroot_dir, '1999-01-01-backdated.html' );
is (-e $renamed_file, 1, 'Source file with dateless filename named as expected.' );
is (-e $not_renamed_file, 1, 'Source file with backdated filename named as expected.' );
my $renamed_file_with_funky_title =
Path::Class::File->new(
$docroot_dir,
$ymd . '-apostrophes-and-html-shouldnt-turn-into-garbage.html',
);
is (
-e $renamed_file_with_funky_title,
1,
'Source file with formatted title received a nice clean published filename.'
);
}
### Make sure re-titling posts works as expected
{
my $source_file = Path::Class::File->new( $source_dir, 'good-source-file.md' );
my $text = $source_file->slurp;
$text =~ s/title: A good source file/title: A retitled source file/;
$source_file->spew( $text );
$plerd->publish_all;
my $welcome_file = Path::Class::File->new(
$docroot_dir,
$ymd . '-a-good-source-file.html',
);
my $unwelcome_file = Path::Class::File->new(
$docroot_dir,
$ymd . '-a-retitled-source-file.html',
);
is ( $docroot_dir->contains( $welcome_file ),
1,
'A file named after the old title is still there.',
);
isnt ( $docroot_dir->contains( $unwelcome_file ),
1,
'A file named after the new title is not there.',
);
$text =~ s/-a-good-source-file/-a-retitled-source-file/;
$source_file->spew( $text );
$plerd->publish_all;
is ( $docroot_dir->contains( $unwelcome_file ),
1,
'A file named after the new title is there now.',
);
### Test GUIDs
$plerd->publish_all;
like ( $source_file->slurp,
qr/guid: /,
'Source file contains a GUID, as expected.',
);
### Test miscellaneous-attribute pass-through
my $byline_post =
Path::Class::File->new(
$docroot_dir,
'2000-01-01-this-post-has-extra-headers.html',
);
like( $byline_post->slurp,
qr/"byline">Sam Handwich/,
'Miscellaneous header passed through to the template',
);
### Test newer / older post links
# (Including robustness after new posts are added)
my $new_post =
Path::Class::File->new(
$source_dir,
'another_post.md',
);
$new_post->spew( "title:Blah\n\nWords, words, words." );
$expected_docroot_count++;
$plerd->publish_all;
my $first_post = $plerd->posts->[0];
my $second_post = $plerd->posts->[1];
my $third_post = $plerd->posts->[2];
my $last_post = $plerd->posts->[-1];
my $penultimate_post = $plerd->posts->[-2];
is( $first_post->newer_post, undef, 'First post has no newer post.' );
is( $first_post->older_post,
$second_post,
'First post has correct older post.'
);
is( $second_post->newer_post,
$first_post,
'Second post has correct newer post.'
);
is( $second_post->older_post,
$third_post,
'Second post has correct older post.'
);
is( $last_post->older_post,
undef,
'Last post has no older post.'
);
is( $last_post->newer_post,
$penultimate_post,
'Last post has correct newer post.'
);
}
### Test trailing no slash on base_uri
{
my $plerd = Plerd->new(
path => $FindBin::Bin,
title => 'Test Blog',
author_name => 'Nobody',
author_email => 'nobody@example.com',
base_uri => URI->new ( 'http://www.example.com/blog' ),
);
$plerd->publish_all;
like ( Path::Class::File->new( $docroot_dir, 'recent.html' )->slurp,
qr{http://www.example.com/blog/1999-01-02-unicode.html},
'Base URIs missing trailing slashes work',
);
}
### Test using alternate config paths
{
$docroot_dir->rmtree;
$docroot_dir->mkpath;
my $alt_config_plerd = Plerd->new(
source_path => "$FindBin::Bin/source",
publication_path => "$FindBin::Bin/docroot",
template_path => "$FindBin::Bin/templates",
title => 'Test Blog',
author_name => 'Nobody',
author_email => 'nobody@example.com',
base_uri => URI->new ( 'http://blog.example.com/' ),
);
$alt_config_plerd->publish_all;
is( scalar( $docroot_dir->children ),
$expected_docroot_count,
"Correct number of files generated in docroot."
);
}
### Test social-media metatags.
{
my $social_plerd = Plerd->new(
path => $FindBin::Bin,
title => 'Test Blog',
author_name => 'Nobody',
author_email => 'nobody@example.com',
base_uri => URI->new ( 'http://blog.example.com/' ),
image => URI->new ( 'http://blog.example.com/logo.png' ),
facebook_id => 'This is a fake Facebook ID',
twitter_id => 'This is a fake Twitter ID',
);
$social_plerd->publish_all;
my $post = Path::Class::File->new( $docroot_dir, "$ymd-metatags.html" )->slurp;
my $image_post = Path::Class::File->new( $docroot_dir, "$ymd-metatags-with-image.html" )->slurp;
my $alt_image_post = Path::Class::File->new( $docroot_dir, "$ymd-metatags-with-image-and-alt.html" )->slurp;
like( $post,
qr{name="twitter:image" content="http://blog.example.com/logo.png"},
'Metatags: Default image',
);
like( $image_post,
qr{name="twitter:image" content="http://blog.example.com/example.png"},
'Metatags: Post image',
);
like( $alt_image_post,
qr{name="twitter:image:alt" content="A lovely bunch of coconuts."},
'Metatags: Post-specific alt-text',
);
like( $image_post,
qr{name="twitter:image:alt" content=""},
'Metatags: Empty default alt-text',
);
like( $post,
qr{name="twitter:card" content="summary"},
'Metatags: Default image is a thumbnail',
);
like( $image_post,
qr{name="twitter:card" content="summary_large_image"},
'Metatags: Post image is full-sized',
);
like( $post,
qr{name="twitter:description" content="Fun with social metatags.},
'Metatags: Defined description',
);
like ( $image_post,
qr{name="twitter:description" content="This file sets up some attributes},
'Metatags: Default description',
);
# Now add some alt text...
$social_plerd = Plerd->new(
path => $FindBin::Bin,
title => 'Test Blog',
author_name => 'Nobody',
author_email => 'nobody@example.com',
base_uri => URI->new ( 'http://blog.example.com/' ),
image => URI->new ( 'http://blog.example.com/logo.png' ),
facebook_id => 'This is a fake Facebook ID',
twitter_id => 'This is a fake Twitter ID',
image_alt => 'Just a test image.',
);
$social_plerd->publish_all;
$post = Path::Class::File->new( $docroot_dir, "$ymd-metatags.html" )->slurp;
like( $post,
qr{name="twitter:image:alt" content="Just a test image."},
'Metatags: Defined default alt-text',
);
}
done_testing();