cirandas.net

ref: master

plugins/recent_activities/test/unit/recent_activities_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
require_relative '../test_helper'

class RecentActivitiesBlockTest < ActiveSupport::TestCase
  should 'describe itself' do
    assert_not_equal Block.description, RecentActivitiesPlugin::ActivitiesBlock.description
  end

  should 'is editable' do
    block = RecentActivitiesPlugin::ActivitiesBlock.new
    assert block.editable?
  end

  should 'return last activities' do
    profile = create_user('testuser').person
    a1 = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    a2 = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    ProfileActivity.create! profile_id: profile.id, activity: a1
    ProfileActivity.create! profile_id: profile.id, activity: a2

    block = RecentActivitiesPlugin::ActivitiesBlock.new
    block.stubs(:owner).returns(profile)

    assert_equal [a2, a1].map(&:id), block.activities.map(&:id)
  end

  should 'return last activities with limit' do
    profile = create_user('testuser').person
    a1 = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    a2 = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    ProfileActivity.create! profile_id: profile.id, activity: a1
    ProfileActivity.create! profile_id: profile.id, activity: a2

    block = RecentActivitiesPlugin::ActivitiesBlock.new
    block.stubs(:owner).returns(profile)
    block.limit = 1

    assert_equal [a2].map(&:id), block.activities.map(&:id)
  end

  should 'return only action tracker records as activities' do
    profile = create_user('testuser').person
    friend = create_user('friend').person
    scrap = create(Scrap, defaults_for_scrap(sender: friend, receiver: profile))
    a1 = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    a2 = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    ProfileActivity.create! profile_id: profile.id, activity: a1
    ProfileActivity.create! profile_id: profile.id, activity: a2

    block = RecentActivitiesPlugin::ActivitiesBlock.new
    block.stubs(:owner).returns(profile)

    assert_equal [a2, a1, scrap], block.owner.activities.map(&:activity)
    assert_equal [a2, a1], block.activities
  end
end

require 'boxes_helper'

class RecentActivitiesBlockViewTest < ActionView::TestCase
  include BoxesHelper

  should 'return activities in api_content' do
    profile = create_user('testuser').person

    a = fast_create(ActionTracker::Record, user_id: profile.id, created_at: Time.now, updated_at: Time.now)
    ProfileActivity.create! profile_id: profile.id, activity: a

    block = RecentActivitiesPlugin::ActivitiesBlock.new
    block.stubs(:owner).returns(profile)

    api_activity = block.api_content['activities'].last
    assert_equal [a.id], block.api_content['activities'].map{ |a| a[:id] }
    assert_not_nil api_activity[:label]
    assert_nil api_activity[:start_date]
  end

  should 'return event information in api_content' do
    person = fast_create(Person)
    event = build(Event, { name: 'Event', start_date: DateTime.new(2020, 1, 1) })
    event.profile = person
    event.save!
    activity = create_activity(person, event)

    block = RecentActivitiesPlugin::ActivitiesBlock.new
    block.stubs(:owner).returns(person)

    api_activity = block.api_content['activities'].last
    assert_not_nil api_activity[:start_date]
  end

  protected

  def create_activity(person, target)
    activity = ActionTracker::Record.create! verb: :leave_scrap, user: person, target: target
    ProfileActivity.create! profile_id: target.id, activity: activity
    activity.reload
  end
end