Commit 2daca633e85b2f11c37d76b8538d5c27c332d5da
Committed by
Rafael Reggiani Manzo
1 parent
8cd2832e
Exists in
web_steps_improvements
and in
9 other branches
fix LinkListBlock unit tests
Showing
2 changed files
with
24 additions
and
16 deletions
Show diff stats
app/helpers/boxes_helper.rb
@@ -88,8 +88,13 @@ module BoxesHelper | @@ -88,8 +88,13 @@ module BoxesHelper | ||
88 | end | 88 | end |
89 | 89 | ||
90 | def render_block_content(block) | 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 | end | 98 | end |
94 | 99 | ||
95 | def display_block_content(block, main_content = nil) | 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,6 +2,8 @@ require_relative "../test_helper" | ||
2 | 2 | ||
3 | class LinkListBlockTest < ActiveSupport::TestCase | 3 | class LinkListBlockTest < ActiveSupport::TestCase |
4 | 4 | ||
5 | + include BoxesHelper | ||
6 | + | ||
5 | should 'default describe' do | 7 | should 'default describe' do |
6 | assert_not_equal Block.description, LinkListBlock.description | 8 | assert_not_equal Block.description, LinkListBlock.description |
7 | end | 9 | end |
@@ -23,7 +25,7 @@ class LinkListBlockTest < ActiveSupport::TestCase | @@ -23,7 +25,7 @@ class LinkListBlockTest < ActiveSupport::TestCase | ||
23 | 25 | ||
24 | should 'list links' do | 26 | should 'list links' do |
25 | l = LinkListBlock.new(:links => [{:name => 'products', :address => '/cat/products'}]) | 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 | end | 29 | end |
28 | 30 | ||
29 | should 'remove links with blank fields' do | 31 | should 'remove links with blank fields' do |
@@ -36,7 +38,7 @@ class LinkListBlockTest < ActiveSupport::TestCase | @@ -36,7 +38,7 @@ class LinkListBlockTest < ActiveSupport::TestCase | ||
36 | profile = Profile.new(:identifier => 'test_profile') | 38 | profile = Profile.new(:identifier => 'test_profile') |
37 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{profile}/address'}]) | 39 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{profile}/address'}]) |
38 | l.stubs(:owner).returns(profile) | 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 | end | 42 | end |
41 | 43 | ||
42 | should 'replace {portal} with environment portal identifier' do | 44 | should 'replace {portal} with environment portal identifier' do |
@@ -49,7 +51,7 @@ class LinkListBlockTest < ActiveSupport::TestCase | @@ -49,7 +51,7 @@ class LinkListBlockTest < ActiveSupport::TestCase | ||
49 | stubs(:environment).returns(env) | 51 | stubs(:environment).returns(env) |
50 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{portal}/address'}]) | 52 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{portal}/address'}]) |
51 | l.stubs(:owner).returns(env) | 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 | end | 55 | end |
54 | 56 | ||
55 | should 'not change address if no {portal} there' do | 57 | should 'not change address if no {portal} there' do |
@@ -62,19 +64,19 @@ class LinkListBlockTest < ActiveSupport::TestCase | @@ -62,19 +64,19 @@ class LinkListBlockTest < ActiveSupport::TestCase | ||
62 | stubs(:environment).returns(env) | 64 | stubs(:environment).returns(env) |
63 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/address'}]) | 65 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/address'}]) |
64 | l.stubs(:owner).returns(env) | 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 | end | 68 | end |
67 | 69 | ||
68 | should 'handle /prefix if not already added' do | 70 | should 'handle /prefix if not already added' do |
69 | Noosfero.stubs(:root).returns('/prefix') | 71 | Noosfero.stubs(:root).returns('/prefix') |
70 | l = LinkListBlock.new(:links => [{:name => "foo", :address => '/bar'}] ) | 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 | end | 74 | end |
73 | 75 | ||
74 | should 'not add /prefix if already there' do | 76 | should 'not add /prefix if already there' do |
75 | Noosfero.stubs(:root).returns('/prefix') | 77 | Noosfero.stubs(:root).returns('/prefix') |
76 | l = LinkListBlock.new(:links => [{:name => "foo", :address => '/prefix/bar'}] ) | 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 | end | 80 | end |
79 | 81 | ||
80 | should 'display options for icons' do | 82 | should 'display options for icons' do |
@@ -85,29 +87,29 @@ class LinkListBlockTest < ActiveSupport::TestCase | @@ -85,29 +87,29 @@ class LinkListBlockTest < ActiveSupport::TestCase | ||
85 | end | 87 | end |
86 | 88 | ||
87 | should 'link with icon' do | 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 | end | 92 | end |
91 | 93 | ||
92 | should 'no class without icon' do | 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 | end | 97 | end |
96 | 98 | ||
97 | should 'not add link to javascript' do | 99 | should 'not add link to javascript' do |
98 | l = LinkListBlock.new(:links => [{:name => 'link', :address => "javascript:alert('Message test')"}]) | 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 | end | 102 | end |
101 | 103 | ||
102 | should 'not add link to onclick' do | 104 | should 'not add link to onclick' do |
103 | l = LinkListBlock.new(:links => [{:name => 'link', :address => "#\" onclick=\"alert(123456)"}]) | 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 | end | 107 | end |
106 | 108 | ||
107 | should 'add protocol in front of incomplete external links' do | 109 | should 'add protocol in front of incomplete external links' do |
108 | {'/local/link' => '/local/link', 'http://example.org' => 'http://example.org', 'example.org' => '//example.org'}.each do |input, output| | 110 | {'/local/link' => '/local/link', 'http://example.org' => 'http://example.org', 'example.org' => '//example.org'}.each do |input, output| |
109 | l = LinkListBlock.new(:links => [{:name => 'categ', :address => input}]) | 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 | end | 113 | end |
112 | end | 114 | end |
113 | 115 | ||
@@ -128,7 +130,8 @@ class LinkListBlockTest < ActiveSupport::TestCase | @@ -128,7 +130,8 @@ class LinkListBlockTest < ActiveSupport::TestCase | ||
128 | 130 | ||
129 | should 'link with title' do | 131 | should 'link with title' do |
130 | l = LinkListBlock.new | 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 | end | 135 | end |
133 | 136 | ||
134 | end | 137 | end |