cirandas.net

ref: master

plugins/display_content/test/unit/display_content_block_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
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
require_relative '../test_helper'
class DisplayContentBlockTest < ActiveSupport::TestCase

  INVALID_KIND_OF_ARTICLE = [Event, RssFeed, UploadedFile, Gallery]
  VALID_KIND_OF_ARTICLE = [TextArticle, Folder, Blog, Forum]

  should 'describe itself' do
    assert_not_equal Block.description, DisplayContentBlock.description
  end

  should 'is editable' do
    block = DisplayContentBlock.new
    assert block.editable?
  end

  should 'have field nodes' do
    block = DisplayContentBlock.new
    assert_respond_to block, :nodes
  end

  should 'default value of nodes be an empty array' do
    block = DisplayContentBlock.new
    assert_equal [], block.nodes
  end

  should 'not expand nodes if there is no holder' do
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => 1)

    checked_articles= {a1.id => true}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(nil)
    block.checked_nodes= checked_articles
    a1.delete
    block.save!
    assert_equal [a1.id], block.nodes
  end

  should 'nodes be the article ids in hash of checked nodes' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    assert_equal [], [a1.id, a2.id, a3.id] - block.nodes
    assert_equal [], block.nodes - [a1.id, a2.id, a3.id]
  end

  should 'nodes be save in database' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    block.save
    block.reload
    assert_equal [], [a1.id, a2.id, a3.id] - block.nodes
    assert_equal [], block.nodes - [a1.id, a2.id, a3.id]
  end

  should 'be able to update nodes' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id)
    a4 = fast_create(TextArticle, :name => 'test article 4', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    block.save

    block.reload
    checked_articles= {a1.id => true, a4.id => true}
    block.checked_nodes= checked_articles
    block.save
    block.reload

    assert_equal [], [a1.id, a4.id] - block.nodes
    assert_equal [], block.nodes - [a1.id, a4.id]
  end

  should "save selected folders and articles" do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id)
    f2 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id, :parent_id => f1.id)
    a4 = fast_create(TextArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => f2.id)
    a5 = fast_create(TextArticle, :name => 'test article 5', :profile_id => profile.id, :parent_id => f2.id)

    checked_articles= {a1.id => true, a2.id => true, f1.id => false}

    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles

    assert_equivalent [a1.id, a2.id, f1.id], block.nodes
  end

  should "save selected articles and blogs" do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    b1 = fast_create(Blog, :name => 'test blog 1', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => b1.id)
    b2 = fast_create(Blog, :name => 'test blog 2', :profile_id => profile.id)
    a4 = fast_create(TextArticle, :name => 'test article 4', :profile_id => profile.id, :parent_id => b2.id)
    a5 = fast_create(TextArticle, :name => 'test article 5', :profile_id => profile.id, :parent_id => b2.id)

    checked_articles= {a1.id => true, a2.id => true, b1.id => false, b2.id => true}

    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles

    assert_equivalent [a1.id, a2.id, b1.id, b2.id], block.nodes
  end

  should 'TextArticle be saved as node' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)

    checked_articles= {a1.id => true}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    assert_equal [], [a1.id] - block.nodes
    assert_equal [], block.nodes - [a1.id]
  end

  should 'Event be saved as node' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(Event, :name => 'test article 3', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    assert_equivalent [a1.id, a2.id, a3.id], block.nodes
  end

  should 'Folder be saved as node' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(Folder, :name => 'test article 3', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    assert_equivalent [a1.id, a2.id, a3.id], block.nodes
  end

  should 'Forum be saved as node' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(Forum, :name => 'test article 3', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    assert_equivalent [a1.id, a2.id, a3.id], block.nodes
  end

  should 'Blog be saved as node' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(Blog, :name => 'test article 3', :profile_id => profile.id)

    checked_articles= {a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    assert_equivalent [a1.id, a2.id, a3.id], block.nodes
  end

  should "return all root articles from profile" do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => a2.id)

    block = DisplayContentBlock.new
    block.nodes= [a1.id, a2.id, a3.id]
    box = mock()
    box.stubs(:owner).returns(profile)
    box.stubs(:environment).returns(Environment.default)
    block.stubs(:box).returns(box)
    assert_equal [], [a1, a2] - block.articles_of_parent
    assert_equal [], block.articles_of_parent - [a1, a2]
  end

  should "return all children of an articles's profile" do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => a2.id)

    block = DisplayContentBlock.new
    box = mock()
    box.stubs(:owner).returns(profile)
    box.stubs(:environment).returns(Environment.default)
    block.stubs(:box).returns(box)
    assert_equal [], [a3] - block.articles_of_parent(a2)
    assert_equal [], block.articles_of_parent(a2) - [a3]
  end

  should "return all root articles from environment" do
    profile = fast_create(Community, :name => 'my test community', :identifier => 'mytestcommunity')
    environment = Environment.default
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => a2.id)

    block = DisplayContentBlock.new
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(environment)
    box.stubs(:environment).returns(Environment.default)
    environment.stubs(:portal_community).returns(profile)

    assert_equal [], [a1, a2] - block.articles_of_parent
    assert_equal [], block.articles_of_parent - [a1, a2]
  end

  should "return all children of an articles's portal community of environment" do
    profile = fast_create(Community, :name => 'my test community', :identifier => 'mytestcommunity')
    environment = Environment.default
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => a2.id)

    block = DisplayContentBlock.new
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(environment)
    box.stubs(:environment).returns(Environment.default)
    environment.stubs(:portal_community).returns(profile)

    assert_equal [], [a3] - block.articles_of_parent(a2)
    assert_equal [], block.articles_of_parent(a2) - [a3]
  end

  should "return an empty array if environment there is no portal community defined" do
    environment = Environment.default

    block = DisplayContentBlock.new
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(environment)
    box.stubs(:environment).returns(Environment.default)

    assert_equal [], block.articles_of_parent()
  end

  INVALID_KIND_OF_ARTICLE.map do |invalid_article|

    define_method "test_should_not_return_#{invalid_article.name}_articles_in_articles_of_parent_method" do
      profile = create_user('testuser').person
      Article.delete_all
      a1 = fast_create(invalid_article, :name => 'test article 1', :profile_id => profile.id)
      a2 = fast_create(VALID_KIND_OF_ARTICLE.first, :name => 'test article 2', :profile_id => profile.id)

      block = DisplayContentBlock.new
      box = mock()
      box.stubs(:owner).returns(profile)
      box.stubs(:environment).returns(Environment.default)
      block.stubs(:box).returns(box)
      assert_equal [], [a2] - block.articles_of_parent
      assert_equal [], block.articles_of_parent - [a2]
    end

  end

  VALID_KIND_OF_ARTICLE.map do |valid_article|

    define_method "test_should_return_#{valid_article.name}_articles_in_articles_of_parent_method" do
      profile = create_user('testuser').person
      Article.delete_all
      a1 = fast_create(valid_article, :name => 'test article 1', :profile_id => profile.id)
      a2 = fast_create(INVALID_KIND_OF_ARTICLE.first, :name => 'test article 2', :profile_id => profile.id)

      block = DisplayContentBlock.new
      box = mock()
      box.stubs(:owner).returns(profile)
      box.stubs(:environment).returns(Environment.default)
      block.stubs(:box).returns(box)
      assert_equal [a1], block.articles_of_parent
    end

  end

  include ActionView::Helpers
  include Rails.application.routes.url_helpers

  should 'url_params return myprofile url params if the owner is a profile' do
    profile = create_user('testuser').person
    block = DisplayContentBlock.new
    block.box = profile.boxes.first
    block.save!

    params = {:block_id => block.id}
    params[:controller] = "display_content_plugin_myprofile"
    params[:profile] = profile.identifier
    assert_equal params, block.url_params
  end

  should 'url_params return admin url params if the owner is an environment' do
    environment = Environment.default
    block = DisplayContentBlock.new
    block.box = environment.boxes.first
    block.save!

    params = {:block_id => block.id}
    params[:controller] = "display_content_plugin_admin"
    assert_equal params, block.url_params
  end

  should 'display_attribute be true for title by default' do
    profile = create_user('testuser').person

    block = DisplayContentBlock.new

    assert block.display_section?({:value => 'title', :checked => true})
  end

  should 'display_attribute be true if the attribute was chosen' do
    profile = create_user('testuser').person

    block = DisplayContentBlock.new

    section = {:value => 'body', :checked => true}
    block.sections = [section]

    assert block.display_section?(section)
  end

  should 'display_attribute be true for publish date by default' do
    profile = create_user('testuser').person

    block = DisplayContentBlock.new

    assert block.display_section?({:value => 'publish_date', :checked => true})
  end

  should 'do not save children if a folder is checked' do
    profile = create_user('testuser').person
    Article.delete_all
    f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f1.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id)

    checked_articles= {f1.id => true, a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    block.save!
    assert_equivalent [f1.id], block.nodes
  end

  should 'save folder and children if display_folder_children is false' do
    profile = create_user('testuser').person
    Article.delete_all
    f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :parent_id => f1.id)
    a3 = fast_create(TextArticle, :name => 'test article 3', :profile_id => profile.id, :parent_id => f1.id)

    checked_articles= {f1.id => true, a1.id => true, a2.id => true, a3.id => false}
    block = DisplayContentBlock.new
    block.display_folder_children = false
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    block.save!
    assert_equivalent [f1.id, a1.id, a2.id, a3.id], block.nodes
  end

  should "test should return plugins articles in articles of parent method" do
    class PluginArticle < Article; end

    class Plugin1 < Noosfero::Plugin
      def content_types
        [PluginArticle]
      end
    end

    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(PluginArticle, :name => 'test article 1', :profile_id => profile.id)
    Noosfero::Plugin.stubs(:all).returns([Plugin1.name])
    env = fast_create(Environment)
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([Plugin1.new])

    block = DisplayContentBlock.new
    box = mock()
    box.stubs(:owner).returns(profile)
    Noosfero::Plugin.stubs(:all).returns(['DisplayContentBlockTest::Plugin1'])
    box.stubs(:environment).returns(env)
    block.stubs(:box).returns(box)
    assert_equal [a1], block.articles_of_parent
  end

  should "the section display all available sections" do
    block = DisplayContentBlock.new
    assert_equivalent ['publish_date', 'abstract', 'body', 'image' ,'tags', 'title'], block.sections.map{|e|e[:value]}
  end

  should "the section display all available sections if the section value has only one key" do
    block = DisplayContentBlock.new
    block.sections = [{:value => 'abstract', :checked => true}]
    assert_equivalent ['publish_date', 'abstract', 'body', 'image' ,'tags', 'title'], block.sections.map{|e|e[:value]}
  end

  should 'return available content types with checked types first' do
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    block = DisplayContentBlock.create!
    block.types = ['TextArticle']

    block.types = ['TextArticle', 'Folder']
    assert_equivalent [TextArticle, Folder, UploadedFile, Event, Blog, Forum, Gallery, RssFeed], block.available_content_types
  end

  should 'return available content types' do
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    block = DisplayContentBlock.create!
    block.types = ['TextArticle']
    block.types = []
    assert_equivalent [UploadedFile, Event, TextArticle, Folder, Blog, Forum, Gallery, RssFeed], block.available_content_types
  end

  should 'return first 2 content types' do
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    block = DisplayContentBlock.create!
    block.types = ['TextArticle']
    assert_equal 2, block.first_content_types.length
  end

  should 'return all but first 2 content types' do
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    block = DisplayContentBlock.create!
    block.types = ['TextArticle']
    assert_equal block.available_content_types.length - 2, block.more_content_types.length
  end

  should 'return 2 as default value for first_types_count' do
    block = DisplayContentBlock.create!
    block.types = ['TextArticle']
    assert_equal 2, block.first_types_count
  end

  should 'return types length if it has more than 2 selected types' do
    block = DisplayContentBlock.create!
    block.types = ['UploadedFile', 'Event', 'Folder']
    assert_equal 3, block.first_types_count
  end

  should 'return selected types at first_content_types' do
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([])
    block = DisplayContentBlock.create!
    block.types = ['UploadedFile', 'Event', 'Folder']
    assert_equal [UploadedFile, Event, Folder], block.first_content_types
    assert_equal block.available_content_types - [UploadedFile, Event, Folder], block.more_content_types
  end

  should 'include plugin content at available content types' do
    block = DisplayContentBlock.create!
    class SomePluginContent;end
    class SomePlugin; def content_types; SomePluginContent end end
    Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([SomePlugin.new])

    block.types = []
    assert_equivalent [UploadedFile, Event, TextArticle, Folder, Blog, Forum, Gallery, RssFeed, SomePluginContent], block.available_content_types
  end

  should 'do not fail if a selected article was removed' do
    profile = create_user('testuser').person
    Article.delete_all
    f1 = fast_create(Folder, :name => 'test folder 1', :profile_id => profile.id)
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :parent_id => f1.id)

    checked_articles= {a1.id => true}

    block = DisplayContentBlock.new
    block.stubs(:holder).returns(profile)
    block.checked_nodes= checked_articles
    a1.destroy
    assert_equal [], block.parent_nodes
  end

end

class DisplayContentBlockViewTest < ActionView::TestCase
  include BoxesHelper
  include DatesHelper
  helper :dates

  should 'list links for all articles title defined in nodes' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id)

    block = DisplayContentBlock.new
    block.sections = [{:value => 'title', :checked => true}]
    block.nodes = [a1.id, a2.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_match /.*<a.*>#{a1.title}<\/a>/, render_block_content(block)
    assert_match /.*<a.*>#{a2.title}<\/a>/, render_block_content(block)
  end

  should 'list content for all articles lead defined in nodes' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :abstract => 'abstract article 1')
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :abstract => 'abstract article 2')

    block = DisplayContentBlock.new
    block.sections = [{:value => 'abstract', :checked => true}]
    block.nodes = [a1.id, a2.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_match /<div class="lead">#{a1.lead}<\/div>/, render_block_content(block)
    assert_match /<div class="lead">#{a2.lead}<\/div>/, render_block_content(block)
  end

  should 'not crash when referenced article is removed' do
    profile = create_user('testuser').person
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)

    block = DisplayContentBlock.new
    block.nodes = [a1.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    Article.delete_all
    assert_match /<ul><\/ul>/, render_block_content(block)
  end

  should 'show title if defined by user' do
    profile = create_user('testuser').person
    a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id)

    block = DisplayContentBlock.new
    block.nodes = [a.id]
    block.sections = [{:value => 'title', :checked => true}]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_match /.*<a.*>#{a.title}<\/a>/, render_block_content(block)
  end

  should 'show abstract if defined by user' do
    profile = create_user('testuser').person
    a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :abstract => 'some abstract')

    block = DisplayContentBlock.new
    block.nodes = [a.id]
    block.sections = [{:value => 'abstract', :checked => true}]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_match /#{a.abstract}/, render_block_content(block)
  end

  should 'show body if defined by user' do
    profile = create_user('testuser').person
    a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')

    block = DisplayContentBlock.new
    block.nodes = [a.id]
    block.sections = [{:value => 'body', :checked => true}]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_match /#{a.body}/, render_block_content(block)
  end

  should 'show published date if defined by user' do
    profile = create_user('testuser').person
    a = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :body => 'some body')

    block = DisplayContentBlock.new
    block.nodes = [a.id]
    block.sections = [{:value => 'publish_date', :checked => true}]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_match /#{a.published_at}/, render_block_content(block)
  end

  should 'show image if defined by user' do
    profile = create_user('testuser').person
    a = create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')})
    a.save!

    process_delayed_job_queue

    block = DisplayContentBlock.new
    block.nodes = [a.id]
    block.sections = [{:value => 'image', :checked => true}]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_tag_in_string render_block_content(block), :tag => 'div', :attributes => {:class => 'image'}
  end

  should 'show articles in recent order' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1))

    block = DisplayContentBlock.new
    block.sections = [{:value => 'title', :checked => true}]
    block.nodes = [a1.id, a2.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    block.order_by_recent = true

    a1_index = render_block_content(block).index(a1.name)
    a2_index = render_block_content(block).index(a2.name)

    assert a2_index < a1_index
  end

  should 'show articles in oldest order' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1))

    block = DisplayContentBlock.new
    block.sections = [{:value => 'title', :checked => true}]
    block.nodes = [a1.id, a2.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    block.order_by_recent = false

    a1_index = render_block_content(block).index(a1.name)
    a2_index = render_block_content(block).index(a2.name)

    assert a1_index < a2_index
  end

  should 'show articles in recent order with limit option' do
    profile = create_user('testuser').person
    Article.delete_all
    a1 = fast_create(TextArticle, :name => 'test article 1', :profile_id => profile.id, :published_at => DateTime.current)
    a2 = fast_create(TextArticle, :name => 'test article 2', :profile_id => profile.id, :published_at => (DateTime.current + 1))

    block = DisplayContentBlock.new
    block.sections = [{:value => 'title', :checked => true}]
    block.display_folder_children = true
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    block.order_by_recent = true
    block.limit_to_show = 1

    a1_index = render_block_content(block).index(a1.name)

    assert a1_index.nil?
  end

  should 'show only articles with current locale translation' do
    FastGettext.stubs(:locale).returns('pt')
    profile = create_user('testuser').person
    Article.delete_all

    en_article = fast_create(TextArticle, :profile_id => profile.id, :name => 'en_article', :language => 'en')
    en_article2 = fast_create(TextArticle, :profile_id => profile.id, :name => 'en_article 2', :language => 'en')

    pt_article = fast_create TextArticle, profile_id: profile.id, name: 'pt_article', language: 'pt', translation_of_id: en_article.id

    block = DisplayContentBlock.new
    block.sections = [{:value => 'title', :checked => true}]
    block.content_with_translations = true
    block.limit_to_show = 2

    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_nil render_block_content(block).index(en_article.name)
    assert_nil render_block_content(block).index(en_article2.name)
    assert render_block_content(block).index(pt_article.name).present?

    FastGettext.stubs(:locale).returns('en')

    assert render_block_content(block).index(en_article.name)
    assert render_block_content(block).index(en_article2.name)
    assert_nil render_block_content(block).index(pt_article.name)
  end

  should 'replace article with its translation' do
    FastGettext.stubs(:locale).returns('pt')
    profile = create_user('testuser').person
    Article.delete_all

    en_article = fast_create(TextArticle, :profile_id => profile.id, :name => 'en_article', :language => 'en')
    pt_article = fast_create(TextArticle, :profile_id => profile.id, :name => 'pt_article', :language => 'pt', :translation_of_id => en_article)

    block = DisplayContentBlock.new
    block.sections = [{:value => 'title', :checked => true}]
    block.content_with_translations = true
    block.limit_to_show = 2

    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)

    assert_nil render_block_content(block).index(en_article.name)
    assert render_block_content(block).index(pt_article.name).present?

    FastGettext.stubs(:locale).returns('en')

    assert render_block_content(block).index(en_article.name).present?
    assert_nil render_block_content(block).index(pt_article.name)
  end

  should 'not escape abstract html of articles' do
    profile = create_user('testuser').person
    a1 = fast_create(TextArticle, abstract: "<p class='test-article-abstract'>Test</p>", name: 'test article 1', profile_id: profile.id, published_at: DateTime.current)
    
    block = DisplayContentBlock.new
    block.sections = [{:value => 'abstract', :checked => true}] 
    block.nodes = [a1.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)
    assert_tag_in_string  render_block_content(block), tag: 'p', attributes: { class: 'test-article-abstract' }
  end

  should 'not raise if abstract of article is nil' do
    profile = create_user('testuser').person
    a1 = fast_create(TextArticle, name: 'test article 1', profile_id: profile.id, published_at: DateTime.current)
    
    block = DisplayContentBlock.new
    block.sections = [{:value => 'abstract', :checked => true}] 
    block.nodes = [a1.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)
    assert_nil a1.abstract
    assert_nothing_raised do
      render_block_content(block)
    end
  end

  should 'not escape body html of articles' do
    profile = create_user('testuser').person
    a1 = fast_create(TextArticle, body: "<p class='test-article-body'>Test</p>", name: 'test article 1', profile_id: profile.id, published_at: DateTime.current)
    
    block = DisplayContentBlock.new
    block.sections = [{:value => 'body', :checked => true}] 
    block.nodes = [a1.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)
    assert_tag_in_string  render_block_content(block), tag: 'p', attributes: { class: 'test-article-body' }
  end

  should 'not raise if body of article is nil' do
    profile = create_user('testuser').person
    a1 = fast_create(TextArticle, name: 'test article 1', profile_id: profile.id, published_at: DateTime.current)
    
    block = DisplayContentBlock.new
    block.sections = [{:value => 'abstract', :checked => true}] 
    block.nodes = [a1.id]
    box = mock()
    block.stubs(:box).returns(box)
    box.stubs(:owner).returns(profile)
    assert_nil a1.body
    assert_nothing_raised do
      render_block_content(block)
    end
  end

end