Project

General

Profile

Defect #1941 ยป latest_news_should_only_return_news_from_projects_with_news_enabled.diff

Jakob Skjerning, 2008-09-24 14:36

View differences:

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

  
18
require File.dirname(__FILE__) + '/../test_helper'
19

  
20
class NewsTest < Test::Unit::TestCase
21
  fixtures :projects, :versions, :users, :roles, :members, :issues, :journals, :journal_details,
22
           :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages
23

  
24
  def setup
25
    @project = Project.find(1)
26
  end
27
  
28
  def test_should_include_news_for_projects_with_news_enabled
29
    project = projects(:projects_001)
30
    assert project.enabled_modules.any?{ |em| em.name == 'news' }
31

  
32
    # News.latest should return news from projects_001
33
    assert News.latest.any? { |news| news.project == project }
34
  end
35
  
36
  def test_should_not_include_news_for_projects_with_news_disabled
37
    # The projects_002 (OnlineStore) doesn't have the news module enabled, use that project for this test
38
    project = projects(:projects_002)
39
    assert ! project.enabled_modules.any?{ |em| em.name == 'news' }
40

  
41
    # Add a piece of news to the project
42
    news = project.news.create(:title => 'Test news', :description => 'This should not be returned by News.latest')
43

  
44
    # News.latest should not return that new piece of news
45
    assert News.latest.include?(news) == false
46
  end
47
  
48
  def test_should_only_include_news_from_projects_visibly_to_the_user
49
    # users_001 has no memberships so can only get news from public project
50
    assert News.latest(users(:users_001)).all? { |news| news.project.is_public? } 
51
  end
52
  
53
  def test_should_limit_the_amount_of_returned_news
54
    # Make sure we have a bunch of news stories
55
    10.times { projects(:projects_001).news.create(:title => 'Test news', :description => 'Lorem ipsum etc') }
56
    assert_equal 2, News.latest(users(:users_002), 2).size
57
    assert_equal 6, News.latest(users(:users_002), 6).size
58
  end
59

  
60
  def test_should_return_5_news_stories_by_default
61
    # Make sure we have a bunch of news stories
62
    10.times { projects(:projects_001).news.create(:title => 'Test news', :description => 'Lorem ipsum etc') }
63
    assert_equal 5, News.latest(users(:users_004)).size
64
  end
65

  
66

  
67
end
app/models/news.rb (working copy)
28 28
  acts_as_event :url => Proc.new {|o| {:controller => 'news', :action => 'show', :id => o.id}}
29 29
  acts_as_activity_provider :find_options => {:include => [:project, :author]}
30 30
  
31
  # returns latest news for projects visible by user
32
  def self.latest(user=nil, count=5)
33
    find(:all, :limit => count, :conditions => Project.visible_by(user), :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")	
31
  # Returns latest news for projects that have the news module enabled and are visible by user
32
  def self.latest(user=nil, count=5)    
33
    result = find(:all, :limit => count, :conditions => Project.visible_by(user), :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")	
34
    
35
    # Only returns news from projects that have the news module enabled
36
    result.select { |news| news.project.enabled_modules.any? { |em| em.name == 'news' } }
34 37
  end
35 38
end
    (1-1/1)