Commit 2daca633e85b2f11c37d76b8538d5c27c332d5da

Authored by Joenio Costa
Committed by Rafael Reggiani Manzo
1 parent 8cd2832e

fix LinkListBlock unit tests

app/helpers/boxes_helper.rb
... ... @@ -88,8 +88,13 @@ module BoxesHelper
88 88 end
89 89  
90 90 def render_block_content(block)
91   - # TODO: se nao existe retornar nil
92   - render :file => "blocks/#{block.name.underscore.gsub('_block', '')}", :locals => { :block => block }
  91 + template_name = block.class.name.underscore.gsub('_block', '')
  92 + template_filename = "#{template_name}.html.erb"
  93 + if File.exists? Rails.root.join('app', 'views', 'blocks', template_filename)
  94 + render :file => "blocks/#{template_name}", :locals => { :block => block }
  95 + else
  96 + nil
  97 + end
93 98 end
94 99  
95 100 def display_block_content(block, main_content = nil)
... ...
test/unit/link_list_block_test.rb
... ... @@ -2,6 +2,8 @@ require_relative "../test_helper"
2 2  
3 3 class LinkListBlockTest < ActiveSupport::TestCase
4 4  
  5 + include BoxesHelper
  6 +
5 7 should 'default describe' do
6 8 assert_not_equal Block.description, LinkListBlock.description
7 9 end
... ... @@ -23,7 +25,7 @@ class LinkListBlockTest &lt; ActiveSupport::TestCase
23 25  
24 26 should 'list links' do
25 27 l = LinkListBlock.new(:links => [{:name => 'products', :address => '/cat/products'}])
26   - assert_match /products/, l.content
  28 + assert_match /products/, render_block_content(l)
27 29 end
28 30  
29 31 should 'remove links with blank fields' do
... ... @@ -36,7 +38,7 @@ class LinkListBlockTest &lt; ActiveSupport::TestCase
36 38 profile = Profile.new(:identifier => 'test_profile')
37 39 l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{profile}/address'}])
38 40 l.stubs(:owner).returns(profile)
39   - assert_tag_in_string l.content, :tag => 'a', :attributes => {:href => '/test_profile/address'}
  41 + assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => {:href => '/test_profile/address'}
40 42 end
41 43  
42 44 should 'replace {portal} with environment portal identifier' do
... ... @@ -49,7 +51,7 @@ class LinkListBlockTest &lt; ActiveSupport::TestCase
49 51 stubs(:environment).returns(env)
50 52 l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{portal}/address'}])
51 53 l.stubs(:owner).returns(env)
52   - assert_tag_in_string l.content, :tag => 'a', :attributes => {:href => '/portal-community/address'}
  54 + assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => {:href => '/portal-community/address'}
53 55 end
54 56  
55 57 should 'not change address if no {portal} there' do
... ... @@ -62,19 +64,19 @@ class LinkListBlockTest &lt; ActiveSupport::TestCase
62 64 stubs(:environment).returns(env)
63 65 l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/address'}])
64 66 l.stubs(:owner).returns(env)
65   - assert_tag_in_string l.content, :tag => 'a', :attributes => {:href => '/address'}
  67 + assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => {:href => '/address'}
66 68 end
67 69  
68 70 should 'handle /prefix if not already added' do
69 71 Noosfero.stubs(:root).returns('/prefix')
70 72 l = LinkListBlock.new(:links => [{:name => "foo", :address => '/bar'}] )
71   - assert_tag_in_string l.content, :tag => 'a', :attributes => { :href => '/prefix/bar' }
  73 + assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => { :href => '/prefix/bar' }
72 74 end
73 75  
74 76 should 'not add /prefix if already there' do
75 77 Noosfero.stubs(:root).returns('/prefix')
76 78 l = LinkListBlock.new(:links => [{:name => "foo", :address => '/prefix/bar'}] )
77   - assert_tag_in_string l.content, :tag => 'a', :attributes => { :href => '/prefix/bar' }
  79 + assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => { :href => '/prefix/bar' }
78 80 end
79 81  
80 82 should 'display options for icons' do
... ... @@ -85,29 +87,29 @@ class LinkListBlockTest &lt; ActiveSupport::TestCase
85 87 end
86 88  
87 89 should 'link with icon' do
88   - l = LinkListBlock.new
89   - assert_match /class="icon-save"/, l.link_html({:icon => 'save', :name => 'test', :address => 'test.com'})
  90 + l = LinkListBlock.new(:links => [{:icon => 'save', :name => 'test', :address => 'test.com'}])
  91 + assert_match /a class="icon-[^"]+"/, render_block_content(l)
90 92 end
91 93  
92 94 should 'no class without icon' do
93   - l = LinkListBlock.new
94   - assert_no_match /class="/, l.link_html({:icon => nil, :name => 'test', :address => 'test.com'})
  95 + l = LinkListBlock.new(:links => [{:icon => nil, :name => 'test', :address => 'test.com'}])
  96 + assert_no_match /a class="icon-[^"]+"/, render_block_content(l)
95 97 end
96 98  
97 99 should 'not add link to javascript' do
98 100 l = LinkListBlock.new(:links => [{:name => 'link', :address => "javascript:alert('Message test')"}])
99   - assert_no_match /href="javascript/, l.link_html(l.links.first)
  101 + assert_no_match /href="javascript/, render_block_content(l)
100 102 end
101 103  
102 104 should 'not add link to onclick' do
103 105 l = LinkListBlock.new(:links => [{:name => 'link', :address => "#\" onclick=\"alert(123456)"}])
104   - assert_no_tag_in_string l.link_html(l.links.first), :attributes => { :onclick => /.*/ }
  106 + assert_no_tag_in_string render_block_content(l), :attributes => { :onclick => /.*/ }
105 107 end
106 108  
107 109 should 'add protocol in front of incomplete external links' do
108 110 {'/local/link' => '/local/link', 'http://example.org' => 'http://example.org', 'example.org' => '//example.org'}.each do |input, output|
109 111 l = LinkListBlock.new(:links => [{:name => 'categ', :address => input}])
110   - assert_tag_in_string l.content, :tag => 'a', :attributes => {:href => output}
  112 + assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => {:href => output}
111 113 end
112 114 end
113 115  
... ... @@ -128,7 +130,8 @@ class LinkListBlockTest &lt; ActiveSupport::TestCase
128 130  
129 131 should 'link with title' do
130 132 l = LinkListBlock.new
131   - assert_match /title="mytitle"/, l.link_html({:name => 'mylink', :address => '/myaddress', :title => 'mytitle'})
  133 + l = LinkListBlock.new(:links => [{:name => 'mylink', :address => '/myaddress', :title => 'mytitle'}])
  134 + assert_match /title="mytitle"/, render_block_content(l)
132 135 end
133 136  
134 137 end
... ...