cirandas.net

ref: master

test/unit/application_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
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
# encoding: UTF-8
require_relative "../test_helper"

class ApplicationHelperTest < ActionView::TestCase

  include ApplicationHelper

  def setup
    self.stubs(:session).returns({})
  end

  should 'calculate correctly partial for models' do
    p1 = 'path1/'
    p2 = 'path2/'
    @controller = mock()
    @controller.stubs(:view_paths).returns([p1,p2])

    self.stubs(:params).returns({:controller => 'test'})
    File.stubs(:exists?).returns(false)

    File.expects(:exists?).with(p1+"test/_integer.html.erb").returns(true)
    assert_equal 'integer', partial_for_class(Integer)

    File.expects(:exists?).with(p1+"test/_numeric.html.erb").returns(true)
    assert_equal 'numeric', partial_for_class(Float)

    assert_raises ArgumentError do
      partial_for_class(Object)
    end
  end

  should 'calculate correctly partial for namespaced models' do
    p = 'path/'
    @controller = mock()
    @controller.stubs(:view_paths).returns([p])
    self.stubs(:params).returns({:controller => 'test'})

    class School; class Project; end; end

    File.stubs(:exists?).returns(false)
    File.expects(:exists?).with(p+"test/application_helper_test/school/_project.html.erb").returns(true)

    assert_equal 'test/application_helper_test/school/project', partial_for_class(School::Project)
  end

  should 'give error when there is no partial for class' do
    assert_raises ArgumentError do
      partial_for_class(nil)
    end
  end

  should 'plugins path take precedence over core path' do
    core_path = 'core/'
    plugin_path = 'path/'
    @controller = mock()
    @controller.stubs(:view_paths).returns([plugin_path, core_path])
    self.stubs(:params).returns({:controller => 'test'})

    File.stubs(:exists?).returns(false)
    File.stubs(:exists?).with(core_path+"test/_block.html.erb").returns(true)
    File.stubs(:exists?).with(plugin_path+"test/_raw_html_block.html.erb").returns(true)

    assert_equal 'raw_html_block', partial_for_class(RawHTMLBlock)
  end

  should 'generate link to stylesheet' do
    File.stubs(:exists?).returns(false)
    File.expects(:exists?).with(Rails.root.join('public', 'stylesheets', 'something.css')).returns(true)
    expects(:filename_for_stylesheet).with('something', nil).returns('/stylesheets/something.css')
    assert_match '@import url(/stylesheets/something.css)', stylesheet_import('something')
  end

  should 'not generate link to unexisting stylesheet' do
    File.expects(:exists?).with(Rails.root.join('public', 'stylesheets', 'something.css')).returns(false)
    expects(:filename_for_stylesheet).with('something', nil).returns('/stylesheets/something.css')
    assert_no_match %r{@import url(/stylesheets/something.css)}, stylesheet_import('something')
  end

  should 'handle nil dates' do
    assert_equal '', show_date(nil)
  end

  should 'generate correct link to category' do
    cat = mock
    cat.expects(:path).returns('my-category/my-subcatagory')
    cat.expects(:full_name).returns('category name')
    cat.expects(:environment).returns(Environment.default)
    Environment.any_instance.expects(:default_hostname).returns('example.com')

    result = "/cat/my-category/my-subcatagory"
    expects(:link_to).with('category name', {:controller => 'search', :action => 'category_index', :category_path => ['my-category', 'my-subcatagory'], :host => 'example.com'}, {}).returns(result)
    assert_same result, link_to_category(cat)
  end

  should 'nil theme option when no exists theme' do
    stubs(:current_theme).returns('something-very-unlikely')
    File.expects(:exists?).returns(false)
    assert_nil theme_option()
  end

  should 'nil javascript theme when no exists theme' do
    stubs(:current_theme).returns('something-very-unlikely')
    File.expects(:exists?).returns(false)
    assert_nil theme_javascript
  end

  should 'role color for admin role' do
    assert_equal 'blue', role_color(Profile::Roles.admin(Environment.default.id), Environment.default.id)
  end
  should 'role color for member role' do
    assert_equal 'green', role_color(Profile::Roles.member(Environment.default.id), Environment.default.id)
  end
  should 'role color for moderator role' do
    assert_equal 'gray', role_color(Profile::Roles.moderator(Environment.default.id), Environment.default.id)
  end
  should 'default role color' do
    assert_equal 'black', role_color('none', Environment.default.id)
  end

  should 'rolename for first organization member' do
    person = create_user('usertest').person
    community = fast_create(Community, :name => 'new community', :identifier => 'new-community', :environment_id => Environment.default.id)
    community.add_member(person)
    assert_tag_in_string rolename_for(person, community), :tag => 'span', :content => 'Profile Administrator'
  end

  should 'rolename for a member' do
    member1 = create_user('usertest1').person
    member2 = create_user('usertest2').person
    community = fast_create(Community, :name => 'new community', :identifier => 'new-community', :environment_id => Environment.default.id)
    community.add_member(member1)
    community.add_member(member2)
    assert_tag_in_string rolename_for(member2, community), :tag => 'span', :content => 'Profile Member'
  end

  should 'rolenames for a member admin' do
    member1 = create_user('usertest1').person
    member2 = create_user('usertest2').person
    community = fast_create(Community, :name => 'new community', :identifier => 'new-community', :environment_id => Environment.default.id)
    community.add_member(member1)
    # member2 is both a admin and a member
    community.add_member(member2)
    community.add_admin(member2)
    assert_tag_in_string rolename_for(member2, community), :tag => 'span', :content => 'Profile Member'
    assert_tag_in_string rolename_for(member2, community), :tag => 'span', :content => 'Profile Administrator'
  end

  should 'render theme footer' do
    stubs(:theme_path).returns('/user_themes/mytheme')
    footer_path = Rails.root.join('public', 'user_themes', 'mytheme', 'footer.html.erb')

    File.expects(:exists?).with(footer_path).returns(true)
    expects(:render).with(:file => footer_path, :use_full_path => false).returns("BLI")

    assert_equal "BLI", theme_footer
  end

  should 'ignore unexisting theme footer' do
    stubs(:theme_path).returns('/user_themes/mytheme')
    footer_path = Rails.root.join('public', 'user_themes', 'mytheme', 'footer.html.erb')

    File.expects(:exists?).with(footer_path).returns(false)
    expects(:render).with(:file => footer).never

    assert_nil theme_footer
  end

  should 'render theme site title' do
    stubs(:theme_path).returns('/user_themes/mytheme')
    site_title_path = Rails.root.join('public', 'user_themes', 'mytheme', 'site_title.html.erb')

    File.expects(:exists?).with(site_title_path).returns(true)
    expects(:render).with(:file => site_title_path, :use_full_path => false).returns("Site title")

    assert_equal "Site title", theme_site_title
  end

  should 'ignore unexisting theme site title' do
    stubs(:theme_path).returns('/user_themes/mytheme')
    site_title_path = Rails.root.join('public', 'user_themes', 'mytheme', 'site_title.html.erb')

    File.expects(:exists?).with(site_title_path).returns(false)
    expects(:render).with(:file => site_title_path).never

    assert_nil theme_site_title
  end

  should 'expose theme owner' do
    theme = mock
    profile = mock
    Theme.expects(:find).with('theme-under-test').returns(theme)
    theme.expects(:owner).returns(profile)
    profile.expects(:identifier).returns('sampleuser')

    stubs(:current_theme).returns('theme-under-test')

    assert_equal 'sampleuser', theme_owner
  end

  should 'use environment´s template when there is no profile' do
    stubs(:profile).returns(nil)
    self.stubs(:environment).returns(Environment.default)
    environment.expects(:layout_template).returns('sometemplate')
    assert_equal "/designs/templates/sometemplate/stylesheets/style.css", template_stylesheet_path
  end

  should 'use template from profile' do
    profile = mock
    profile.expects(:layout_template).returns('mytemplate')
    stubs(:profile).returns(profile)

    assert_equal '/designs/templates/mytemplate/stylesheets/style.css', template_stylesheet_path
  end

  should 'not display templates options when there is no template' do
    self.stubs(:environment).returns(Environment.default)
    [:people, :communities, :enterprises].each do |klass|
      assert_equal '', template_options(klass, 'profile_data')
    end
  end

  should 'define the community default template as checked' do
    environment = Environment.default
    self.stubs(:environment).returns(environment)
    community = fast_create(Community, :is_template => true, :environment_id => environment.id)
    fast_create(Community, :is_template => true, :environment_id => environment.id)
    environment.community_default_template= community
    environment.save

    assert_tag_in_string template_options(:communities, 'community'), :tag => 'input',
                                 :attributes => { :name => "community[template_id]", :value => community.id, :checked => true }
  end

  should 'define the person default template as checked' do
    environment = Environment.default
    self.stubs(:environment).returns(environment)
    person = fast_create(Person, :is_template => true, :environment_id => environment.id)
    fast_create(Person, :is_template => true, :environment_id => environment.id)
    environment.person_default_template= person
    environment.save

    assert_tag_in_string template_options(:people, 'profile_data'), :tag => 'input',
                                 :attributes => { :name => "profile_data[template_id]", :value => person.id, :checked => true }
  end

  should 'define the enterprise default template as checked' do
    environment = Environment.default
    self.stubs(:environment).returns(environment)
    enterprise = fast_create(Enterprise, :is_template => true, :environment_id => environment.id)
    fast_create(Enterprise, :is_template => true, :environment_id => environment.id)

    environment.enterprise_default_template= enterprise
    environment.save
    environment.reload

    assert_tag_in_string template_options(:enterprises, 'create_enterprise'), :tag => 'input',
                                 :attributes => { :name => "create_enterprise[template_id]", :value => enterprise.id, :checked => true }
  end

  should 'return nil if disable_categories is enabled' do
    env = fast_create(Environment, :name => 'env test')
    stubs(:environment).returns(env)
    assert_not_nil env
    env.enable(:disable_categories)
    assert env.enabled?(:disable_categories)
    assert_nil select_categories(mock)
  end

  should 'display field on person signup' do
    env = create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.expects(:action_name).returns('signup')

    person = Person.new
    person.expects(:signup_fields).returns(['field'])
    assert_equal 'SIGNUP_FIELD', optional_field(person, 'field', 'SIGNUP_FIELD')
  end

  should 'display field on enterprise registration' do
    env = create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:controller_name).returns('enterprise_registration')
    controller.stubs(:action_name).returns('index')

    enterprise = Enterprise.new
    enterprise.expects(:signup_fields).returns(['field'])
    assert_equal 'SIGNUP_FIELD', optional_field(enterprise, 'field', 'SIGNUP_FIELD')
  end

  should 'display field on home for a not logged user' do
    env = create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:controller_name).returns('home')
    controller.stubs(:action_name).returns('index')

    stubs(:user).returns(nil)


    person = Person.new
    person.expects(:signup_fields).returns(['field'])
    assert_equal 'SIGNUP_FIELD', optional_field(person, 'field', 'SIGNUP_FIELD')
  end

  should 'display field on community creation' do
    env = create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:action_name).returns('new_community')

    community = Community.new
    community.expects(:signup_fields).returns(['field'])
    assert_equal 'SIGNUP_FIELD', optional_field(community, 'field', 'SIGNUP_FIELD')
  end

  should 'not display field on signup' do
    env = fast_create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.expects(:action_name).returns('signup')

    person = Person.new
    person.expects(:signup_fields).returns([])
    assert_equal '', optional_field(person, 'field', 'SIGNUP_FIELD')
  end

  should 'not display field on enterprise registration' do
    env = create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:controller_name).returns('enterprise_registration')
    controller.stubs(:action_name).returns('index')

    enterprise = Enterprise.new
    enterprise.expects(:signup_fields).returns([])
    assert_equal '', optional_field(enterprise, 'field', 'SIGNUP_FIELD')
  end

  should 'not display field on community creation' do
    env = create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:action_name).returns('new_community')

    community = Community.new
    community.stubs(:signup_fields).returns([])
    assert_equal '', optional_field(community, 'field', 'SIGNUP_FIELD')
  end

  should 'display active fields' do
    env = fast_create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:controller_name).returns('')
    controller.stubs(:action_name).returns('edit')

    profile = Person.new
    profile.stubs(:active_fields).returns(['field'])

    expects(:profile_field_privacy_selector).with(profile, 'field').returns('')
    assert_tag_in_string optional_field(profile, 'field', 'EDIT_FIELD'), :tag => 'div', :content => 'EDIT_FIELD', :attributes => {:class => 'field-with-privacy-selector'}
  end

  should 'not display active fields' do
    env = fast_create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:action_name).returns('edit')
    controller.stubs(:controller_name).returns('')

    profile = Person.new
    profile.expects(:active_fields).returns([])
    assert_equal '', optional_field(profile, 'field', 'EDIT_FIELD')
  end

  should 'display required fields' do
    env = fast_create(Environment, :name => 'env test')
    stubs(:environment).returns(env)

    controller = mock
    stubs(:controller).returns(controller)
    controller.stubs(:controller_name).returns('')
    controller.stubs(:action_name).returns('edit')

    profile = Person.new
    profile.stubs(:active_fields).returns(['field'])
    profile.expects(:required_fields).returns(['field'])

    stubs(:required).with(anything).returns('<span>EDIT_FIELD</span>')
    expects(:profile_field_privacy_selector).with(profile, 'field').returns('')
    assert_equal '<span>EDIT_FIELD</span>', optional_field(profile, 'field', 'EDIT_FIELD')
  end

  should 'base theme uses default icon theme' do
    stubs(:current_theme).returns('base')
    assert_equal "designs/icons/default/style.css", icon_theme_stylesheet_path.first
  end

  should 'base theme uses config to specify more then an icon theme' do
    stubs(:current_theme).returns('base')
    assert_includes icon_theme_stylesheet_path, "designs/icons/default/style.css"
    assert_includes icon_theme_stylesheet_path, "designs/icons/pidgin/style.css"
  end

  should 'not display active field if only required' do
    profile = mock
    profile.expects(:required_fields).returns([])

    assert_equal '', optional_field(profile, :field_name, '<html tags>', true)
  end

  should 'display name on page title if profile doesnt have nickname' do
    stubs(:environment).returns(Environment.default)
    @controller = ApplicationController.new

    c = fast_create(Community, :name => 'Comm name', :identifier => 'test_comm')
    stubs(:profile).returns(c)
    assert_match(/Comm name/, page_title)
  end

  should 'display nickname on page title if profile has nickname' do
    stubs(:environment).returns(Environment.default)
    @controller = ApplicationController.new

    c = fast_create(Community, :name => 'Community for tests', :nickname => 'Community nick', :identifier => 'test_comm')
    stubs(:profile).returns(c)
    assert_match(/Community nick/, page_title)
  end

  should 'not display environment name if is a profile' do
    stubs(:environment).returns(Environment.default)
    @controller = ApplicationController.new

    c = fast_create(Community, :name => 'Community for tests', :nickname => 'Community nick', :identifier => 'test_comm')
    stubs(:profile).returns(c)
    assert_equal c.short_name, page_title
  end

  should 'display only environment if no profile and page' do
    stubs(:environment).returns(Environment.default)
    @controller = ApplicationController.new

    assert_equal Environment.default.name, page_title
  end

  should 'use theme passed via param when in development mode' do
    stubs(:environment).returns(build(Environment, :theme => 'environment-theme'))
    Rails.env.stubs(:development?).returns(true)
    self.stubs(:params).returns({:user_theme => 'skyblue'})
    assert_equal 'skyblue', current_theme
  end

  should 'not use theme passed via param when in production mode' do
    stubs(:environment).returns(build(Environment, :theme => 'environment-theme'))
    ENV.stubs(:[]).with('RAILS_ENV').returns('production')
    self.stubs(:params).returns({:theme => 'skyblue'})
    stubs(:profile).returns(build(Profile, :theme => 'profile-theme'))
    assert_equal 'profile-theme', current_theme
  end

  should 'use environment theme if the profile theme is nil' do
    stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
    stubs(:profile).returns(fast_create(Profile))
    assert_equal environment.theme, current_theme
  end

  should 'use favicon from environment theme if does not have profile' do
    stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
    stubs(:profile).returns(nil)
    assert_equal '/designs/themes/new-theme/favicon.ico', theme_favicon
  end

  should 'use favicon from environment theme if the profile theme is nil' do
    stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
    stubs(:profile).returns(fast_create(Profile))
    assert_equal '/designs/themes/new-theme/favicon.ico', theme_favicon
  end

  should 'use favicon from profile theme if the profile has theme' do
    stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
    stubs(:profile).returns(fast_create(Profile, :theme => 'profile-theme'))
    File.expects(:exists?).with(Rails.root.join('public', '/designs/themes/profile-theme', 'favicon.ico')).returns(true)
    assert_equal '/designs/themes/profile-theme/favicon.ico', theme_favicon
  end

  should 'use favicon from profile articles if the profile theme does not have' do
    stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
    stubs(:profile).returns(fast_create(Profile, :theme => 'profile-theme'))
    file = create(UploadedFile, :uploaded_data => fixture_file_upload('/files/favicon.ico', 'image/x-ico'), :profile => profile)
    File.expects(:exists?).with(Rails.root.join('public', theme_path, 'favicon.ico')).returns(false)

    assert_match /favicon.ico/, theme_favicon
  end

  should 'use favicon from environment if the profile theme and profile articles do not have' do
    stubs(:environment).returns(fast_create(Environment, :theme => 'new-theme'))
    stubs(:profile).returns(fast_create(Profile, :theme => 'profile-theme'))
    File.expects(:exists?).with(Rails.root.join('public', theme_path, 'favicon.ico')).returns(false)
    assert_equal '/designs/themes/new-theme/favicon.ico', theme_favicon
  end

  should 'not inlude administration link if user is not an environment administrator' do
    user = mock()
    stubs(:environment).returns(Environment.default)
    user.stubs(:is_admin?).with(environment).returns(false)
    stubs(:user).returns(user)
    assert admin_link.blank?
  end

  should 'inlude administration link if user is an environment administrator' do
    user = mock()
    stubs(:environment).returns(Environment.default)
    user.stubs(:is_admin?).with(environment).returns(true)
    stubs(:user).returns(user)
    assert admin_link.present?
  end

  should 'pluralize without count' do
    assert_equal "tests", pluralize_without_count(2, "test")
    assert_equal "test", pluralize_without_count(1, "test")
    assert_equal "testes", pluralize_without_count(2, "test", "testes")
  end

  should 'unique with count' do
    assert_equal ["1 for b", "2 for c", "3 for a"], unique_with_count(%w(a b c a c a))
  end

  should 'return nil when :show_zoom_button_on_article_images is not enabled in environment' do
    env = Environment.default
    env.stubs(:enabled?).with(:show_zoom_button_on_article_images).returns(false)
    stubs(:environment).returns(env)
    assert_nil add_zoom_to_article_images
  end

  should 'return code when :show_zoom_button_on_article_images is enabled in environment' do
    env = Environment.default
    env.stubs(:enabled?).with(:show_zoom_button_on_article_images).returns(true)
    stubs(:environment).returns(env)
    assert_not_nil add_zoom_to_article_images
  end

  should 'return code when add_zoom_to_images' do
    env = Environment.default
    assert_not_nil add_zoom_to_images
  end

  should 'parse macros' do
    class Plugin1 < Noosfero::Plugin
    end
    Noosfero::Plugin.stubs(:all).returns(['ApplicationHelperTest::Plugin1'])

    class Plugin1::Macro1 < Noosfero::Plugin::Macro
      def parse(params, inner_html, source)
        'Test1'
      end
    end

    class Plugin1::Macro2 < Noosfero::Plugin::Macro
      def parse(params, inner_html, source)
        'Test2'
      end
    end

    environment = Environment.default
    environment.enable_plugin(Plugin1)
    @plugins = Noosfero::Plugin::Manager.new(environment, self)
    macro1_name = Plugin1::Macro1.identifier
    macro2_name = Plugin1::Macro2.identifier

    html = "
      <div class='macro nonEdit' data-macro='#{macro1_name}' data-macro-param='123'></div>
      <div class='macro nonEdit' data-macro='#{macro2_name}'></div>
      <div class='macro nonEdit' data-macro='unexistent' data-macro-param='987'></div>
    "
    parsed_html = convert_macro(html, mock())
    parsed_divs = Nokogiri::HTML.fragment(parsed_html).css('div')
    expected_divs = Nokogiri::HTML.fragment("
      <div class='parsed-macro #{macro1_name}' data-macro='#{macro1_name}'>Test1</div>
      <div class='parsed-macro #{macro2_name}' data-macro='#{macro2_name}'>Test2</div>
      <div data-macro='unexistent' class='failed-macro unexistent'>Unsupported macro unexistent!</div>
    ").css('div')

    # comparing div attributes between parsed and expected html
    parsed_divs.each_with_index do |div, i|
      assert_equal expected_divs[i].attributes.to_xml, div.attributes.to_xml
      assert_equal expected_divs[i].inner_text, div.inner_text
    end
  end

  should 'reference to article' do
    c = fast_create(Community)
    a = fast_create(TextArticle, :profile_id => c.id)
    assert_equal(
      "<a href=\"/#{c.identifier}/#{a.slug}\">x</a>",
      reference_to_article('x', a) )
  end

  should 'reference to article, with anchor' do
    c = fast_create(Community)
    a = fast_create(TextArticle, :profile_id => c.id)
    assert_equal(
      "<a href=\"/#{c.identifier}/#{a.slug}#place\">x</a>",
      reference_to_article('x', a, 'place') )
  end

  should 'reference to article, in a blog' do
    c = fast_create(Community)
    b = fast_create(Blog, :profile_id => c.id)
    a = fast_create(TextArticle, :profile_id => c.id, :parent_id => b.id)
    a.save! # needed to link to the parent blog
    assert_equal(
      "<a href=\"/#{c.identifier}/#{b.slug}/#{a.slug}\">x</a>",
      reference_to_article('x', a) )
  end

  should 'reference to article, in a profile with domain' do
    c = fast_create(Community)
    c.domains << build(Domain, :name=>'domain.xyz')
    b = fast_create(Blog, :profile_id => c.id)
    a = fast_create(TextArticle, :profile_id => c.id, :parent_id => b.id)
    a.save!
    assert_equal(
      "<a href=\"http://domain.xyz/#{b.slug}/#{a.slug}\">x</a>",
      reference_to_article('x', a) )
  end

  should 'content_id_to_str return the content id as string' do
    article = fast_create(Article, :name => 'my article')
    response = content_id_to_str(article)
    assert_equal String, response.class
    refute response.empty?
  end

  should 'content_id_to_str return empty string when receiving nil' do
    assert_equal '', content_id_to_str(nil)
  end

  should 'select gallery as default folder for image upload' do
    profile = create_user('testuser').person
    folder  = fast_create(Folder,  :profile_id => profile.id)
    gallery = fast_create(Gallery, :profile_id => profile.id)
    blog    = fast_create(Blog,    :profile_id => profile.id)
    assert_equal gallery, default_folder_for_image_upload(profile)
  end

  should 'select generic folder as default folder for image upload when no gallery' do
    profile = create_user('testuser').person
    folder  = fast_create(Folder,  :profile_id => profile.id)
    blog    = fast_create(Blog,    :profile_id => profile.id)
    assert_equal folder, default_folder_for_image_upload(profile)
  end

  should 'return nil as default folder for image upload when no gallery or generic folder' do
    profile = create_user('testuser').person
    blog    = fast_create(Blog,    :profile_id => profile.id)
    assert_nil default_folder_for_image_upload(profile)
  end

  should 'not filter html if source does not have macros' do
    class Plugin1 < Noosfero::Plugin
    end

    class Plugin1::Macro1 < Noosfero::Plugin::Macro
      def parse(params, inner_html, source)
        'Test1'
      end
    end

    environment = Environment.default
    environment.enable_plugin(Plugin1)
    @plugins = Noosfero::Plugin::Manager.new(environment, self)
    macro1_name = Plugin1::Macro1.identifier
    source = mock
    source.stubs(:has_macro?).returns(false)

    html = "<div class='macro nonEdit' data-macro='#{macro1_name}' data-macro-param='123'></div>"
    parsed_html = filter_html(html, source)

    assert_no_match /Test1/, parsed_html
  end

  should 'not convert macro if source is nil' do
    profile = create_user('testuser').person
    article = fast_create(Article,  :profile_id => profile.id)
    class Plugin1 < Noosfero::Plugin; end

    environment = Environment.default
    environment.enable_plugin(Plugin1)
    @plugins = Noosfero::Plugin::Manager.new(environment, self)

    expects(:convert_macro).never
    filter_html(article.body, nil)
  end

  should 'not convert macro if there is no macro plugin active' do
    profile = create_user('testuser').person
    article = fast_create(Article,  :profile_id => profile.id)
    class Plugin1 < Noosfero::Plugin; end

    environment = Environment.default
    environment.enable_plugin(Plugin1)
    @plugins = Noosfero::Plugin::Manager.new(environment, self)

    expects(:convert_macro).never
    filter_html(article.body, article)
  end

  should 'not display enterprises if not logged' do
    @controller = ApplicationController.new
    profile = create_user('testuser').person
    profile.environment.enable('display_my_enterprises_on_user_menu')
    enterprise = fast_create(Enterprise)
    enterprise.add_admin(profile)

    stubs(:user).returns(nil)
    expects(:manage_link).with(profile.enterprises, :enterprises, _('My enterprises')).never
    assert_equal '', manage_enterprises
  end

  should 'display enterprises if logged and enabled on environment' do
    @controller = ApplicationController.new
    profile = create_user('testuser').person
    profile.environment.enable('display_my_enterprises_on_user_menu')
    enterprise = fast_create(Enterprise)
    enterprise.add_admin(profile)

    stubs(:user).returns(profile)
    expects(:manage_link).with(profile.enterprises, :enterprises, _('My enterprises')).returns('enterprises list')
    assert_equal 'enterprises list', manage_enterprises
  end

  should 'not display enterprises if logged and disabled on environment' do
    @controller = ApplicationController.new
    profile = create_user('testuser').person
    profile.environment.disable('display_my_enterprises_on_user_menu')
    enterprise = fast_create(Enterprise)
    enterprise.add_admin(profile)

    stubs(:user).returns(profile)
    expects(:manage_link).with(profile.enterprises, :enterprises, _('My enterprises')).never
    assert_equal '', manage_enterprises
  end

  should 'not display communities if not logged' do
    @controller = ApplicationController.new
    profile = create_user('testuser').person
    profile.environment.enable('display_my_communities_on_user_menu')
    community = fast_create(Community)
    community.add_admin(profile)

    stubs(:user).returns(nil)
    expects(:manage_link).with(profile.communities, :communities, _('My communities')).never
    assert_equal '', manage_communities
  end

  should 'display communities if logged and enabled on environment' do
    @controller = ApplicationController.new
    profile = create_user('testuser').person
    profile.environment.enable('display_my_communities_on_user_menu')
    community = fast_create(Community)
    community.add_admin(profile)

    stubs(:user).returns(profile)
    expects(:manage_link).with(profile.communities, :communities, _('My communities')).returns('communities list')
    assert_equal 'communities list', manage_communities
  end

  should 'not display communities if logged and disabled on environment' do
    @controller = ApplicationController.new
    profile = create_user('testuser').person
    profile.environment.disable('display_my_communities_on_user_menu')
    community = fast_create(Community)
    community.add_admin(profile)

    stubs(:user).returns(profile)
    expects(:manage_link).with(profile.communities, :communities, _('My communities')).never
    assert_equal '', manage_communities
  end

  should 'include file from current theme out of a profile page' do
    def profile; nil; end
    def environment; e={}; def e.theme; 'env-theme'; end; e; end
    def render(opt); opt; end
    File.stubs(:exists?).returns(false)
    file = Rails.root.join 'public/designs/themes/env-theme/somefile.html.erb'
    assert_nil theme_include('somefile') # exists? = false
    File.expects(:exists?).with(file).returns(true).at_least_once
    assert_equal file, theme_include('somefile')[:file] # exists? = true
  end

  should 'include file from current theme inside a profile page' do
    def profile; p={}; def p.theme; 'my-theme'; end; p; end
    def render(opt); opt; end
    File.stubs(:exists?).returns(false)
    file = Rails.root.join 'public/designs/themes/my-theme/otherfile.html.erb'
    assert_nil theme_include('otherfile') # exists? = false
    File.expects(:exists?).with(file).returns(true).at_least_once
    assert_equal file, theme_include('otherfile')[:file] # exists? = true
  end

  should 'include file from env theme' do
    def profile; p={}; def p.theme; 'my-theme'; end; p; end
    def environment; e={}; def e.theme; 'env-theme'; end; e; end
    def render(opt); opt; end
    File.stubs(:exists?).returns(false)
    file = Rails.root.join 'public/designs/themes/env-theme/afile.html.erb'
    assert_nil env_theme_include('afile') # exists? = false
    File.expects(:exists?).with(file).returns(true).at_least_once
    assert_equal file, env_theme_include('afile')[:file] # exists? = true
  end

  should 'include file from some theme' do
    def render(opt); opt; end
    File.stubs(:exists?).returns(false)
    file = Rails.root.join 'public/designs/themes/atheme/afile.html.erb'
    assert_nil from_theme_include('atheme', 'afile') # exists? = false
    File.expects(:exists?).with(file).returns(true).at_least_once
    assert_equal file, from_theme_include('atheme', 'afile')[:file] # exists? = true
  end

  should 'enable fullscreen buttons' do
    html = fullscreen_buttons("#article")
    assert html.include?("<script>fullscreenPageLoad('#article')</script>")
    assert html.include?("class=\"button with-text icon-fullscreen\"")
    assert html.include?("onClick=\"toggle_fullwidth(&#39;#article&#39;)\"")
  end

  should "return the related class string" do
    assert_equal "Clone Folder", label_for_clone_article(Folder.new)
    assert_equal "Clone Blog", label_for_clone_article(Blog.new)
    assert_equal "Clone Event", label_for_clone_article(Event.new)
    assert_equal "Clone Forum", label_for_clone_article(Forum.new)
    assert_equal "Clone Article", label_for_clone_article(TextArticle.new)
  end

  should "return top url of environment" do
    env = Environment.default
    request = mock()
    request.expects(:scheme).returns('http')
    stubs(:request).returns(request)
    stubs(:environment).returns(env)
    stubs(:profile).returns(nil)
    assert_equal env.top_url('http'), top_url
  end

  should "return top url considering profile" do
    env = Environment.default
    c = fast_create(Community)
    request = mock()
    request.stubs(:scheme).returns('http')
    stubs(:request).returns(request)
    stubs(:environment).returns(env)
    stubs(:profile).returns(c)
    assert_equal c.top_url, top_url
  end

  should "current editor return the editor defined in article" do
    person = fast_create(Person)
    @article = fast_create(Article)
    @article.editor = Article::Editor::TEXTILE
    @article.save
    stubs(:current_person).returns(person)
    assert_equal Article::Editor::TEXTILE, current_editor
  end

  should "current editor be tiny mce if an article is present and no editor is defined" do
    person = fast_create(Person)
    @article = fast_create(Article)
    @article.stubs(:editor).returns(nil)
    stubs(:current_person).returns(person)
    assert_equal Article::Editor::TINY_MCE, current_editor
  end

  should "current editor be the person editor if there is no article" do
    person = fast_create(Person)
    request = mock()
    stubs(:current_person).returns(person)
    person.stubs(:editor).returns(Article::Editor::TEXTILE)
    assert_equal Article::Editor::TEXTILE, current_editor
  end


  should "current editor be tiny mce if there is no article and no person editor is defined" do
    person = fast_create(Person)
    stubs(:current_person).returns(person)
    person.stubs(:editor).returns(nil)
    assert_equal Article::Editor::TINY_MCE, current_editor
  end

  should "current editor return the editor defined in article even if there is a person editor defined" do
    person = fast_create(Person)
    @article = fast_create(Article)
    @article.editor = Article::Editor::TEXTILE
    @article.save
    stubs(:current_person).returns(person)
    person.stubs(:editor).returns(Article::Editor::TINY_MCE)
    assert_equal Article::Editor::TEXTILE, current_editor
  end

  should "current editor be tiny mce if an article is present and no editor is defined  even if there is a person editor defined" do
    person = fast_create(Person)
    @article = fast_create(Article)
    @article.stubs(:editor).returns(nil)
    stubs(:current_person).returns(person)
    person.stubs(:editor).returns(Article::Editor::TINY_MCE)
    assert_equal Article::Editor::TINY_MCE, current_editor
  end

  should "current editor concat the mode passed as parameter" do
    person = fast_create(Person)
    @article = fast_create(Article)
    @article.editor = Article::Editor::TEXTILE
    @article.save
    stubs(:current_person).returns(person)
    mode = 'something'
    assert_equal Article::Editor::TEXTILE + '_' + mode, current_editor(mode)
  end
  should "current_editor_is? be true if the test editor is equal to defined one" do
    stubs(:current_editor).returns(Article::Editor::TEXTILE)
    assert current_editor_is?(Article::Editor::TEXTILE)
  end

  should "current_editor_is? be false if the test editor is different to defined one" do
    stubs(:current_editor).returns(Article::Editor::TINY_MCE)
    refute current_editor_is?(Article::Editor::TEXTILE)
  end

  should "current_editor_is? be false if the test editor is nil" do
    stubs(:current_editor).returns(Article::Editor::TEXTILE)
    refute current_editor_is?(nil)
    stubs(:current_editor).returns(Article::Editor::TINY_MCE)
    refute current_editor_is?(nil)
  end

  should 'show task information with the requestor' do
    person = create_user('usertest').person
    task = create(Task, :requestor => person)
    assert_match person.name, task_information(task)
  end

  should 'show task information with variables information on suggest article tasks' do
    person = create_user('usertest').person
    task = create(SuggestArticle, :name => person.name, :target => person)
    assert_match person.name, task_information(task)
  end

  should 'show task information with target detail information on suggest article tasks' do
    person = create_user('usertest').person
    task = create(SuggestArticle, :target => person)
    assert_match /in.*#{person.name}/, task_information(task)
  end

  should "show task information without target detail information on suggest article tasks if it's in the same profile" do
    profile = fast_create(Community)
    task = create(SuggestArticle, :target => profile)
    assert_no_match /in.*#{profile.name}/, task_information(task, {:profile => profile.identifier})
  end

  should "show task information with target detail information on suggest article with profile parameter to another profile" do
    profile = fast_create(Community)
    another_profile = fast_create(Community)
    task = create(SuggestArticle, :target => profile)
    assert_match /in.*#{profile.name}/, task_information(task, {:profile => another_profile.identifier})
  end

  protected
  include NoosferoTestHelper

  def capture
    yield
  end

  def concat(str)
    str
  end

end