cirandas.net

ref: master

plugins/admin_notifications/test/unit/notification_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
require_relative '../../../../test/test_helper'
require_relative '../helpers/notification_test_helper'

class NotificationTest < ActiveSupport::TestCase

  include NotificationTestHelper

  def setup
    @env = Environment.default
    @env.enable_plugin('AdminNotificationsPlugin')

    @user = User.create!(:environment_id => @env.id, :email => "user@domain.com", :login   => "new_user", :password => "test", :password_confirmation => "test")
    @danger_notification = AdminNotificationsPlugin::DangerNotification.create!(
                      :target => @env,
                      :message => "Danger Message",
                      :active => true,
                    )

    @warning_notification = AdminNotificationsPlugin::WarningNotification.create!(
                      :target => @env,
                      :message => "Warning Message",
                      :active => true,
                    )

    @information_notification = AdminNotificationsPlugin::InformationNotification.create!(
                      :target => @env,
                      :message => "Information Message",
                      :active => true,
                    )
  end

  should 'get all notifications that a user did not close' do
    @information_notification.users << @user

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, @user, nil)

    assert notifications.include?(@danger_notification)
    assert notifications.include?(@warning_notification)
    assert !notifications.include?(@information_notification)
  end

  should 'get only notifications configured to be displayed to all users' do
    @information_notification.display_to_all_users = true
    @information_notification.save!

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, nil, nil)

    assert !notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)
  end

  should 'get only notifications configured to be displayed to all users and in all pages' do
    @information_notification.display_to_all_users = true
    @information_notification.display_only_in_homepage = true
    @information_notification.save!

    @danger_notification.display_to_all_users = true
    @danger_notification.save!

    @warning_notification.display_only_in_homepage = true
    @warning_notification.save!

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, nil, 'not_home')

    assert notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert !notifications.include?(@information_notification)
  end

  should 'get only notifications configured to be displayed in all pages' do
    @danger_notification.display_to_all_users = true
    @danger_notification.display_only_in_homepage = true
    @danger_notification.save!

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, @user, "not_home")

    assert !notifications.include?(@danger_notification)
    assert notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, nil, "home")

    assert notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert !notifications.include?(@information_notification)
  end

  should 'get notifications configured to be displayed on profile' do
    community = fast_create(Community)

    AdminNotificationsPlugin::Notification.destroy_all
    env_home_notification = create_notification(@env, true)
    env_not_home_notification = create_notification(@env, false)
    profile_not_home_notification = create_notification(community, false)
    profile_home_notification = create_notification(community, true)

    notifications = AdminNotificationsPlugin::Notification.visibles(community, @user, "profile")
    assert_equivalent notifications.to_a, [env_not_home_notification, profile_not_home_notification, profile_home_notification]

    notifications = AdminNotificationsPlugin::Notification.visibles(community, @user, "profile_but_bot_homepage")
    assert_equivalent notifications.to_a, [env_not_home_notification, profile_not_home_notification]
  end

  should 'get notifications configured to be displayed on environment' do
    community = fast_create(Community)

    AdminNotificationsPlugin::Notification.destroy_all
    env_home_notification = create_notification(@env, true)
    env_not_home_notification = create_notification(@env, false)
    profile_not_home_notification = create_notification(community, false)
    profile_home_notification = create_notification(community, true)

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, @user, "home")
    assert_equivalent notifications.to_a, [env_home_notification, env_not_home_notification]

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, @user, "not_home_not_profile")
    assert_equivalent notifications.to_a, [env_not_home_notification]
  end

  should 'get only notifications configured to be displayed to all users and in all pages and not closed by an user' do
    @information_notification.display_to_all_users = true
    @information_notification.save!

    @danger_notification.display_to_all_users = true
    @danger_notification.display_only_in_homepage = true
    @danger_notification.save!

    @warning_notification.display_to_all_users = true
    @warning_notification.save!

    @warning_notification.users << @user

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, @user, 'not_home')

    assert !notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)
  end

  should 'get only active notifications' do
    @information_notification.active = false
    @information_notification.save!

    notifications = AdminNotificationsPlugin::Notification.visibles(@env, @user, 'home')

    assert notifications.include?(@danger_notification)
    assert notifications.include?(@warning_notification)
    assert !notifications.include?(@information_notification)
  end

  should 'get only notifications with popup' do
    @information_notification.display_popup = true
    @information_notification.display_to_all_users = true
    @information_notification.save!

    notifications = AdminNotificationsPlugin::Notification.with_popup(@env, @user, 'home')

    assert !notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)

    notifications = AdminNotificationsPlugin::Notification.with_popup(@env, nil, nil)

    assert !notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)
  end

  should 'get only notifications with popup not closed by an user' do
    @information_notification.display_popup = true
    @information_notification.display_to_all_users = true
    @information_notification.save!

    @danger_notification.display_popup = true
    @danger_notification.display_to_all_users = true
    @danger_notification.save!

    @danger_notification.users << @user

    notifications = AdminNotificationsPlugin::Notification.with_popup(@env, @user, 'home')

    assert !notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)

    notifications = AdminNotificationsPlugin::Notification.with_popup(@env, nil, nil)

    assert notifications.include?(@danger_notification)
    assert !notifications.include?(@warning_notification)
    assert notifications.include?(@information_notification)
  end
end