news_test.rb 3.24 KB
Newer Older
1
# frozen_string_literal: true
2

3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
5 6 7 8 9
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
10
#
11 12 13 14
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
15
#
16 17 18 19
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

20
require File.expand_path('../../test_helper', __FILE__)
21

22
class NewsTest < ActiveSupport::TestCase
jplang's avatar
jplang committed
23
  fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles, :enabled_modules, :news
24

25
  def valid_news
jplang's avatar
jplang committed
26
    { :title => 'Test news', :description => 'Lorem ipsum etc', :author => User.first }
27 28
  end

29
  def setup
30
    User.current = nil
31
  end
32

33 34
  def test_create_should_send_email_notification
    ActionMailer::Base.deliveries.clear
jplang's avatar
jplang committed
35
    news = Project.find(1).news.new(valid_news)
36

37 38 39
    with_settings :notified_events => %w(news_added) do
      assert news.save
    end
40
    assert_equal 2, ActionMailer::Base.deliveries.size
41
  end
42

43 44 45 46 47 48 49
  def test_should_include_news_for_projects_with_news_enabled
    project = projects(:projects_001)
    assert project.enabled_modules.any?{ |em| em.name == 'news' }

    # News.latest should return news from projects_001
    assert News.latest.any? { |news| news.project == project }
  end
50

51
  def test_should_not_include_news_for_projects_with_news_disabled
52
    EnabledModule.where(["project_id = ? AND name = ?", 2, 'news']).delete_all
53
    project = Project.find(2)
54 55

    # Add a piece of news to the project
56
    news = project.news.create(valid_news)
57 58 59 60

    # News.latest should not return that new piece of news
    assert News.latest.include?(news) == false
  end
61

62
  def test_should_only_include_news_from_projects_visibly_to_the_user
63
    assert News.latest(User.anonymous).all? { |news| news.project.is_public? }
64
  end
65

66 67
  def test_should_limit_the_amount_of_returned_news
    # Make sure we have a bunch of news stories
68
    10.times { projects(:projects_001).news.create(valid_news) }
69 70 71 72 73 74
    assert_equal 2, News.latest(users(:users_002), 2).size
    assert_equal 6, News.latest(users(:users_002), 6).size
  end

  def test_should_return_5_news_stories_by_default
    # Make sure we have a bunch of news stories
75
    10.times { projects(:projects_001).news.create(valid_news) }
76 77
    assert_equal 5, News.latest(users(:users_004)).size
  end
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

  def test_attachments_should_be_visible
    assert News.find(1).attachments_visible?(User.anonymous)
  end

  def test_attachments_should_be_deletable_with_manage_news_permission
    manager = User.find(2)
    assert News.find(1).attachments_deletable?(manager)
  end

  def test_attachments_should_not_be_deletable_without_manage_news_permission
    manager = User.find(2)
    Role.find_by_name('Manager').remove_permission!(:manage_news)
    assert !News.find(1).attachments_deletable?(manager)
  end
93
end