summaryrefslogtreecommitdiff
path: root/test/functional/nodes_controller_test.rb
blob: 08e50fda2e358d45dfed37f5180533f6c9abe04c (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
require 'test_helper'

class NodesControllerTest < ActionController::TestCase

  include AuthenticatedTestHelper

  def test_get_index
    Node.root.descendants.delete_all
    test_node = Node.create :slug => "foo"
    test_node.move_to_child_of Node.root
    login_as :quentin
    get :index
    assert_response :success
  end
  
  def test_new
    login_as :quentin
    get :new
    assert_response :success
  end
  
  test "create generic node with parent_id provided" do
    login_as :quentin
    assert_difference "Node.count", +1  do
      post( 
        :create, 
        :kind => "generic", 
        :parent_id => Node.first.id,
        :title => "Hello Spaceboy"
      )
    end
    
    assert_response :redirect
    assert_equal "hello-spaceboy", Node.last.slug
    assert_equal Node.last.parent_id, Node.first.id
    assert_equal 1, Node.last.level
  end
  
  test "create update node" do
    login_as :quentin
    #difference of three because "updates" and "2009" node get created as well
    assert_difference "Node.count", +3  do
      post( 
        :create,
        :kind => "update",
        :title => "Hello Spaceboy"
      )
    end
    
    assert_response :redirect
    expected = "updates/#{Time.now.year.to_s}/hello-spaceboy"
    assert_equal expected, Node.last.unique_name
    assert_equal 3, Node.last.level
  end
  
  test "create top level node" do
    login_as :quentin
    
    assert_difference "Node.count", +1  do
      post( 
        :create,
        :kind => "top_level",
        :title => "Hello Spaceboy"
      )
    end
    
    assert_response :redirect
    expected = "hello-spaceboy"
    assert_equal expected, Node.last.unique_name
    assert_equal 1, Node.last.level
  end
  
  def test_editing_a_node
    login_as :quentin
    
    node = Node.find_by_unique_name("fourth_child")
    node.pages.create
    node.draft = node.pages.last
    node.save
    
    assert_equal 1, node.pages.length
    
    draft = node.find_or_create_draft( User.first )
    draft.title = "Hello"
    draft.body = "World"
    draft.save
    node.publish_draft!
    
    get :edit, :id => node.id
    assert_response :success
    assert_select("#page_title[value=Hello]")
    assert_select("#page_body", "World")
    
    node.reload
    assert_equal 2, node.pages.length
    assert_equal "Hello", node.find_or_create_draft( User.first ).title
    assert_equal "World", node.find_or_create_draft( User.first ).body
  end
  
  def test_update_a_draft
    test_node = Node.create! :slug => "test_node"
    test_node.move_to_child_of Node.root
    
    login_as :quentin
    put :update, :id => test_node.id, :page => {:title => "Hello", :body => "There"}
    
    assert_equal "Hello", test_node.draft.title
    assert_equal "There", test_node.draft.body
  end
  
  def test_update_a_draft_with_changing_the_template
    test_node = Node.create! :slug => "test_node"
    test_node.move_to_child_of Node.root
    
    login_as :quentin
    put :update, {
      :id => test_node.id, 
      :page => {
        :title => "Hello", 
        :body => "There",
        :template_name => "Foobar"
      }
    }
    
    test_node.reload
    assert_equal "Hello", test_node.draft.title
    assert_equal "There", test_node.draft.body
    assert_equal "Foobar", test_node.draft.template_name
  end
  
  
  test "publish draft with staged_slug unqueal slug" do
    login_as :quentin
    
    test_node = Node.create! :slug => "test_node", :staged_slug => "peter_pan"
    test_node.move_to_child_of Node.root
    
    put :publish, :id => test_node.id
    
    test_node.reload
    assert_equal "peter_pan", test_node.slug
    assert_equal "peter_pan", test_node.unique_name
  end
  
  test "publish draft with staged_slug with more levels of nodes" do
    login_as :quentin
    
    test_node = Node.create! :slug => "test_node", :staged_slug => "peter_pan"
    test_node.move_to_child_of Node.root
    test_node2 = Node.create! :slug => "test_node2"
    test_node2.move_to_child_of test_node

    put :publish, :id => test_node.id
    
    test_node.reload; test_node2.reload
    assert_equal "peter_pan/test_node2", test_node2.unique_name
    assert_equal "peter_pan", test_node.unique_name
  end
  
  test "publish draft with staged_parent_id" do
    login_as :quentin
    
    parent = Node.create! :slug => "parent"
    parent.move_to_child_of Node.root
    test_node = Node.create! :slug => "test_node", :staged_parent_id => parent.id
    test_node.move_to_child_of Node.root
    test_node2 = Node.create! :slug => "test_node2"
    test_node2.move_to_child_of test_node
    
    put :publish, :id => test_node.id
    
    test_node.reload; test_node2.reload
    assert_equal "parent/test_node", test_node.unique_name
    assert_equal "parent/test_node/test_node2", test_node2.unique_name
  end
  
  test "publish draft with staged_parent_id and staged_slug" do
    login_as :quentin
    
    parent = Node.create! :slug => "parent"
    parent.move_to_child_of Node.root
    
    test_node = Node.create!(
      :slug => "test_node", 
      :staged_parent_id => parent.id,
      :staged_slug => "peter_pan"
    )
    test_node.move_to_child_of Node.root
    
    test_node2 = Node.create! :slug => "test_node2"
    test_node2.move_to_child_of test_node
    
    put :publish, :id => test_node.id
    
    test_node.reload; test_node2.reload
    assert_equal "parent/peter_pan", test_node.unique_name
    assert_equal "parent/peter_pan/test_node2", test_node2.unique_name
  end
  
end