2019-03-16 15:03:47 +00:00
|
|
|
# frozen_string_literal: true
|
2019-03-15 01:32:57 +00:00
|
|
|
|
2011-08-30 16:03:18 +00:00
|
|
|
# Redmine - project management software
|
2022-01-02 05:29:10 +00:00
|
|
|
# Copyright (C) 2006-2022 Jean-Philippe Lang
|
2007-03-17 15:18:50 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
2011-08-30 16:03:18 +00:00
|
|
|
#
|
2007-03-17 15:18:50 +00:00
|
|
|
# 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.
|
2011-08-30 16:03:18 +00:00
|
|
|
#
|
2007-03-17 15:18:50 +00:00
|
|
|
# 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.
|
|
|
|
|
|
2010-12-12 23:24:34 +00:00
|
|
|
require File.expand_path('../../test_helper', __FILE__)
|
2007-03-17 15:18:50 +00:00
|
|
|
|
2016-07-14 09:35:49 +00:00
|
|
|
class NewsControllerTest < Redmine::ControllerTest
|
2015-01-17 14:14:12 +00:00
|
|
|
fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
|
2014-09-11 17:19:42 +00:00
|
|
|
:enabled_modules, :news, :comments,
|
2019-01-09 14:26:56 +00:00
|
|
|
:attachments, :user_preferences
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2007-03-17 15:18:50 +00:00
|
|
|
def setup
|
2007-11-05 22:22:51 +00:00
|
|
|
User.current = nil
|
2007-03-17 15:18:50 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2007-11-05 22:22:51 +00:00
|
|
|
def test_index
|
|
|
|
|
get :index
|
2007-03-17 15:18:50 +00:00
|
|
|
assert_response :success
|
2016-07-19 20:06:01 +00:00
|
|
|
assert_select 'h3 a', :text => 'eCookbook first release !'
|
2007-03-17 15:18:50 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2007-11-05 22:22:51 +00:00
|
|
|
def test_index_with_project
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:index, :params => {:project_id => 1})
|
2007-03-17 15:18:50 +00:00
|
|
|
assert_response :success
|
2016-07-19 20:06:01 +00:00
|
|
|
assert_select 'h3 a', :text => 'eCookbook first release !'
|
2007-03-17 15:18:50 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2012-03-10 13:47:28 +00:00
|
|
|
def test_index_with_invalid_project_should_respond_with_404
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:index, :params => {:project_id => 999})
|
2012-03-10 13:47:28 +00:00
|
|
|
assert_response 404
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-26 19:49:02 +00:00
|
|
|
def test_index_without_permission_should_fail
|
|
|
|
|
Role.all.each {|r| r.remove_permission! :view_news}
|
|
|
|
|
@request.session[:user_id] = 2
|
|
|
|
|
|
|
|
|
|
get :index
|
|
|
|
|
assert_response 403
|
|
|
|
|
end
|
|
|
|
|
|
2021-05-29 06:44:54 +00:00
|
|
|
def test_index_without_manage_news_permission_should_not_display_add_news_link
|
|
|
|
|
user = User.find(2)
|
|
|
|
|
@request.session[:user_id] = user.id
|
|
|
|
|
Role.all.each {|r| r.remove_permission! :manage_news}
|
|
|
|
|
get :index
|
|
|
|
|
assert_select '.add-news-link', count: 0
|
|
|
|
|
|
|
|
|
|
user.members.first.roles.first.add_permission! :manage_news
|
|
|
|
|
get :index
|
|
|
|
|
assert_select '.add-news-link', count: 1
|
|
|
|
|
end
|
|
|
|
|
|
2008-03-17 20:48:22 +00:00
|
|
|
def test_show
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:show, :params => {:id => 1})
|
2008-03-17 20:48:22 +00:00
|
|
|
assert_response :success
|
2016-07-19 20:06:01 +00:00
|
|
|
assert_select 'h2', :text => 'eCookbook first release !'
|
2008-03-17 20:48:22 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2012-01-29 22:39:06 +00:00
|
|
|
def test_show_should_show_attachments
|
|
|
|
|
attachment = Attachment.first
|
|
|
|
|
attachment.container = News.find(1)
|
|
|
|
|
attachment.save!
|
|
|
|
|
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:show, :params => {:id => 1})
|
2012-01-29 22:39:06 +00:00
|
|
|
assert_response :success
|
2014-11-22 09:38:21 +00:00
|
|
|
assert_select 'a', :text => attachment.filename
|
2012-01-29 22:39:06 +00:00
|
|
|
end
|
|
|
|
|
|
2014-11-14 20:53:14 +00:00
|
|
|
def test_show_with_comments_in_reverse_order
|
|
|
|
|
user = User.find(1)
|
|
|
|
|
user.pref[:comments_sorting] = 'desc'
|
|
|
|
|
user.pref.save!
|
|
|
|
|
|
|
|
|
|
@request.session[:user_id] = 1
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:show, :params => {:id => 1})
|
2014-11-14 20:53:14 +00:00
|
|
|
assert_response :success
|
2016-07-19 20:06:01 +00:00
|
|
|
|
|
|
|
|
comments = css_select('#comments .wiki').map(&:text).map(&:strip)
|
|
|
|
|
assert_equal ["This is an other comment", "my first comment"], comments
|
2014-11-14 20:53:14 +00:00
|
|
|
end
|
|
|
|
|
|
2008-03-17 20:48:22 +00:00
|
|
|
def test_show_not_found
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:show, :params => {:id => 999})
|
2008-03-17 20:48:22 +00:00
|
|
|
assert_response 404
|
|
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2021-03-25 05:16:54 +00:00
|
|
|
def test_get_new_with_project_id
|
2008-03-17 20:48:22 +00:00
|
|
|
@request.session[:user_id] = 2
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:new, :params => {:project_id => 1})
|
2008-03-17 20:48:22 +00:00
|
|
|
assert_response :success
|
2021-03-25 05:16:54 +00:00
|
|
|
assert_select 'select[name=project_id]', false
|
2016-07-19 20:06:01 +00:00
|
|
|
assert_select 'input[name=?]', 'news[title]'
|
2008-03-17 20:48:22 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2021-03-25 05:16:54 +00:00
|
|
|
def test_get_new_without_project_id
|
|
|
|
|
@request.session[:user_id] = 2
|
|
|
|
|
get(:new)
|
|
|
|
|
assert_response :success
|
|
|
|
|
assert_select 'select[name=project_id]'
|
|
|
|
|
assert_select 'input[name=?]', 'news[title]'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_get_new_if_user_does_not_have_permission
|
|
|
|
|
@request.session[:user_id] = 2
|
|
|
|
|
User.find(2).roles.each{|u| u.remove_permission! :manage_news }
|
|
|
|
|
|
|
|
|
|
get(:new)
|
|
|
|
|
assert_response :forbidden
|
|
|
|
|
assert_select 'select[name=project_id]', false
|
|
|
|
|
assert_select 'input[name=?]', 'news[title]', count: 0
|
|
|
|
|
end
|
|
|
|
|
|
2010-09-20 15:13:48 +00:00
|
|
|
def test_post_create
|
2009-03-28 00:38:57 +00:00
|
|
|
ActionMailer::Base.deliveries.clear
|
2008-03-17 20:48:22 +00:00
|
|
|
@request.session[:user_id] = 2
|
2012-07-08 12:23:47 +00:00
|
|
|
|
|
|
|
|
with_settings :notified_events => %w(news_added) do
|
2019-11-24 05:25:12 +00:00
|
|
|
post(
|
|
|
|
|
:create,
|
|
|
|
|
:params => {
|
2017-05-31 17:32:34 +00:00
|
|
|
:project_id => 1,
|
|
|
|
|
:news => {
|
|
|
|
|
:title => 'NewsControllerTest',
|
|
|
|
|
:description => 'This is the description',
|
2019-06-06 14:53:23 +00:00
|
|
|
:summary => ''
|
2017-05-31 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-24 05:25:12 +00:00
|
|
|
)
|
2012-07-08 12:23:47 +00:00
|
|
|
end
|
2010-11-14 16:45:32 +00:00
|
|
|
assert_redirected_to '/projects/ecookbook/news'
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2008-03-17 20:48:22 +00:00
|
|
|
news = News.find_by_title('NewsControllerTest')
|
|
|
|
|
assert_not_nil news
|
|
|
|
|
assert_equal 'This is the description', news.description
|
|
|
|
|
assert_equal User.find(2), news.author
|
|
|
|
|
assert_equal Project.find(1), news.project
|
2018-10-06 13:08:52 +00:00
|
|
|
assert_equal 2, ActionMailer::Base.deliveries.size
|
2008-03-17 20:48:22 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2021-03-25 05:16:54 +00:00
|
|
|
def test_post_create_with_cross_project_param
|
|
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
|
@request.session[:user_id] = 2
|
|
|
|
|
|
|
|
|
|
with_settings :notified_events => %w(news_added) do
|
|
|
|
|
post(
|
|
|
|
|
:create,
|
|
|
|
|
:params => {
|
|
|
|
|
:project_id => 1,
|
|
|
|
|
:cross_project => '1',
|
|
|
|
|
:news => {
|
|
|
|
|
:title => 'NewsControllerTest',
|
|
|
|
|
:description => 'This is the description',
|
|
|
|
|
:summary => ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
assert_redirected_to '/news'
|
|
|
|
|
|
|
|
|
|
news = News.find_by(title: 'NewsControllerTest')
|
|
|
|
|
assert_not_nil news
|
|
|
|
|
assert_equal 'This is the description', news.description
|
|
|
|
|
assert_equal User.find(2), news.author
|
|
|
|
|
assert_equal Project.find(1), news.project
|
|
|
|
|
assert_equal 2, ActionMailer::Base.deliveries.size
|
|
|
|
|
end
|
|
|
|
|
|
2012-01-29 22:39:06 +00:00
|
|
|
def test_post_create_with_attachment
|
|
|
|
|
set_tmp_attachments_directory
|
2020-02-24 03:19:00 +00:00
|
|
|
ActionMailer::Base.deliveries.clear
|
2012-01-29 22:39:06 +00:00
|
|
|
@request.session[:user_id] = 2
|
|
|
|
|
assert_difference 'News.count' do
|
|
|
|
|
assert_difference 'Attachment.count' do
|
2020-02-24 03:19:00 +00:00
|
|
|
with_settings :notified_events => %w(news_added) do
|
|
|
|
|
post(
|
|
|
|
|
:create,
|
|
|
|
|
:params => {
|
|
|
|
|
:project_id => 1,
|
|
|
|
|
:news => {
|
|
|
|
|
:title => 'Test',
|
|
|
|
|
:description => 'This is the description'
|
|
|
|
|
},
|
|
|
|
|
:attachments => {
|
|
|
|
|
'1' => {
|
|
|
|
|
'file' => uploaded_test_file('testfile.txt', 'text/plain')
|
|
|
|
|
}
|
2019-11-24 05:25:12 +00:00
|
|
|
}
|
2017-05-31 17:32:34 +00:00
|
|
|
}
|
2020-02-24 03:19:00 +00:00
|
|
|
)
|
|
|
|
|
end
|
2012-01-29 22:39:06 +00:00
|
|
|
end
|
|
|
|
|
end
|
2014-01-08 06:26:25 +00:00
|
|
|
attachment = Attachment.order('id DESC').first
|
|
|
|
|
news = News.order('id DESC').first
|
2012-01-29 22:39:06 +00:00
|
|
|
assert_equal news, attachment.container
|
2020-02-24 03:19:00 +00:00
|
|
|
assert_select_email do
|
|
|
|
|
# link to the attachments download
|
|
|
|
|
assert_select 'fieldset.attachments' do
|
|
|
|
|
assert_select 'a[href=?]',
|
|
|
|
|
"http://localhost:3000/attachments/download/#{attachment.id}/testfile.txt",
|
|
|
|
|
:text => 'testfile.txt'
|
|
|
|
|
end
|
|
|
|
|
end
|
2012-01-29 22:39:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def test_post_create_with_validation_failure
|
|
|
|
|
@request.session[:user_id] = 2
|
2019-11-24 05:25:12 +00:00
|
|
|
post(
|
|
|
|
|
:create,
|
|
|
|
|
:params => {
|
2017-05-31 17:32:34 +00:00
|
|
|
:project_id => 1,
|
|
|
|
|
:news => {
|
|
|
|
|
:title => '',
|
|
|
|
|
:description => 'This is the description',
|
2019-06-06 14:53:23 +00:00
|
|
|
:summary => ''
|
2017-05-31 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-24 05:25:12 +00:00
|
|
|
)
|
2012-01-29 22:39:06 +00:00
|
|
|
assert_response :success
|
2014-12-21 12:39:48 +00:00
|
|
|
assert_select_error /title cannot be blank/i
|
2012-01-29 22:39:06 +00:00
|
|
|
end
|
|
|
|
|
|
2008-03-17 20:48:22 +00:00
|
|
|
def test_get_edit
|
|
|
|
|
@request.session[:user_id] = 2
|
2019-11-24 05:25:12 +00:00
|
|
|
get(:edit, :params => {:id => 1})
|
2008-03-17 20:48:22 +00:00
|
|
|
assert_response :success
|
2016-07-19 20:06:01 +00:00
|
|
|
assert_select 'input[name=?][value=?]', 'news[title]', 'eCookbook first release !'
|
2008-03-17 20:48:22 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2010-09-21 15:20:37 +00:00
|
|
|
def test_put_update
|
2008-03-17 20:48:22 +00:00
|
|
|
@request.session[:user_id] = 2
|
2019-11-24 05:25:12 +00:00
|
|
|
put(
|
|
|
|
|
:update,
|
|
|
|
|
:params => {
|
2017-05-31 17:32:34 +00:00
|
|
|
:id => 1,
|
|
|
|
|
:news => {
|
2019-06-06 14:53:23 +00:00
|
|
|
:description => 'Description changed by test_post_edit'
|
2017-05-31 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-24 05:25:12 +00:00
|
|
|
)
|
2010-11-14 16:45:32 +00:00
|
|
|
assert_redirected_to '/news/1'
|
2008-03-17 20:48:22 +00:00
|
|
|
news = News.find(1)
|
|
|
|
|
assert_equal 'Description changed by test_post_edit', news.description
|
|
|
|
|
end
|
|
|
|
|
|
2012-01-29 22:39:06 +00:00
|
|
|
def test_put_update_with_attachment
|
|
|
|
|
set_tmp_attachments_directory
|
2008-03-17 20:48:22 +00:00
|
|
|
@request.session[:user_id] = 2
|
2012-01-29 22:39:06 +00:00
|
|
|
assert_no_difference 'News.count' do
|
|
|
|
|
assert_difference 'Attachment.count' do
|
2019-11-24 05:25:12 +00:00
|
|
|
put(
|
|
|
|
|
:update,
|
|
|
|
|
:params => {
|
2017-05-31 17:32:34 +00:00
|
|
|
:id => 1,
|
|
|
|
|
:news => {
|
2019-06-06 14:53:23 +00:00
|
|
|
:description => 'This is the description'
|
|
|
|
|
},
|
2017-05-31 17:32:34 +00:00
|
|
|
:attachments => {
|
|
|
|
|
'1' => {
|
2019-11-24 05:25:12 +00:00
|
|
|
'file' => uploaded_test_file('testfile.txt', 'text/plain')
|
|
|
|
|
}
|
2017-05-31 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-24 05:25:12 +00:00
|
|
|
)
|
2012-01-29 22:39:06 +00:00
|
|
|
end
|
|
|
|
|
end
|
2014-01-08 06:23:43 +00:00
|
|
|
attachment = Attachment.order('id DESC').first
|
2012-01-29 22:39:06 +00:00
|
|
|
assert_equal News.find(1), attachment.container
|
2008-03-17 20:48:22 +00:00
|
|
|
end
|
2011-08-30 16:03:18 +00:00
|
|
|
|
2012-03-02 11:43:33 +00:00
|
|
|
def test_update_with_failure
|
|
|
|
|
@request.session[:user_id] = 2
|
2019-11-24 05:25:12 +00:00
|
|
|
put(
|
|
|
|
|
:update,
|
|
|
|
|
:params => {
|
2017-05-31 17:32:34 +00:00
|
|
|
:id => 1,
|
|
|
|
|
:news => {
|
2019-06-06 14:53:23 +00:00
|
|
|
:description => ''
|
2017-05-31 17:32:34 +00:00
|
|
|
}
|
|
|
|
|
}
|
2019-11-24 05:25:12 +00:00
|
|
|
)
|
2012-03-02 11:43:33 +00:00
|
|
|
assert_response :success
|
2014-12-21 12:39:48 +00:00
|
|
|
assert_select_error /description cannot be blank/i
|
2012-03-02 11:43:33 +00:00
|
|
|
end
|
|
|
|
|
|
2008-03-17 20:48:22 +00:00
|
|
|
def test_destroy
|
|
|
|
|
@request.session[:user_id] = 2
|
2019-11-24 05:25:12 +00:00
|
|
|
delete(:destroy, :params => {:id => 1})
|
2010-11-14 16:45:32 +00:00
|
|
|
assert_redirected_to '/projects/ecookbook/news'
|
2020-04-04 00:43:28 +00:00
|
|
|
assert_equal 'Successful deletion.', flash[:notice]
|
2008-03-17 20:48:22 +00:00
|
|
|
assert_nil News.find_by_id(1)
|
|
|
|
|
end
|
2007-03-17 15:18:50 +00:00
|
|
|
end
|