summaryrefslogtreecommitdiff
path: root/test/models/page_test.rb
blob: bcddc89527ab437d6a232b90e47cedacb7932b5b (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
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 = n1.find_or_create_draft( @user1 )
    assert_not_nil d3 = n2.find_or_create_draft( @user1 )
    
    # tag and double publish so we have 4 pages tagged with "update"
    d1.tag_list = "update"
    d1.save
    n1.publish_draft!
  
    d2 = n1.find_or_create_draft @user1
    n1.publish_draft!
    
    
    d3.tag_list = "update, pressemitteilung"
    d3.save
    n2.publish_draft!
  
    d4 = n2.find_or_create_draft @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 = n.find_or_create_draft @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 = n.find_or_create_draft @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 = n.find_or_create_draft @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 = node.find_or_create_draft(@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 = node.find_or_create_draft(@user1)
    first_draft.title = "First version"
    first_draft.save!
    first_token = first_draft.ensure_preview_token!
    node.publish_draft!

    second_draft = node.find_or_create_draft(@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

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

    d2 = n.find_or_create_draft @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 = n.find_or_create_draft @user1
    d.title = "Old heading"
    d.save!
    n.publish_draft!

    d2 = n.find_or_create_draft @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 = n.find_or_create_draft @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 = n.find_or_create_draft @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 = n.find_or_create_draft @user1
    d.tag_list = "update"
    d.template_name = "standard_template"
    d.save!
    n.publish_draft!

    d2 = n.find_or_create_draft @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 = n.find_or_create_draft @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 = n.find_or_create_draft @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
end