cirandas.net

ref: master

test/functional/profile_editor_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
 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
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
require_relative '../test_helper'

class ProfileEditorControllerTest < ActionController::TestCase
  all_fixtures

  def setup
    @controller = ProfileEditorController.new

    @profile = create_user('default_user').person
    Environment.default.affiliate(@profile, [Environment::Roles.admin(Environment.default.id)] + Profile::Roles.all_roles(Environment.default.id))
    login_as('default_user')
  end
  attr_reader :profile

  def test_index
    get :index, :profile => profile.identifier
    assert_template 'index'
    assert_response :success
    assert_not_nil assigns(:profile)
  end

  def test_should_present_pending_tasks_in_index
    ze = Profile['ze'] # a fixture >:-(
    @controller.expects(:profile).returns(ze).at_least_once
    t1 = ze.tasks.build; t1.save!
    t2 = ze.tasks.build; t2.save!
    get :index, :profile => ze.identifier
    assert_includes assigns(:pending_tasks), t1
    assert_includes assigns(:pending_tasks), t2
    assert_tag :tag => 'div', :attributes => { :class => 'pending-tasks' }, :descendant => { :tag => 'a', :attributes =>  { :href => '/myprofile/ze/tasks' } }
  end

  def test_edit_person_info
    get :edit, :profile => profile.identifier
    assert_response :success
    assert_template 'edit'
  end

  should 'saving profile info' do
    person = profile
    post :edit, :profile => profile.identifier, :profile_data => { 'name' => 'new person', 'contact_information' => 'new contact information', 'address' => 'new address', 'sex' => 'female' }
    assert_redirected_to :controller => 'profile_editor', :action => 'index'
    person = Person.find(person.id)
    assert_equal 'new person', person.name
    assert_equal 'new contact information', person.contact_information
    assert_equal 'new address', person.address
    assert_equal 'female', person.sex
  end

  should 'mass assign all environment configurable person fields' do
    person = profile

    post :edit, :profile => profile.identifier, :profile_data => { "nickname" => "ze", "description" => "Just a regular ze.", "contact_information" => "What?", "contact_phone" => "+0551133445566", "cell_phone" => "+0551188889999", "comercial_phone" => "+0551144336655", "jabber_id" => "ze1234", "personal_website" => "http://ze.com.br", "sex" => "male", "birth_date" => "2014-06-04", "nationality" => "Brazilian", "country" => "BR", "state" => "DF", "city" => "Brasilia", "zip_code" => "70300-010", "address" => "Palacio do Planalto", "address_reference" => "Praca dos tres poderes", "district" => "DF", "schooling" => "Undergraduate", "schooling_status" => "Concluded", "formation" => "Engineerings", "area_of_study" => "Metallurgy", "professional_activity" => "Metallurgic", "organization" => "Metal Corp.", "organization_website" => "http://metal.com" }

    assert_response :redirect
    assert_redirected_to :controller => 'profile_editor', :action => 'index'
  end

  should 'not permmit if not logged' do
    logout
    get :index, :profile => profile.identifier
    assert_response 302
  end

  should 'display categories to choose to associate profile' do
    cat1 = Environment.default.categories.build(:display_in_menu => true, :name => 'top category'); cat1.save!
    cat2 = Environment.default.categories.build(:display_in_menu => true, :name => 'sub category', :parent_id => cat1.id); cat2.save!
    person = profile
    get :edit, :profile => profile.identifier
    assert_response :success
    assert_template 'edit'
    assert_tag :tag => 'input', :attributes => {:name => 'profile_data[category_ids][]'}
    assert_tag :tag => 'a', :attributes => { :class => 'select-subcategory-link', :id => "select-category-#{cat1.id}-link" }
  end

  should 'save categorization of profile' do
    cat1 = Environment.default.categories.build(:name => 'top category'); cat1.save!
    cat2 = Environment.default.categories.build(:name => 'sub category', :parent_id => cat1.id); cat2.save!
    person = profile
    post :edit, :profile => profile.identifier, :profile_data => {:category_ids => [cat2.id]}
    assert_response :redirect
    assert_redirected_to :controller => 'profile_editor', :action => 'index'
    assert_includes person.categories, cat2
  end

  should 'display profile categories and regions' do
    profile_region = fast_create(Region, name: 'Profile Region')
    region = fast_create(Region, name: 'Region')
    category = fast_create(Category, name: 'Category')

    profile.region = profile_region
    profile.update_attributes(category_ids: [region.id, category.id])

    get :edit, :profile => profile.identifier
    assert_tag :tag => 'td', :content => profile_region.name, :ancestor => { :tag => 'table', :attributes => { :id => 'selected-categories'}}
    assert_tag :tag => 'td', :content => region.name, :ancestor => { :tag => 'table', :attributes => { :id => 'selected-categories'}}
    assert_tag :tag => 'td', :content => category.name, :ancestor => { :tag => 'table', :attributes => { :id => 'selected-categories'}}
  end

  should 'filter html from person name' do
    name = "name <strong id='name_html_test'>with</strong> html"
    post :edit, :profile => profile.identifier, :profile_data => { :name => name }
    assert_sanitized assigns(:profile).name
  end

  should 'filter html from organization fields' do
    org = fast_create(Organization)
    contact = "name <strong id='name_html_test'>with</strong> html"
    acronym = "name <strong id='name_html_test'>with</strong> html"
    legal_form = "name <strong id='name_html_test'>with</strong> html"
    economic_activity = "name <strong id='name_html_test'>with</strong> html"
    management_information = "name <strong id='name_html_test'>with</strong> html"
    name = "name <strong id='name_html_test'>with</strong> html"

    post :edit, :profile => org.identifier, :profile_data => { :name => name, :contact_person => contact, :acronym => acronym, :legal_form => legal_form, :economic_activity => economic_activity, :management_information =>  management_information}

    assert_sanitized assigns(:profile).contact_person
    assert_sanitized assigns(:profile).acronym
    assert_sanitized assigns(:profile).legal_form
    assert_sanitized assigns(:profile).economic_activity
    assert_sanitized assigns(:profile).management_information
  end

  should 'saving profile organization_info' do
    org = fast_create(Organization)
    post :edit, :profile => org.identifier, :profile_data => { :contact_person => 'contact person' }
    assert_equal 'contact person', Organization.find(org.id).contact_person
  end

  should 'show contact_phone field on edit enterprise' do
    org = fast_create(Enterprise)
    Enterprise.any_instance.expects(:active_fields).returns(['contact_phone']).at_least_once
    get :edit, :profile => org.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'profile_data[contact_phone]' }
  end

  should 'save community description' do
    org = fast_create(Community)
    post :edit, :profile => org.identifier, :profile_data => { :description => 'my description' }
    assert_equal 'my description', Organization.find(org.id).description
  end

  should 'show community description' do
    org = fast_create(Community)
    Community.any_instance.expects(:active_fields).returns(['description']).at_least_once
    get :edit, :profile => org.identifier
    assert_tag :tag => 'textarea', :attributes => { :name => 'profile_data[description]' }
  end

  should 'not show enterprise description' do
    org = fast_create(Enterprise)
    get :edit, :profile => org.identifier
    assert_no_tag :tag => 'textarea', :attributes => { :name => 'profile_data[description]' }
  end

  should 'save organization contact_person' do
    org = fast_create(Organization)
    post :edit, :profile => org.identifier, :profile_data => { :contact_person => 'my contact' }
    assert_equal 'my contact', Organization.find(org.id).contact_person
  end

  should 'save enterprise contact_person' do
    org = fast_create(Enterprise)
    post :edit, :profile => org.identifier, :profile_data => { :contact_person => 'my contact' }
    assert_equal 'my contact', Enterprise.find(org.id).contact_person
  end

  should 'show field values on edit community info' do
    Community.any_instance.expects(:active_fields).returns(['contact_person']).at_least_once
    org = fast_create(Community)
    org.contact_person = 'my contact'
    org.save!
    get :edit, :profile => org.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'profile_data[contact_person]', :value => 'my contact' }
  end

  should 'mass assign all environment configurable community fields' do
    cmm = fast_create(Community)

    post :edit, :profile => cmm.identifier, :profile_data => { "name" => "new name", "display_name" => "N&w N@me", "description"=>"We sell food and other stuff.", "contact_person"=>"Joseph of the Jungle", "contact_email"=>"sac@company.net", "contact_phone"=>"+0551133445566", "legal_form"=>"New Name corp.", "economic_activity"=>"Food", "management_information"=>"No need for that here.", "address"=>"123, baufas street", "address_reference"=>"Next to baufas house", "district"=>"DC", "zip_code"=>"123456", "city"=>"Whashington", "state"=>"DC", "country"=>"US", "tag_list"=>"food, corporations", "language"=>"English" }

    assert_response :redirect
    assert_redirected_to :controller => 'profile_editor', :action => 'index'
  end

  should 'show field values on edit enterprise info' do
    Enterprise.any_instance.expects(:active_fields).returns(['contact_person']).at_least_once
    org = fast_create(Enterprise)
    org.contact_person = 'my contact'
    org.save!
    get :edit, :profile => org.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'profile_data[contact_person]', :value => 'my contact' }
  end

  should 'mass assign all environment configurable enterprise fields' do
    enterprise = fast_create(Enterprise)

    post :edit, :profile => enterprise.identifier, :profile_data => { "name"=>"Enterprise", "display_name"=>"Enterprise name", "business_name"=>"Enterprise", "description"=>"Hello IT.", "contact_person"=>"Joseph", "contact_email"=>"joe@enterprise.net", "contact_phone"=>"+0551133445566", "legal_form"=>"Enterprise corp.", "economic_activity"=>"Food", "management_information"=>"None.", "address"=>"123, baufas street", "address_reference"=>"Next to baufas house", "district"=>"DC", "zip_code"=>"123456", "city"=>"Washington", "state"=>"DC", "country"=>"US", "tag_list"=>"food, corporations", "organization_website"=>"http://enterprise.net", "historic_and_current_context"=>"Historic.", "activities_short_description"=>"Activies.", "acronym"=>"E", "foundation_year"=>"1995",}

    assert_response :redirect
    assert_redirected_to :controller => 'profile_editor', :action => 'index'
  end

  should 'display profile publication option in edit profile screen' do
    get :edit, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => { :type => 'radio', :checked => 'checked', :name => 'profile_data[public_profile]', :value => 'true' }
    assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[public_profile]', :value => 'false' }
  end

  should 'display properly that the profile is non-public' do
    profile.update!(:public_profile => false)
    get :edit, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => { :type => 'radio', :checked => 'checked', :name => 'profile_data[public_profile]', :value => 'false' }
    assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[public_profile]', :value => 'true' }
  end

  should 'save profile publication option set to true' do
    post :edit, :profile => profile.identifier, :profile_data => { :public_profile => 'true' }
    assert_equal true, profile.public_profile
  end

  should 'save profile publication option set to false' do
    post :edit, :profile => profile.identifier, :profile_data => { :public_profile => 'false' }
    profile = Person.find(@profile.id)
    assert_equal false, profile.public_profile
  end

  should 'show error messages for invalid foundation_year' do
    org = fast_create(Community)
    post :edit, :profile => org.identifier, :profile_data => { :foundation_year => 'aaa' }
    assert_tag :tag => 'div', :attributes => { :id => 'errorExplanation' }
  end

  should 'edit enterprise' do
    ent = fast_create(Enterprise)
    get :edit, :profile => ent.identifier
    assert_response :success
  end

  should 'back when update community info fail' do
    org = fast_create(Community)
    Community.any_instance.expects(:update!).raises(ActiveRecord::RecordInvalid)
    post :edit, :profile => org.identifier

    assert_template 'edit'
    assert_response :success
  end

  should 'back when update enterprise info fail' do
    org = fast_create(Enterprise)

    Enterprise.any_instance.expects(:update!).raises(ActiveRecord::RecordInvalid)
    post :edit, :profile => org.identifier
    assert_template 'edit'
    assert_response :success
  end

  should 'show edit profile button' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_editor/edit" }
  end

  should 'show image field on edit profile' do
    get :edit, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'profile_data[image_builder][uploaded_data]' }
  end

  should 'show categories links on edit profile' do
    cat1 = Environment.default.categories.create!(:display_in_menu => true, :name => 'top category')
    cat2 = Environment.default.categories.create!(:display_in_menu => true, :name => 'sub category', :parent_id => cat1.id)
    person = create_user('testuser').person
    get :edit, :profile => 'testuser'
    assert_tag :tag => 'a', :attributes => { :class => 'select-subcategory-link', :id => "select-category-#{cat1.id}-link" }
  end

  should 'render edit template' do
    get :edit, :profile => profile.identifier
    assert_template 'edit'
  end

  should 'render person partial' do
    person = profile
    Person.any_instance.expects(:active_fields).returns(['contact_phone', 'nickname']).at_least_once
    get :edit, :profile => person.identifier
    person.active_fields.each do |field|
      assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
    end
  end

  should 'display only active person fields' do
    Person.any_instance.expects(:active_fields).returns(['cell_phone']).at_least_once

    get :edit, :profile => profile.identifier

    assert_tag :tag => 'input', :attributes => { :name => "profile_data[cell_phone]" }
    assert_no_tag :tag => 'input', :attributes => { :name => "profile_data[comercial_phone]" }
  end

  should 'be able to upload an image' do
    assert_nil profile.image
    post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
    assert_not_nil assigns(:profile).image
  end

  should 'display closed attribute for communities when it is set' do
    org = fast_create(Community)
    org.closed = true
    org.save!

    get :edit, :profile => org.identifier

    assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'true', :checked => 'checked' }
    assert_no_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'false', :checked =>  'checked' }
  end

  should 'display closed attribute for communities when it is set to false' do
    org = fast_create(Community)

    [false, nil].each do |value|
      org.closed = value
      org.save!
      get :edit, :profile => org.identifier
      assert_no_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'true', :checked => 'checked' }
      assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'false', :checked => 'checked' }
    end
  end

  should 'set closed attribute correctly' do
    org = fast_create(Organization)
    org.closed = false
    org.save!

    post :edit, :profile => org.identifier, :profile_data => { :closed => 'true' }
    org.reload
    assert org.closed
  end

  should 'unset closed attribute correctly' do
    org = fast_create(Organization)
    org.closed = true
    org.save!

    post :edit, :profile => org.identifier, :profile_data => { :closed => 'false' }
    org.reload
    refute org.closed
  end

  should 'not display option to close when it is enterprise' do
    org = fast_create(Enterprise)
    get :edit, :profile => org.identifier

    assert_no_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'true' }
    assert_no_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'false' }
  end

  should 'display option to close when it is community' do
    org = fast_create(Community)
    get :edit, :profile => org.identifier

    assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'true' }
    assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'profile_data[closed]', :value => 'false' }
  end

  should 'display manage members options if has permission' do
    profile = Profile['ze']
    community = fast_create(Community)
    @controller.stubs(:user).returns(profile)
    @controller.stubs(:profile).returns(community)
    profile.stubs(:has_permission?).returns(true)
    get :index, :profile => community.identifier
    assert_tag :tag => 'a', :content => 'Manage Members'
  end

  should 'not display manage members options if has no permission' do
    profile = Profile['ze']
    community = fast_create(Community)
    @controller.stubs(:user).returns(profile)
    @controller.stubs(:profile).returns(community)
    profile.stubs(:has_permission?).returns(false)
    get :index, :profile => community.identifier
    assert_no_tag :tag => 'a', :content => 'Manage Members'
  end

  should 'render enterprise partial' do
    ent = fast_create(Enterprise)
    Enterprise.any_instance.expects(:active_fields).returns(['contact_phone', 'contact_person', 'contact_email']).at_least_once
    get :edit, :profile => ent.identifier
    ent.active_fields.each do |field|
      assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
    end
  end

  should 'render community partial' do
    community = fast_create(Community)
    Community.any_instance.expects(:active_fields).returns(['contact_person', 'language']).at_least_once
    get :edit, :profile => community.identifier
    community.active_fields.each do |field|
      assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
    end
  end

  should 'show task if user has permission' do
    user1 = profile
    user2 = create_user('usertwo').person
    AddFriend.create!(:person => user1, :friend => user2)
    @controller.stubs(:user).returns(user2)
    login_as('usertwo')
    get :index, :profile => 'usertwo'
    assert_tag :tag => 'div', :attributes => { :class => 'pending-tasks' }
  end

  should 'not show task if user has no permission' do
    user1 = profile
    community = fast_create(Community)
    user2 = create_user('usertwo').person
    task = AddMember.create!(person: user1, organization: community)
    @controller.stubs(:user).returns(user2)
    give_permission(user2, 'invite_members', community)
    login_as('usertwo')
    get :index, :profile => 'usertwo'
    assert_no_tag :tag => 'div', :attributes => { :class => 'pending-tasks' }
  end

  should 'limit task list' do
    user2 = create_user('usertwo').person
    6.times { AddFriend.create!(:person => create_user.person, :friend => user2) }
    login_as('usertwo')
    get :index, :profile => 'usertwo'
    assert_select '.pending-tasks > ul > li', 5
  end

  should 'display task count in task list' do
    user2 = create_user('usertwo').person
    6.times { AddFriend.create!(:person => create_user.person, :friend => user2) }
    login_as('usertwo')
    get :index, :profile => 'usertwo'
    assert_select '.pending-tasks h2' do |elements|
      assert_match /6/, elements.first.content
    end
  end

  should 'show favorite enterprises button for person' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :content => 'Favorite Enterprises'
  end

  should 'not show favorite enterprises button for organization' do
    org = fast_create(Organization)
    get :index, :profile => org.identifier
    assert_no_tag :tag => 'a', :content => 'Favorite Enterprises'
  end

  should 'link to mailconf' do
    MailConf.expects(:enabled?).returns(true).at_least_once
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/mailconf" }
  end

  should 'not link to mailconf for organizations' do
    MailConf.stubs(:enabled?).returns(true)
    org = fast_create(Organization)
    get :index, :profile => org.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{org.identifier}/mailconf" }
  end

  should 'not link to mailconf if mail not enabled' do
    MailConf.expects(:enabled?).returns(false).at_least_once
    get :index, :profile => profile.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/mailconf" }
  end

  should 'link to enable enterprise' do
    ent = fast_create(Enterprise, :enabled => false)
    get :index, :profile => ent.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{ent.identifier}/profile_editor/enable" }
  end

  should 'link to disable enterprise' do
    ent = fast_create(Enterprise, :enabled => true)
    get :index, :profile => ent.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{ent.identifier}/profile_editor/disable" }
  end

  should 'not link to enable/disable for non enterprises' do
    ent = fast_create(Organization, :enabled => true)
    get :index, :profile => ent.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{ent.identifier}/profile_editor/disable" }
  end

  should 'request enable enterprise confirmation' do
    ent = fast_create(Enterprise, :enabled => false)
    get :enable, :profile => ent.identifier
    assert_tag :tag => 'form', :attributes => { :action => "/myprofile/#{ent.identifier}/profile_editor/enable", :method => 'post' }
  end

  should 'enable enterprise after confirmation' do
    ent = fast_create(Enterprise, :enabled => false)
    post :enable, :profile => ent.identifier, :confirmation => 1
    assert assigns(:to_enable).enabled?
  end

  should 'not enable enterprise without confirmation' do
    ent = fast_create(Enterprise, :enabled => false)
    post :enable, :profile => ent.identifier
    refute assigns(:to_enable).enabled?
  end

  should 'disable enterprise after confirmation' do
    ent = fast_create(Enterprise, :enabled => true)
    post :disable, :profile => ent.identifier, :confirmation => 1
    refute assigns(:to_disable).enabled?
  end

  should 'not disable enterprise without confirmation' do
    ent = fast_create(Enterprise, :enabled => true)
    post :disable, :profile => ent.identifier
    assert assigns(:to_disable).enabled?
  end

  should 'update categories' do
    env = Environment.default
    top = env.categories.create!(:display_in_menu => true, :name => 'Top-Level category')
    c1  = env.categories.create!(:display_in_menu => true, :name => "Test category 1", :parent_id => top.id)
    c2  = env.categories.create!(:display_in_menu => true, :name => "Test category 2", :parent_id => top.id)
    xhr :get, :update_categories, :profile => profile.identifier, :category_id => top.id
    assert_template 'shared/update_categories'
    assert_equal top, assigns(:current_category)
    assert_equivalent [c1, c2], assigns(:categories)
  end

  should 'display manage my groups button for person' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :content => 'Manage my groups'
  end

  should 'display footer edit screen' do

    person = profile
    person.custom_header = 'my custom header'
    person.custom_footer = 'my custom footer'
    person.save!

    get :header_footer, :profile => profile.identifier
    assert_tag :tag => 'textarea', :content => /my custom header/
    assert_tag :tag => 'textarea', :content => /my custom footer/
  end

  should 'render TinyMce Editor for header and footer' do
    get :header_footer, :profile => profile.identifier
    assert_tag :tag => 'textarea', :attributes => { :id => 'custom_header', :class => Article::Editor::TINY_MCE }
    assert_tag :tag => 'textarea', :attributes => { :id => 'custom_footer', :class => Article::Editor::TINY_MCE }
  end

  should 'save footer and header' do
    person = profile
    post :header_footer, :profile => profile.identifier, :custom_header => 'new header', :custom_footer => 'new footer'
    person = Person.find(person.id)
    assert_equal 'new header', person.custom_header
    assert_equal 'new footer', person.custom_footer
  end

  should 'save header and footer even if model is invalid' do
    person = profile
    person.sex = nil; person.save!
    person.environment.custom_person_fields = {'sex' => {'required' => 'true', 'active' => 'true'} }; person.environment.save!

    post :header_footer, :profile => profile.identifier, :custom_header => 'new header', :custom_footer => 'new footer'
    person = Person.find(person.id)
    assert_equal 'new header', person.custom_header
    assert_equal 'new footer', person.custom_footer
  end

  should 'go back to editor after saving header/footer' do
    post :header_footer, :profile => profile.identifier, :custom_header => 'new header', :custom_footer => 'new footer'
    assert_redirected_to :action => 'index'
  end

  should 'point to header/footer editing in control panel' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_editor/header_footer" }
  end

  should 'not display header/footer button to enterprises if the environment disabled it' do
    env = Environment.default
    env.enable('disable_header_and_footer')
    env.save!

    enterprise = fast_create(Enterprise)

    u = create_user_with_permission('test_user', 'edit_profile', enterprise)
    login_as('test_user')

    get :index, :profile => enterprise.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{enterprise.identifier}/profile_editor/header_footer" }
  end

  should 'display header/footer button to enterprises if the environment disabled it but user is admin' do
    env = Environment.default
    env.enable('disable_header_and_footer')
    env.save!

    enterprise = fast_create(Enterprise)

    Person.any_instance.expects(:is_admin?).returns(true).at_least_once

    get :index, :profile => enterprise.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{enterprise.identifier}/profile_editor/header_footer" }
  end

  should 'display categories if environment disable_categories disabled' do
    Environment.any_instance.stubs(:enabled?).with(anything).returns(false)
    get :edit, :profile => profile.identifier
    assert_tag :tag => 'div', :descendant => { :tag => 'h2', :content => 'Categories of your interest' }
  end

  should 'not display categories if environment disable_categories enabled' do
    Environment.any_instance.stubs(:enabled?).with(anything).returns(true)
    get :edit, :profile => profile.identifier
    assert_no_tag :tag => 'div', :descendant => { :tag => 'h2', :content => 'Categories of your interest' }
  end

  should 'show a e-mail field in profile editor' do
    get :edit, :profile => profile.identifier

    assert_tag :tag => 'input',
               :attributes => { :name=>'profile_data[email]', :value => profile.email }
  end

  should 'display enable contact us for enterprise' do
    org = fast_create(Enterprise)
    get :edit, :profile => org.identifier
    assert_tag :tag => 'input', :attributes => {:name => 'profile_data[enable_contact_us]', :type => 'checkbox'}
  end

  should 'display link to CMS' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => '/myprofile/default_user/cms' }
  end

  should 'display email template link for organizations in control panel' do
    profile = fast_create(Organization)
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/profile_email_templates" }
  end

  should 'not display email template link in control panel for person' do
    get :index, :profile => profile.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/email_templates" }
  end

  should 'offer to create blog in control panel' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/default_user/cms/new?type=Blog" }
  end

  should 'offer to config blog in control panel' do
    profile.articles << Blog.new(:name => 'My blog', :profile => profile)
    get :index, :profile => profile.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/edit/#{profile.blog.id}" }
  end

  should 'not show select preferred domain if not enabled in environment' do
    profile.environment.custom_person_fields = {}; profile.environment.save!

    get :edit, :profile => profile.identifier
    assert_no_tag :tag => 'select', :attributes => { :name => 'profile_data[preferred_domain_id]' }
  end

  should 'be able to choose preferred domain' do
    profile.environment.custom_person_fields = {'preferred_domain' => {'required' => 'true', 'active' => 'true'} }; profile.environment.save!

    profile.domains << Domain.new(:name => 'myowndomain.net')
    profile.environment.domains << Domain.new(:name => 'myenv.net')

    @request.env['HTTP_HOST'] = profile.hostname
    get :edit, :profile => profile.identifier

    assert_tag :tag => 'select', :attributes => { :name => 'profile_data[preferred_domain_id]' }, :descendant => { :tag => "option", :content => 'myowndomain.net' }
    assert_tag :tag => 'select', :attributes => { :name => 'profile_data[preferred_domain_id]' }, :descendant => { :tag => "option", :content => 'myenv.net' }

    post :edit, :profile => profile.identifier, :profile_data => { :preferred_domain_id => profile.domains.first.id.to_s }

    assert_equal 'myowndomain.net', Profile.find(profile.id).preferred_domain.name
  end

  should 'be able to set no preferred domain at all' do
    profile.environment.custom_person_fields = {'preferred_domain' => {'required' => 'true', 'active' => 'true'} }; profile.environment.save!

    profile.domains << Domain.new(:name => 'myowndomain.net')
    profile.environment.domains << Domain.new(:name => 'myenv.net')

    @request.env['HTTP_HOST'] = profile.hostname
    get :edit, :profile => profile.identifier

    assert_tag :tag => "select", :attributes => { :name => 'profile_data[preferred_domain_id]'}, :descendant => { :tag => 'option', :content => '&lt;Select domain&gt;', :attributes => { :value => '' } }

    post :edit, :profile => profile.identifier, :profile_data => { :preferred_domain_id => '' }
    assert_nil Profile.find(profile.id).preferred_domain
  end

  should 'not be able to upload an image bigger than max size' do
    Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
    person = profile
    assert_nil person.image
    post :edit, :profile => person.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
    assert_nil person.image
  end

  should 'display error message when image has more than max size' do
    Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
    post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
    assert_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
  end

  should 'not display error message when image has less than max size' do
    Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] - 1024)
    post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
    assert_no_tag :tag => 'div', :attributes => { :class => 'errorExplanation', :id => 'errorExplanation' }
  end

  should 'not redirect when some file has errors' do
    Image.any_instance.stubs(:size).returns(Image.attachment_options[:max_size] + 1024)
    post :edit, :profile => profile.identifier, :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
    assert_response :success
    assert_template 'edit'
  end

  should 'not display form for enterprise activation if disabled in environment' do
    env = Environment.default
    env.disable('enterprise_activation')
    env.save!

    get :index, :profile => profile.identifier
    assert_no_tag :tag => 'div', :attributes => { :id => 'activation_enterprise' }, :descendant => {:tag => 'form', :attributes => {:action => '/account/activation_question'}}
  end

  should 'display form for enterprise activation if enabled on environment' do
    env = Environment.default
    env.enable('enterprise_activation')
    env.save!

    get :index, :profile => profile.identifier
    assert_tag :tag => 'div', :attributes => { :id => 'activation_enterprise' }, :descendant => {:tag => 'form', :attributes => {:action => '/account/activation_question'}}
  end

  should 'not display enterprise activation to enterprises' do
    env = Environment.default
    env.enable('enterprise_activation')
    env.save!

    enterprise = fast_create(Enterprise)
    enterprise.add_admin(profile)

    get :index, :profile => enterprise.identifier
    assert_no_tag :tag => 'div', :attributes => { :id => 'activation_enterprise' }, :descendant => {:tag => 'form', :attributes => {:action => '/account/activation_question'}}
  end

  should 'have url field for identifier when environment allows' do
    c = fast_create(Community)
    env = c.environment
    env.enable('enable_profile_url_change')
    env.save!

    get :edit, :profile => c.identifier
    assert_tag :tag => 'div',
               :attributes => { :class => 'formfield type-text' },
               :content => /https?:\/\/#{c.environment.default_hostname}\//,
               :descendant => {:tag => 'input', :attributes => {:id => 'profile_data_identifier'} }
  end

  should 'not have url field for identifier when environment not allows' do
    c = fast_create(Community)
    env = c.environment
    env.disable('enable_profile_url_change')
    env.save!

    get :edit, :profile => c.identifier
    assert_no_tag :tag => 'div',
               :attributes => { :class => 'formfield type-text' },
               :content => /https?:\/\/#{c.environment.default_hostname}\//,
               :descendant => {:tag => 'input', :attributes => {:id => 'profile_data_identifier'} }
  end

  should 'redirect to new url when is changed' do
    c = fast_create(Community)
    post :edit, :profile => c.identifier, :profile_data => {:identifier => 'new_address'}
    assert_response :redirect
    assert_redirected_to :action => 'index', :profile => 'new_address'
  end

  should 'not crash if identifier is left blank' do
    c = fast_create(Community)
    assert_nothing_raised do
      post :edit, :profile => c.identifier, :profile_data => {:identifier => ''}
    end
  end

  should 'show active fields when edit community' do
    env = Environment.default
    env.custom_community_fields = {
      'contact_email' => {'active' => 'true', 'required' => 'false'},
      'contact_phone' => {'active' => 'true', 'required' => 'false'}
    }
    env.save!
    community = fast_create(Community)

    get :edit, :profile => community.identifier

    community.active_fields.each do |field|
      assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
    end
  end

  should 'not show disabled fields when edit community' do
    env = Environment.default
    env.custom_community_fields = {
      'contact_email' => {'active' => 'false', 'required' => 'false'},
      'contact_phone' => {'active' => 'false', 'required' => 'false'}
    }
    env.save!
    community = fast_create(Community)

    get :edit, :profile => community.identifier

    (Community.fields - community.active_fields).each do |field|
      assert_no_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
    end
  end

  should 'show profile nickname on title' do
    profile.update(:nickname => 'my nick')
    get :index, :profile => profile.identifier
    assert_tag :tag => 'h1', :attributes => { :class => 'block-title'}, :descendant => {
      :tag => 'span', :attributes => { :class => 'control-panel-title' }, :content => 'my nick'
    }
  end

  should 'show profile name on title when no nickname' do
    get :index, :profile => profile.identifier
    assert_tag :tag => 'h1', :attributes => { :class => 'block-title'}, :descendant => {
      :tag => 'span', :attributes => { :class => 'control-panel-title' }, :content => profile.identifier
    }
  end

  should 'render destroy_profile template' do
    community = fast_create(Community)
    get :destroy_profile, :profile => community.identifier
    assert_template 'destroy_profile'
  end

  should 'not be able to destroy profile if forbid_destroy_profile is enabled' do
    environment = Environment.default
    user = create_user('user').person
    login_as('user')
    environment.enable('forbid_destroy_profile')
    assert_no_difference 'Profile.count' do
      post :destroy_profile, :profile => user.identifier
    end
  end

  should 'display destroy_profile button' do
    environment = Environment.default
    user = create_user_with_permission('user', 'destroy_profile')
    login_as('user')
    community = fast_create(Community)
    community.add_admin(user)
    get :edit, :profile => community.identifier
    assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{community.identifier}/profile_editor/destroy_profile" }
  end

  should 'not display destroy_profile button' do
    environment = Environment.default
    environment.enable('forbid_destroy_profile')
    environment.save!
    user = create_user_with_permission('user', 'destroy_profile')
    login_as('user')
    community = fast_create(Community)
    community.add_admin(user)
    get :edit, :profile => community.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{community.identifier}/profile_editor/destroy_profile" }
  end

  should 'be able to destroy a person' do
    person = fast_create(Person)

    assert_difference 'Person.count', -1 do
      post :destroy_profile, :profile => person.identifier
    end
  end

  should 'be able to destroy communities' do
    community = fast_create(Community)

    person = create_user('foo').person
    community.add_admin(person)

    assert_difference 'Community.count', -1 do
      post :destroy_profile, :profile => community.identifier
    end
  end

  should 'not be able to destroy communities if is a regular member' do
    community = fast_create(Community)
    community.add_admin(fast_create(Person)) # first member is admin by default

    person = create_user('foo').person
    community.add_member(person)

    login_as 'foo'
    assert_difference 'Community.count', 0 do
      post :destroy_profile, :profile => community.identifier
    end
  end

  should 'be able to destroy enterprise' do
    enterprise = fast_create(Enterprise)

    person = create_user('foo').person
    enterprise.add_admin(person)

    assert_difference 'Enterprise.count', -1 do
      post :destroy_profile, :profile => enterprise.identifier
    end
  end

  should 'not be able to destroy enterprise if is a regular member' do
    enterprise = fast_create(Enterprise)
    enterprise.add_member(create_user.person) # first member is admin by default

    person = create_user('foo').person
    enterprise.add_member(person)

    login_as('foo')
    assert_difference 'Enterprise.count', 0 do
      post :destroy_profile, :profile => enterprise.identifier
    end
  end

  should 'have welcome_page only for template' do
    organization = fast_create(Organization, :is_template => false)
    @controller.stubs(:profile).returns(organization)
    refute @controller.send(:has_welcome_page)

    organization = fast_create(Organization, :is_template => true)
    @controller.stubs(:profile).returns(organization)
    assert @controller.send(:has_welcome_page)

    person = fast_create(Person, :is_template => false)
    @controller.stubs(:profile).returns(person)
    refute @controller.send(:has_welcome_page)

    person = fast_create(Person, :is_template => true)
    @controller.stubs(:profile).returns(person)
    assert @controller.send(:has_welcome_page)
  end

  should 'display welcome_page button only if profile has_welcome_page' do
    @controller.stubs(:has_welcome_page).returns(true)
    get :index, :profile => fast_create(Profile).identifier
    assert_tag :tag => 'a', :content => 'Edit welcome page'

    @controller.stubs(:has_welcome_page).returns(false)
    get :index, :profile => fast_create(Profile).identifier
    assert_no_tag :tag => 'a', :content => 'Edit welcome page'
  end

  should 'not be able to access welcome_page if profile does not has_welcome_page' do
    @controller.stubs(:has_welcome_page).returns(false)
    get :welcome_page, :profile => fast_create(Profile).identifier
    assert_response :forbidden
  end

  should 'create welcome_page with public false by default' do
    get :welcome_page, :profile => fast_create(Person, :is_template => true).identifier
    refute assigns(:welcome_page).published
  end

  should 'update welcome page and redirect to index' do
    person_template = create_user('person_template').person
    person_template.is_template = true

    welcome_page = fast_create(TextArticle, :body => 'Initial welcome page')
    person_template.welcome_page = welcome_page
    person_template.save!
    welcome_page.profile = person_template
    welcome_page.save!
    new_content = 'New welcome page'

    post :welcome_page, :profile => person_template.identifier, :welcome_page => {:body => new_content}
    assert_redirected_to :action => 'index'

    welcome_page.reload
    assert_equal new_content, welcome_page.body
  end

  should 'display plugins buttons on the control panel' do

    class TestControlPanelButtons1 < Noosfero::Plugin
      def control_panel_buttons
        {:title => "Plugin1 button", :icon => 'plugin1_icon', :url => 'plugin1_url', :html_options => { :data => {extra: true} }}
      end
    end
    class TestControlPanelButtons2 < Noosfero::Plugin
      def control_panel_buttons
        {:title => "Plugin2 button", :icon => 'plugin2_icon', :url => 'plugin2_url'}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([TestControlPanelButtons1.to_s, TestControlPanelButtons2.to_s])

    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestControlPanelButtons1.new, TestControlPanelButtons2.new])

    get :index, :profile => profile.identifier

    assert_tag :tag => 'a', :content => 'Plugin1 button', :attributes => {:class => /plugin1_icon/, :href => /plugin1_url/, :'data-extra' => true}
    assert_tag :tag => 'a', :content => 'Plugin2 button', :attributes => {:class => /plugin2_icon/, :href => /plugin2_url/, :'data-extra' => nil}
  end

  should 'add extra content provided by plugins on edit' do
    class TestProfileEditPlugin < Noosfero::Plugin
      def profile_editor_extras
        "<input id='field_added_by_plugin' value='value_of_field_added_by_plugin'/>".html_safe
      end
    end
    Noosfero::Plugin.stubs(:all).returns([TestProfileEditPlugin.to_s])

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

    get :edit, :profile => profile.identifier

    assert_tag :tag => 'input', :attributes => {:id => 'field_added_by_plugin', :value => 'value_of_field_added_by_plugin'}
  end

  should 'add extra content with block provided by plugins on edit' do
    class TestProfileEditPlugin < Noosfero::Plugin
      def profile_editor_extras
        lambda do
          (render :text => "<input id='field_added_by_plugin' value='value_of_field_added_by_plugin'/>".html_safe).html_safe
        end
      end
    end
    Noosfero::Plugin.stubs(:all).returns([TestProfileEditPlugin.to_s])

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

    get :edit, :profile => profile.identifier

    assert_tag :tag => 'input', :attributes => {:id => 'field_added_by_plugin', :value => 'value_of_field_added_by_plugin'}
  end

  should 'show image upload field from profile editor' do
    env = Environment.default
    env.custom_person_fields = { }
    env.save!
    get :edit, :profile => profile.identifier
    assert_tag :tag => 'input', :attributes => { :name => 'profile_data[image_builder][uploaded_data]' }
    assert_tag :tag => 'div', :attributes => { :id => 'profile_change_picture' }
  end

  should 'add extra content on person info from plugins' do
    class Plugin1 < Noosfero::Plugin
      def profile_info_extra_contents
        proc {"<strong>Plugin1 text</strong>".html_safe}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def profile_info_extra_contents
        proc {"<strong>Plugin2 text</strong>".html_safe}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.to_s, Plugin2.to_s])

    Environment.default.enable_plugin(Plugin1)
    Environment.default.enable_plugin(Plugin2)

    get :edit, :profile => profile.identifier

    assert_tag :tag => 'strong', :content => 'Plugin1 text'
    assert_tag :tag => 'strong', :content => 'Plugin2 text'
  end

  should 'add extra content on organization info from plugins' do
    class Plugin1 < Noosfero::Plugin
      def profile_info_extra_contents
        proc {"<strong>Plugin1 text</strong>".html_safe}
      end
    end
    class Plugin2 < Noosfero::Plugin
      def profile_info_extra_contents
        proc {"<strong>Plugin2 text</strong>".html_safe}
      end
    end
    Noosfero::Plugin.stubs(:all).returns([Plugin1.to_s, Plugin2.to_s])

    Environment.default.enable_plugin(Plugin1)
    Environment.default.enable_plugin(Plugin2)
    organization = fast_create(Community)

    get :edit, :profile => organization.identifier

    assert_tag :tag => 'strong', :content => 'Plugin1 text'
    assert_tag :tag => 'strong', :content => 'Plugin2 text'
  end

  should 'see is_template check_box' do
    give_permission(profile, 'manage_environment_templates', profile.environment)
    get :edit, :profile =>  profile.identifier
    assert_tag :tag => 'input', :attributes => {:name => 'profile_data[is_template]'}
  end

  should 'not see is_template check_box' do
    another_user = create_user('another_user').person
    login_as('another_user')
    get :edit, :profile => profile.identifier
    assert_no_tag :tag => 'input', :attributes => {:name => 'profile_data[is_template]'}
  end

  should 'display select to change redirection after login if enabled' do
    e = Environment.default
    e.enable('allow_change_of_redirection_after_login')
    e.save

    get :edit, :profile => profile.identifier
    assert_tag :tag => 'select', :attributes => {:id => 'profile_data_redirection_after_login'}
  end

  should 'not display select to change redirection after login if not enabled' do
    e = Environment.default
    e.disable('allow_change_of_redirection_after_login')
    e.save

    get :edit, :profile => profile.identifier
    assert_no_tag :tag => 'select', :attributes => {:id => 'profile_data_redirection_after_login'}
  end

  should 'uncheck all field privacy fields' do
    person = profile
    assert_equal({}, person.fields_privacy)
    post :edit, :profile => profile.identifier, :profile_data => {}
    assert_equal({}, person.reload.fields_privacy)
  end

  should 'not redirect if the profile_hostname is the same as environment hostname' do
    Person.any_instance.stubs(:hostname).returns('hostname.org')
    Environment.any_instance.stubs(:default_hostname).returns('hostname.org')

    @request.env['HTTP_HOST'] = 'hostname.org'
    get :index, :profile => profile.identifier

    assert_response :success
  end

  should 'show head and footer for admin' do
    login_as('default_user')
    get :index, :profile => profile.identifier
    assert_tag :tag => 'div', :descendant => { :tag => 'a', :content => 'Edit Header and Footer' }
  end

  should 'not display header and footer for user when feature is enable' do
    user = create_user('user').person
    login_as('user')
    profile.environment.enable('disable_header_and_footer')
    get :index, :profile => user.identifier
    assert_no_tag :tag => 'div', :descendant => { :tag => 'a', :content => 'Edit Header and Footer' }
  end

  should 'display header and footer for user when feature is disabled ' do
    user = create_user('user').person
    login_as('user')
    profile.environment.disable('disable_header_and_footer')
    get :index, :profile => user.identifier
    assert_tag :tag => 'div', :descendant => { :tag => 'a', :content => 'Edit Header and Footer' }
  end

  should 'user cant edit header and footer if environment dont permit' do
    environment = Environment.default
    environment.settings[:disable_header_and_footer_enabled] = true
    environment.save!

    user = create_user('user').person
    login_as('user')

    get :header_footer, :profile => user.identifier
    assert_response :redirect
  end

  should 'admin can edit header and footer if environment dont permit' do
    user = create_user('user').person

    environment = Environment.default
    environment.add_admin(user)
    environment.settings[:disable_header_and_footer_enabled] = true
    environment.save!

    login_as('user')

    get :header_footer, :profile => user.identifier
    assert_response :success
  end

  should 'not display button to manage roles on control panel of person' do
    get :index, :profile => profile.identifier
    assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/default_user/profile_roles" }
  end

  should 'save profile admin option to receive email for every task' do
    comm = fast_create(Community)
    assert comm.profile_admin_mail_notification
    post :edit, :profile => comm.identifier, :profile_data => { :profile_admin_mail_notification => '0' }
    refute comm.reload.profile_admin_mail_notification
  end

  should 'not display option to change identifier for person' do
    get :edit, :profile => profile.identifier
    assert_select '#profile-identifier-formitem', 0
  end

  should 'display option to change identifier for person when allowed by environment' do
    profile.environment.enable(:enable_profile_url_change)
    get :edit, :profile => profile.identifier
    assert_select '#profile-identifier-formitem', 1
  end

  should 'response of search_tags be json' do
    get :search_tags, :profile => profile.identifier, :term => 'linux'
    assert_equal 'application/json', @response.content_type
  end

  should 'return empty json if does not find tag' do
    get :search_tags, :profile => profile.identifier, :term => 'linux'
    assert_equal "[]", @response.body
  end

  should 'return tags found' do
    a = profile.articles.create(:name => 'blablabla')
    a.tags.create! name: 'linux'
    get :search_tags, :profile => profile.identifier, :term => 'linux'
    assert_equal '[{"label":"linux","value":"linux"}]', @response.body
  end

  should 'not display location fields when editing a profile' do
    Environment.any_instance.stubs(:custom_person_fields).returns({ 'location' => { 'active' => 'true' } })
    get :edit, :profile => profile.identifier

    assert_no_tag 'input', attributes: { id: 'profile_data_state' }
    assert_no_tag 'input', attributes: { id: 'profile_data_city' }
  end
end