Commit b2c228b4297e709afaa0f224a4dbaa3066ea2f85

Authored by Marcos Pereira
1 parent aa28efd0

fixes visibles_method and unit tests

plugins/environment_notification/models/environment_notification_plugin/environment_notification.rb
@@ -9,7 +9,7 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas @@ -9,7 +9,7 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas
9 "EnvironmentNotificationPlugin::DangerNotification" 9 "EnvironmentNotificationPlugin::DangerNotification"
10 ] 10 ]
11 11
12 - attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title 12 + attr_accessible :message, :target_id, :active, :type, :display_only_in_homepage, :display_to_all_users, :display_popup, :title, :target
13 13
14 has_many :environment_notifications_users 14 has_many :environment_notifications_users
15 has_many :users, :through => :environment_notifications_users 15 has_many :users, :through => :environment_notifications_users
@@ -41,7 +41,10 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas @@ -41,7 +41,10 @@ class EnvironmentNotificationPlugin::EnvironmentNotification < ActiveRecord::Bas
41 end 41 end
42 42
43 if controller_path != "home" 43 if controller_path != "home"
44 - notifications = notifications.where(display_only_in_homepage: false) 44 + notifications = notifications.where.not("display_only_in_homepage = ? AND target_type = ?",true,"Environment")
  45 + if controller_path != "profile"
  46 + notifications = notifications.where.not("display_only_in_homepage = ? AND target_type = ?",true,"Profile")
  47 + end
45 end 48 end
46 49
47 notifications 50 notifications
plugins/environment_notification/test/helpers/environment_notification_test_helper.rb 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +module EnvironmentNotificationTestHelper
  2 + def create_notification target, display_only_in_homepage=false, message="any_message", active=true
  3 + EnvironmentNotificationPlugin::WarningNotification.create!(
  4 + :target => target,
  5 + :message => message,
  6 + :active => active,
  7 + :display_only_in_homepage => display_only_in_homepage
  8 + )
  9 + end
  10 +end
plugins/environment_notification/test/unit/environment_notification_test.rb
1 require_relative '../../../../test/test_helper' 1 require_relative '../../../../test/test_helper'
  2 +require_relative '../helpers/environment_notification_test_helper'
2 3
3 class EnvironmentNotificationTest < ActiveSupport::TestCase 4 class EnvironmentNotificationTest < ActiveSupport::TestCase
4 5
  6 + include EnvironmentNotificationTestHelper
  7 +
5 def setup 8 def setup
6 @env = Environment.default 9 @env = Environment.default
7 @env.enable_plugin('EnvironmentNotificationPlugin') 10 @env.enable_plugin('EnvironmentNotificationPlugin')
8 11
9 @user = User.create!(:environment_id => @env.id, :email => "user@domain.com", :login => "new_user", :password => "test", :password_confirmation => "test") 12 @user = User.create!(:environment_id => @env.id, :email => "user@domain.com", :login => "new_user", :password => "test", :password_confirmation => "test")
10 @danger_notification = EnvironmentNotificationPlugin::DangerNotification.create!( 13 @danger_notification = EnvironmentNotificationPlugin::DangerNotification.create!(
11 - :target_id => @env.id, 14 + :target => @env,
12 :message => "Danger Message", 15 :message => "Danger Message",
13 :active => true, 16 :active => true,
14 ) 17 )
15 18
16 @warning_notification = EnvironmentNotificationPlugin::WarningNotification.create!( 19 @warning_notification = EnvironmentNotificationPlugin::WarningNotification.create!(
17 - :target_id => @env.id, 20 + :target => @env,
18 :message => "Warning Message", 21 :message => "Warning Message",
19 :active => true, 22 :active => true,
20 ) 23 )
21 24
22 @information_notification = EnvironmentNotificationPlugin::InformationNotification.create!( 25 @information_notification = EnvironmentNotificationPlugin::InformationNotification.create!(
23 - :target_id => @env.id, 26 + :target => @env,
24 :message => "Information Message", 27 :message => "Information Message",
25 :active => true, 28 :active => true,
26 ) 29 )
@@ -83,6 +86,38 @@ class EnvironmentNotificationTest &lt; ActiveSupport::TestCase @@ -83,6 +86,38 @@ class EnvironmentNotificationTest &lt; ActiveSupport::TestCase
83 assert !notifications.include?(@information_notification) 86 assert !notifications.include?(@information_notification)
84 end 87 end
85 88
  89 + should 'get notifications configured to be displayed on profile' do
  90 + community = fast_create(Community)
  91 +
  92 + EnvironmentNotificationPlugin::EnvironmentNotification.destroy_all
  93 + env_home_notification = create_notification(@env, true)
  94 + env_not_home_notification = create_notification(@env, false)
  95 + profile_not_home_notification = create_notification(community, false)
  96 + profile_home_notification = create_notification(community, true)
  97 +
  98 + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(community, @user, "profile")
  99 + assert_equivalent notifications.to_a, [env_not_home_notification, profile_not_home_notification, profile_home_notification]
  100 +
  101 + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(community, @user, "profile_but_bot_homepage")
  102 + assert_equivalent notifications.to_a, [env_not_home_notification, profile_not_home_notification]
  103 + end
  104 +
  105 + should 'get notifications configured to be displayed on environment' do
  106 + community = fast_create(Community)
  107 +
  108 + EnvironmentNotificationPlugin::EnvironmentNotification.destroy_all
  109 + env_home_notification = create_notification(@env, true)
  110 + env_not_home_notification = create_notification(@env, false)
  111 + profile_not_home_notification = create_notification(community, false)
  112 + profile_home_notification = create_notification(community, true)
  113 +
  114 + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, "home")
  115 + assert_equivalent notifications.to_a, [env_home_notification, env_not_home_notification]
  116 +
  117 + notifications = EnvironmentNotificationPlugin::EnvironmentNotification.visibles(@env, @user, "not_home_not_profile")
  118 + assert_equivalent notifications.to_a, [env_not_home_notification]
  119 + end
  120 +
86 should 'get only notifications configured to be displayed to all users and in all pages and not closed by an user' do 121 should 'get only notifications configured to be displayed to all users and in all pages and not closed by an user' do
87 @information_notification.display_to_all_users = true 122 @information_notification.display_to_all_users = true
88 @information_notification.save! 123 @information_notification.save!