fesplugas-typus 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. data/.gitignore +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +80 -0
  4. data/Rakefile +61 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/admin/master_controller.rb +324 -0
  7. data/app/controllers/typus_controller.rb +127 -0
  8. data/app/helpers/admin/form_helper.rb +351 -0
  9. data/app/helpers/admin/master_helper.rb +99 -0
  10. data/app/helpers/admin/public_helper.rb +24 -0
  11. data/app/helpers/admin/sidebar_helper.rb +259 -0
  12. data/app/helpers/admin/table_helper.rb +227 -0
  13. data/app/helpers/typus_helper.rb +169 -0
  14. data/app/models/typus_mailer.rb +14 -0
  15. data/app/models/typus_user.rb +5 -0
  16. data/app/views/admin/dashboard/_sidebar.html.erb +9 -0
  17. data/app/views/admin/resources/edit.html.erb +29 -0
  18. data/app/views/admin/resources/index.html.erb +28 -0
  19. data/app/views/admin/resources/new.html.erb +27 -0
  20. data/app/views/admin/resources/show.html.erb +21 -0
  21. data/app/views/admin/shared/_footer.html.erb +1 -0
  22. data/app/views/admin/shared/_pagination.html.erb +28 -0
  23. data/app/views/layouts/admin.html.erb +72 -0
  24. data/app/views/layouts/typus.html.erb +29 -0
  25. data/app/views/typus/dashboard.html.erb +13 -0
  26. data/app/views/typus/recover_password.html.erb +7 -0
  27. data/app/views/typus/reset_password.html.erb +13 -0
  28. data/app/views/typus/sign_in.html.erb +9 -0
  29. data/app/views/typus/sign_up.html.erb +7 -0
  30. data/app/views/typus_mailer/reset_password_link.erb +11 -0
  31. data/config/locales/es.yml +106 -0
  32. data/config/locales/pt-BR.yml +108 -0
  33. data/config/locales/typus_hacks.yml +14 -0
  34. data/config/routes.rb +14 -0
  35. data/generators/typus/templates/config/initializers/typus.rb +27 -0
  36. data/generators/typus/templates/config/typus/application.yml +45 -0
  37. data/generators/typus/templates/config/typus/application_roles.yml +23 -0
  38. data/generators/typus/templates/config/typus/typus.yml +14 -0
  39. data/generators/typus/templates/config/typus/typus_roles.yml +2 -0
  40. data/generators/typus/templates/db/create_typus_users.rb +21 -0
  41. data/generators/typus/templates/public/images/admin/arrow_down.gif +0 -0
  42. data/generators/typus/templates/public/images/admin/arrow_up.gif +0 -0
  43. data/generators/typus/templates/public/images/admin/spinner.gif +0 -0
  44. data/generators/typus/templates/public/images/admin/status_false.gif +0 -0
  45. data/generators/typus/templates/public/images/admin/status_true.gif +0 -0
  46. data/generators/typus/templates/public/images/admin/trash.gif +0 -0
  47. data/generators/typus/templates/public/javascripts/admin/application.js +14 -0
  48. data/generators/typus/templates/public/stylesheets/admin/reset.css +68 -0
  49. data/generators/typus/templates/public/stylesheets/admin/screen.css +709 -0
  50. data/generators/typus/typus_generator.rb +141 -0
  51. data/generators/typus_update_schema_to_01/templates/config/typus.yml +14 -0
  52. data/generators/typus_update_schema_to_01/templates/migration.rb +11 -0
  53. data/generators/typus_update_schema_to_01/typus_update_schema_to_01_generator.rb +19 -0
  54. data/init.rb +19 -0
  55. data/lib/typus/active_record.rb +298 -0
  56. data/lib/typus/authentication.rb +155 -0
  57. data/lib/typus/configuration.rb +92 -0
  58. data/lib/typus/format.rb +56 -0
  59. data/lib/typus/generator.rb +173 -0
  60. data/lib/typus/hash.rb +10 -0
  61. data/lib/typus/locale.rb +17 -0
  62. data/lib/typus/object.rb +22 -0
  63. data/lib/typus/quick_edit.rb +33 -0
  64. data/lib/typus/reloader.rb +17 -0
  65. data/lib/typus/string.rb +11 -0
  66. data/lib/typus/user.rb +137 -0
  67. data/lib/typus.rb +133 -0
  68. data/lib/vendor/active_record.rb +15 -0
  69. data/lib/vendor/paginator.rb +143 -0
  70. data/tasks/typus_tasks.rake +26 -0
  71. data/test/config/broken/application.yml +68 -0
  72. data/test/config/broken/application_roles.yml +20 -0
  73. data/test/config/broken/empty.yml +0 -0
  74. data/test/config/broken/empty_roles.yml +0 -0
  75. data/test/config/broken/undefined.yml +3 -0
  76. data/test/config/broken/undefined_roles.yml +6 -0
  77. data/test/config/default/typus.yml +14 -0
  78. data/test/config/default/typus_roles.yml +2 -0
  79. data/test/config/empty/empty_01.yml +0 -0
  80. data/test/config/empty/empty_01_roles.yml +0 -0
  81. data/test/config/empty/empty_02.yml +0 -0
  82. data/test/config/empty/empty_02_roles.yml +0 -0
  83. data/test/config/locales/es.yml +10 -0
  84. data/test/config/ordered/001_roles.yml +2 -0
  85. data/test/config/ordered/002_roles.yml +2 -0
  86. data/test/config/unordered/app_one_roles.yml +2 -0
  87. data/test/config/unordered/app_two_roles.yml +2 -0
  88. data/test/config/working/application.yml +67 -0
  89. data/test/config/working/application_roles.yml +22 -0
  90. data/test/config/working/typus.yml +14 -0
  91. data/test/config/working/typus_roles.yml +2 -0
  92. data/test/fixtures/app/controllers/admin/assets_controller.rb +2 -0
  93. data/test/fixtures/app/controllers/admin/categories_controller.rb +2 -0
  94. data/test/fixtures/app/controllers/admin/comments_controller.rb +2 -0
  95. data/test/fixtures/app/controllers/admin/pages_controller.rb +2 -0
  96. data/test/fixtures/app/controllers/admin/posts_controller.rb +2 -0
  97. data/test/fixtures/app/controllers/admin/status_controller.rb +6 -0
  98. data/test/fixtures/app/controllers/admin/typus_users_controller.rb +2 -0
  99. data/test/fixtures/app/controllers/admin/watch_dog_controller.rb +6 -0
  100. data/test/fixtures/app/views/admin/comments/_edit_bottom.html.erb +1 -0
  101. data/test/fixtures/app/views/admin/comments/_edit_sidebar.html.erb +1 -0
  102. data/test/fixtures/app/views/admin/comments/_edit_top.html.erb +1 -0
  103. data/test/fixtures/app/views/admin/comments/_index_bottom.html.erb +1 -0
  104. data/test/fixtures/app/views/admin/comments/_index_sidebar.html.erb +1 -0
  105. data/test/fixtures/app/views/admin/comments/_index_top.html.erb +1 -0
  106. data/test/fixtures/app/views/admin/comments/_new_bottom.html.erb +1 -0
  107. data/test/fixtures/app/views/admin/comments/_new_sidebar.html.erb +1 -0
  108. data/test/fixtures/app/views/admin/comments/_new_top.html.erb +1 -0
  109. data/test/fixtures/app/views/admin/comments/_show_bottom.html.erb +1 -0
  110. data/test/fixtures/app/views/admin/comments/_show_sidebar.html.erb +1 -0
  111. data/test/fixtures/app/views/admin/comments/_show_top.html.erb +1 -0
  112. data/test/fixtures/app/views/admin/dashboard/_bottom.html.erb +1 -0
  113. data/test/fixtures/app/views/admin/dashboard/_sidebar.html.erb +1 -0
  114. data/test/fixtures/app/views/admin/dashboard/_top.html.erb +1 -0
  115. data/test/fixtures/app/views/admin/shared/_footer.html.erb +1 -0
  116. data/test/fixtures/app/views/admin/status/index.html.erb +1 -0
  117. data/test/fixtures/app/views/admin/templates/_datepicker.html.erb +1 -0
  118. data/test/fixtures/assets.yml +11 -0
  119. data/test/fixtures/categories.yml +14 -0
  120. data/test/fixtures/comments.yml +27 -0
  121. data/test/fixtures/pages.yml +41 -0
  122. data/test/fixtures/posts.yml +37 -0
  123. data/test/fixtures/typus_users.yml +54 -0
  124. data/test/functional/admin/assets_controller_test.rb +57 -0
  125. data/test/functional/admin/categories_controller_test.rb +106 -0
  126. data/test/functional/admin/comments_controller_test.rb +121 -0
  127. data/test/functional/admin/master_controller_test.rb +5 -0
  128. data/test/functional/admin/posts_controller_test.rb +278 -0
  129. data/test/functional/admin/status_controller_test.rb +43 -0
  130. data/test/functional/admin/typus_users_controller_test.rb +239 -0
  131. data/test/functional/typus_controller_test.rb +315 -0
  132. data/test/helper.rb +51 -0
  133. data/test/helpers/admin/form_helper_test.rb +316 -0
  134. data/test/helpers/admin/master_helper_test.rb +65 -0
  135. data/test/helpers/admin/public_helper_test.rb +22 -0
  136. data/test/helpers/admin/sidebar_helper_test.rb +351 -0
  137. data/test/helpers/admin/table_helper_test.rb +255 -0
  138. data/test/helpers/typus_helper_test.rb +106 -0
  139. data/test/lib/active_record_test.rb +372 -0
  140. data/test/lib/configuration_test.rb +91 -0
  141. data/test/lib/hash_test.rb +11 -0
  142. data/test/lib/routes_test.rb +82 -0
  143. data/test/lib/string_test.rb +25 -0
  144. data/test/lib/typus_test.rb +105 -0
  145. data/test/models.rb +51 -0
  146. data/test/schema.rb +64 -0
  147. data/test/unit/typus_mailer_test.rb +33 -0
  148. data/test/unit/typus_test.rb +17 -0
  149. data/test/unit/typus_user_roles_test.rb +90 -0
  150. data/test/unit/typus_user_test.rb +177 -0
  151. data/test/vendor/active_record_test.rb +18 -0
  152. data/test/vendor/paginator_test.rb +138 -0
  153. data/typus.gemspec +225 -0
  154. metadata +241 -0
@@ -0,0 +1,315 @@
1
+ require 'test/helper'
2
+
3
+ class TypusControllerTest < ActionController::TestCase
4
+
5
+ def setup
6
+ Typus::Configuration.options[:recover_password] = true
7
+ Typus::Configuration.options[:app_name] = 'whatistypus.com'
8
+ end
9
+
10
+ def test_should_render_login
11
+ get :sign_in
12
+ assert_response :success
13
+ assert_template 'sign_in'
14
+ end
15
+
16
+ def test_should_sign_in_and_redirect_to_dashboard
17
+ typus_user = typus_users(:admin)
18
+ post :sign_in, { :user => { :email => typus_user.email,
19
+ :password => '12345678' } }
20
+ assert_equal typus_user.id, @request.session[:typus_user_id]
21
+ assert_response :redirect
22
+ assert_redirected_to admin_dashboard_path
23
+ end
24
+
25
+ def test_should_return_message_when_sign_in_fails
26
+ post :sign_in, { :user => { :email => 'john@example.com',
27
+ :password => 'XXXXXXXX' } }
28
+ assert_response :redirect
29
+ assert_redirected_to admin_sign_in_path
30
+ assert flash[:error]
31
+ assert_equal "The email and/or password you entered is invalid.", flash[:error]
32
+ end
33
+
34
+ def test_should_not_sign_in_a_disabled_user
35
+ typus_user = typus_users(:disabled_user)
36
+ post :sign_in, { :user => { :email => typus_user.email,
37
+ :password => '12345678' } }
38
+ assert_nil @request.session[:typus_user_id]
39
+ assert_response :redirect
40
+ assert_redirected_to admin_sign_in_path
41
+ end
42
+
43
+ def test_should_not_sign_in_a_removed_role
44
+ typus_user = typus_users(:removed_role)
45
+ post :sign_in, { :user => { :email => typus_user.email,
46
+ :password => '12345678' } }
47
+ assert_equal typus_user.id, @request.session[:typus_user_id]
48
+ assert_response :redirect
49
+ assert_redirected_to admin_dashboard_path
50
+ get :dashboard
51
+ assert_redirected_to admin_sign_in_path
52
+ assert_nil @request.session[:typus_user_id]
53
+ assert flash[:notice]
54
+ assert_equal 'Role does no longer exists.', flash[:notice]
55
+ end
56
+
57
+ def test_should_not_send_recovery_password_link_to_unexisting_user
58
+ post :recover_password, { :user => { :email => 'unexisting' } }
59
+ assert_response :redirect
60
+ assert_redirected_to admin_recover_password_path
61
+ [ :notice, :error, :warning ].each { |f| assert !flash[f] }
62
+ end
63
+
64
+ def test_should_send_recovery_password_link_to_existing_user
65
+ admin = typus_users(:admin)
66
+ post :recover_password, { :user => { :email => admin.email } }
67
+ assert_response :redirect
68
+ assert_redirected_to admin_sign_in_path
69
+ assert flash[:success]
70
+ assert_match /Password recovery link sent to your email/, flash[:success]
71
+ end
72
+
73
+ def test_should_sign_out
74
+ admin = typus_users(:admin)
75
+ @request.session[:typus_user_id] = admin.id
76
+ get :sign_out
77
+ assert_nil @request.session[:typus_user_id]
78
+ assert_response :redirect
79
+ assert_redirected_to admin_sign_in_path
80
+ [ :notice, :error, :warning ].each { |f| assert !flash[f] }
81
+ end
82
+
83
+ def test_should_verify_we_can_disable_users_and_block_acess_on_the_fly
84
+
85
+ admin = typus_users(:admin)
86
+ @request.session[:typus_user_id] = admin.id
87
+ get :dashboard
88
+ assert_response :success
89
+
90
+ # Disable user ...
91
+
92
+ admin.update_attributes :status => false
93
+
94
+ get :dashboard
95
+ assert_response :redirect
96
+ assert_redirected_to admin_sign_in_path
97
+
98
+ assert flash[:notice]
99
+ assert_equal "Typus user has been disabled.", flash[:notice]
100
+ assert_nil @request.session[:typus_user_id]
101
+
102
+ end
103
+
104
+ def test_should_not_allow_reset_password_if_disabled
105
+
106
+ typus_user = typus_users(:admin)
107
+ get :reset_password, { :token => typus_user.token }
108
+ assert_response :success
109
+ assert_template 'reset_password'
110
+
111
+ options = Typus::Configuration.options.merge(:recover_password => false)
112
+ Typus::Configuration.stubs(:options).returns(options)
113
+
114
+ get :reset_password
115
+ assert_response :redirect
116
+ assert_redirected_to admin_sign_in_path
117
+
118
+ end
119
+
120
+ def test_should_be_redirected_if_password_does_not_match_confirmation
121
+ typus_user = typus_users(:admin)
122
+ post :reset_password, { :token => typus_user.token, :user => { :password => 'drowssap', :password_confirmation => 'drowssap2' } }
123
+ assert_response :redirect
124
+ assert_redirected_to admin_reset_password_path(:token => typus_user.token)
125
+ end
126
+
127
+ def test_should_only_be_allowed_to_reset_password
128
+ typus_user = typus_users(:admin)
129
+ post :reset_password, { :token => typus_user.token, :user => { :password => 'drowssap', :password_confirmation => 'drowssap', :role => 'superadmin' } }
130
+ typus_user.reload
131
+ assert_not_equal typus_user.role, 'superadmin'
132
+ end
133
+
134
+ def test_should_return_404_when_reseting_passsowrd_if_token_is_invalid
135
+ assert_raise(ActiveRecord::RecordNotFound) { get :reset_password, { :token => 'INVALID' } }
136
+ end
137
+
138
+ def test_should_allow_a_user_with_valid_token_to_change_password
139
+ typus_user = typus_users(:admin)
140
+ get :reset_password, { :token => typus_user.token }
141
+ assert_response :success
142
+ assert_template 'reset_password'
143
+ end
144
+
145
+ def test_should_verify_typus_sign_in_layout_includes_recover_password_link
146
+ options = Typus::Configuration.options.merge(:recover_password => true)
147
+ Typus::Configuration.stubs(:options).returns(options)
148
+ get :sign_in
149
+ assert @response.body.include?('Recover password')
150
+ end
151
+
152
+ def test_should_verify_typus_sign_in_layout_does_not_include_recover_password_link
153
+ options = Typus::Configuration.options.merge(:recover_password => false)
154
+ Typus::Configuration.stubs(:options).returns(options)
155
+ get :sign_in
156
+ assert !@response.body.include?('Recover password')
157
+ end
158
+
159
+ def test_should_render_typus_login_footer
160
+ expected = 'Typus'
161
+ get :sign_in
162
+ assert_response :success
163
+ assert_match /#{expected}/, @response.body
164
+ assert_match /layouts\/typus/, @controller.active_layout.to_s
165
+ end
166
+
167
+ def test_should_render_admin_login_bottom
168
+ get :sign_in
169
+ assert_response :success
170
+ assert_select 'h1', 'whatistypus.com'
171
+ assert_match /layouts\/typus/, @controller.active_layout.to_s
172
+ end
173
+
174
+ def test_should_verify_page_title_on_sign_in
175
+ get :sign_in
176
+ assert_select 'title', "#{Typus::Configuration.options[:app_name]} &rsaquo; Sign in"
177
+ end
178
+
179
+ def test_should_create_first_typus_user
180
+
181
+ TypusUser.destroy_all
182
+ assert_nil @request.session[:typus_user_id]
183
+ assert TypusUser.find(:all).empty?
184
+
185
+ get :sign_in
186
+ assert_response :redirect
187
+ assert_redirected_to admin_sign_up_path
188
+
189
+ get :sign_up
190
+ assert flash[:notice]
191
+ assert_equal 'Enter your email below to create the first user.', flash[:notice]
192
+
193
+ post :sign_up, :user => { :email => 'example.com' }
194
+ assert_response :success
195
+ assert flash[:error]
196
+ assert_equal 'That doesn\'t seem like a valid email address.', flash[:error]
197
+
198
+ post :sign_up, :user => { :email => 'john@example.com' }
199
+ assert_response :redirect
200
+ assert_redirected_to admin_dashboard_path
201
+ assert flash[:notice]
202
+ assert_equal "Password set to \"columbia\".", flash[:notice]
203
+ assert @request.session[:typus_user_id]
204
+ assert !TypusUser.find(:all).empty?
205
+
206
+ get :sign_out
207
+ assert_nil @request.session[:typus_user_id]
208
+ assert_redirected_to admin_sign_in_path
209
+
210
+ get :sign_up
211
+ assert_redirected_to admin_sign_in_path
212
+
213
+ end
214
+
215
+ def test_should_redirect_to_login_if_not_logged
216
+ @request.session[:typus_user_id] = nil
217
+ get :dashboard
218
+ assert_response :redirect
219
+ assert_redirected_to admin_sign_in_path
220
+ end
221
+
222
+ def test_should_render_dashboard
223
+ @request.session[:typus_user_id] = typus_users(:admin).id
224
+ get :dashboard
225
+ assert_response :success
226
+ assert_template 'dashboard'
227
+ assert_match 'whatistypus.com', @response.body
228
+ assert_match /layouts\/admin/, @controller.active_layout.to_s
229
+ end
230
+
231
+ def test_should_verify_sign_up_works
232
+ @request.session[:typus_user_id] = typus_users(:admin).id
233
+ TypusUser.destroy_all
234
+ get :sign_up
235
+ assert_response :success
236
+ assert_template 'sign_up'
237
+ assert_match /layouts\/typus/, @controller.active_layout.to_s
238
+ end
239
+
240
+ def test_should_verify_page_title_on_dashboard
241
+ @request.session[:typus_user_id] = typus_users(:admin).id
242
+ get :dashboard
243
+ assert_select 'title', "#{Typus::Configuration.options[:app_name]} &rsaquo; Dashboard"
244
+ end
245
+
246
+ def test_should_verify_link_to_edit_typus_user
247
+
248
+ typus_user = typus_users(:admin)
249
+ @request.session[:typus_user_id] = typus_user.id
250
+ get :dashboard
251
+ assert_response :success
252
+
253
+ assert_match "href=\"\/admin\/typus_users\/edit\/#{typus_user.id}\"", @response.body
254
+
255
+ assert_select 'body div#header' do
256
+ assert_select 'a', 'Admin Example (admin)'
257
+ assert_select 'a', 'Sign out'
258
+ end
259
+
260
+ end
261
+
262
+ def test_should_verify_link_to_sign_out
263
+
264
+ @request.session[:typus_user_id] = typus_users(:admin).id
265
+ get :dashboard
266
+ assert_response :success
267
+
268
+ assert_match "href=\"\/admin\/sign_out\"", @response.body
269
+
270
+ end
271
+
272
+ def test_should_show_add_links_in_resources_list_for_admin
273
+
274
+ @request.session[:typus_user_id] = typus_users(:admin).id
275
+ get :dashboard
276
+
277
+ %w( typus_users posts pages assets ).each do |resource|
278
+ assert_match "/admin/#{resource}/new", @response.body
279
+ end
280
+
281
+ %w( statuses orders ).each do |resource|
282
+ assert_no_match /\/admin\/#{resource}\n/, @response.body
283
+ end
284
+
285
+ end
286
+
287
+ def test_should_show_add_links_in_resources_list_for_editor
288
+ editor = typus_users(:editor)
289
+ @request.session[:typus_user_id] = editor.id
290
+ get :dashboard
291
+ assert_match '/admin/posts/new', @response.body
292
+ assert_no_match /\/admin\/typus_users\/new/, @response.body
293
+ # We have loaded categories as a module, so are not displayed
294
+ # on the applications list.
295
+ assert_no_match /\/admin\/categories\/new/, @response.body
296
+ end
297
+
298
+ def test_should_show_add_links_in_resources_list_for_designer
299
+ designer = typus_users(:designer)
300
+ @request.session[:typus_user_id] = designer.id
301
+ get :dashboard
302
+ assert_no_match /\/admin\/posts\/new/, @response.body
303
+ assert_no_match /\/admin\/typus_users\/new/, @response.body
304
+ end
305
+
306
+ def test_should_render_application_dashboard_template_extensions
307
+ admin = typus_users(:admin)
308
+ @request.session[:typus_user_id] = admin.id
309
+ get :dashboard
310
+ assert_response :success
311
+ partials = %w( _sidebar.html.erb _top.html.erb _bottom.html.erb )
312
+ partials.each { |p| assert_match p, @response.body }
313
+ end
314
+
315
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,51 @@
1
+
2
+ ENV['RAILS_ENV'] = 'test'
3
+ require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
4
+ require 'test_help'
5
+ require 'mocha'
6
+
7
+ begin
8
+ require 'redgreen'
9
+ rescue LoadError
10
+ end
11
+
12
+ ##
13
+ # Different DB settings and load our schema.
14
+ #
15
+
16
+ connection = case ENV['DB']
17
+ when /mysql/
18
+ { :adapter => 'mysql', :username => 'root', :database => 'typus_test' }
19
+ when /postgresql/
20
+ { :adapter => 'postgresql', :encoding => 'unicode', :database => 'typus_test' }
21
+ else
22
+ { :adapter => 'sqlite3', :dbfile => ':memory:' }
23
+ end
24
+
25
+ ActiveRecord::Base.establish_connection(connection)
26
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/debug.log')
27
+
28
+ require File.dirname(__FILE__) + '/schema'
29
+
30
+ ##
31
+ # To test the plugin without touching the application we set our
32
+ # load_paths and view_paths.
33
+ #
34
+
35
+ ActiveSupport::Dependencies.load_paths = []
36
+ %w( models controllers helpers ).each do |folder|
37
+ ActiveSupport::Dependencies.load_paths << File.join(Rails.root, 'vendor/plugins/typus/app', folder)
38
+ ActiveSupport::Dependencies.load_paths << File.join(Rails.root, 'vendor/plugins/typus/test/fixtures/app', folder)
39
+ end
40
+
41
+ ActionController::Base.view_paths = []
42
+ %w( app/views test/fixtures/app/views ).each do |folder|
43
+ ActionController::Base.append_view_path(File.join(Rails.root, 'vendor/plugins/typus', folder))
44
+ end
45
+
46
+ class ActiveSupport::TestCase
47
+ self.fixture_path = File.dirname(__FILE__) + '/fixtures'
48
+ self.use_transactional_fixtures = true
49
+ self.use_instantiated_fixtures = false
50
+ fixtures :all
51
+ end
@@ -0,0 +1,316 @@
1
+ require 'test/helper'
2
+
3
+ class Admin::FormHelperTest < ActiveSupport::TestCase
4
+
5
+ include Admin::FormHelper
6
+
7
+ include ActionView::Helpers::FormHelper
8
+ include ActionView::Helpers::FormOptionsHelper
9
+ include ActionView::Helpers::DateHelper
10
+ include ActionView::Helpers::UrlHelper
11
+ include ActionView::Helpers::TagHelper
12
+ include ActionController::UrlWriter
13
+
14
+ def test_build_form
15
+ assert true
16
+ end
17
+
18
+ def test_typus_belongs_to_field
19
+
20
+ params = { :controller => 'admin/post', :id => 1, :action => :create }
21
+ self.expects(:params).at_least_once.returns(params)
22
+
23
+ @current_user = mock()
24
+ @current_user.expects(:can_perform?).with(Post, 'create').returns(false)
25
+
26
+ expected = <<-HTML
27
+ <li><label for="item_post">Post
28
+ <small></small>
29
+ </label>
30
+ <select id="item_post_id" name="item[post_id]"><option value=""></option>
31
+ <option value="3">Post#3</option>
32
+ <option value="4">Post#4</option>
33
+ <option value="1">Post#1</option>
34
+ <option value="2">Post#2</option></select></li>
35
+ HTML
36
+
37
+ assert_equal expected, typus_belongs_to_field('post', Comment)
38
+
39
+ end
40
+
41
+ def test_typus_belongs_to_field_with_different_attribute_name
42
+
43
+ default_url_options[:host] = 'test.host'
44
+
45
+ params = { :controller => 'admin/post', :id => 1, :action => :edit }
46
+ self.expects(:params).at_least_once.returns(params)
47
+
48
+ @current_user = mock()
49
+ @current_user.expects(:can_perform?).with(Comment, 'create').returns(true)
50
+
51
+ expected = <<-HTML
52
+ <li><label for="item_favorite_comment">Favorite comment
53
+ <small><a href="http://test.host/admin/comments/new?back_to=%2Fadmin%2Fpost%2Fedit%2F1&selected=favorite_comment_id" onclick="return confirm('Are you sure you want to leave this page?\\n\\nIf you have made any changes to the fields without clicking the Save/Update entry button, your changes will be lost.\\n\\nClick OK to continue, or click Cancel to stay on this page.');">Add</a></small>
54
+ </label>
55
+ <select id="item_favorite_comment_id" name="item[favorite_comment_id]"><option value=""></option>
56
+ <option value="1">John</option>
57
+ <option value="2">Me</option>
58
+ <option value="3">John</option>
59
+ <option value="4">Me</option></select></li>
60
+ HTML
61
+ assert_equal expected, typus_belongs_to_field('favorite_comment', Post)
62
+
63
+ end
64
+
65
+ def test_typus_boolean_field
66
+
67
+ output = typus_boolean_field('test', Post)
68
+
69
+ expected = <<-HTML
70
+ <li><label for="item_test">Test</label>
71
+ <input name="item[test]" type="hidden" value="0" /><input id="item_test" name="item[test]" type="checkbox" value="1" /> Checked if active</li>
72
+ HTML
73
+
74
+ assert_equal expected, output
75
+
76
+ end
77
+
78
+ def test_typus_date_field
79
+
80
+ output = typus_date_field('test', {}, Post)
81
+ expected = <<-HTML
82
+ <li><label for="item_test">Test</label>
83
+ HTML
84
+
85
+ assert_match expected, output
86
+
87
+ end
88
+
89
+ def test_typus_datetime_field
90
+
91
+ output = typus_datetime_field('test', {}, Post)
92
+ expected = <<-HTML
93
+ <li><label for="item_test">Test</label>
94
+ HTML
95
+
96
+ assert_match expected, output
97
+
98
+ end
99
+
100
+ def test_typus_file_field
101
+
102
+ output = typus_file_field('asset_file_name', Post)
103
+ expected = <<-HTML
104
+ <li><label for="item_asset_file_name">Asset</label>
105
+ <input id="item_asset" name="item[asset]" size="30" type="file" /></li>
106
+ HTML
107
+
108
+ assert_equal expected, output
109
+
110
+ end
111
+
112
+ def test_typus_password_field
113
+
114
+ output = typus_password_field('test', Post)
115
+ expected = <<-HTML
116
+ <li><label for="item_test">Test</label>
117
+ <input class="text" id="item_test" name="item[test]" size="30" type="password" /></li>
118
+ HTML
119
+
120
+ assert_equal expected, output
121
+
122
+ end
123
+
124
+ def test_typus_selector_field
125
+
126
+ @resource = { :class => Post }
127
+ @item = posts(:published)
128
+
129
+ output = typus_selector_field('status')
130
+
131
+ expected = <<-HTML
132
+ <li><label for="item_status">Status</label>
133
+ <select id="item_status" name="item[status]">
134
+ <option value=""></option>
135
+ <option selected value="true">true</option>
136
+ <option value="false">false</option>
137
+ <option value="pending">pending</option>
138
+ <option value="published">published</option>
139
+ <option value="unpublished">unpublished</option>
140
+ </select></li>
141
+ HTML
142
+
143
+ assert_equal expected, output
144
+
145
+ end
146
+
147
+ def test_typus_text_field
148
+
149
+ output = typus_text_field('test', Post)
150
+ expected = <<-HTML
151
+ <li><label for="item_test">Test</label>
152
+ <textarea class="text" cols="40" id="item_test" name="item[test]" rows="10"></textarea></li>
153
+ HTML
154
+
155
+ assert_equal expected, output
156
+
157
+ end
158
+
159
+ def test_typus_time_field
160
+
161
+ output = typus_time_field('test', {}, Post)
162
+ expected = <<-HTML
163
+ <li><label for="item_test">Test</label>
164
+ HTML
165
+
166
+ assert_match expected, output
167
+
168
+ end
169
+
170
+ def test_typus_tree_field
171
+
172
+ return if !defined?(ActiveRecord::Acts::Tree)
173
+
174
+ self.stubs(:expand_tree_into_select_field).returns('expand_tree_into_select_field')
175
+
176
+ @resource = { :class => Page }
177
+ items = @resource[:class].roots
178
+
179
+ output = typus_tree_field('parent', items)
180
+ expected = <<-HTML
181
+ <li><label for="item_parent">Parent</label>
182
+ <select id="item_parent" name="item[parent]">
183
+ <option value=""></option>
184
+ expand_tree_into_select_field
185
+ </select></li>
186
+ HTML
187
+
188
+ assert_equal expected, output
189
+
190
+ end
191
+
192
+ def test_typus_string_field
193
+
194
+ output = typus_string_field('test', Post)
195
+ expected = <<-HTML
196
+ <li><label for="item_test">Test</label>
197
+ <input class="text" id="item_test" name="item[test]" size="30" type="text" /></li>
198
+ HTML
199
+
200
+ assert_equal expected, output
201
+
202
+ end
203
+
204
+ def test_typus_relationships
205
+ assert true
206
+ end
207
+
208
+ def test_typus_form_has_many_with_items
209
+
210
+ @current_user = typus_users(:admin)
211
+ @resource = { :class => Post, :self => 'posts' }
212
+ @item = Post.find(1)
213
+
214
+ params = { :controller => 'admin/posts', :id => 1, :action => 'edit' }
215
+ self.expects(:params).at_least_once.returns(params)
216
+
217
+ self.stubs(:build_list).returns('<!-- a_nice_list -->')
218
+
219
+ output = typus_form_has_many('comments')
220
+ expected = <<-HTML
221
+ <a name="comments"></a>
222
+ <div class="box_relationships">
223
+ <h2>
224
+ <a href="http://test.host/admin/comments">Comments</a>
225
+ <small><a href="http://test.host/admin/comments/new?resource=post&resource_id=1">Add new</a></small>
226
+ </h2>
227
+ <!-- a_nice_list --></div>
228
+ HTML
229
+
230
+ assert_equal expected, output
231
+
232
+ end
233
+
234
+ def test_typus_form_has_many_without_items
235
+
236
+ @current_user = typus_users(:admin)
237
+ @resource = { :class => Post, :self => 'posts' }
238
+ @item = Post.find(1)
239
+ @item.comments.destroy_all
240
+
241
+ params = { :controller => 'admin/posts', :id => 1, :action => 'edit' }
242
+ self.expects(:params).at_least_once.returns(params)
243
+
244
+ output = typus_form_has_many('comments')
245
+ expected = <<-HTML
246
+ <a name="comments"></a>
247
+ <div class="box_relationships">
248
+ <h2>
249
+ <a href="http://test.host/admin/comments">Comments</a>
250
+ <small><a href="http://test.host/admin/comments/new?resource=post&resource_id=1">Add new</a></small>
251
+ </h2>
252
+ <div id="flash" class="notice"><p>There are no comments.</p></div>
253
+ </div>
254
+ HTML
255
+
256
+ assert_equal expected, output
257
+
258
+ end
259
+
260
+ def test_typus_form_has_and_belongs_to_many
261
+ assert true
262
+ end
263
+
264
+ def test_typus_template_field
265
+ assert true
266
+ end
267
+
268
+ def test_attribute_disabled
269
+
270
+ assert !attribute_disabled?('test', Post)
271
+
272
+ Post.expects(:accessible_attributes).returns(['test'])
273
+ assert !attribute_disabled?('test', Post)
274
+
275
+ Post.expects(:accessible_attributes).returns(['no_test'])
276
+ assert attribute_disabled?('test', Post)
277
+
278
+ end
279
+
280
+ def test_expand_tree_into_select_field
281
+
282
+ return if !defined?(ActiveRecord::Acts::Tree)
283
+
284
+ items = Page.roots
285
+
286
+ # Page#1 is a root.
287
+
288
+ @item = Page.find(1)
289
+ output = expand_tree_into_select_field(items, 'parent_id')
290
+ expected = <<-HTML
291
+ <option value="1"> &#92;_ Page#1</option>
292
+ <option value="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#2</option>
293
+ <option value="3"> &#92;_ Page#3</option>
294
+ <option value="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#4</option>
295
+ <option value="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#5</option>
296
+ <option value="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#6</option>
297
+ HTML
298
+ assert_equal expected, output
299
+
300
+ # Page#4 is a children.
301
+
302
+ @item = Page.find(4)
303
+ output = expand_tree_into_select_field(items, 'parent_id')
304
+ expected = <<-HTML
305
+ <option value="1"> &#92;_ Page#1</option>
306
+ <option value="2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#2</option>
307
+ <option selected value="3"> &#92;_ Page#3</option>
308
+ <option value="4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#4</option>
309
+ <option value="5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#5</option>
310
+ <option value="6">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#92;_ Page#6</option>
311
+ HTML
312
+ assert_equal expected, output
313
+
314
+ end
315
+
316
+ end