cirandas.net

ref: master

test/functional/environment_design_controller_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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
require_relative '../test_helper'

class EnvironmentDesignControllerTest < ActionController::TestCase

  ALL_BLOCKS = [ArticleBlock, LoginBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, CategoriesBlock, RawHTMLBlock, TagsCloudBlock ]

  def setup
    @controller = EnvironmentDesignController.new
    @controller.stubs(:boxes_holder).returns(Environment.default)

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
  end

  should 'indicate only actual blocks as such' do
    @controller.stubs(:user).returns(create_user.person)
    assert(@controller.available_blocks.all? {|item| item.new.is_a? Block})
  end

  ALL_BLOCKS.map do |block|
    define_method "test_should_#{block.to_s}_is_available" do
      @controller.stubs(:user).returns(create_user.person)
      assert_includes @controller.available_blocks,block
    end
  end

  should 'all available block in test' do
    @controller.stubs(:user).returns(create_user.person)
    assert_equal ALL_BLOCKS, @controller.available_blocks
  end

  should 'be able to edit LinkListBlock' do
    login_as(create_admin_user(Environment.default))
    l = LinkListBlock.create!(:links => [{:name => 'link 1', :address => '/address_1'}])
    Environment.default.boxes.create!
    Environment.default.boxes.first.blocks << l
    get :edit, :id => l.id
    assert_tag :tag => 'input', :attributes => { :name => 'block[links][][name]' }
    assert_tag :tag => 'input', :attributes => { :name => 'block[links][][address]' }
  end

  should 'be able to save LinkListBlock' do
    login_as(create_admin_user(Environment.default))
    l = LinkListBlock.create!()
    Environment.default.boxes.create!
    Environment.default.boxes.first.blocks << l
    post :save, :id => l.id, :block => { :links => [{:name => 'link 1', :address => '/address_1'}] }
    l.reload
    assert_equal [{'name' => 'link 1', 'address' => '/address_1'}], l.links
  end

  should 'be able to edit ArticleBlock with portal community' do
    login_as(create_admin_user(Environment.default))
    l = ArticleBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << l
    community = mock()
    Environment.any_instance.stubs(:portal_community).returns(community)
    article = fast_create(Article)
    community.stubs(:articles).returns([article])
    get :edit, :id => l.id
    assert_tag :tag => 'select', :attributes => { :name => 'block[article_id]' }
  end

  should 'be able to edit ArticleBlock without portal community' do
    login_as(create_admin_user(Environment.default))
    l = ArticleBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << l
    community = mock()
    Environment.any_instance.expects(:portal_community).returns(nil)
    get :edit, :id => l.id
    assert_tag :tag => 'p', :attributes => { :id => 'no_portal_community' }
  end

  should 'be able to edit EnterprisesBlock' do
    login_as(create_admin_user(Environment.default))
    b = EnterprisesBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'input', :attributes => { :id => 'block_limit' }
  end

  should 'be able to edit SlideshowBlock' do
    login_as(create_admin_user(Environment.default))
    b = SlideshowBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'select', :attributes => { :id => 'block_gallery_id' }
  end

  should 'be able to edit LoginBlock' do
    login_as(create_admin_user(Environment.default))
    b = LoginBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'input', :attributes => { :id => 'block_title' }
  end

  should 'be able to edit RecentDocumentsBlock' do
    login_as(create_admin_user(Environment.default))
    b = RecentDocumentsBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'input', :attributes => { :id => 'block_limit' }
  end

  should 'be able to edit CommunitiesBlock' do
    login_as(create_admin_user(Environment.default))
    b = CommunitiesBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'input', :attributes => { :id => 'block_limit' }
  end

  should 'be able to edit FeedReaderBlock' do
    login_as(create_admin_user(Environment.default))
    b = FeedReaderBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'input', :attributes => { :id => 'block_address' }
  end

  should 'be able to edit TagsCloudBlock' do
    login_as(create_admin_user(Environment.default))
    b = TagsCloudBlock.create!
    e = Environment.default
    e.boxes.create!
    e.boxes.first.blocks << b
    get :edit, :id => b.id
    assert_tag :tag => 'input', :attributes => { :id => 'block_title' }
  end

  should 'create back link to environment control panel' do
    Environment.default.boxes.create!.blocks << CommunitiesBlock.new
    Environment.default.boxes.create!.blocks << EnterprisesBlock.new
    Environment.default.boxes.create!.blocks << LoginBlock.new
    login_as(create_admin_user(Environment.default))
    get :index

    assert_tag :tag => 'a', :attributes => {:href => '/admin'}, :child => {:tag => 'span', :content => "Back to control panel"}
  end

  should 'a environment block plugin add new blocks for environments' do
    class CustomBlock1 < Block; end;

    class TestBlockPlugin < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => Environment},
        }
      end
    end
    @controller.stubs(:user).returns(create_user.person)
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new])
    assert @controller.available_blocks.include?(CustomBlock1)
  end

  should 'a person, enterprise and community blocks plugins do not add new blocks for environments' do
    @controller.stubs(:user).returns(create_user.person)

    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class TestBlockPlugin < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => Environment},
          CustomBlock2 => {:type => Enterprise},
          CustomBlock3 => {:type => Community},
          CustomBlock4 => {:type => Person},
        }
      end
    end

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new])
    assert @controller.available_blocks.include?(CustomBlock1)
    refute @controller.available_blocks.include?(CustomBlock2)
    refute @controller.available_blocks.include?(CustomBlock3)
    refute @controller.available_blocks.include?(CustomBlock4)
  end

  should 'a block plugin add new blocks' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;
    class CustomBlock6 < Block; end;
    class CustomBlock7 < Block; end;
    class CustomBlock8 < Block; end;
    class CustomBlock9 < Block; end;

    class TestBlockPlugin < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => Environment, :position => [1]},
          CustomBlock2 => {:type => Environment, :position => 1},
          CustomBlock3 => {:type => Environment, :position => '1'},
          CustomBlock4 => {:type => Environment, :position => [2]},
          CustomBlock5 => {:type => Environment, :position => 2},
          CustomBlock6 => {:type => Environment, :position => '2'},
          CustomBlock7 => {:type => Environment, :position => [3]},
          CustomBlock8 => {:type => Environment, :position => 3},
          CustomBlock9 => {:type => Environment, :position => '3'},
        }
      end
    end

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new])
    login_as(create_admin_user(Environment.default))
    get :index
    assert_response :success

    (1..9).each {|i| assert_tag :tag => 'div', :attributes => { 'data-block-type' => "EnvironmentDesignControllerTest::CustomBlock#{i}" }}
  end

  should 'a block plugin cannot be listed for unspecified types' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;
    class CustomBlock6 < Block; end;
    class CustomBlock7 < Block; end;
    class CustomBlock8 < Block; end;

    class TestBlockPlugin < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => Person, :position => 1},
          CustomBlock2 => {:type => Community, :position => 1},
          CustomBlock3 => {:type => Enterprise, :position => 1},
          CustomBlock4 => {:type => Environment, :position => 1},
          CustomBlock5 => {:type => Person, :position => 2},
          CustomBlock6 => {:type => Community, :position => 3},
          CustomBlock7 => {:type => Enterprise, :position => 2},
          CustomBlock8 => {:type => Environment, :position => 3},
        }
      end
    end

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new])
    login_as(create_admin_user(Environment.default))
    get :index
    assert_response :success

    [4, 8].each {|i| assert_tag :tag => 'div', :attributes => { 'data-block-type' => "EnvironmentDesignControllerTest::CustomBlock#{i}" }}
    [1, 2, 3, 5, 6, 7].each {|i| assert_no_tag :tag => 'div', :attributes => { 'data-block-type' => "EnvironmentDesignControllerTest::CustomBlock#{i}" }}
  end

  should 'clone a block' do
    login_as(create_admin_user(Environment.default))
    block = TagsCloudBlock.create!
    assert_difference 'TagsCloudBlock.count', 1 do
      post :clone_block, :id => block.id
      assert_response :redirect
    end
  end

  should 'return a list of paths from portal related to the words used in the query search' do
    env = Environment.default
    login_as(create_admin_user(env))
    community = fast_create(Community, :environment_id => env)
    env.portal_community = community
    env.enable('use_portal_community')
    env.save
    @controller.stubs(:boxes_holder).returns(env)
    article1 = fast_create(Article, :profile_id => community.id, :name => "Some thing")
    article2 = fast_create(Article, :profile_id => community.id, :name => "Some article")
    article3 = fast_create(Article, :profile_id => community.id, :name => "Not an article")

    xhr :get, :search_autocomplete, :query => 'Some'

    json_response = ActiveSupport::JSON.decode(@response.body)

    assert_response :success
    assert_equal json_response.include?("/{portal}/"+article1.path), true
    assert_equal json_response.include?("/{portal}/"+article2.path), true
    assert_equal json_response.include?("/{portal}/"+article3.path), false
  end

  should 'return empty if portal not configured' do
    env = Environment.default
    login_as(create_admin_user(env))

    xhr :get, :search_autocomplete, :query => 'Some'

    json_response = ActiveSupport::JSON.decode(@response.body)

    assert_response :success
    assert_equal json_response, []
  end

end