ref: master
test/unit/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 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 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
require_relative "../test_helper" class BlockTest < ActiveSupport::TestCase should 'describe itself' do assert_kind_of String, Block.description end should 'access owner through box' do user = create_user('testinguser').person box = fast_create(Box, :owner_id => user, :owner_type => 'Person') block = Block.new block.box = box block.save! assert_equal user, block.owner end should 'have no owner when there is no box' do assert_nil Block.new.owner end should 'provide an empty default title' do assert_equal '', Block.new.default_title end should 'provide an empty default subtitle' do assert_equal '', Block.new.subtitle end should 'be editable by default' do assert Block.new.editable? end should 'be editable if edit modes is all' do block = Block.new block.edit_modes = 'all' assert block.editable? end should 'be movable by default' do assert Block.new.movable? end should 'be movable if move modes is all' do block = Block.new block.move_modes = 'all' assert block.movable? end should 'have default titles' do b = Block.new b.expects(:default_title).returns('my title') assert_equal 'my title', b.title end should 'have default view_title ' do b = Block.new b.expects(:title).returns('my title') assert_equal 'my title', b.view_title end should 'be cacheable' do b = Block.new assert b.cacheable? end should 'list enabled blocks' do block1 = fast_create(Block, :title => 'test 1') block2 = fast_create(Block, :title => 'test 2', :enabled => false) assert_includes Block.enabled, block1 assert_not_includes Block.enabled, block2 end should 'be displayed everywhere by default' do assert_equal true, Block.new.visible? end should 'not display when set to hidden' do assert_equal false, build(Block, :display => 'never').visible? assert_equal false, build(Block, :display => 'never').visible?(:article => Article.new) end should 'be able to be displayed only in the homepage' do profile = Profile.new home_page = Article.new profile.home_page = home_page block = build(Block, :display => 'home_page_only') block.stubs(:owner).returns(profile) assert_equal true, block.visible?(:article => home_page) assert_equal false, block.visible?(:article => Article.new) end should 'be able to be displayed only in the homepage (index) of the environment' do block = build(Block, :display => 'home_page_only') assert_equal true, block.visible?(:article => nil, :request_path => "#{Noosfero.root('/')}") assert_equal false, block.visible?(:article => nil) end should 'be able to be displayed everywhere except in the homepage' do profile = Profile.new home_page = Article.new profile.home_page = home_page block = build(Block, :display => 'except_home_page') block.stubs(:owner).returns(profile) assert_equal false, block.visible?(:article => home_page) assert_equal true, block.visible?(:article => Article.new) end should 'be able to be displayed everywhere except on profile index' do profile = build(Profile, :identifier => 'testinguser') block = build(Block, :display => 'except_home_page') block.stubs(:owner).returns(profile) assert_equal false, block.visible?(:article => nil, :request_path => "#{Noosfero.root('/')}profile/testinguser") assert_equal true, block.visible?(:article => nil) end should 'be able to save display setting' do user = create_user('testinguser').person box = fast_create(Box, :owner_id => user.id, :owner_type => 'Profile') block = create(Block, :display => 'never', :box_id => box.id) block.reload assert_equal 'never', block.display end should 'be able to update display setting' do user = create_user('testinguser').person box = fast_create(Box, :owner_id => user.id, :owner_type => 'Profile') block = create(Block, :display => 'never', :box_id => box.id) assert block.update!(:display => 'always') block.reload assert_equal 'always', block.display end should 'display block in all languages by default' do profile = Profile.new block = Block.new block.stubs(:owner).returns(profile) assert_equal 'all', block.language end should 'be able to be displayed in all languages' do profile = Profile.new block = build(Block, :language => 'all') block.stubs(:owner).returns(profile) assert_equal true, block.visible?(:locale => 'pt') assert_equal true, block.visible?(:locale => 'en') end should 'be able to be displayed only in the selected language' do profile = Profile.new block = build(Block, :language => 'pt') block.stubs(:owner).returns(profile) assert_equal true, block.visible?(:locale => 'pt') assert_equal false, block.visible?(:locale => 'en') end should 'delegate environment to box' do box = fast_create(Box, :owner_id => fast_create(Profile).id) block = build(Block, :box => box) box.stubs(:environment).returns(Environment.default) assert_equal box.environment, block.environment end should 'inform conditions for expiration on profile context' do conditions = Block.expire_on assert conditions[:profile].kind_of?(Array) end should 'inform conditions for expiration on environment context' do conditions = Block.expire_on assert conditions[:environment].kind_of?(Array) end should 'create a cloned block' do block = fast_create(Block, :title => 'test 1', :position => 1) assert_difference 'Block.count', 1 do block.duplicate end end should 'clone and keep some fields' do box = fast_create(Box, :owner_id => fast_create(Profile).id) block = create(TagsCloudBlock, :title => 'test 1', :box_id => box.id, :settings => {:test => 'test'}) duplicated = block.duplicate [:title, :box_id, :type].each do |f| assert_equal duplicated.send(f), block.send(f) end assert 'test', duplicated[:settings][:test] end should 'clone block and set fields' do box = fast_create(Box, :owner_id => fast_create(Profile).id) block = create(TagsCloudBlock, :title => 'test 1', :box_id => box.id, :settings => {:test => 'test'}, :position => 1) block2 = create(TagsCloudBlock, :title => 'test 2', :box_id => box.id, :settings => {:test => 'test'}, :position => 2) duplicated = block.duplicate block2.reload block.reload assert_equal 'never', duplicated.display assert_equal 1, block.position assert_equal 2, duplicated.position assert_equal 3, block2.position end should 'not clone date creation and update attributes' do box = fast_create(Box, :owner_id => fast_create(Profile).id) block = create(TagsCloudBlock, :title => 'test 1', :box_id => box.id, :settings => {:test => 'test'}, :position => 1) duplicated = block.duplicate assert_not_equal block.created_at, duplicated.created_at assert_not_equal block.updated_at, duplicated.updated_at end should 'support custom display options for blocks visible' do class MyBlock < Block def display 'even_context' end def display_even_context(context) context[:value] % 2 == 0 end end block = MyBlock.new assert block.visible?({:value => 2}) refute block.visible?({:value => 3}) end should 'not be embedable by default' do refute Block.new.embedable? end should 'generate embed code' do b = Block.new b.stubs(:url_for).returns('http://myblogtest.com/embed/block/1') assert_equal "<iframe src=\"http://myblogtest.com/embed/block/1\" frameborder=\"0\" width=\"1024\" height=\"768\" class=\"embed block block\"></iframe>", b.embed_code.call end should 'default value for display_user is all' do block = Block.new assert_equal 'all', block.display_user end should 'display block to not logged users for display_user = all' do block = Block.new assert block.display_to_user?(nil) end should 'display block to logged users for display_user = all' do block = Block.new assert block.display_to_user?(User.new) end should 'display block to logged users for display_user = logged' do block = Block.new block.display_user = 'logged' assert block.display_to_user?(User.new) end should 'do not display block to logged users for display_user = not_logged' do block = Block.new block.display_user = 'not_logged' refute block.display_to_user?(User.new) end should 'do not display block to not logged users for display_user = logged' do block = Block.new block.display_user = 'logged' refute block.display_to_user?(nil) end should 'display block to not logged users for display_user = not_logged' do block = Block.new block.display_user = 'not_logged' assert block.display_to_user?(nil) end should 'not be visible if display_to_user? is false' do block = Block.new block.expects(:display_to_user?).once.returns(false) refute block.visible?({}) end should 'accept user as parameter on cache_key without change its value' do person = fast_create(Person) block = Block.new assert_equal block.cache_key('en'), block.cache_key('en', person) end should 'use language in cache key' do block = Block.new assert_not_equal block.cache_key('en'), block.cache_key('pt') end should 'display block to members of community for display_user = members' do community = fast_create(Community) user = create_user('testinguser') community.add_member(user.person) box = fast_create(Box, :owner_id => community.id, :owner_type => 'Community') block = create(Block, :box_id => box.id) block.display_user = 'followers' block.save! assert block.display_to_user?(user.person) end should 'do not display block to non members of community for display_user = members' do community = fast_create(Community) user = create_user('testinguser') box = fast_create(Box, :owner_id => community.id, :owner_type => 'Community') block = create(Block, :box_id => box.id) block.display_user = 'followers' block.save! refute block.display_to_user?(user.person) end should 'display block to friends of person for display_user = friends' do person = create_user('person_one').person person_friend = create_user('person_friend').person person.add_friend(person_friend) box = fast_create(Box, :owner_id => person.id, :owner_type => 'Person') block = create(Block, :box_id => box.id) block.display_user = 'followers' block.save! assert block.display_to_user?(person_friend) end should 'do not display block to non friends of person for display_user = friends' do person = create_user('person_one').person person_friend = create_user('person_friend').person box = fast_create(Box, :owner_id => person.id, :owner_type => 'Person') block = create(Block, :box_id => box.id) block.display_user = 'followers' block.save! refute block.display_to_user?(person_friend) end should 'get limit as a number when limit is string' do block = RecentDocumentsBlock.new block.settings[:limit] = '5' assert block.get_limit.is_a?(Fixnum) end should 'return true at visible_to_user? when block is visible' do block = Block.new person = create_user('person_one').person assert block.visible_to_user?(person) end should 'return false at visible_to_user? when block is not visible and user is nil' do block = Block.new person = create_user('person_one').person block.stubs(:owner).returns(person) block.expects(:visible?).returns(false) assert !block.visible_to_user?(nil) end should 'return false at visible_to_user? when block is not visible and user does not has permission' do block = Block.new person = create_user('person_one').person community = fast_create(Community) block.stubs(:owner).returns(community) block.expects(:visible?).returns(false) assert !block.visible_to_user?(person) end should 'return true at visible_to_user? when block is not visible and user has permission' do block = Block.new person = create_user('person_one').person community = fast_create(Community) give_permission(person, 'edit_profile_design', community) block.stubs(:owner).returns(community) block.expects(:visible?).returns(false) assert block.visible_to_user?(person) end should 'return false at visible_to_user? when block is not visible and user does not has permission in environment' do block = Block.new environment = Environment.default person = create_user('person_one').person block.stubs(:owner).returns(environment) block.expects(:visible?).returns(false) assert !block.visible_to_user?(person) end should 'return true at visible_to_user? when block is not visible and user has permission in environment' do block = Block.new environment = Environment.default person = create_user('person_one').person give_permission(person, 'edit_environment_design', environment) block.stubs(:owner).returns(environment) block.expects(:visible?).returns(false) assert block.visible_to_user?(person) end should 'return false at visible_to_user? when block is not visible to user' do block = Block.new person = create_user('person_one').person block.stubs(:owner).returns(person) block.expects(:visible?).returns(true) block.expects(:display_to_user?).returns(false) assert !block.visible_to_user?(nil) end should 'not allow block edition when user has not the permission for profile design' do block = Block.new profile = fast_create(Profile) block.stubs(:owner).returns(profile) person = create_user('person_one').person assert !block.allow_edit?(person) end should 'allow block edition when user has permission to edit profile design' do block = Block.new profile = fast_create(Profile) block.stubs(:owner).returns(profile) person = create_user('person_one').person give_permission(person, 'edit_profile_design', profile) assert block.allow_edit?(person) end should 'not allow block edition when user is nil' do block = Block.new assert !block.allow_edit?(nil) end should 'not allow block edition when block is not editable' do block = Block.new person = create_user('person_one').person block.expects(:editable?).returns(false) assert !block.allow_edit?(person) end should 'allow block edition when block is not editable but user is admin' do block = Block.new profile = fast_create(Profile) block.stubs(:owner).returns(profile) person = create_user('person_one').person Environment.default.add_admin(person) block.stubs(:editable?).returns(false) assert block.allow_edit?(person) end should 'not allow block edition when user has not the permission for environment design' do block = Block.new environment = Environment.default block.stubs(:owner).returns(environment) person = create_user('person_one').person assert !block.allow_edit?(person) end should 'allow block edition when user has the permission for environment design' do block = Block.new environment = Environment.default block.stubs(:owner).returns(environment) person = create_user('person_one').person give_permission(person, 'edit_environment_design', environment) assert block.allow_edit?(person) end should 'be able to create images' do block = fast_create(Block) 5.times { block.images.create } assert_equal 5, block.images.size end should 'be able to upload images when creating a block' do block = create(Block, images_builder: [{ uploaded_data: fixture_file_upload('/files/rails.png', 'image/png') }]) assert_equal 1, block.images.size end should 'be able to update existing images when update a block' do block = create(Block, images_builder: [{ uploaded_data: fixture_file_upload('/files/rails.png', 'image/png') }]) block.update!(images_builder: [{ id: block.images.first.id, remove_image: 'true' }, { uploaded_data: fixture_file_upload('/files/shoes.png', 'image/png') }]) assert_equal 'shoes.png', block.reload.images.first.filename end end |