cirandas.net

ref: master

test/unit/boxes_helper_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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require_relative "../test_helper"
require 'boxes_helper'

class BoxesHelperTest < ActionView::TestCase

  include ApplicationHelper
  include BoxesHelper
  include ActionView::Helpers::TagHelper

  def setup
    @controller = mock
    @controller.stubs(:custom_design).returns({})
    @controller.stubs(:boxes_editor?).returns(false)
    @controller.stubs(:uses_design_blocks?).returns(true)
  end

  should 'include profile-specific header' do
    holder = mock
    holder.stubs(:boxes).returns(boxes = [])
    boxes.stubs(:with_position).returns([])
    holder.stubs(:boxes_limit).returns(0)
    holder.stubs(:custom_header_expanded).returns('my custom header')
    @controller.stubs(:boxes_holder).returns(holder)

    assert_tag_in_string insert_boxes('main content'), :tag => "div", :attributes => { :id => 'profile-header' }, :content => 'my custom header'
  end

  should 'include profile-specific footer' do
    holder = mock
    holder.stubs(:boxes).returns(boxes = [])
    boxes.stubs(:with_position).returns([])
    holder.stubs(:boxes_limit).returns(0)
    holder.stubs(:custom_footer_expanded).returns('my custom footer')
    @controller.stubs(:boxes_holder).returns(holder)

    assert_tag_in_string insert_boxes('main content'), :tag => "div", :attributes => { :id => 'profile-footer' }, :content => 'my custom footer'
  end

  def create_user_with_blocks
    p = create_user('test_user').person
    create(LinkListBlock, :box => p.boxes.first)
    p
  end

  should 'display invisible block for editing' do
    p = create_user_with_blocks
    request = mock()
    request.expects(:path).returns(nil)
    request.expects(:params).returns({})


    b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0]
    b.display = 'never'; b.save!
    box = b.box
    box.blocks = [b]
    box.save!
    expects(:display_block).with(b, '')
    stubs(:request).returns(request)
    stubs(:block_target).returns('')
    stubs(:user).returns(nil)
    expects(:locale).returns('en')
    with_box_decorator self do
      display_box_content(box, '')
    end
  end

  should 'not display invisible block' do
    p = create_user_with_blocks
    request = mock()
    request.expects(:path).returns(nil)
    request.expects(:params).returns({})

    b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0]
    b.display = 'never'; b.save!
    box = b.box
    box.blocks = [b]
    box.save!
    expects(:display_block).with(b, '').never
    stubs(:request).returns(request)
    stubs(:user).returns(nil)
    stubs(:block_target).returns('')
    expects(:locale).returns('en')
    display_box_content(box, '')
  end

  should 'include profile-specific header without side boxes' do
    @controller.stubs(:uses_design_blocks?).returns(false)
    holder = mock
    holder.stubs(:boxes).returns([])
    holder.stubs(:boxes_limit).returns(0)
    holder.stubs(:custom_header_expanded).returns('my custom header')
    @controller.stubs(:boxes_holder).returns(holder)

    assert_tag_in_string insert_boxes('main content'), :tag => "div", :attributes => { :id => 'profile-header' }, :content => 'my custom header'
  end

  should 'include profile-specific footer without side boxes' do
    @controller.stubs(:uses_design_blocks?).returns(false)
    holder = mock
    holder.stubs(:boxes).returns([])
    holder.stubs(:boxes_limit).returns(0)
    holder.stubs(:custom_footer_expanded).returns('my custom footer')
    @controller.stubs(:boxes_holder).returns(holder)

    assert_tag_in_string insert_boxes('main content'), :tag => "div", :attributes => { :id => 'profile-footer' }, :content => 'my custom footer'
  end

  should 'add invisible CSS class name for invisible blocks' do
    refute block_css_classes(build(Block, :display => 'always')).split.any? { |item| item == 'invisible-block'}
    assert block_css_classes(build(Block, :display => 'never')).split.any? { |item| item == 'invisible-block'}
  end

  should 'fill context with the article, request_path and locale' do
    request = mock()
    box = create(Box, :owner => fast_create(Profile))
    request.expects(:path).returns('/')
    request.expects(:params).returns({})
    stubs(:request).returns(request)
    stubs(:user).returns(nil)
    expects(:locale).returns('en')
    box_decorator.expects(:select_blocks).with(box, [], {:article => nil, :request_path => '/', :locale => 'en', :params => {}, :user => nil, :controller => @controller}).returns([])

    display_box_content(box, '')
  end

  should 'not show move options on block when block has no permission to edit' do
    p = create_user_with_blocks

    b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0]
    b.move_modes = "none"
    b.save!

    stubs(:environment).returns(p.environment)
    stubs(:user).returns(p)

    assert_equal false, movable?(b)
  end

  should 'show move options on block when block has no permission to edit and user is admin' do
    p = create_user_with_blocks

    b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0]
    b.edit_modes = "none"
    b.save!

    p.environment.add_admin(p)

    stubs(:environment).returns(p.environment)
    stubs(:user).returns(p)

    assert_equal true, movable?(b)
  end

  should 'consider boxes_limit without custom_design' do
    holder = mock
    holder.stubs(:boxes_limit).with(nil).returns(2)
    assert_equal 2, boxes_limit(holder)
  end

  should 'consider boxes_limit with custom_design' do
    holder = mock
    @controller.expects(:custom_design).returns({boxes_limit: 1})

    assert_equal 1, boxes_limit(holder)
  end

  should 'insert block using custom_design' do
    request = mock
    request.expects(:path).returns('/')
    request.expects(:params).returns({})
    stubs(:request).returns(request)
    stubs(:user).returns(nil)
    expects(:locale).returns('en')

    box = create(Box, position: 1, owner: fast_create(Profile))
    block = ProfileImageBlock
    block.expects(:new).with(box: box)

    @controller.expects(:custom_design).returns({insert: {position: 1, block: block, box: 1}})

    stubs(:display_block)
    display_box_content(box, '')
  end

  should 'display embed button when a block is embedable' do
    box = create(Box, position: 1, owner: fast_create(Profile))
    block = Block.create!(:box => box)
    block.stubs(:embedable?).returns(true)
    stubs(:url_for).returns('')
    @controller.stubs(:user).returns(box.owner)
    assert_tag_in_string block_edit_buttons(block), :tag => 'a', :attributes => {:class => 'button icon-button icon-embed '}
  end

  should 'not display embed button when a block is not embedable' do
    box = create(Box, position: 1, owner: fast_create(Profile))
    block = Block.create!(:box => box)
    block.stubs(:embedable?).returns(false)
    stubs(:url_for).returns('')
    @controller.stubs(:user).returns(box.owner)
    assert_no_tag_in_string block_edit_buttons(block), :tag => 'a', :attributes => {:class => 'button icon-button icon-embed '}
  end

  should 'only show edit option on block' do
    p = create_user_with_blocks

    b = p.blocks.select{|bk| !bk.kind_of?(MainBlock) }[0]
    b.edit_modes = "only_edit"
    b.save!

    stubs(:environment).returns(p.environment)
    stubs(:user).returns(p)

    assert_equal false, b.editable?
  end
end