cirandas.net

ref: master

test/unit/link_list_block_test.rb


  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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require_relative "../test_helper"

class LinkListBlockTest < ActiveSupport::TestCase

  include BoxesHelper

  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'}])
    assert_match /products/, render_block_content(l)
  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 render_block_content(l), :tag => 'a', :attributes => {:href => '/test_profile/address'}
  end

  should 'replace {portal} with environment portal identifier' do
    env = Environment.default
    env.enable('use_portal_community')
    portal = fast_create(Community, :identifier => 'portal-community', :environment_id => env.id)
    env.portal_community = portal
    env.save

    stubs(:environment).returns(env)
    l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/{portal}/address'}])
    l.stubs(:owner).returns(env)
    assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => {:href => '/portal-community/address'}
  end

  should 'not change address if no {portal} there' do
    env = Environment.default
    env.enable('use_portal_community')
    portal = fast_create(Community, :identifier => 'portal-community', :environment_id => env.id)
    env.portal_community = portal
    env.save

    stubs(:environment).returns(env)
    l = LinkListBlock.new(:links => [{:name => 'categ', :address => '/address'}])
    l.stubs(:owner).returns(env)
    assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => {:href => '/address'}
  end

  should 'handle /prefix if not already added' do
    Noosfero.stubs(:root).returns('/prefix')
    l = LinkListBlock.new(:links => [{:name => "foo", :address => '/bar'}] )
    assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => { :href => '/prefix/bar' }
  end

  should 'not add /prefix if already there' do
    Noosfero.stubs(:root).returns('/prefix')
    l = LinkListBlock.new(:links => [{:name => "foo", :address => '/prefix/bar'}] )
    assert_tag_in_string render_block_content(l), :tag => 'a', :attributes => { :href => '/prefix/bar' }
  end

  should 'link with icon' do
    l = LinkListBlock.new(:links => [{:icon => 'save', :name => 'test', :address => 'test.com'}])
    assert_match /a class="icon-[^"]+"/, render_block_content(l)
  end

  should 'no class without icon' do
    l = LinkListBlock.new(:links => [{:icon => nil, :name => 'test', :address => 'test.com'}])
    assert_no_match /a class="icon-[^"]+"/, render_block_content(l)
  end

  should 'not add link to javascript' do
    l = LinkListBlock.new(:links => [{:name => 'link', :address => "javascript:alert('Message test')"}])
    assert_no_match /href="javascript/, render_block_content(l)
  end

  should 'not add link to onclick' do
    l = LinkListBlock.new(:links => [{:name => 'link', :address => "#\" onclick=\"alert(123456)"}])
    assert_no_tag_in_string render_block_content(l), :attributes => { :onclick => /.*/ }
  end

  should 'add protocol in front of incomplete external links' do
    {'/local/link' => '/local/link', 'http://example.org' => 'http://example.org', 'example.org' => '//example.org'}.each do |input, output|
      l = LinkListBlock.new(:links => [{:name => 'categ', :address => input}])
      assert_tag_in_string render_block_content(l), :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.new(:display => 'never').tap do |b|
      b.box = box
    end
    assert block.update!(:display => 'always')
    block.reload
    assert_equal 'always', block.display
  end

  should 'have options for links target' do
    assert_equivalent LinkListBlock::TARGET_OPTIONS.map {|t|t[1]}, ['_self', '_blank', '_new']
  end

  should 'link with title' do
    l = LinkListBlock.new(:links => [{:name => 'mylink', :address => '/myaddress', :title => 'mytitle'}])
    assert_match /title="mytitle"/, render_block_content(l)
  end

  should 'display default message to brand new blocks with no links' do
    l = LinkListBlock.new
    assert_match /Please, edit this block to add links/, render_block_content(l)
  end

end