feature-33167.patch

Mizuki ISHIKAWA, 2020-04-20 10:41

Download (7.93 KB)

View differences:

app/controllers/news_controller.rb
22 22
  model_object News
23 23
  before_action :find_model_object, :except => [:new, :create, :index]
24 24
  before_action :find_project_from_association, :except => [:new, :create, :index]
25
  before_action :find_project_by_project_id, :only => [:new, :create]
26
  before_action :authorize, :except => [:index]
27
  before_action :find_optional_project, :only => :index
25
  before_action :find_project_by_project_id, :only => :create
26
  before_action :authorize, :except => [:index, :new]
27
  before_action :find_optional_project, :only => [:index, :new]
28 28
  accept_rss_auth :index
29 29
  accept_api_auth :index, :show, :create, :update, :destroy
30 30

  
......
65 65
  end
66 66

  
67 67
  def new
68
    unless User.current.allowed_to?(:manage_news, @project, :global => true)
69
      raise ::Unauthorized
70
    end
68 71
    @news = News.new(:project => @project, :author => User.current)
69 72
  end
70 73

  
......
77 80
        format.html {
78 81
          render_attachment_warning_if_needed(@news)
79 82
          flash[:notice] = l(:notice_successful_create)
80
          redirect_to project_news_index_path(@project)
83
          redirect_to params[:cross_project] ? news_index_path : project_news_index_path(@project)
81 84
        }
82 85
        format.api  { render_api_ok }
83 86
      end
app/views/news/_form.html.erb
1 1
<%= error_messages_for @news %>
2 2

  
3 3
<div class="box tabular">
4
<% if @project.nil? %>
5
  <p>
6
    <label><%= l(:field_project) %> <span class="required">*</span></label>
7
    <%= select_tag :project_id, options_for_select(project_tree_options_for_select(Project.allowed_to(:manage_news).to_a), Project.allowed_to(:manage_news).first), {:required => true} %>
8
    <%= hidden_field_tag :cross_project, 1, id: nil %>
9
  </p>
10
<% end %>
4 11
<p><%= f.text_field :title, :required => true, :size => 60 %></p>
5 12
<p><%= f.text_area :summary, :cols => 60, :rows => 2 %></p>
6 13
<p><%= f.text_area :description, :required => true, :cols => 60, :rows => 15, :class => 'wiki-edit',
app/views/news/index.html.erb
1 1
<div class="contextual">
2 2
<%= link_to(l(:label_news_new),
3
            new_project_news_path(@project),
3
            (@project ? project_news_index_path(@project) : news_index_path),
4 4
            :class => 'icon icon-add',
5
            :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') if @project && User.current.allowed_to?(:manage_news, @project) %>
5
            :onclick => 'showAndScrollTo("add-news", "news_title"); return false;') if (@project.nil? || (@project && User.current.allowed_to?(:manage_news, @project))) %>
6 6
<%= watcher_link(@project.enabled_module('news'), User.current) if @project && User.current.logged? %>
7 7
</div>
8 8

  
9 9
<div id="add-news" style="display:none;">
10 10
<h2><%=l(:label_news_new)%></h2>
11
<%= labelled_form_for @news, :url => project_news_index_path(@project),
11
<%= labelled_form_for @news, :url => (@project ? project_news_index_path(@project) : news_index_path),
12 12
                                           :html => { :id => 'news-form', :multipart => true } do |f| %>
13 13
<%= render :partial => 'news/form', :locals => { :f => f } %>
14 14
<%= submit_tag l(:button_create) %>
15 15
<%= link_to l(:button_cancel), "#", :onclick => '$("#add-news").hide()' %>
16
<% end if @project %>
16
<% end %>
17 17
</div>
18 18

  
19 19
<h2><%=l(:label_news_plural)%></h2>
app/views/news/new.html.erb
1 1
<h2><%=l(:label_news_new)%></h2>
2 2

  
3
<%= labelled_form_for @news, :url => project_news_index_path(@project),
3
<%= labelled_form_for @news, :url => (@project ? project_news_index_path(@project) : news_index_path),
4 4
                                           :html => { :id => 'news-form', :multipart => true } do |f| %>
5 5
  <%= render :partial => 'news/form', :locals => { :f => f } %>
6 6
  <%= submit_tag l(:button_create) %>
config/routes.rb
206 206
  resources :queries, :except => [:show]
207 207
  get '/queries/filter', :to => 'queries#filter', :as => 'queries_filter'
208 208

  
209
  resources :news, :only => [:index, :show, :edit, :update, :destroy]
209
  resources :news, :only => [:index, :show, :edit, :update, :destroy, :create, :new]
210 210
  match '/news/:id/comments', :to => 'comments#create', :via => :post
211 211
  match '/news/:id/comments/:comment_id', :to => 'comments#destroy', :via => :delete
212 212

  
test/functional/news_controller_test.rb
87 87
    assert_response 404
88 88
  end
89 89

  
90
  def test_get_new
90
  def test_get_new_with_project_id
91 91
    @request.session[:user_id] = 2
92 92
    get(:new, :params => {:project_id => 1})
93 93
    assert_response :success
94
    assert_select 'select[name=project_id]', false
94 95
    assert_select 'input[name=?]', 'news[title]'
95 96
  end
96 97

  
98
  def test_get_new_without_project_id
99
    @request.session[:user_id] = 2
100
    get(:new)
101
    assert_response :success
102
    assert_select 'select[name=project_id]'
103
    assert_select 'input[name=?]', 'news[title]'
104
  end
105

  
106
  def test_get_new_if_user_does_not_have_permission
107
    @request.session[:user_id] = 2
108
    User.find(2).roles.each{|u| u.remove_permission! :manage_news }
109

  
110
    get(:new)
111
    assert_response :forbidden
112
    assert_select 'select[name=project_id]', false
113
    assert_select 'input[name=?]', 'news[title]', count: 0
114
  end
115

  
97 116
  def test_post_create
98 117
    ActionMailer::Base.deliveries.clear
99 118
    @request.session[:user_id] = 2
......
121 140
    assert_equal 2, ActionMailer::Base.deliveries.size
122 141
  end
123 142

  
143
  def test_post_create_with_cross_project_param
144
    ActionMailer::Base.deliveries.clear
145
    @request.session[:user_id] = 2
146

  
147
    with_settings :notified_events => %w(news_added) do
148
      post(
149
        :create,
150
        :params => {
151
          :project_id => 1,
152
          :cross_project => '1',
153
          :news => {
154
            :title => 'NewsControllerTest',
155
            :description => 'This is the description',
156
            :summary => ''
157
          }
158
        }
159
      )
160
    end
161
    assert_redirected_to '/news'
162

  
163
    news = News.find_by_title('NewsControllerTest')
164
    assert_not_nil news
165
    assert_equal 'This is the description', news.description
166
    assert_equal User.find(2), news.author
167
    assert_equal Project.find(1), news.project
168
    assert_equal 2, ActionMailer::Base.deliveries.size
169
  end
170

  
124 171
  def test_post_create_with_attachment
125 172
    set_tmp_attachments_directory
126 173
    ActionMailer::Base.deliveries.clear
test/integration/routing/news_test.rb
29 29

  
30 30
  def test_news
31 31
    should_route 'GET /news' => 'news#index'
32
    should_route 'GET /news/new' => 'news#new'
33
    should_route 'POST /news' => 'news#create'
32 34
    should_route 'GET /news.atom' => 'news#index', :format => 'atom'
33 35
    should_route 'GET /news/2' => 'news#show', :id => '2'
34 36
    should_route 'GET /news/2/edit' => 'news#edit', :id => '2'