summaryrefslogtreecommitdiff
path: root/test/models/page_test.rb
blob: edb7c378d998bc7c2c3665bbec0bc3a21d29d5f3 (plain)
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
require 'test_helper'

class PageTest < ActiveSupport::TestCase
  
  def setup
    @user1 = User.create :login => 'demo', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar'
    @user2 = User.create :login => 'show', :email => "f@b.com", :password => 'foobar', :password_confirmation => 'foobar'
  end
  
  def test_aggregation
    # Create two nodes and move them beneath the root node
    n1 = Node.root.children.create! :slug => "one"
    n2 = Node.root.children.create! :slug => "two"
    
    # get the drafts and assign a user to it
    assert_not_nil d1 = find_or_create_draft(n1, @user1)
    assert_not_nil d3 = find_or_create_draft(n2, @user1)
    
    # tag and double publish so we have 4 pages tagged with "update"
    d1.tag_list = "update"
    d1.save
    n1.publish_draft!
  
    d2 = find_or_create_draft(n1, @user1)
    n1.publish_draft!
    
    
    d3.tag_list = "update, pressemitteilung"
    d3.save
    n2.publish_draft!
  
    d4 = find_or_create_draft(n2, @user1)
    n2.publish_draft!
    
    # Set up two options hashes for the assertions
    options1 = {
      :tags => "update"
    }
    
    options2 = {
      :tags => "update, pressemitteilung"
    }
    
    assert_equal 2, Page.aggregate( options1 ).length
    assert_equal 1, Page.aggregate( options2 ).length
    assert_equal 4, Page.tagged_with( "update" ).length
    assert_equal [d2.id, d4.id], Page.aggregate( options1 ).map {|x| x.id}
  end
  
  def test_before_save_rewrite_links_in_body
    n = Node.root.children.create :slug => "link_test"
    d = find_or_create_draft(n, @user1)
    
    before = "<h1>Hello World</h1>\n" \
             "<a href=\"/club\" target=\"_blank\">Linkme</a>"
    
    after  = "<h1>Hello World</h1>\n" \
             "<a href=\"/de/club\" target=\"_blank\">Linkme</a>"
    
    I18n.locale = :de
    
    d.body = before
    d.save!
    
    assert_equal after, d.body
  end
  
  def test_before_save_rewrite_links_in_body_if_no_locale_prefix_present
    n = Node.root.children.create :slug => "link_test"
    d = find_or_create_draft(n, @user1)
    
    before = "<h1>Hello World</h1>\n" \
             "<a href=\"/de/club\" target=\"_blank\">Linkme</a>"
    
    after  = "<h1>Hello World</h1>\n" \
             "<a href=\"/de/club\" target=\"_blank\">Linkme</a>"
    
    I18n.locale = :de
    
    d.body = before
    d.save
    
    assert_equal after, d.body
  end
  
  def test_before_save_rewrite_links_skips_on_external_links
    n = Node.root.children.create :slug => "link_test"
    d = find_or_create_draft(n, @user1)
    
    before = "<h1>Hello World</h1>\n" \
             "<a href=\"http://www.ccc.de/club\" target=\"_blank\">Linkme</a>"
    
    after  = "<h1>Hello World</h1>\n" \
             "<a href=\"http://www.ccc.de/club\" target=\"_blank\">Linkme</a>"
    
    I18n.locale = :de
    
    d.body = before
    d.save
    
    assert_equal after, d.body
  end
  
  def test_find_with_outdated_translations
    Node.delete_all
    Page.delete_all
    I18n.locale = :de
    
    assert_not_nil page = Page.create!( :title => "Hallo" )
    page.reload
    assert_equal 1, page.translations.size
    assert_equal [], Page.find_with_outdated_translations
    
    I18n.locale = :en
    page.title = "Hello"
    page.save
    
    assert_equal 2, page.translations.size
    assert_equal 0, Page.find_with_outdated_translations.size
    
    english = page.translations.select {|x| x.locale == :en}.first
    Page::Translation.record_timestamps = false
    english.update(:updated_at => (Time.now+25.hours))    
    Page::Translation.record_timestamps = true
    assert_equal 1, Page.find_with_outdated_translations.count
    
    I18n.locale = :de
    page2 = Page.create!( :title => "Hallo2" )
    I18n.locale = :en
    page2.title = "Hello2"
    page2.save!
    
    assert_equal 0, Page.find_with_outdated_translations(:delta_time => 23.days).count
    assert_equal 1, Page.find_with_outdated_translations(:delta_time => 23.minutes).count
    assert_equal 2, Page.count
  end
  
  test "pages under /updates node get the update template assigned" do
    Node.root.descendants.delete_all
    updates       = Node.root.children.create!( :slug => "updates" )
    updates2009   = updates.children.create!( :slug => "2009" )
    update        = updates2009.children.create!( :slug => "my-first-update" )
    assert_equal "update", update.draft.template_name
  end

  test "a page scheduled for future publication is not yet public even after being published" do
    node = Node.root.children.create!(slug: "preview-scheduled-test")
    draft = find_or_create_draft(node, @user1)
    draft.title = "Scheduled test"
    draft.published_at = 1.day.from_now
    draft.save!
    token = draft.ensure_preview_token!

    node.publish_draft!
    page = Page.find_by(preview_token: token)

    assert_equal page.id, page.node.head_id
    assert_not page.public?
  end

  test "a superseded page is no longer the head, even though it was once published" do
    node = Node.root.children.create!(slug: "preview-superseded-test")
    first_draft = find_or_create_draft(node, @user1)
    first_draft.title = "First version"
    first_draft.save!
    first_token = first_draft.ensure_preview_token!
    node.publish_draft!

    second_draft = find_or_create_draft(node, @user1)
    second_draft.title = "Second version"
    second_draft.save!
    node.publish_draft!

    first_page = Page.find_by(preview_token: first_token)

    assert_not_equal first_page.id, first_page.node.head_id
    assert first_page.published_at.present?
  end

  test "clone_attributes_from preserves an unchanged locale's original timestamp" do
    n = Node.root.children.create!(:slug => "clone_preserve_timestamp_test")
    source = n.draft
    Globalize.with_locale(:de) { source.update!(:title => "Deutscher Titel") }
    Globalize.with_locale(:en) { source.update!(:title => "English Title") }

    target = Page.create!
    target.clone_attributes_from(source)
    original_en_updated_at = target.translations.find_by(:locale => :en).updated_at

    Globalize.with_locale(:de) { source.update!(:title => "Deutscher Titel (bearbeitet)") }
    target.clone_attributes_from(source)

    en_translation = target.translations.find_by(:locale => :en)
    assert_equal "English Title", en_translation.title
    assert_equal original_en_updated_at, en_translation.updated_at
  end

  test "clone_attributes_from gives a genuinely changed locale a fresh timestamp" do
    n = Node.root.children.create!(:slug => "clone_fresh_timestamp_test")
    source = n.draft
    Globalize.with_locale(:de) { source.update!(:title => "Erste Version") }

    target = Page.create!
    target.clone_attributes_from(source)
    original_de_updated_at = target.translations.find_by(:locale => :de).updated_at

    Globalize.with_locale(:de) { source.update!(:title => "Zweite Version") }
    target.clone_attributes_from(source)

    de_translation = target.translations.find_by(:locale => :de)
    assert_equal "Zweite Version", de_translation.title
    assert_operator de_translation.updated_at, :>, original_de_updated_at
  end

  test "clone_attributes_from removes a locale no longer present in the source" do
    n = Node.root.children.create!(:slug => "clone_removed_locale_test")
    source = n.draft
    Globalize.with_locale(:en) { source.update!(:title => "English Title") }

    target = Page.create!
    target.clone_attributes_from(source)
    assert_includes target.translations.map(&:locale), :en

    source.translations.where(:locale => :en).delete_all
    target.clone_attributes_from(source)

    assert_not_includes target.reload.translations.map(&:locale), :en
  end

  def test_diff_against_inline_keeps_tags_and_marks_only_the_changed_word
    n = Node.root.children.create! :slug => "diff_against_test"
    d = find_or_create_draft(n, @user1)
    d.title = "Old heading"
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    d2.title = "New heading"
    d2.save!

    diff = d2.diff_against(n.head)

    assert_match "<del>Old</del>", diff[:title]
    assert_match "<ins>New</ins>", diff[:title]
  end

  def test_diff_against_side_by_side_returns_two_annotated_strings
    n = Node.root.children.create! :slug => "diff_against_sbs_test"
    d = find_or_create_draft(n, @user1)
    d.title = "Old heading"
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    d2.title = "New heading"
    d2.save!

    old_html, new_html = d2.diff_against(n.head, view: :side_by_side)[:title]

    assert_match "<del>Old</del>", old_html
    assert_match "<ins>New</ins>", new_html
  end

  test "diff_against handles an inserted paragraph split without corrupting the document" do
    n = Node.root.children.create! :slug => "paragraph_split_test"
    d = find_or_create_draft(n, @user1)
    d.body = "<p>Der Vortragsraum ist ab 19 Uhr geöffnet, der Zugang erfolgt über den Hinterhof.</p>"
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    d2.body = "<p>Der Vortragsraum ist ab 19 Uhr geöffnet,</p>\n<p>der Zugang erfolgt über den Hinterhof.</p>"
    d2.save!

    diff = d2.diff_against(n.head)
    fragment = Nokogiri::HTML::DocumentFragment.parse(diff[:body])

    assert_equal 2, fragment.css('ins.diff_structural').length
    assert_match "der Zugang erfolgt über den Hinterhof.", fragment.text
  end

  test "diff_against reports tag and template changes" do
    n = Node.root.children.create! :slug => "field_diff_test"
    d = find_or_create_draft(n, @user1)
    d.tag_list = "update"
    d.template_name = "standard_template"
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    d2.tag_list = "update, pressemitteilung"
    d2.template_name = "title_only"
    d2.save!

    diff = d2.diff_against(n.head)

    assert_equal ["pressemitteilung"], diff[:tags][:added]
    assert_equal [], diff[:tags][:removed]
    assert diff[:template_name][:changed]
    assert_equal "standard_template", diff[:template_name][:from]
    assert_equal "title_only", diff[:template_name][:to]
  end

  test "diff_against reports added and removed assets by filename" do
    n = Node.root.children.create! :slug => "asset_diff_test"
    d = find_or_create_draft(n, @user1)
    d.save!
    n.publish_draft!

    kept_asset    = Asset.create!(:upload_file_name => "kept.png", :upload_content_type => "image/png", :upload_file_size => 1)
    removed_asset = Asset.create!(:upload_file_name => "removed.pdf", :upload_content_type => "application/pdf", :upload_file_size => 1)
    n.head.update_assets([kept_asset.id, removed_asset.id])

    d2 = find_or_create_draft(n, @user1)
    added_asset = Asset.create!(:upload_file_name => "added.png", :upload_content_type => "image/png", :upload_file_size => 1)
    d2.update_assets([kept_asset.id, added_asset.id])
    d2.save!

    diff = d2.diff_against(n.head)

    assert_equal [added_asset], diff[:assets][:added]
    assert_equal [removed_asset], diff[:assets][:removed]
  end

  test "diff_against with an explicit locale compares that locale's own translation on each side" do
    n = Node.root.children.create!(:slug => "diff_locale_test")
    d = find_or_create_draft(n, @user1)
    Globalize.with_locale(:en) { d.update!(:title => "Old English") }
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    Globalize.with_locale(:en) { d2.update!(:title => "New English") }
    d2.save!

    diff = d2.diff_against(n.head, :locale => :en)

    assert_match "<del>Old</del>", diff[:title]
    assert_match "<ins>New</ins>", diff[:title]
  end

  test "diff_against with an explicit locale ignores content in other locales entirely" do
    n = Node.root.children.create!(:slug => "diff_locale_isolation_test")
    d = find_or_create_draft(n, @user1)
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    Globalize.with_locale(:de) { d2.update!(:title => "Nur Deutsch geändert") }
    d2.save!

    diff = d2.diff_against(n.head, :locale => :en)

    assert_no_match(/Deutsch/, diff[:title])
  end

  test "locale_diff_summary flags a locale that only exists on one side as changed" do
    n = Node.root.children.create!(:slug => "diff_locale_summary_test")
    d = find_or_create_draft(n, @user1)
    d.save!
    n.publish_draft!

    d2 = find_or_create_draft(n, @user1)
    Globalize.with_locale(:en) { d2.update!(:title => "New English translation") }
    d2.save!

    summary = d2.locale_diff_summary(n.head)
    en_entry = summary.find { |s| s[:locale] == :en }

    assert en_entry[:changed]
    refute en_entry[:exists_there]
  end
end