summaryrefslogtreecommitdiff
path: root/test/unit/page_test.rb
blob: 401d7778c91ceb19c219b182350fdb1a77c9390d (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
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.find_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_attributes(: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
  
end