dynamic_sitemaps 2.0.0.beta → 2.0.0.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,404 @@
1
+ require "test_helper"
2
+ require "nokogiri"
3
+ require "timecop"
4
+
5
+ class GeneratorTest < ActiveSupport::TestCase
6
+ setup do
7
+ Timecop.freeze Time.new(2013, 7, 10, 13, 46, 23, "+10:00")
8
+ FileUtils.rm_rf Rails.root.join("public", "sitemaps")
9
+ DynamicSitemaps.reset!
10
+ end
11
+
12
+ teardown do
13
+ Timecop.return
14
+ end
15
+
16
+ test "basic sitemap with default settings" do
17
+ DynamicSitemaps.generate_sitemap
18
+
19
+ doc = open_sitemap(remove_namespaces: false)
20
+ assert_equal ({ "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" }), doc.namespaces
21
+ doc.remove_namespaces!
22
+
23
+ doc.xpath("urlset/url").tap do |url|
24
+ assert_equal 1, url.count
25
+ assert_equal "http://www.mytest.com/", url.xpath("loc").text
26
+ assert_equal "2013-07-10T03:46:23+00:00", url.xpath("lastmod").text
27
+ assert_equal "daily", url.xpath("changefreq").text
28
+ assert_equal "1.0", url.xpath("priority").text
29
+ end
30
+
31
+ # Test that it only keeps sitemap.xml when there's no need for an index
32
+ assert !File.exists?(Rails.root.join("public", "sitemaps", "site.xml"))
33
+ end
34
+
35
+ test "custom path and folder" do
36
+ path = File.dirname(__FILE__) + "/tmp/testpath"
37
+ folder = "myfolder"
38
+
39
+ FileUtils.rm_rf path
40
+ DynamicSitemaps.path = path
41
+ DynamicSitemaps.folder = folder
42
+
43
+ DynamicSitemaps.generate_sitemap do
44
+ host "www.mydomain.com"
45
+
46
+ sitemap :one do
47
+ url root_url
48
+ end
49
+ sitemap :two do
50
+ url root_url
51
+ end
52
+ end
53
+
54
+ assert_equal ["one.xml", "sitemap.xml", "two.xml"],
55
+ Dir["#{path}/#{folder}/*"].sort.map { |p| File.basename(p) }.sort
56
+ end
57
+
58
+ test "index" do
59
+ 14.times { Product.create }
60
+
61
+ DynamicSitemaps.per_page = 5
62
+ DynamicSitemaps.generate_sitemap do
63
+ host "www.test.com"
64
+
65
+ sitemap :first do
66
+ url root_url
67
+ end
68
+
69
+ sitemap :second do
70
+ 1.upto(32) do |num|
71
+ url "http://#{host}/test#{num}"
72
+ end
73
+ end
74
+
75
+ sitemap_for Product.scoped do |product|
76
+ url product
77
+ url product_comments_url(product)
78
+ end
79
+ end
80
+
81
+ doc = open_sitemap(remove_namespaces: false)
82
+ assert_equal ({ "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" }), doc.namespaces
83
+ doc.remove_namespaces!
84
+
85
+ assert_equal ["first.xml", "products.xml", "products2.xml", "products3.xml",
86
+ "products4.xml", "products5.xml", "products6.xml", "second.xml",
87
+ "second2.xml", "second3.xml", "second4.xml", "second5.xml", "second6.xml",
88
+ "second7.xml", "sitemap.xml"],
89
+ Dir[Rails.root.join("public/sitemaps/*")].map { |p| File.basename(p) }.sort
90
+
91
+ assert_equal ["http://www.test.com/sitemaps/first.xml", "http://www.test.com/sitemaps/second.xml",
92
+ "http://www.test.com/sitemaps/second2.xml", "http://www.test.com/sitemaps/second3.xml",
93
+ "http://www.test.com/sitemaps/second4.xml", "http://www.test.com/sitemaps/second5.xml",
94
+ "http://www.test.com/sitemaps/second6.xml", "http://www.test.com/sitemaps/second7.xml",
95
+ "http://www.test.com/sitemaps/products.xml", "http://www.test.com/sitemaps/products2.xml",
96
+ "http://www.test.com/sitemaps/products3.xml", "http://www.test.com/sitemaps/products4.xml",
97
+ "http://www.test.com/sitemaps/products5.xml", "http://www.test.com/sitemaps/products6.xml"],
98
+ doc.xpath("sitemapindex/sitemap/loc").map(&:text)
99
+ end
100
+
101
+ test "indexes in multiple folders" do
102
+ DynamicSitemaps.generate_sitemap do
103
+ ["one", "two"].each do |domain|
104
+ host "www.#{domain}.com"
105
+ folder "sitemaps/#{domain}"
106
+
107
+ sitemap :first do
108
+ url root_url
109
+ end
110
+
111
+ sitemap :second do
112
+ url "http://#{host}/test"
113
+ end
114
+ end
115
+ end
116
+
117
+ ["one", "two"].each do |folder|
118
+ assert_equal ["first.xml", "second.xml", "sitemap.xml"],
119
+ Dir[Rails.root.join("public/sitemaps/#{folder}/*")].map { |p| File.basename(p) }.sort
120
+
121
+ doc = open_sitemap(Rails.root.join("public/sitemaps/#{folder}/sitemap.xml"))
122
+
123
+ assert_equal ["http://www.#{folder}.com/sitemaps/#{folder}/first.xml",
124
+ "http://www.#{folder}.com/sitemaps/#{folder}/second.xml"],
125
+ doc.xpath("sitemapindex/sitemap/loc").map(&:text)
126
+ end
127
+ end
128
+
129
+ test "custom index file name" do
130
+ DynamicSitemaps.index_file_name = "custom.xml"
131
+ DynamicSitemaps.generate_sitemap do
132
+ host "www.mydomain.com"
133
+
134
+ sitemap :one do
135
+ url root_url
136
+ end
137
+ sitemap :two do
138
+ url root_url
139
+ end
140
+ end
141
+
142
+ doc = open_sitemap(Rails.root.join("public", "sitemaps", "custom.xml"))
143
+ assert_equal 2, doc.xpath("sitemapindex/sitemap/loc").count
144
+ end
145
+
146
+ test "ensure unique sitemap names" do
147
+ assert_raises ArgumentError do
148
+ DynamicSitemaps.generate_sitemap do
149
+ host "www.example.com"
150
+ sitemap :site do
151
+ url root_url
152
+ end
153
+ sitemap :site do
154
+ url root_url
155
+ end
156
+ end
157
+ end
158
+ end
159
+
160
+ test "ensure unique sitemap names for relations" do
161
+ assert_raises ArgumentError do
162
+ DynamicSitemaps.generate_sitemap do
163
+ host "www.example.com"
164
+ sitemap_for Product.scoped
165
+ sitemap_for Product.where(id: 1)
166
+ end
167
+ end
168
+ end
169
+
170
+ test "unique sitemap names are checked in scope of folder" do
171
+ assert_nothing_raised do
172
+ DynamicSitemaps.generate_sitemap do
173
+ host "www.example.com"
174
+
175
+ folder "sitemaps/one"
176
+ sitemap_for Product.scoped
177
+
178
+ folder "sitemaps/two"
179
+ sitemap_for Product.scoped
180
+ end
181
+ end
182
+ end
183
+
184
+ test "sitemap name conflict with index file name" do
185
+ assert_raises ArgumentError do
186
+ DynamicSitemaps.generate_sitemap do
187
+ host "www.example.com"
188
+
189
+ sitemap :sitemap do
190
+ url root_url
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ test "always generate index" do
197
+ DynamicSitemaps.always_generate_index = true
198
+ DynamicSitemaps.generate_sitemap
199
+
200
+ doc = open_sitemap
201
+ assert_equal "http://www.mytest.com/sitemaps/site.xml", doc.xpath("sitemapindex/sitemap/loc").text
202
+
203
+ doc = open_sitemap(Rails.root.join("public", "sitemaps", "site.xml"))
204
+ assert_equal "http://www.mytest.com/", doc.xpath("urlset/url/loc").text
205
+ end
206
+
207
+ test "sitemap based on block" do
208
+ DynamicSitemaps.generate_sitemap do
209
+ host "www.test.com"
210
+ sitemap :test do
211
+ url "http://www.test.com/test"
212
+ url "http://www.test.com/test2"
213
+ end
214
+ end
215
+
216
+ doc = open_sitemap
217
+
218
+ assert_equal 2, doc.xpath("urlset/url").count
219
+ assert_equal "http://www.test.com/test2", doc.xpath("urlset/url/loc").last.text
220
+ end
221
+
222
+ test "resourceful routes" do
223
+ Product.create.tap do |product|
224
+ product.update_column :updated_at, 213434.seconds.ago
225
+ end
226
+ Product.create slug: "test-slug"
227
+
228
+ DynamicSitemaps.generate_sitemap do
229
+ host "www.mytest.com"
230
+ sitemap_for Product.scoped
231
+ end
232
+
233
+ doc = open_sitemap
234
+ urls = doc.xpath("urlset/url")
235
+ assert_equal 2, urls.count
236
+
237
+ urls.first.tap do |url|
238
+ assert_equal "http://www.mytest.com/products/1", url.xpath("loc").text
239
+ assert_equal "2013-07-07T16:29:09+00:00", url.xpath("lastmod").text
240
+ assert_nil url.at_xpath("priority")
241
+ assert_nil url.at_xpath("changefreq")
242
+ end
243
+
244
+ urls.last.tap do |url|
245
+ assert_equal "http://www.mytest.com/products/test-slug", url.xpath("loc").text
246
+ assert_equal "2013-07-10T03:46:23+00:00", url.xpath("lastmod").text
247
+ assert_nil url.at_xpath("priority")
248
+ assert_nil url.at_xpath("changefreq")
249
+ end
250
+ end
251
+
252
+ test "resourceful routes with custom urls" do
253
+ Product.create featured: true
254
+ Product.create featured: false
255
+
256
+ DynamicSitemaps.generate_sitemap do
257
+ host "www.mytest.com"
258
+ sitemap_for Product.scoped do |product|
259
+ url product, last_mod: 1234.seconds.ago, priority: (product.featured? ? 1.0 : nil), change_freq: "weekly"
260
+ url product_comments_url(product)
261
+ end
262
+ end
263
+
264
+ doc = open_sitemap
265
+ urls = doc.xpath("urlset/url")
266
+ assert_equal 4, urls.count
267
+
268
+ urls[0].tap do |url|
269
+ assert_equal "http://www.mytest.com/products/1", url.xpath("loc").text
270
+ assert_equal "2013-07-10T03:25:49+00:00", url.xpath("lastmod").text
271
+ assert_equal "weekly", url.xpath("changefreq").text
272
+ assert_equal "1.0", url.xpath("priority").text
273
+ end
274
+
275
+ urls[1].tap do |url|
276
+ assert_equal "http://www.mytest.com/products/1/comments", url.xpath("loc").text
277
+ assert_nil url.at_xpath("lastmod")
278
+ assert_nil url.at_xpath("changefreq")
279
+ assert_nil url.at_xpath("priority")
280
+ end
281
+
282
+ urls[2].tap do |url|
283
+ assert_equal "http://www.mytest.com/products/2", url.xpath("loc").text
284
+ assert_nil url.at_xpath("priority")
285
+ end
286
+ end
287
+
288
+ test "sitemap_for complains if not given a relation" do
289
+ assert_raises ArgumentError do
290
+ DynamicSitemaps.generate_sitemap do
291
+ host "www.mytest.com"
292
+ sitemap_for Product.all
293
+ end
294
+ end
295
+ end
296
+
297
+ test "generates to temporary folder first" do
298
+ raise_test_error = false
299
+
300
+ block = Proc.new do
301
+ host "www.example.com"
302
+
303
+ sitemap :site do
304
+ url root_url
305
+ end
306
+
307
+ raise RuntimeError if raise_test_error
308
+
309
+ sitemap :second do
310
+ url root_url
311
+ end
312
+ end
313
+
314
+ valid_files = ["second.xml", "site.xml", "sitemap.xml"]
315
+
316
+ DynamicSitemaps.generate_sitemap &block
317
+ assert_equal valid_files, Dir[Rails.root.join("public/sitemaps/*")].map { |p| File.basename(p) }.sort
318
+
319
+ raise_test_error = true
320
+ assert_raises RuntimeError do
321
+ DynamicSitemaps.generate_sitemap &block
322
+ end
323
+ assert_equal valid_files, Dir[Rails.root.join("public/sitemaps/*")].map { |p| File.basename(p) }.sort
324
+
325
+ assert !Dir.exists?(DynamicSitemaps.temp_path)
326
+ end
327
+
328
+ test "removes temporary folder if failing" do
329
+ assert_raises RuntimeError do
330
+ DynamicSitemaps.generate_sitemap do
331
+ host "www.example.com"
332
+
333
+ sitemap :site do
334
+ url root_url
335
+ end
336
+
337
+ raise RuntimeError
338
+ end
339
+ end
340
+
341
+ assert !Dir.exists?(DynamicSitemaps.temp_path)
342
+ end
343
+
344
+ test "large sitemap" do
345
+ DynamicSitemaps.generate_sitemap do
346
+ host "www.mydomain.com"
347
+ sitemap :large do
348
+ 1.upto(123456) do |num|
349
+ url "http://#{host}/test/#{num}"
350
+ end
351
+ end
352
+ end
353
+
354
+ doc = open_sitemap
355
+ assert_equal 3, doc.xpath("sitemapindex/sitemap/loc").count
356
+
357
+ doc = open_sitemap(Rails.root.join("public/sitemaps/large.xml"))
358
+ assert_equal 50000, doc.xpath("urlset/url").count
359
+
360
+ doc = open_sitemap(Rails.root.join("public/sitemaps/large2.xml"))
361
+ assert_equal 50000, doc.xpath("urlset/url").count
362
+
363
+ doc = open_sitemap(Rails.root.join("public/sitemaps/large3.xml"))
364
+ assert_equal 23456, doc.xpath("urlset/url").count
365
+ end
366
+
367
+ test "pinging search engines" do
368
+ stub_request :get, //
369
+ DynamicSitemaps.ping_environments << "test"
370
+
371
+ DynamicSitemaps.generate_sitemap do
372
+ ["www.test.com", "www.example.com"].each do |domain|
373
+ host domain
374
+ folder "sitemaps/#{domain}"
375
+ sitemap :site do
376
+ url root_url
377
+ end
378
+ ping_with "http://#{host}/sitemap.xml"
379
+ end
380
+ end
381
+
382
+ ["http://www.google.com/webmasters/sitemaps/ping?sitemap=http://www.test.com/sitemap.xml",
383
+ "http://www.bing.com/webmaster/ping.aspx?siteMap=http://www.test.com/sitemap.xml",
384
+ "http://www.google.com/webmasters/sitemaps/ping?sitemap=http://www.example.com/sitemap.xml",
385
+ "http://www.bing.com/webmaster/ping.aspx?siteMap=http://www.example.com/sitemap.xml"].each do |url|
386
+ assert_requested :get, url
387
+ end
388
+ end
389
+
390
+ private
391
+
392
+ # Opens a sitemap file using Nokogiri::XML and removes namespaces by default.
393
+ #
394
+ # open_sitemap # => Nokogiri::XML with the contents of <rails root>/public/sitemaps/sitemap.xml
395
+ # open_sitemap "/path/to/sitemap.xml"
396
+ # open_sitemap "/path/to/sitemap.xml", remove_namespaces: false
397
+ def open_sitemap(*args)
398
+ options = args.extract_options!
399
+ doc = Nokogiri::XML(open(args[0] || Rails.root.join("public", "sitemaps", "sitemap.xml")))
400
+ doc.remove_namespaces! unless options[:remove_namespaces] == false
401
+ doc
402
+ end
403
+
404
+ end
@@ -0,0 +1,73 @@
1
+ require 'test_helper'
2
+
3
+ class PingerTest < ActiveSupport::TestCase
4
+ setup do
5
+ DynamicSitemaps.reset!
6
+ end
7
+
8
+ test "pinging search engines" do
9
+ stub_request :get, /^http:\/\/www\.google\.com\/webmasters\/sitemaps\/ping\?sitemap=.*/
10
+ stub_request :get, /^http:\/\/www\.bing\.com\/webmaster\/ping\.aspx\?siteMap=.*/
11
+
12
+ DynamicSitemaps.ping_environments << "test"
13
+ DynamicSitemaps::Pinger.ping_search_engines_with ["http://test.dk/sitemap.xml", "http://other.com/test.xml"]
14
+
15
+ ["http://www.google.com/webmasters/sitemaps/ping?sitemap=http%3A%2F%2Ftest.dk%2Fsitemap.xml",
16
+ "http://www.bing.com/webmaster/ping.aspx?siteMap=http%3A%2F%2Ftest.dk%2Fsitemap.xml",
17
+ "http://www.google.com/webmasters/sitemaps/ping?sitemap=http%3A%2F%2Fother.com%2Ftest.xml",
18
+ "http://www.bing.com/webmaster/ping.aspx?siteMap=http%3A%2F%2Fother.com%2Ftest.xml"].each do |url|
19
+ assert_requested :get, url
20
+ end
21
+ end
22
+
23
+ test "doesnt ping when given no sitemap urls" do
24
+ stub_request :get, //
25
+ DynamicSitemaps.ping_environments << "test"
26
+ DynamicSitemaps::Pinger.ping_search_engines_with []
27
+ assert_not_requested :get, //
28
+ end
29
+
30
+ test "pings only for environments specified" do
31
+ stub_request :get, //
32
+
33
+ DynamicSitemaps::Pinger.ping_search_engines_with "http://test.com/sitemap.xml"
34
+ assert_not_requested :get, //
35
+
36
+ DynamicSitemaps.ping_environments << "test"
37
+ DynamicSitemaps::Pinger.ping_search_engines_with "http://test.com/sitemap.xml"
38
+ assert_requested :get, //, times: 2
39
+ end
40
+
41
+ test "custom search engine ping url" do
42
+ stub_request :get, //
43
+ DynamicSitemaps.ping_environments << "test"
44
+ DynamicSitemaps.search_engine_ping_urls << "http://testsearch.com/ping?url=%s"
45
+ DynamicSitemaps::Pinger.ping_search_engines_with "http://test.dk/sitemap.xml"
46
+
47
+ [/www\.google\.com/,
48
+ /www\.bing\.com/,
49
+ "http://testsearch.com/ping?url=http%3A%2F%2Ftest.dk%2Fsitemap.xml"].each do |url|
50
+ assert_requested :get, url
51
+ end
52
+ end
53
+
54
+ test "handles failure when pinging" do
55
+ stub_request(:get, //).to_raise(StandardError)
56
+ DynamicSitemaps.ping_environments << "test"
57
+
58
+ assert_nothing_raised do
59
+ DynamicSitemaps::Pinger.ping_search_engines_with "http://test.dk/sitemap.xml"
60
+ end
61
+ end
62
+
63
+ test "ping for environment" do
64
+ assert DynamicSitemaps::Pinger.ping_for_environment?("production")
65
+ assert !DynamicSitemaps::Pinger.ping_for_environment?("development")
66
+ assert !DynamicSitemaps::Pinger.ping_for_environment?("test")
67
+ assert !DynamicSitemaps::Pinger.ping_for_environment?("staging")
68
+
69
+ DynamicSitemaps.ping_environments << "staging"
70
+ assert DynamicSitemaps::Pinger.ping_for_environment?("staging")
71
+ assert DynamicSitemaps::Pinger.ping_for_environment?("production")
72
+ end
73
+ end