cirandas.net

ref: master

test/unit/approve_article_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
require_relative "../test_helper"

class ApproveArticleTest < ActiveSupport::TestCase

  def setup
    ActionMailer::Base.delivery_method = :test
    ActionMailer::Base.perform_deliveries = true
    ActionMailer::Base.deliveries = []
    User.current = @user = create_user 'test_user'
    @profile = @user.person
    @article = fast_create(TextArticle, :profile_id => @profile.id, :name => 'test name', :abstract => 'Lead of article', :body => 'This is my article')
    @community = fast_create(Community)
    @community.add_member(@profile)
  end
  attr_reader :profile, :article, :community

  should 'have name, reference article and profile' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)

    assert_equal article, a.article
    assert_equal community, a.target
  end

  should 'have abstract and body' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)

    assert_equal ['Lead of article', 'This is my article'], [a.abstract, a.body]
  end

  should 'create an article with the same class as original when finished' do
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)

    assert_difference 'article.class.count' do
      a.finish
    end
  end

  should 'override target notification message method from Task' do
    p1 = profile
    p2 = create_user('testuser2').person
    task = build(AddFriend, :person => p1, :friend => p2)
    assert_nothing_raised NotImplementedError do
      task.target_notification_message
    end
  end

  should 'have parent if defined' do
    folder = create(Folder, :name => 'test folder', :profile => profile)

    a = create(ApproveArticle, :name => 'test name', :article => article, :target => profile, :requestor => profile, :article_parent_id => folder.id)

    assert_equal folder, a.article_parent
  end

  should 'not have parent if not defined' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => profile, :requestor => profile)

    assert_nil a.article_parent
  end

  should 'alert when reference article is removed' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => profile, :requestor => profile)

    article.destroy
    a.reload

    assert_equal "The article was removed.", a.information[:message]
  end

  should 'preserve article_parent' do
    a = build(ApproveArticle, :article_parent => article)

    assert_equal article, a.article_parent
  end

  should 'handle blank names' do
    a = create(ApproveArticle, :name => '', :article => article, :target => community, :requestor => profile)

    assert_difference 'article.class.count' do
      a.finish
    end
  end

  should 'notify target if group is moderated' do
    community.moderated_articles = true
    community.save
    community.stubs(:notification_emails).returns(['adm@example.com'])

    a = create(ApproveArticle, :name => '', :article => article, :target => community, :requestor => profile)
    refute ActionMailer::Base.deliveries.empty?
  end

  should 'not notify target if group is not moderated' do
    community.moderated_articles = false
    community.save

    a = create(ApproveArticle, :name => '', :article => article, :target => community, :requestor => profile)
    assert ActionMailer::Base.deliveries.empty?
  end

  should 'copy the source from the original article' do
    article.source = 'sample-feed.com'
    article.save

    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal article.class.last.source, article.source
  end

  should 'have a reference article and profile on published article' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.finish

    published = article.class.last
    assert_equal [article, community], [published.reference_article, published.profile]
  end

  should 'copy name from original article' do
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal 'test name', article.class.last.name
  end

  should 'be able to edit name of generated article' do
    a = create(ApproveArticle, :name => 'Other name', :article => article, :target => community, :requestor => profile)
    a.abstract = 'Abstract edited';a.save
    a.finish

    assert_equal 'Other name', article.class.last.name
  end

  should 'copy abstract from original article' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal 'Lead of article', article.class.last.abstract
  end

  should 'be able to edit abstract of generated article' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.abstract = 'Abstract edited';a.save
    a.finish

    assert_equal 'Abstract edited', article.class.last.abstract
  end

  should 'copy body from original article' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal 'This is my article', article.class.last.body
  end

  should 'be able to edit body of generated article' do
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.body = 'Body edited';a.save
    a.finish

    assert_equal 'Body edited', article.class.last.body
  end

  should 'not be created in blog if community does not have a blog' do
    profile_blog = fast_create(Blog, :profile_id => profile.id)
    article.parent = profile_blog
    article.save

    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    refute community.has_blog?
    assert_nil article.class.last.parent
  end

  should 'be created in community blog if came from a blog' do
    profile_blog = fast_create(Blog, :profile_id => profile.id)
    article.parent = profile_blog
    article.save

    community.articles << Blog.new(:profile => community)
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal community.blog, article.class.last.parent
  end

  should 'not be created in community blog if did not come from a blog' do
    profile_folder = fast_create(Folder, :profile_id => profile.id)
    article.parent = profile_folder
    article.save

    blog = fast_create(Blog, :profile_id => community.id)
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    assert_nil article.class.last.parent
  end

  should 'overwrite blog if parent was choosen on published' do
    profile_blog = fast_create(Blog, :profile_id => profile.id)
    article.parent = profile_blog
    article.save

    community.articles << Blog.new(:profile => community)
    community_folder = fast_create(Folder, :profile_id => profile.id)

    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile, :article_parent => community_folder)
    a.finish

    assert_equal community_folder, article.class.last.parent
  end

  should 'use author from original article on published' do
    article.class.any_instance.stubs(:author).returns(profile)
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal profile, article.class.last.author
  end

  should 'use original article author even if article is destroyed' do
    article.class.any_instance.stubs(:author).returns(profile)
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    article.destroy

    assert_equal profile, article.class.last.author
  end

  should 'the published article have parent if defined' do
    folder = fast_create(Folder, :profile_id => community.id)
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile, :article_parent => folder)
    a.finish

    assert_equal folder, article.class.last.parent
  end

  should 'copy to_html from reference_article' do
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal article.to_html, article.class.last.to_html
  end

  should 'notify activity on creating published' do
    ActionTracker::Record.delete_all
    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    assert_equal 1, ActionTracker::Record.count
  end

  should 'not group trackers activity of article\'s creation' do
    other_community = fast_create(Community)
    other_community.add_member(profile)
    ActionTracker::Record.delete_all

    article = fast_create(TextArticle)
    a = create(ApproveArticle, :name => 'bar', :article => article, :target => community, :requestor => profile)
    a.finish

    article = fast_create(TextArticle)
    a = create(ApproveArticle, :name => 'another bar', :article => article, :target => community, :requestor => profile)
    a.finish

    article = fast_create(TextArticle)
    a = create(ApproveArticle, :name => 'another bar', :article => article, :target => other_community, :requestor => profile)
    a.finish
    assert_equal 3, ActionTracker::Record.count
  end

  should 'not create trackers activity when updating articles' do
    other_community = fast_create(Community)
    other_community.add_member(profile)
    ActionTracker::Record.delete_all
    article1 = fast_create(TextArticle)
    a = create(ApproveArticle, :name => 'bar', :article => article1, :target => community, :requestor => profile)
    a.finish

    article2 = fast_create(TextArticle)
    a = create(ApproveArticle, :name => 'another bar', :article => article2, :target => other_community, :requestor => profile)
    a.finish
    assert_equal 2, ActionTracker::Record.count

    assert_no_difference 'ActionTracker::Record.count' do
      published = article1.class.last
      published.name = 'foo';published.save!

      published = article2.class.last
      published.name = 'another foo';published.save!
    end
  end

  should "the tracker action target be defined as the article on articles'creation in communities" do
    ActionTracker::Record.delete_all
    person = fast_create(Person)
    community.add_member(person)

    a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    a.finish

    approved_article = community.articles.find_by(name: article.name)

    assert_equal approved_article, ActionTracker::Record.last.target
  end

  should "the tracker action target be defined as the article on articles'creation in profile" do
    ActionTracker::Record.delete_all
    person = fast_create(Person)
    person.stubs(:notification_emails).returns(['target@example.org'])

    a = create(ApproveArticle, :article => article, :target => person, :requestor => person)
    a.finish

    approved_article = person.articles.find_by(name: article.name)

    assert_equal approved_article, ActionTracker::Record.last.target
  end

  should 'not have target notification message if it is not a moderated oganization' do
    community.moderated_articles = false; community.save
    task = build(ApproveArticle, :article => article, :target => community, :requestor => profile)

    assert_nil task.target_notification_message
  end

  should 'have target notification message if is organization and not moderated' do
    task = build(ApproveArticle, :article => article, :target => community, :requestor => profile)

    community.expects(:moderated_articles?).returns(['true'])

    assert_match(/wants to publish the article.*[\n]*.*to approve or reject/, task.target_notification_message)
  end

  should 'have target notification description' do
    community.moderated_articles = false; community.save
    task = build(ApproveArticle, :article => article, :target => community, :requestor => profile)

    assert_match(/#{task.requestor.name} wants to publish the article: #{article.name}/, task.target_notification_description)
  end

  should 'deliver target notification message' do
    task = build(ApproveArticle, :article => article, :target => community, :requestor => profile)

    community.expects(:notification_emails).returns(['target@example.com'])
    community.expects(:moderated_articles?).returns(['true'])

    email = TaskMailer.target_notification(task, task.target_notification_message).deliver
    assert_match(/#{task.requestor.name} wants to publish the article: #{article.name}/, email.subject)
  end

  should 'deliver target finished message' do
    task = build(ApproveArticle, :article => article, :target => community, :requestor => profile)

    email = task.send(:send_notification, :finished).deliver

    assert_match(/#{task.requestor.name} wants to publish the article: #{article.name}/, email.subject)
  end

  should 'deliver target finished message about article deleted' do
    task = build(ApproveArticle, :article => article, :target => community, :requestor => profile)
    article.destroy

    email = task.send(:send_notification, :finished).deliver

    assert_match(/#{task.requestor.name} wanted to publish an article but it was removed/, email.subject)
  end

  should 'approve an event' do
    event = fast_create(Event, :profile_id => profile.id, :name => 'Event test', :slug => 'event-test', :abstract => 'Lead of article', :body => 'This is my event')
    task = create(ApproveArticle, :name => 'Event test', :article => event, :target => community, :requestor => profile)
    assert_difference 'event.class.count' do
      task.finish
    end
  end

  should 'approve same article twice changing its name' do
    task1 = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    assert_difference 'article.class.count' do
      task1.finish
    end
    task2 = create(ApproveArticle, :name => article.name + ' v2', :article => article, :target => community, :requestor => profile)
    assert_difference 'article.class.count' do
      assert_nothing_raised ActiveRecord::RecordInvalid do
         task2.finish
      end
    end
  end

  should 'not approve same article twice if not changing its name' do
    task1 = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    assert_difference 'article.class.count' do
      task1.finish
    end
    task2 = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    assert_no_difference 'article.class.count' do
      assert_raises ActiveRecord::RecordInvalid do
         task2.finish
      end
    end
  end

  should 'return reject message even without reject explanation' do
    task = build(ApproveArticle, :name => 'My Article')
    assert_not_nil task.task_cancelled_message
  end

  should 'show the name of the article in the reject message' do
    task = build(ApproveArticle, :name => 'My Article')
    assert_match /My Article/, task.task_cancelled_message
  end

  should 'not save 4 on the new article\'s last_changed_by_ud after approval if author is nil' do
    article = fast_create(Article)
    task = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
    task.finish
    new_article = Article.last
    assert_nil new_article.last_changed_by_id
  end

  should 'not crash if target has its own domain' do
    article = fast_create(Article)
    profile.domains << create(Domain, :name => 'example.org')
    assert_nothing_raised do
      create(ApproveArticle, :article => article, :target => profile, :requestor => profile)
    end
  end

  should 'create link to referenced article' do
    article = fast_create(Article)
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => community, :requestor => profile)
    a.create_link = true
    a.finish

    assert_equal article, LinkArticle.last.reference_article
  end

  should 'not allow non-person requestor' do
    task = ApproveArticle.new(:requestor => Community.new)
    task.valid?
    assert task.invalid?(:requestor)
  end

  should 'allow only self requestors when the target is a person' do
    person = fast_create(Person)
    another_person = fast_create(Person)

    t1 = ApproveArticle.new(:requestor => person, :target => person)
    t2 = ApproveArticle.new(:requestor => another_person, :target => person)

    assert t1.valid?
    assert !t2.valid?
    assert t2.invalid?(:requestor)
  end

  should 'allow anyone to be requestors when target is a community' do
    community = fast_create(Community)
    member = fast_create(Person)
    community.add_member(member)
    non_member = fast_create(Person)

    t1 = ApproveArticle.new(:requestor => member, :target => community)
    t2 = ApproveArticle.new(:requestor => non_member, :target => community)

    assert t1.valid?
    assert t2.valid?
  end

  should 'allow any user to be requestor whe the target is the portal community' do
    community = fast_create(Community)
    environment = community.environment
    environment.portal_community = community
    environment.save!
    person = fast_create(Person)

    task = ApproveArticle.new(:requestor => person, :target => community)

    assert task.valid?
  end
end