##// END OF EJS Templates
Fixed that viewing/editing a wiki page without WikiContent raises an error (#14986)....
Jean-Philippe Lang -
r11990:5f747faa58c0
parent child
Show More
@@ -62,7 +62,12 class WikiController < ApplicationController
62
62
63 # display a page (in editing mode if it doesn't exist)
63 # display a page (in editing mode if it doesn't exist)
64 def show
64 def show
65 if @page.new_record?
65 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
66 deny_access
67 return
68 end
69 @content = @page.content_for_version(params[:version])
70 if @content.nil?
66 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
71 if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? && !api_request?
67 edit
72 edit
68 render :action => 'edit'
73 render :action => 'edit'
@@ -71,11 +76,6 class WikiController < ApplicationController
71 end
76 end
72 return
77 return
73 end
78 end
74 if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project)
75 deny_access
76 return
77 end
78 @content = @page.content_for_version(params[:version])
79 if User.current.allowed_to?(:export_wiki_pages, @project)
79 if User.current.allowed_to?(:export_wiki_pages, @project)
80 if params[:format] == 'pdf'
80 if params[:format] == 'pdf'
81 send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
81 send_data(wiki_page_to_pdf(@page, @project), :type => 'application/pdf', :filename => "#{@page.title}.pdf")
@@ -104,19 +104,19 class WikiController < ApplicationController
104 def edit
104 def edit
105 return render_403 unless editable?
105 return render_403 unless editable?
106 if @page.new_record?
106 if @page.new_record?
107 @page.content = WikiContent.new(:page => @page)
108 if params[:parent].present?
107 if params[:parent].present?
109 @page.parent = @page.wiki.find_page(params[:parent].to_s)
108 @page.parent = @page.wiki.find_page(params[:parent].to_s)
110 end
109 end
111 end
110 end
112
111
113 @content = @page.content_for_version(params[:version])
112 @content = @page.content_for_version(params[:version])
113 @content ||= WikiContent.new(:page => @page)
114 @content.text = initial_page_content(@page) if @content.text.blank?
114 @content.text = initial_page_content(@page) if @content.text.blank?
115 # don't keep previous comment
115 # don't keep previous comment
116 @content.comments = nil
116 @content.comments = nil
117
117
118 # To prevent StaleObjectError exception when reverting to a previous version
118 # To prevent StaleObjectError exception when reverting to a previous version
119 @content.version = @page.content.version
119 @content.version = @page.content.version if @page.content
120
120
121 @text = @content.text
121 @text = @content.text
122 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
122 if params[:section].present? && Redmine::WikiFormatting.supports_section_edit?
@@ -130,10 +130,9 class WikiController < ApplicationController
130 def update
130 def update
131 return render_403 unless editable?
131 return render_403 unless editable?
132 was_new_page = @page.new_record?
132 was_new_page = @page.new_record?
133 @page.content = WikiContent.new(:page => @page) if @page.new_record?
134 @page.safe_attributes = params[:wiki_page]
133 @page.safe_attributes = params[:wiki_page]
135
134
136 @content = @page.content
135 @content = @page.content || WikiContent.new(:page => @page)
137 content_params = params[:content]
136 content_params = params[:content]
138 if content_params.nil? && params[:wiki_page].is_a?(Hash)
137 if content_params.nil? && params[:wiki_page].is_a?(Hash)
139 content_params = params[:wiki_page].slice(:text, :comments, :version)
138 content_params = params[:wiki_page].slice(:text, :comments, :version)
@@ -152,7 +151,7 class WikiController < ApplicationController
152 end
151 end
153 @content.author = User.current
152 @content.author = User.current
154
153
155 if @page.save_with_content
154 if @page.save_with_content(@content)
156 attachments = Attachment.attach_files(@page, params[:attachments])
155 attachments = Attachment.attach_files(@page, params[:attachments])
157 render_attachment_warning_if_needed(@page)
156 render_attachment_warning_if_needed(@page)
158 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
157 call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page})
@@ -175,9 +175,10 class WikiPage < ActiveRecord::Base
175 end
175 end
176
176
177 # Saves the page and its content if text was changed
177 # Saves the page and its content if text was changed
178 def save_with_content
178 def save_with_content(content)
179 ret = nil
179 ret = nil
180 transaction do
180 transaction do
181 self.content = content
181 if new_record?
182 if new_record?
182 # Rails automatically saves associated content
183 # Rails automatically saves associated content
183 ret = save
184 ret = save
@@ -177,6 +177,16 class WikiControllerTest < ActionController::TestCase
177 assert_response 302
177 assert_response 302
178 end
178 end
179
179
180 def test_show_page_without_content_should_display_the_edit_form
181 @request.session[:user_id] = 2
182 WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
183
184 get :show, :project_id => 1, :id => 'NoContent'
185 assert_response :success
186 assert_template 'edit'
187 assert_select 'textarea[name=?]', 'content[text]'
188 end
189
180 def test_create_page
190 def test_create_page
181 @request.session[:user_id] = 2
191 @request.session[:user_id] = 2
182 assert_difference 'WikiPage.count' do
192 assert_difference 'WikiPage.count' do
@@ -412,6 +422,19 class WikiControllerTest < ActionController::TestCase
412 assert_equal 2, c.version
422 assert_equal 2, c.version
413 end
423 end
414
424
425 def test_update_page_without_content_should_create_content
426 @request.session[:user_id] = 2
427 page = WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
428
429 assert_no_difference 'WikiPage.count' do
430 assert_difference 'WikiContent.count' do
431 put :update, :project_id => 1, :id => 'NoContent', :content => {:text => 'Some content'}
432 assert_response 302
433 end
434 end
435 assert_equal 'Some content', page.reload.content.text
436 end
437
415 def test_update_section
438 def test_update_section
416 @request.session[:user_id] = 2
439 @request.session[:user_id] = 2
417 page = WikiPage.find_by_title('Page_with_sections')
440 page = WikiPage.find_by_title('Page_with_sections')
General Comments 0
You need to be logged in to leave comments. Login now