cirandas.net

ref: master

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

class StepTest < ActiveSupport::TestCase

  def setup
    @profile = fast_create(Community)
    @track = CommunityTrackPlugin::Track.new(:profile => @profile, :name => 'track')
    @category = fast_create(Category)
    @track.add_category(@category)
    @track.save!

    @step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day - 1.day)
    Delayed::Job.destroy_all
  end

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

  should 'has a short description' do
    assert CommunityTrackPlugin::Step.short_description
  end

  should 'set accept_comments to false on create' do
    today = DateTime.now
    step = CommunityTrackPlugin::Step.create(:name => 'Step', :body => 'body', :profile => @profile, :parent => @track, :start_date => today, :end_date => today, :published => true)
    refute step.accept_comments
  end

  should 'do not allow step creation with a parent that is not a track' do
    today = DateTime.now
    blog = fast_create(Blog)
    step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => blog, :start_date => today, :end_date => today, :published => true)
    refute step.save
  end

  should 'do not allow step creation without a parent' do
    today = DateTime.now
    step = CommunityTrackPlugin::Step.new(:name => 'Step', :body => 'body', :profile => @profile, :parent => nil, :start_date => today, :end_date => today, :published => true)
    refute step.save
  end

  should 'create step if end date is equal to start date' do
    @step.start_date = DateTime.now
    @step.end_date = DateTime.now
    assert @step.save
  end

  should 'create step if end date is after start date' do
    @step.start_date = DateTime.now
    @step.end_date = DateTime.now + 1.day
    assert @step.save
  end

  should 'do not create step if end date is before start date' do
    @step.start_date = DateTime.now
    @step.end_date = DateTime.now - 1.day
    refute @step.save
  end

  should 'do not validate date period if start date is nil' do
    @step.start_date = nil
    @step.end_date_equal_or_after_start_date.inspect
    assert @step.errors.empty?
  end

  should 'do not validate date period if end date is nil' do
    @step.end_date = nil
    @step.end_date_equal_or_after_start_date.inspect
    assert @step.errors.empty?
  end

  should 'be active if today is between start and end dates' do
    @step.start_date = DateTime.now
    @step.end_date = DateTime.now + 1.day
    assert @step.active?
  end

  should 'be finished if today is after the end date' do
    @step.start_date = DateTime.now - 2.day
    @step.end_date = DateTime.now - 1.day
    assert @step.finished?
  end

  should 'be waiting if today is before the end date' do
    @step.start_date = DateTime.now + 1.day
    @step.end_date = DateTime.now + 2.day
    assert @step.waiting?
  end

  should 'return delayed job created with a specific step_id' do
    step_id = 0
    CommunityTrackPlugin::ActivationJob.new(step_id)
    assert CommunityTrackPlugin::ActivationJob.find(step_id)
  end

  should 'create delayed job' do
    @step.start_date = DateTime.now.beginning_of_day
    @step.end_date = DateTime.now.end_of_day
    @step.accept_comments = false
    @step.schedule_activation
    assert_equal 1, Delayed::Job.count
    assert_equal @step.start_date, Delayed::Job.first.run_at
  end

  should 'do not duplicate delayed job' do
    @step.start_date = DateTime.now
    @step.end_date = DateTime.now
    @step.schedule_activation
    assert_equal 1, Delayed::Job.count
    @step.schedule_activation
    assert_equal 1, Delayed::Job.count
  end

  should 'create delayed job when a step is saved' do
    @step.start_date = DateTime.now.beginning_of_day
    @step.end_date = DateTime.now.end_of_day
    @step.save!
    assert_equal @step.start_date, Delayed::Job.first.run_at
  end

  should 'create delayed job even if start date has passed' do
    @step.start_date = DateTime.now - 2.days
    @step.end_date = DateTime.now.end_of_day
    @step.accept_comments = false
    @step.schedule_activation
    assert_in_delta @step.start_date, Delayed::Job.first.run_at
  end

  should 'create delayed job if end date has passed' do
    @step.start_date = DateTime.now - 5.days
    @step.end_date = DateTime.now - 2.days
    @step.schedule_activation
    assert_in_delta @step.end_date + 1.day, Delayed::Job.first.run_at
  end

  should 'do not schedule delayed job if save but do not modify date fields' do
    @step.start_date = DateTime.now
    @step.end_date = DateTime.now.end_of_day
    @step.save!
    assert_equal 1, Delayed::Job.count
    Delayed::Job.destroy_all
    @step.name = 'changed name'
    @step.save!
    assert_equal 0, Delayed::Job.count
  end

  should 'set position on save' do
    refute @step.position
    @step.save!
    assert_equal 1, @step.position
    step2 = CommunityTrackPlugin::Step.new(:name => 'Step2', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day)
    step2.save!
    assert_equal 2, step2.position
  end

  should 'accept comments if step is active' do
    @step.start_date = DateTime.now
    @step.save!
    refute @step.accept_comments
    @step.toggle_activation
    @step.reload
    assert @step.accept_comments
  end

  should 'do not accept comments if step is not active' do
    @step.start_date = DateTime.now + 2.days
    @step.end_date = DateTime.now + 3.days
    @step.save!
    refute @step.published
    @step.toggle_activation
    @step.reload
    refute @step.published
  end

  should 'do not accept comments if step is not active anymore' do
    @step.end_date = DateTime.now.end_of_day
    @step.save!
    @step.toggle_activation
    @step.reload
    assert @step.accept_comments

    @step.start_date = DateTime.now - 2.days
    @step.end_date = DateTime.now - 1.day
    @step.save!
    @step.toggle_activation
    @step.reload
    refute @step.accept_comments
  end

  should 'set position to zero if step is hidden' do
    @step.hidden = true
    @step.save!
    assert_equal 0, @step.position
  end

  should 'change position to zero if step becomes hidden' do
    @step.save!
    assert_equal 1, @step.position
    @step.hidden = true
    @step.save!
    assert_equal 0, @step.position
  end

  should 'change position to botton if a hidden step becomes visible' do
    step1 = CommunityTrackPlugin::Step.new(:name => 'Step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day)
    step1.save!
    @step.hidden = true
    @step.save!
    assert_equal 0, @step.position
    @step.hidden = false
    @step.save!
    assert_equal 2, @step.position
  end

  should 'decrement lower items positions if a step becomes hidden' do
    @step.save!
    step1 = CommunityTrackPlugin::Step.new(:name => 'Step1', :body => 'body', :profile => @profile, :parent => @track, :published => false, :end_date => DateTime.now.end_of_day, :start_date => DateTime.now.beginning_of_day)
    step1.save!
    assert_equal 2, step1.position
    @step.hidden = true
    @step.save!
    step1.reload
    assert_equal 1, step1.position
  end

  should 'do not publish a hidden step' do
    @step.start_date = DateTime.now
    @step.hidden = true
    @step.save!
    refute @step.published
    @step.toggle_activation
    @step.reload
    refute @step.published
  end

  should 'return enabled tools for a step' do
    assert_includes @step.enabled_tools, TextArticle
    assert_includes @step.enabled_tools, Forum
  end

  should 'return class for selected tool' do
    @step.tool_type = 'Forum'
    assert_equal Forum, @step.tool_class
  end

  should 'return tool for selected type' do
    @step.tool_type = 'Forum'
    @step.save!
    article = fast_create(Article, :parent_id => @step.id)
    forum = fast_create(Forum, :parent_id => @step.id)
    assert_equal forum, @step.tool
  end

  should 'not return tool with different type' do
    @step.tool_type = 'Forum'
    @step.save!
    article = fast_create(Article, :parent_id => @step.id)
    assert_not_equal article, @step.tool
  end

  should 'initialize start date and end date with default values' do
    step = CommunityTrackPlugin::Step.new
    assert step.start_date
    assert step.end_date
  end

  should 'enable comments on children when step is activated' do
    @step.start_date = DateTime.now
    @step.save!
    refute @step.accept_comments
    article = fast_create(Article, :parent_id => @step.id, :profile_id => @step.profile.id, :accept_comments => false)
    refute article.accept_comments
    @step.toggle_activation
    assert article.reload.accept_comments
  end

  should 'enable comments on children when step is active' do
    @step.start_date = DateTime.now
    @step.save!
    refute @step.accept_comments
    @step.toggle_activation
    article = Article.create!(:parent => @step, :profile => @step.profile, :accept_comments => false, :name => "article")
    assert article.reload.accept_comments
  end

end