cirandas.net

ref: master

plugins/community_track/test/unit/community_track_plugin/track_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
require_relative '../../test_helper'

class TrackTest < ActiveSupport::TestCase

  def setup
    @profile = create(Community)
    @track = create_track('track', @profile)
    @step = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => DateTime.now, :end_date => DateTime.now, :name => 'step', :profile => @profile)
    @track.children << @step
    @tool = fast_create(Article, :parent_id => @step.id, :profile_id => @profile.id)
    @step.children << @tool
  end

  should 'describe yourself' do
    assert CommunityTrackPlugin::Track.description
  end

  should 'has a short descriptionf' do
    assert CommunityTrackPlugin::Track.short_description
  end

  should 'has a css class name' do
    assert_equal 'community-track-plugin-track', @track.css_class_name
  end

  should 'return comments count of children tools' do
    assert_equal 0, @track.comments_count
    owner = create_user('testuser').person
    article = create(Article, :name => 'article', :parent_id => @step.id, :profile_id => owner.id)
    @track.children << @step
    @step.children << article
    comment = create(Comment, :source => article, :author_id => owner.id)
    @step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => DateTime.now, :end_date => DateTime.now, :name => 'step2', :profile => @profile)
    @step2.tool_type = 'Forum'
    forum = fast_create(Forum, :parent_id => @step2.id, :profile_id => owner.id)
    article_forum = create(Article, :name => 'article_forum', :parent_id => forum.id, :profile_id => owner.id)
    forum.children << article_forum
    forum_comment = create(Comment, :source => article_forum, :author_id => owner.id)
    @track.children = [@step, @step2]
    @track = Article.find(@track.id)
    assert_equal 2, @track.comments_count
  end

  should 'return children steps' do
    assert_equal [@step], @track.steps_unsorted
  end

  should 'do not return other articles type at steps' do
    article = fast_create(Article, :parent_id => @track.id, :profile_id => @track.profile.id)
    @track = Article.find(@track.id)
    assert_includes @track.children, article
    assert_equal [@step], @track.steps_unsorted
  end

  should 'return name of the top category' do
    top = fast_create(Category, :name => 'top category')
    category1 = fast_create(Category, :name => 'category1', :parent_id => top.id )
    category2 = fast_create(Category, :name => 'category2', :parent_id => category1.id )
    @track.categories.delete_all
    @track.add_category(category2, true)
    assert_equal 'top category', @track.category_name
  end

  should 'return name of the top category when has no subcategory' do
    top = create(Category, :name => 'top category', :environment => Environment.default)
    @track.categories.delete_all
    @track.add_category(top, true)
    assert_equal 'top category', @track.category_name
  end

  should 'return empty for category name if it has no category' do
    @track.categories.delete_all
    assert_equal '', @track.category_name
  end

  should 'return category name of first category' do
    category = fast_create(Category, :name => 'category')
    @track.categories.delete_all
    @track.add_category(category, true)
    category2 = fast_create(Category, :name => 'category2')
    @track.add_category(category2, true)
    assert_equal 'category', @track.category_name
  end

  should 'return steps with insert order' do
    @track.children.destroy_all
    step1 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step1", :profile => @track.profile)
    step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step2", :profile => @track.profile)
    step3 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step3", :profile => @track.profile)
    assert_equal 1, step1.position
    assert_equal 2, step2.position
    assert_equal 3, step3.position
    assert_equal [step1, step2, step3], @track.steps
  end

  should 'return steps with order defined by position attribute' do
    @track.children.destroy_all
    step1 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step1", :profile => @track.profile)
    step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step2", :profile => @track.profile)
    step3 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step3", :profile => @track.profile)
    step1.position = 3
    step1.save!
    step2.position = 1
    step2.save!
    step3.position = 2
    step3.save!
    assert_equal [step2, step3, step1], @track.steps
  end

  should 'save steps in a new order' do
    @track.children.destroy_all

    step1 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step1", :profile => @track.profile)
    step2 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step2", :profile => @track.profile)
    step3 = CommunityTrackPlugin::Step.create!(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => "step3", :profile => @track.profile)

    assert_equal [step1.id, step2.id, step3.id], @track.steps.map(&:id)
    @track.reorder_steps([step3.id, step1.id, step2.id])
    @track.reload
    assert_equal [step3.id, step1.id, step2.id], @track.steps.map(&:id)
  end

  should 'do not return hidden steps' do
    hidden_step = CommunityTrackPlugin::Step.new(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'hidden step', :profile => @track.profile)
    hidden_step.hidden = true
    hidden_step.save!
    assert_equal [@step], @track.steps
  end

  should 'return hidden steps' do
    hidden_step = CommunityTrackPlugin::Step.new(:parent => @track, :start_date => Date.today, :end_date => Date.today, :name => 'hidden step', :profile => @track.profile)
    hidden_step.hidden = true
    hidden_step.save!
    assert_equal [hidden_step], @track.hidden_steps
  end

   should 'get first paragraph' do
    @track.body = '<p>First</p><p>Second</p>'
    assert_equal '<p>First</p>', @track.first_paragraph
  end

  should 'provide first_paragraph even if body was not given' do
    assert_equal '', @track.first_paragraph
  end

  should 'provide first_paragraph even if body is nil' do
    @track.body = nil
    assert_equal '', @track.first_paragraph
  end

  should 'not be able to create a track without category' do
    track = CommunityTrackPlugin::Track.create(:profile => @profile, :name => 'track')
    assert track.errors.include?(:categories)
  end

  should 'not be able to save a track without category' do
    @track.categories.delete_all
    @track.save
    assert @track.errors.include?(:categories)
  end

end