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