forked from refinery/refinerycms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
page_spec.rb
555 lines (450 loc) · 16.4 KB
/
page_spec.rb
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
# encoding: utf-8
require 'spec_helper'
module Refinery
describe Page do
let(:page_title) { 'RSpec is great for testing too' }
let(:child_title) { 'The child page' }
# For when we do not need the page persisted.
let(:page) { subject.class.new(:title => page_title, :deletable => true)}
let(:child) { page.children.new(:title => child_title) }
# For when we need the page persisted.
let(:created_page) { subject.class.create!(:title => page_title, :deletable => true) }
let(:created_child) { created_page.children.create!(:title => child_title) }
def page_cannot_be_destroyed
page.should_receive(:puts_destroy_help)
page.destroy.should == false
end
def turn_off_marketable_urls
Pages.stub(:marketable_urls).and_return(false)
end
def turn_on_marketable_urls
Pages.stub(:marketable_urls).and_return(true)
end
context 'cannot be deleted under certain rules' do
it 'if link_url is present' do
page.link_url = '/plugin-name'
page_cannot_be_destroyed
end
it 'if refinery team deems it so' do
page.deletable = false
page_cannot_be_destroyed
end
it 'if menu_match is present' do
page.menu_match = "^/#{page_title}*$"
page_cannot_be_destroyed
end
it 'unless you really want it to! >:]' do
page.deletable = false
page_cannot_be_destroyed
page.destroy!.should be
end
end
context 'page urls' do
let(:page_path) { 'rspec-is-great-for-testing-too' }
let(:child_path) { 'the-child-page' }
it 'return a full path' do
page.path.should == page_title
end
it 'and all of its parent page titles, reversed' do
created_child.path.should == [page_title, child_title].join(' - ')
end
it 'or normally ;-)' do
created_child.path(:reversed => false).should == [child_title, page_title].join(' - ')
end
it 'returns its url' do
page.link_url = '/contact'
page.url.should == '/contact'
end
it 'returns its path with marketable urls' do
created_page.url[:id].should be_nil
created_page.url[:path].should == [page_path]
end
it 'returns its path underneath its parent with marketable urls' do
created_child.url[:id].should be_nil
created_child.url[:path].should == [created_page.url[:path].first, child_path]
end
it 'no path parameter without marketable urls' do
turn_off_marketable_urls
created_page.url[:path].should be_nil
created_page.url[:id].should == page_path
turn_on_marketable_urls
end
it "doesn't mention its parent without marketable urls" do
turn_off_marketable_urls
created_child.url[:id].should == child_path
created_child.url[:path].should be_nil
turn_on_marketable_urls
end
it 'returns its path with slug set by menu_title' do
page.menu_title = 'RSpec is great'
page.save
page.reload
page.url[:id].should be_nil
page.url[:path].should == ['rspec-is-great']
end
end
context 'canonicals' do
before do
Refinery::I18n.stub(:default_frontend_locale).and_return(:en)
Refinery::I18n.stub(:frontend_locales).and_return([I18n.default_frontend_locale, :ru])
Refinery::I18n.stub(:current_frontend_locale).and_return(I18n.default_frontend_locale)
page.save
end
let(:page_title) { 'team' }
let(:child_title) { 'about' }
let(:ru_page_title) { 'Новости' }
describe '#canonical' do
let!(:default_canonical) {
Globalize.with_locale(Refinery::I18n.default_frontend_locale) {
page.canonical
}
}
specify 'page returns itself' do
page.canonical.should == page.url
end
specify 'default canonical matches page#canonical' do
default_canonical.should == page.canonical
end
specify 'translated page returns master page' do
Globalize.with_locale(:ru) do
page.title = ru_page_title
page.save
page.canonical.should == default_canonical
end
end
end
describe '#canonical_slug' do
let!(:default_canonical_slug) {
Globalize.with_locale(Refinery::I18n.default_frontend_locale) {
page.canonical_slug
}
}
specify 'page returns its own slug' do
page.canonical_slug.should == page.slug
end
specify 'default canonical_slug matches page#canonical' do
default_canonical_slug.should == page.canonical_slug
end
specify "translated page returns master page's slug'" do
Globalize.with_locale(:ru) do
page.title = ru_page_title
page.save
page.canonical_slug.should == default_canonical_slug
end
end
end
end
context 'custom slugs' do
let(:custom_page_slug) { 'custom-page-slug' }
let(:custom_child_slug) { 'custom-child-slug' }
let(:page_with_custom_slug) {
subject.class.new(:title => page_title, :custom_slug => custom_page_slug)
}
let(:child_with_custom_slug) {
page.children.new(:title => child_title, :custom_slug => custom_child_slug)
}
after(:each) do
Refinery::I18n.stub(:current_frontend_locale).and_return(I18n.default_frontend_locale)
Refinery::I18n.stub(:current_locale).and_return(I18n.default_locale)
end
it 'returns its path with custom slug' do
page_with_custom_slug.save
page_with_custom_slug.url[:id].should be_nil
page_with_custom_slug.url[:path].should == [custom_page_slug]
end
it 'returns its path underneath its parent with custom urls' do
child_with_custom_slug.save
page.save
child_with_custom_slug.url[:id].should be_nil
child_with_custom_slug.url[:path].should == [page.url[:path].first, custom_child_slug]
end
it 'returns its path with custom slug when using different locale' do
Refinery::I18n.stub(:current_frontend_locale).and_return(:ru)
Refinery::I18n.stub(:current_locale).and_return(:ru)
page_with_custom_slug.custom_slug = "#{custom_page_slug}-ru"
page_with_custom_slug.save
page_with_custom_slug.reload
page_with_custom_slug.url[:id].should be_nil
page_with_custom_slug.url[:path].should == ["#{custom_page_slug}-ru"]
end
it 'returns path underneath its parent with custom urls when using different locale' do
Refinery::I18n.stub(:current_frontend_locale).and_return(:ru)
Refinery::I18n.stub(:current_locale).and_return(:ru)
child_with_custom_slug.custom_slug = "#{custom_child_slug}-ru"
child_with_custom_slug.save
child_with_custom_slug.reload
child_with_custom_slug.url[:id].should be_nil
child_with_custom_slug.url[:path].should == [page.url[:path].first, "#{custom_child_slug}-ru"]
end
context "given a page with a custom_slug exists" do
before do
Factory(:page, :custom_slug => custom_page_slug)
end
it "fails validation when a new record uses that custom_slug" do
new_page = Page.new :custom_slug => custom_page_slug
new_page.valid?
new_page.errors[:custom_slug].should_not be_empty
end
end
end
context 'content sections (page parts)' do
before do
page.parts.new(:title => 'body', :content => "I'm the first page part for this page.", :position => 0)
page.parts.new(:title => 'side body', :content => 'Closely followed by the second page part.', :position => 1)
end
it 'return the content when using content_for' do
page.content_for(:body).should == "<p>I'm the first page part for this page.</p>"
page.content_for('BoDY').should == "<p>I'm the first page part for this page.</p>"
end
context 'when using content_for?' do
it 'return true when page part has content' do
page.content_for?(:body).should be_true
end
it 'return false when page part does not exist' do
page.parts = []
page.content_for?(:body).should be_false
end
it 'return false when page part does not have any content' do
page.parts.first.content = ''
page.content_for?(:body).should be_false
end
end
it 'reposition correctly' do
page.save
page.parts.first.update_attributes :position => 6
page.parts.last.update_attributes :position => 4
page.parts.first.position.should == 6
page.parts.last.position.should == 4
page.reposition_parts!
page.parts.first.position.should == 0
page.parts.last.position.should == 1
end
end
context 'draft pages' do
it 'not live when set to draft' do
page.draft = true
page.live?.should_not be
end
it 'live when not set to draft' do
page.draft = false
page.live?.should be
end
end
context "should add url suffix" do
let(:reserved_word) { subject.class.friendly_id_config.reserved_words.last }
let(:page_with_reserved_title) {
subject.class.create!(:title => reserved_word, :deletable => true)
}
let(:child_with_reserved_title_parent) {
page_with_reserved_title.children.create(:title => 'reserved title child page')
}
before { turn_on_marketable_urls }
it 'when title is set to a reserved word' do
page_with_reserved_title.url[:path].should == ["#{reserved_word}-page"]
end
it "when parent page title is set to a reserved word" do
child_with_reserved_title_parent.url[:path].should == ["#{reserved_word}-page", 'reserved-title-child-page']
end
end
context 'meta data' do
context 'responds to' do
it 'meta_description' do
page.respond_to?(:meta_description)
end
it 'browser_title' do
page.respond_to?(:browser_title)
end
end
context 'allows us to assign to' do
it 'meta_description' do
page.meta_description = 'This is my description of the page for search results.'
page.meta_description.should == 'This is my description of the page for search results.'
end
it 'browser_title' do
page.browser_title = 'An awesome browser title for SEO'
page.browser_title.should == 'An awesome browser title for SEO'
end
end
context 'allows us to update' do
it 'meta_description' do
page.meta_description = 'This is my description of the page for search results.'
page.save
page.reload
page.meta_description.should == 'This is my description of the page for search results.'
end
it 'browser_title' do
page.browser_title = 'An awesome browser title for SEO'
page.save
page.reload
page.browser_title.should == 'An awesome browser title for SEO'
end
end
end
describe "#to_refinery_menu_item" do
let(:page) do
Page.new(
:id => 5,
:parent_id => 8,
:menu_match => "^/foo$"
# Page does not allow setting lft and rgt, so stub them.
).tap do |p|
p[:lft] = 6
p[:rgt] = 7
end
end
subject { page.to_refinery_menu_item }
shared_examples_for("Refinery menu item hash") do
[ [:id, 5],
[:lft, 6],
[:rgt, 7],
[:parent_id, 8],
[:menu_match, "^/foo$"]
].each do |attr, value|
it "returns the correct :#{attr}" do
subject[attr].should eq(value)
end
end
it "returns the correct :url" do
subject[:url].should be_a(Hash) # guard against nil
subject[:url].should eq(page.url)
end
end
context "with #menu_title" do
before do
page[:menu_title] = "Menu Title"
end
it_should_behave_like "Refinery menu item hash"
it "returns the menu_title for :title" do
subject[:title].should eq("Menu Title")
end
end
context "with #title" do
before do
page[:title] = "Title"
end
it_should_behave_like "Refinery menu item hash"
it "returns the title for :title" do
subject[:title].should eq("Title")
end
end
end
describe "#in_menu?" do
context "when live? and show_in_menu? returns true" do
it "returns true" do
page.stub(:live?).and_return(true)
page.stub(:show_in_menu?).and_return(true)
page.in_menu?.should be_true
end
end
context "when live? or show_in_menu? doesn't return true" do
it "returns false" do
page.stub(:live?).and_return(true)
page.stub(:show_in_menu?).and_return(false)
page.in_menu?.should be_false
page.stub(:live?).and_return(false)
page.stub(:show_in_menu?).and_return(true)
page.in_menu?.should be_false
end
end
end
describe "#not_in_menu?" do
context "when in_menu? returns true" do
it "returns false" do
page.stub(:in_menu?).and_return(true)
page.not_in_menu?.should be_false
end
end
context "when in_menu? returns false" do
it "returns true" do
page.stub(:in_menu?).and_return(false)
page.not_in_menu?.should be_true
end
end
end
describe '.find_by_path' do
let(:page_title) { 'team' }
let(:child_title) { 'about' }
let(:created_root_about) { subject.class.create!(:title => child_title, :deletable => true) }
before do
# Ensure pages are created.
created_child
created_root_about
end
it "should return (root) about page when looking for '/about'" do
Page.find_by_path('/about').should == created_root_about
end
it "should return child about page when looking for '/team/about'" do
Page.find_by_path('/team/about').should == created_child
end
end
describe ".find_by_path_or_id" do
let!(:market) { FactoryGirl.create(:page, :title => "market") }
let(:path) { "market" }
let(:id) { market.id }
context "when marketable urls are true and path is present" do
before do
Page.stub(:marketable_urls).and_return(true)
end
context "when path is friendly_id" do
it "finds page using path" do
Page.find_by_path_or_id(path, "").should eq(market)
end
end
context "when path is not friendly_id" do
it "finds page using id" do
Page.find_by_path_or_id(id, "").should eq(market)
end
end
end
context "when id is present" do
before do
Page.stub(:marketable_urls).and_return(false)
end
it "finds page using id" do
Page.find_by_path_or_id("", id).should eq(market)
end
end
end
describe "#deletable?" do
let(:deletable_page) do
page.deletable = true
page.link_url = ""
page.menu_match = ""
page.stub(:puts_destroy_help).and_return('')
page
end
context "when deletable is true and link_url, and menu_match is blank" do
it "returns true" do
deletable_page.deletable?.should be_true
end
end
context "when deletable is false and link_url, and menu_match is blank" do
it "returns false" do
deletable_page.deletable = false
deletable_page.deletable?.should be_false
end
end
context "when deletable is false and link_url or menu_match isn't blank" do
it "returns false" do
deletable_page.deletable = false
deletable_page.link_url = "text"
deletable_page.deletable?.should be_false
deletable_page.menu_match = "text"
deletable_page.deletable?.should be_false
end
end
end
describe "#destroy" do
before do
page.deletable = false
page.link_url = "link_url"
page.menu_match = "menu_match"
page.save!
end
it "shows message" do
page.should_receive(:puts_destroy_help)
page.destroy
end
end
end
end