Commit bc5af220914fa5072d21fd86827ae1e329ba8a4d

Authored by Rodrigo Souto
1 parent 31d0610a

rails3: fix application_helper tests

PS: still has a failing test due to monkey patches not loading.
app/helpers/application_helper.rb
@@ -40,6 +40,8 @@ module ApplicationHelper @@ -40,6 +40,8 @@ module ApplicationHelper
40 40
41 include LayoutHelper 41 include LayoutHelper
42 42
  43 + VIEW_EXTENSIONS = ['.rhtml', '.html.erb']
  44 +
43 def locale 45 def locale
44 (@page && !@page.language.blank?) ? @page.language : FastGettext.locale 46 (@page && !@page.language.blank?) ? @page.language : FastGettext.locale
45 end 47 end
@@ -289,8 +291,10 @@ module ApplicationHelper @@ -289,8 +291,10 @@ module ApplicationHelper
289 search_name = "_" + search_name 291 search_name = "_" + search_name
290 end 292 end
291 293
292 - path = defined?(params) && params[:controller] ? File.join(view_path, params[:controller], search_name + '.html.erb') : File.join(view_path, search_name + '.html.erb')  
293 - return name if File.exists?(File.join(path)) 294 + VIEW_EXTENSIONS.each do |ext|
  295 + path = defined?(params) && params[:controller] ? File.join(view_path, params[:controller], search_name + ext) : File.join(view_path, search_name + ext)
  296 + return name if File.exists?(File.join(path))
  297 + end
294 298
295 partial_for_class_in_view_path(klass.superclass, view_path, prefix, suffix) 299 partial_for_class_in_view_path(klass.superclass, view_path, prefix, suffix)
296 end 300 end
@@ -328,7 +332,7 @@ module ApplicationHelper @@ -328,7 +332,7 @@ module ApplicationHelper
328 "\n" + 332 "\n" +
329 sources.flatten.map do |source| 333 sources.flatten.map do |source|
330 filename = filename_for_stylesheet(source.to_s, themed_source) 334 filename = filename_for_stylesheet(source.to_s, themed_source)
331 - if File.exists?(Rails.root.join('public', filename)) 335 + if File.exists?(Rails.root.join('public', filename[1..-1]))
332 "@import url(#{filename});\n" 336 "@import url(#{filename});\n"
333 else 337 else
334 "/* Not included: url(#{filename}) */\n" 338 "/* Not included: url(#{filename}) */\n"
@@ -369,10 +373,10 @@ module ApplicationHelper @@ -369,10 +373,10 @@ module ApplicationHelper
369 # utility for developers: set the theme to 'random' in development mode and 373 # utility for developers: set the theme to 'random' in development mode and
370 # you will get a different theme every request. This is interesting for 374 # you will get a different theme every request. This is interesting for
371 # testing 375 # testing
372 - if Rails.env == 'development' && environment.theme == 'random' 376 + if Rails.env.development? && environment.theme == 'random'
373 @random_theme ||= Dir.glob('public/designs/themes/*').map { |f| File.basename(f) }.rand 377 @random_theme ||= Dir.glob('public/designs/themes/*').map { |f| File.basename(f) }.rand
374 @random_theme 378 @random_theme
375 - elsif Rails.env == 'development' && params[:theme] && File.exists?(Rails.root.join('public/designs/themes', params[:theme])) 379 + elsif Rails.env.development? && params[:theme] && File.exists?(Rails.root.join('public/designs/themes', params[:theme]))
376 params[:theme] 380 params[:theme]
377 else 381 else
378 if profile && !profile.theme.nil? 382 if profile && !profile.theme.nil?
@@ -397,8 +401,8 @@ module ApplicationHelper @@ -397,8 +401,8 @@ module ApplicationHelper
397 def theme_include(template) 401 def theme_include(template)
398 # XXX Since we cannot control what people are doing in external themes, we 402 # XXX Since we cannot control what people are doing in external themes, we
399 # will keep looking for the deprecated .rhtml extension here. 403 # will keep looking for the deprecated .rhtml extension here.
400 - ['.rhtml', '.html.erb'].each do |ext|  
401 - file = Rails.root.join('public', theme_path, template + ext) 404 + VIEW_EXTENSIONS.each do |ext|
  405 + file = Rails.root.join('public', theme_path[1..-1], template + ext)
402 if File.exists?(file) 406 if File.exists?(file)
403 return render :file => file, :use_full_path => false 407 return render :file => file, :use_full_path => false
404 end 408 end
@@ -923,7 +927,7 @@ module ApplicationHelper @@ -923,7 +927,7 @@ module ApplicationHelper
923 theme_icon_themes = theme_option(:icon_theme) || [] 927 theme_icon_themes = theme_option(:icon_theme) || []
924 for icon_theme in theme_icon_themes do 928 for icon_theme in theme_icon_themes do
925 theme_path = "/designs/icons/#{icon_theme}/style.css" 929 theme_path = "/designs/icons/#{icon_theme}/style.css"
926 - if File.exists?(Rails.root.join('public', theme_path)) 930 + if File.exists?(Rails.root.join('public', theme_path[1..-1]))
927 icon_themes << theme_path 931 icon_themes << theme_path
928 end 932 end
929 end 933 end
test/unit/application_helper_test.rb
1 # encoding: UTF-8 1 # encoding: UTF-8
2 require File.dirname(__FILE__) + '/../test_helper' 2 require File.dirname(__FILE__) + '/../test_helper'
3 3
4 -class ApplicationHelperTest < ActiveSupport::TestCase 4 +class ApplicationHelperTest < ActionView::TestCase
5 5
6 include ApplicationHelper 6 include ApplicationHelper
7 7
@@ -16,26 +16,14 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -16,26 +16,14 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
16 @controller.stubs(:view_paths).returns([p1,p2]) 16 @controller.stubs(:view_paths).returns([p1,p2])
17 17
18 self.stubs(:params).returns({:controller => 'test'}) 18 self.stubs(:params).returns({:controller => 'test'})
  19 + File.stubs(:exists?).returns(false)
19 20
20 File.expects(:exists?).with(p1+"test/_integer.rhtml").returns(true) 21 File.expects(:exists?).with(p1+"test/_integer.rhtml").returns(true)
21 -  
22 - File.expects(:exists?).with(p1+"test/_float.rhtml").returns(false)  
23 - File.expects(:exists?).with(p1+"test/_float.html.erb").returns(false)  
24 - File.expects(:exists?).with(p2+"test/_float.rhtml").returns(false)  
25 - File.expects(:exists?).with(p2+"test/_float.html.erb").returns(false)  
26 - File.expects(:exists?).with(p1+"test/_numeric.rhtml").returns(false)  
27 - File.expects(:exists?).with(p1+"test/_object.rhtml").returns(false)  
28 - File.expects(:exists?).with(p1+"test/_object.html.erb").returns(false)  
29 - File.expects(:exists?).with(p1+"test/_numeric.html.erb").returns(false)  
30 - File.expects(:exists?).with(p2+"test/_numeric.rhtml").returns(true)  
31 -  
32 - File.expects(:exists?).with(p1+"test/_object.rhtml").returns(false)  
33 - File.expects(:exists?).with(p1+"test/_object.html.erb").returns(false)  
34 - File.expects(:exists?).with(p2+"test/_object.rhtml").returns(false)  
35 - File.expects(:exists?).with(p2+"test/_object.html.erb").returns(false)  
36 -  
37 assert_equal 'integer', partial_for_class(Integer) 22 assert_equal 'integer', partial_for_class(Integer)
  23 +
  24 + File.expects(:exists?).with(p1+"test/_numeric.html.erb").returns(true)
38 assert_equal 'numeric', partial_for_class(Float) 25 assert_equal 'numeric', partial_for_class(Float)
  26 +
39 assert_raises ArgumentError do 27 assert_raises ArgumentError do
40 partial_for_class(Object) 28 partial_for_class(Object)
41 end 29 end
@@ -49,15 +37,14 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -49,15 +37,14 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
49 37
50 class School; class Project; end; end 38 class School; class Project; end; end
51 39
  40 + File.stubs(:exists?).returns(false)
52 File.expects(:exists?).with(p+"test/application_helper_test/school/_project.rhtml").returns(true) 41 File.expects(:exists?).with(p+"test/application_helper_test/school/_project.rhtml").returns(true)
53 42
54 assert_equal 'test/application_helper_test/school/project', partial_for_class(School::Project) 43 assert_equal 'test/application_helper_test/school/project', partial_for_class(School::Project)
55 end 44 end
56 45
57 should 'look for superclasses on view_for_profile actions' do 46 should 'look for superclasses on view_for_profile actions' do
58 - File.expects(:exists?).with(Rails.root.join('app', 'views', 'blocks', 'profile_info_actions', 'float.rhtml')).returns(false)  
59 - File.expects(:exists?).with(Rails.root.join('app', 'views', 'blocks', 'profile_info_actions', 'float.html.erb')).returns(false)  
60 - File.expects(:exists?).with(Rails.root.join('app', 'views', 'blocks', 'profile_info_actions', 'numeric.rhtml')).returns(false) 47 + File.stubs(:exists?).returns(false)
61 File.expects(:exists?).with(Rails.root.join('app', 'views', 'blocks', 'profile_info_actions', 'numeric.html.erb')).returns(true) 48 File.expects(:exists?).with(Rails.root.join('app', 'views', 'blocks', 'profile_info_actions', 'numeric.html.erb')).returns(true)
62 49
63 assert_equal 'blocks/profile_info_actions/numeric.html.erb', view_for_profile_actions(Float) 50 assert_equal 'blocks/profile_info_actions/numeric.html.erb', view_for_profile_actions(Float)
@@ -70,6 +57,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -70,6 +57,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
70 end 57 end
71 58
72 should 'generate link to stylesheet' do 59 should 'generate link to stylesheet' do
  60 + File.stubs(:exists?).returns(false)
73 File.expects(:exists?).with(Rails.root.join('public', 'stylesheets', 'something.css')).returns(true) 61 File.expects(:exists?).with(Rails.root.join('public', 'stylesheets', 'something.css')).returns(true)
74 expects(:filename_for_stylesheet).with('something', nil).returns('/stylesheets/something.css') 62 expects(:filename_for_stylesheet).with('something', nil).returns('/stylesheets/something.css')
75 assert_match '@import url(/stylesheets/something.css)', stylesheet_import('something') 63 assert_match '@import url(/stylesheets/something.css)', stylesheet_import('something')
@@ -231,6 +219,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -231,6 +219,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
231 219
232 should 'use environment´s template when there is no profile' do 220 should 'use environment´s template when there is no profile' do
233 stubs(:profile).returns(nil) 221 stubs(:profile).returns(nil)
  222 + self.stubs(:environment).returns(Environment.default)
234 environment.expects(:layout_template).returns('sometemplate') 223 environment.expects(:layout_template).returns('sometemplate')
235 assert_equal "/designs/templates/sometemplate/stylesheets/style.css", template_stylesheet_path 224 assert_equal "/designs/templates/sometemplate/stylesheets/style.css", template_stylesheet_path
236 end 225 end
@@ -263,21 +252,21 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -263,21 +252,21 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
263 stubs(:environment).returns(Environment.default) 252 stubs(:environment).returns(Environment.default)
264 expects(:content_tag).with(anything, 'male').returns('MALE!!') 253 expects(:content_tag).with(anything, 'male').returns('MALE!!')
265 expects(:content_tag).with(anything, 'MALE!!', is_a(Hash)).returns("FINAL") 254 expects(:content_tag).with(anything, 'MALE!!', is_a(Hash)).returns("FINAL")
266 - assert_equal "FINAL", profile_sex_icon(Person.new(:sex => 'male')) 255 + assert_equal "FINAL", profile_sex_icon(build(Person, :sex => 'male'))
267 end 256 end
268 257
269 should 'provide sex icon for females' do 258 should 'provide sex icon for females' do
270 stubs(:environment).returns(Environment.default) 259 stubs(:environment).returns(Environment.default)
271 expects(:content_tag).with(anything, 'female').returns('FEMALE!!') 260 expects(:content_tag).with(anything, 'female').returns('FEMALE!!')
272 expects(:content_tag).with(anything, 'FEMALE!!', is_a(Hash)).returns("FINAL") 261 expects(:content_tag).with(anything, 'FEMALE!!', is_a(Hash)).returns("FINAL")
273 - assert_equal "FINAL", profile_sex_icon(Person.new(:sex => 'female')) 262 + assert_equal "FINAL", profile_sex_icon(build(Person, :sex => 'female'))
274 end 263 end
275 264
276 should 'provide undef sex icon' do 265 should 'provide undef sex icon' do
277 stubs(:environment).returns(Environment.default) 266 stubs(:environment).returns(Environment.default)
278 expects(:content_tag).with(anything, 'undef').returns('UNDEF!!') 267 expects(:content_tag).with(anything, 'undef').returns('UNDEF!!')
279 expects(:content_tag).with(anything, 'UNDEF!!', is_a(Hash)).returns("FINAL") 268 expects(:content_tag).with(anything, 'UNDEF!!', is_a(Hash)).returns("FINAL")
280 - assert_equal "FINAL", profile_sex_icon(Person.new(:sex => nil)) 269 + assert_equal "FINAL", profile_sex_icon(build(Person, :sex => nil))
281 end 270 end
282 271
283 should 'not draw sex icon for non-person profiles' do 272 should 'not draw sex icon for non-person profiles' do
@@ -288,11 +277,11 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -288,11 +277,11 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
288 env = fast_create(Environment, :name => 'env test') 277 env = fast_create(Environment, :name => 'env test')
289 env.expects(:enabled?).with('disable_gender_icon').returns(true) 278 env.expects(:enabled?).with('disable_gender_icon').returns(true)
290 stubs(:environment).returns(env) 279 stubs(:environment).returns(env)
291 - assert_equal '', profile_sex_icon(Person.new(:sex => 'male')) 280 + assert_equal '', profile_sex_icon(build(Person, :sex => 'male'))
292 end 281 end
293 282
294 should 'display field on person signup' do 283 should 'display field on person signup' do
295 - env = Environment.create!(:name => 'env test') 284 + env = create(Environment, :name => 'env test')
296 stubs(:environment).returns(env) 285 stubs(:environment).returns(env)
297 286
298 controller = mock 287 controller = mock
@@ -305,7 +294,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -305,7 +294,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
305 end 294 end
306 295
307 should 'display field on enterprise registration' do 296 should 'display field on enterprise registration' do
308 - env = Environment.create!(:name => 'env test') 297 + env = create(Environment, :name => 'env test')
309 stubs(:environment).returns(env) 298 stubs(:environment).returns(env)
310 299
311 controller = mock 300 controller = mock
@@ -319,7 +308,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -319,7 +308,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
319 end 308 end
320 309
321 should 'display field on community creation' do 310 should 'display field on community creation' do
322 - env = Environment.create!(:name => 'env test') 311 + env = create(Environment, :name => 'env test')
323 stubs(:environment).returns(env) 312 stubs(:environment).returns(env)
324 313
325 controller = mock 314 controller = mock
@@ -345,7 +334,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -345,7 +334,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
345 end 334 end
346 335
347 should 'not display field on enterprise registration' do 336 should 'not display field on enterprise registration' do
348 - env = Environment.create!(:name => 'env test') 337 + env = create(Environment, :name => 'env test')
349 stubs(:environment).returns(env) 338 stubs(:environment).returns(env)
350 339
351 controller = mock 340 controller = mock
@@ -359,7 +348,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -359,7 +348,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
359 end 348 end
360 349
361 should 'not display field on community creation' do 350 should 'not display field on community creation' do
362 - env = Environment.create!(:name => 'env test') 351 + env = create(Environment, :name => 'env test')
363 stubs(:environment).returns(env) 352 stubs(:environment).returns(env)
364 353
365 controller = mock 354 controller = mock
@@ -482,17 +471,17 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -482,17 +471,17 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
482 end 471 end
483 472
484 should 'use theme passed via param when in development mode' do 473 should 'use theme passed via param when in development mode' do
485 - stubs(:environment).returns(Environment.new(:theme => 'environment-theme'))  
486 - ENV.stubs(:[]).with('RAILS_ENV').returns('development') 474 + stubs(:environment).returns(build(Environment, :theme => 'environment-theme'))
  475 + Rails.env.stubs(:development?).returns(true)
487 self.stubs(:params).returns({:theme => 'skyblue'}) 476 self.stubs(:params).returns({:theme => 'skyblue'})
488 assert_equal 'skyblue', current_theme 477 assert_equal 'skyblue', current_theme
489 end 478 end
490 479
491 should 'not use theme passed via param when in production mode' do 480 should 'not use theme passed via param when in production mode' do
492 - stubs(:environment).returns(Environment.new(:theme => 'environment-theme')) 481 + stubs(:environment).returns(build(Environment, :theme => 'environment-theme'))
493 ENV.stubs(:[]).with('RAILS_ENV').returns('production') 482 ENV.stubs(:[]).with('RAILS_ENV').returns('production')
494 self.stubs(:params).returns({:theme => 'skyblue'}) 483 self.stubs(:params).returns({:theme => 'skyblue'})
495 - stubs(:profile).returns(Profile.new(:theme => 'profile-theme')) 484 + stubs(:profile).returns(build(Profile, :theme => 'profile-theme'))
496 assert_equal 'profile-theme', current_theme 485 assert_equal 'profile-theme', current_theme
497 end 486 end
498 487
@@ -578,7 +567,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -578,7 +567,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
578 should 'use favicon from profile articles if the profile theme does not have' do 567 should 'use favicon from profile articles if the profile theme does not have' do
579 stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme')) 568 stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
580 stubs(:profile).returns(fast_create(Profile, :theme => 'profile-theme')) 569 stubs(:profile).returns(fast_create(Profile, :theme => 'profile-theme'))
581 - file = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/favicon.ico', 'image/x-ico'), :profile => profile) 570 + file = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/favicon.ico', 'image/x-ico'), :profile => profile)
582 File.expects(:exists?).with(Rails.root.join('public', theme_path, 'favicon.ico')).returns(false) 571 File.expects(:exists?).with(Rails.root.join('public', theme_path, 'favicon.ico')).returns(false)
583 572
584 assert_match /favicon.ico/, theme_favicon 573 assert_match /favicon.ico/, theme_favicon
@@ -600,7 +589,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -600,7 +589,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
600 path = Rails.root.join('app', 'views') 589 path = Rails.root.join('app', 'views')
601 @controller.stubs(:view_paths).returns([path]) 590 @controller.stubs(:view_paths).returns([path])
602 591
603 - file = path + '/shared/usermenu/xmpp_chat.rhtml' 592 + file = path.join('shared','usermenu', 'xmpp_chat.html.erb')
604 expects(:render).with(:file => file, :use_full_path => false).returns('Open chat') 593 expects(:render).with(:file => file, :use_full_path => false).returns('Open chat')
605 594
606 assert_equal 'Open chat', render_environment_features(:usermenu) 595 assert_equal 'Open chat', render_environment_features(:usermenu)
@@ -636,7 +625,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -636,7 +625,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
636 625
637 should 'show task information with the requestor' do 626 should 'show task information with the requestor' do
638 person = create_user('usertest').person 627 person = create_user('usertest').person
639 - task = Task.create(:requestor => person) 628 + task = create(Task, :requestor => person)
640 assert_match person.name, task_information(task) 629 assert_match person.name, task_information(task)
641 end 630 end
642 631
@@ -729,7 +718,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -729,7 +718,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
729 718
730 should 'reference to article, in a profile with domain' do 719 should 'reference to article, in a profile with domain' do
731 c = fast_create(Community) 720 c = fast_create(Community)
732 - c.domains << Domain.new(:name=>'domain.xyz') 721 + c.domains << build(Domain, :name=>'domain.xyz')
733 b = fast_create(Blog, :profile_id => c.id) 722 b = fast_create(Blog, :profile_id => c.id)
734 a = fast_create(TinyMceArticle, :profile_id => c.id, :parent_id => b.id) 723 a = fast_create(TinyMceArticle, :profile_id => c.id, :parent_id => b.id)
735 a.save! 724 a.save!
@@ -788,8 +777,9 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -788,8 +777,9 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
788 result = button_bar :id=>'bt1' do 777 result = button_bar :id=>'bt1' do
789 '<b>foo</b>' 778 '<b>foo</b>'
790 end 779 end
791 - assert_equal '<div class="button-bar" id="bt1"><b>foo</b>'+  
792 - '<br style=\'clear: left;\' /></div>', result 780 + assert_tag_in_string result, :tag =>'div', :attributes => {:class => 'button-bar', :id => 'bt1'}
  781 + assert_tag_in_string result, :tag =>'b', :content => 'foo', :parent => {:tag => 'div', :attributes => {:id => 'bt1'}}
  782 + assert_tag_in_string result, :tag =>'br', :parent => {:tag => 'div', :attributes => {:id => 'bt1'}}
793 end 783 end
794 784
795 protected 785 protected