link_list_block_test.rb
3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
require File.dirname(__FILE__) + '/../test_helper'
class LinkListBlockTest < ActiveSupport::TestCase
  should 'default describe' do
    assert_not_equal Block.description, LinkListBlock.description
  end
  should 'have field links' do
    l = LinkListBlock.new
    assert_respond_to l, :links
  end
  
  should 'default value of links' do
    l = LinkListBlock.new
    assert_equal [], l.links
  end
  should 'is editable' do
    l = LinkListBlock.new
    assert l.editable?
  end
  should 'list links' do
    l = LinkListBlock.new(:links => [{:name => 'products', :address => '/cat/products'}])
    l.expects(:links).returns([{:name => 'products', :address => '/cat/products'}])
    assert_match /products/, l.content
  end
  should 'remove links with blank fields' do
    l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/address'}, {:name => '', :address => ''}])
    l.save!
    assert_equal [{:name => 'categ', :address => '/address'}], l.links
  end
  should 'replace {profile} with profile identifier' do
    profile = Profile.new(:identifier => 'test_profile')
    l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{profile}/address'}])
    l.stubs(:owner).returns(profile)
    assert_tag_in_string l.content, :tag => 'a', :attributes => {:href => '/test_profile/address'}
  end
  should 'display options for icons' do
    l = LinkListBlock.new
    l.icons_options.each do |option|
      assert_match(/<span title=\".+\" class=\"icon-.+\" onclick=\"changeIcon\(this, '.+'\)\"><\/span>/, option)
    end
  end
  should 'link with icon' do
    l = LinkListBlock.new
    assert_match /class="icon-save"/, l.link_html({:icon => 'save', :name => 'test', :address => 'test.com'})
  end
  should 'no class without icon' do
    l = LinkListBlock.new
    assert_no_match /class="/, l.link_html({:icon => nil, :name => 'test', :address => 'test.com'})
  end
  should 'not add link to javascript' do
    l = LinkListBlock.new(:links => [{:name => 'link', :address => "javascript:alert('Message test')"}])
    assert_no_match /href="javascript/, l.link_html(l.links.first)
  end
  should 'not add link to onclick' do
    l = LinkListBlock.new(:links => [{:name => 'link', :address => "#\" onclick=\"alert(123456)"}])
    assert_no_tag_in_string l.link_html(l.links.first), :attributes => { :onclick => /.*/ }
  end
  should 'add http in front of incomplete external links' do
    {'/local/link' => '/local/link', 'http://example.org' => 'http://example.org', 'example.org' => 'http://example.org'}.each do |input, output|
      l = LinkListBlock.new(:links => [{:name => 'categ', :address => input}])
      assert_tag_in_string l.content, :tag => 'a', :attributes => {:href => output}
    end
  end
  should 'be able to update display setting' do
    user = create_user('testinguser').person
    box = fast_create(Box, :owner_id => user.id)
    block = LinkListBlock.create!(:display => 'never', :box => box)
    assert block.update_attributes!(:display => 'always')
    block.reload
    assert_equal 'always', block.display
  end
end