@@ -1,276 +1,274 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require 'diff' |
|
19 | 19 | |
|
20 | 20 | # The WikiController follows the Rails REST controller pattern but with |
|
21 | 21 | # a few differences |
|
22 | 22 | # |
|
23 | 23 | # * index - shows a list of WikiPages grouped by page or date |
|
24 | 24 | # * new - not used |
|
25 | 25 | # * create - not used |
|
26 | 26 | # * show - will also show the form for creating a new wiki page |
|
27 | 27 | # * edit - used to edit an existing or new page |
|
28 | 28 | # * update - used to save a wiki page update to the database, including new pages |
|
29 | 29 | # * destroy - normal |
|
30 | 30 | # |
|
31 | 31 | # Other member and collection methods are also used |
|
32 | 32 | # |
|
33 | 33 | # TODO: still being worked on |
|
34 | 34 | class WikiController < ApplicationController |
|
35 | 35 | default_search_scope :wiki_pages |
|
36 | 36 | before_filter :find_wiki, :authorize |
|
37 | 37 | before_filter :find_existing_page, :only => [:rename, :protect, :history, :diff, :annotate, :add_attachment, :destroy] |
|
38 | 38 | |
|
39 | 39 | verify :method => :post, :only => [:protect], :redirect_to => { :action => :show } |
|
40 | 40 | |
|
41 | 41 | helper :attachments |
|
42 | 42 | include AttachmentsHelper |
|
43 | 43 | helper :watchers |
|
44 | 44 | |
|
45 | 45 | # List of pages, sorted alphabetically and by parent (hierarchy) |
|
46 | 46 | def index |
|
47 | 47 | load_pages_for_index |
|
48 | 48 | @pages_by_parent_id = @pages.group_by(&:parent_id) |
|
49 | 49 | end |
|
50 | 50 | |
|
51 | 51 | # List of page, by last update |
|
52 | 52 | def date_index |
|
53 | 53 | load_pages_for_index |
|
54 | 54 | @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | # display a page (in editing mode if it doesn't exist) |
|
58 | 58 | def show |
|
59 | 59 | page_title = params[:id] |
|
60 | 60 | @page = @wiki.find_or_new_page(page_title) |
|
61 | 61 | if @page.new_record? |
|
62 | 62 | if User.current.allowed_to?(:edit_wiki_pages, @project) && editable? |
|
63 | 63 | edit |
|
64 | 64 | render :action => 'edit' |
|
65 | 65 | else |
|
66 | 66 | render_404 |
|
67 | 67 | end |
|
68 | 68 | return |
|
69 | 69 | end |
|
70 | 70 | if params[:version] && !User.current.allowed_to?(:view_wiki_edits, @project) |
|
71 | 71 | # Redirects user to the current version if he's not allowed to view previous versions |
|
72 | 72 | redirect_to :version => nil |
|
73 | 73 | return |
|
74 | 74 | end |
|
75 | 75 | @content = @page.content_for_version(params[:version]) |
|
76 | 76 | if User.current.allowed_to?(:export_wiki_pages, @project) |
|
77 | 77 | if params[:format] == 'html' |
|
78 | 78 | export = render_to_string :action => 'export', :layout => false |
|
79 | 79 | send_data(export, :type => 'text/html', :filename => "#{@page.title}.html") |
|
80 | 80 | return |
|
81 | 81 | elsif params[:format] == 'txt' |
|
82 | 82 | send_data(@content.text, :type => 'text/plain', :filename => "#{@page.title}.txt") |
|
83 | 83 | return |
|
84 | 84 | end |
|
85 | 85 | end |
|
86 | 86 | @editable = editable? |
|
87 | 87 | render :action => 'show' |
|
88 | 88 | end |
|
89 | 89 | |
|
90 | 90 | # edit an existing page or a new one |
|
91 | 91 | def edit |
|
92 | 92 | @page = @wiki.find_or_new_page(params[:id]) |
|
93 | 93 | return render_403 unless editable? |
|
94 | 94 | @page.content = WikiContent.new(:page => @page) if @page.new_record? |
|
95 | 95 | |
|
96 | 96 | @content = @page.content_for_version(params[:version]) |
|
97 | 97 | @content.text = initial_page_content(@page) if @content.text.blank? |
|
98 | 98 | # don't keep previous comment |
|
99 | 99 | @content.comments = nil |
|
100 | 100 | |
|
101 | 101 | # To prevent StaleObjectError exception when reverting to a previous version |
|
102 | 102 | @content.version = @page.content.version |
|
103 | rescue ActiveRecord::StaleObjectError | |
|
104 | # Optimistic locking exception | |
|
105 | flash[:error] = l(:notice_locking_conflict) | |
|
106 | 103 | end |
|
107 | 104 | |
|
108 | 105 | verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed } |
|
109 | 106 | # Creates a new page or updates an existing one |
|
110 | 107 | def update |
|
111 | 108 | @page = @wiki.find_or_new_page(params[:id]) |
|
112 | 109 | return render_403 unless editable? |
|
113 | 110 | @page.content = WikiContent.new(:page => @page) if @page.new_record? |
|
114 | 111 | |
|
115 | 112 | @content = @page.content_for_version(params[:version]) |
|
116 | 113 | @content.text = initial_page_content(@page) if @content.text.blank? |
|
117 | 114 | # don't keep previous comment |
|
118 | 115 | @content.comments = nil |
|
119 | 116 | |
|
120 | 117 | if !@page.new_record? && params[:content].present? && @content.text == params[:content][:text] |
|
121 | 118 | attachments = Attachment.attach_files(@page, params[:attachments]) |
|
122 | 119 | render_attachment_warning_if_needed(@page) |
|
123 | 120 | # don't save if text wasn't changed |
|
124 | 121 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
125 | 122 | return |
|
126 | 123 | end |
|
127 | 124 | @content.attributes = params[:content] |
|
128 | 125 | @content.author = User.current |
|
129 | 126 | # if page is new @page.save will also save content, but not if page isn't a new record |
|
130 | 127 | if (@page.new_record? ? @page.save : @content.save) |
|
131 | 128 | attachments = Attachment.attach_files(@page, params[:attachments]) |
|
132 | 129 | render_attachment_warning_if_needed(@page) |
|
133 | 130 | call_hook(:controller_wiki_edit_after_save, { :params => params, :page => @page}) |
|
134 | 131 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
135 | 132 | else |
|
136 | 133 | render :action => 'edit' |
|
137 | 134 | end |
|
138 | 135 | |
|
139 | 136 | rescue ActiveRecord::StaleObjectError |
|
140 | 137 | # Optimistic locking exception |
|
141 | flash[:error] = l(:notice_locking_conflict) | |
|
138 | flash.now[:error] = l(:notice_locking_conflict) | |
|
139 | render :action => 'edit' | |
|
142 | 140 | end |
|
143 | 141 | |
|
144 | 142 | # rename a page |
|
145 | 143 | def rename |
|
146 | 144 | return render_403 unless editable? |
|
147 | 145 | @page.redirect_existing_links = true |
|
148 | 146 | # used to display the *original* title if some AR validation errors occur |
|
149 | 147 | @original_title = @page.pretty_title |
|
150 | 148 | if request.post? && @page.update_attributes(params[:wiki_page]) |
|
151 | 149 | flash[:notice] = l(:notice_successful_update) |
|
152 | 150 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
153 | 151 | end |
|
154 | 152 | end |
|
155 | 153 | |
|
156 | 154 | def protect |
|
157 | 155 | @page.update_attribute :protected, params[:protected] |
|
158 | 156 | redirect_to :action => 'show', :project_id => @project, :id => @page.title |
|
159 | 157 | end |
|
160 | 158 | |
|
161 | 159 | # show page history |
|
162 | 160 | def history |
|
163 | 161 | @version_count = @page.content.versions.count |
|
164 | 162 | @version_pages = Paginator.new self, @version_count, per_page_option, params['p'] |
|
165 | 163 | # don't load text |
|
166 | 164 | @versions = @page.content.versions.find :all, |
|
167 | 165 | :select => "id, author_id, comments, updated_on, version", |
|
168 | 166 | :order => 'version DESC', |
|
169 | 167 | :limit => @version_pages.items_per_page + 1, |
|
170 | 168 | :offset => @version_pages.current.offset |
|
171 | 169 | |
|
172 | 170 | render :layout => false if request.xhr? |
|
173 | 171 | end |
|
174 | 172 | |
|
175 | 173 | def diff |
|
176 | 174 | @diff = @page.diff(params[:version], params[:version_from]) |
|
177 | 175 | render_404 unless @diff |
|
178 | 176 | end |
|
179 | 177 | |
|
180 | 178 | def annotate |
|
181 | 179 | @annotate = @page.annotate(params[:version]) |
|
182 | 180 | render_404 unless @annotate |
|
183 | 181 | end |
|
184 | 182 | |
|
185 | 183 | verify :method => :delete, :only => [:destroy], :redirect_to => { :action => :show } |
|
186 | 184 | # Removes a wiki page and its history |
|
187 | 185 | # Children can be either set as root pages, removed or reassigned to another parent page |
|
188 | 186 | def destroy |
|
189 | 187 | return render_403 unless editable? |
|
190 | 188 | |
|
191 | 189 | @descendants_count = @page.descendants.size |
|
192 | 190 | if @descendants_count > 0 |
|
193 | 191 | case params[:todo] |
|
194 | 192 | when 'nullify' |
|
195 | 193 | # Nothing to do |
|
196 | 194 | when 'destroy' |
|
197 | 195 | # Removes all its descendants |
|
198 | 196 | @page.descendants.each(&:destroy) |
|
199 | 197 | when 'reassign' |
|
200 | 198 | # Reassign children to another parent page |
|
201 | 199 | reassign_to = @wiki.pages.find_by_id(params[:reassign_to_id].to_i) |
|
202 | 200 | return unless reassign_to |
|
203 | 201 | @page.children.each do |child| |
|
204 | 202 | child.update_attribute(:parent, reassign_to) |
|
205 | 203 | end |
|
206 | 204 | else |
|
207 | 205 | @reassignable_to = @wiki.pages - @page.self_and_descendants |
|
208 | 206 | return |
|
209 | 207 | end |
|
210 | 208 | end |
|
211 | 209 | @page.destroy |
|
212 | 210 | redirect_to :action => 'index', :project_id => @project |
|
213 | 211 | end |
|
214 | 212 | |
|
215 | 213 | # Export wiki to a single html file |
|
216 | 214 | def export |
|
217 | 215 | if User.current.allowed_to?(:export_wiki_pages, @project) |
|
218 | 216 | @pages = @wiki.pages.find :all, :order => 'title' |
|
219 | 217 | export = render_to_string :action => 'export_multiple', :layout => false |
|
220 | 218 | send_data(export, :type => 'text/html', :filename => "wiki.html") |
|
221 | 219 | else |
|
222 | 220 | redirect_to :action => 'show', :project_id => @project, :id => nil |
|
223 | 221 | end |
|
224 | 222 | end |
|
225 | 223 | |
|
226 | 224 | def preview |
|
227 | 225 | page = @wiki.find_page(params[:id]) |
|
228 | 226 | # page is nil when previewing a new page |
|
229 | 227 | return render_403 unless page.nil? || editable?(page) |
|
230 | 228 | if page |
|
231 | 229 | @attachements = page.attachments |
|
232 | 230 | @previewed = page.content |
|
233 | 231 | end |
|
234 | 232 | @text = params[:content][:text] |
|
235 | 233 | render :partial => 'common/preview' |
|
236 | 234 | end |
|
237 | 235 | |
|
238 | 236 | def add_attachment |
|
239 | 237 | return render_403 unless editable? |
|
240 | 238 | attachments = Attachment.attach_files(@page, params[:attachments]) |
|
241 | 239 | render_attachment_warning_if_needed(@page) |
|
242 | 240 | redirect_to :action => 'show', :id => @page.title, :project_id => @project |
|
243 | 241 | end |
|
244 | 242 | |
|
245 | 243 | private |
|
246 | 244 | |
|
247 | 245 | def find_wiki |
|
248 | 246 | @project = Project.find(params[:project_id]) |
|
249 | 247 | @wiki = @project.wiki |
|
250 | 248 | render_404 unless @wiki |
|
251 | 249 | rescue ActiveRecord::RecordNotFound |
|
252 | 250 | render_404 |
|
253 | 251 | end |
|
254 | 252 | |
|
255 | 253 | # Finds the requested page and returns a 404 error if it doesn't exist |
|
256 | 254 | def find_existing_page |
|
257 | 255 | @page = @wiki.find_page(params[:id]) |
|
258 | 256 | render_404 if @page.nil? |
|
259 | 257 | end |
|
260 | 258 | |
|
261 | 259 | # Returns true if the current user is allowed to edit the page, otherwise false |
|
262 | 260 | def editable?(page = @page) |
|
263 | 261 | page.editable_by?(User.current) |
|
264 | 262 | end |
|
265 | 263 | |
|
266 | 264 | # Returns the default content of a new wiki page |
|
267 | 265 | def initial_page_content(page) |
|
268 | 266 | helper = Redmine::WikiFormatting.helper_for(Setting.text_formatting) |
|
269 | 267 | extend helper unless self.instance_of?(helper) |
|
270 | 268 | helper.instance_method(:initial_page_content).bind(self).call(page) |
|
271 | 269 | end |
|
272 | 270 | |
|
273 | 271 | def load_pages_for_index |
|
274 | 272 | @pages = @wiki.pages.with_updated_on.all(:order => 'title', :include => {:wiki => :project}) |
|
275 | 273 | end |
|
276 | 274 | end |
@@ -1,460 +1,496 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | require 'wiki_controller' |
|
20 | 20 | |
|
21 | 21 | # Re-raise errors caught by the controller. |
|
22 | 22 | class WikiController; def rescue_action(e) raise e end; end |
|
23 | 23 | |
|
24 | 24 | class WikiControllerTest < ActionController::TestCase |
|
25 | 25 | fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, :attachments |
|
26 | 26 | |
|
27 | 27 | def setup |
|
28 | 28 | @controller = WikiController.new |
|
29 | 29 | @request = ActionController::TestRequest.new |
|
30 | 30 | @response = ActionController::TestResponse.new |
|
31 | 31 | User.current = nil |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_show_start_page |
|
35 | 35 | get :show, :project_id => 'ecookbook' |
|
36 | 36 | assert_response :success |
|
37 | 37 | assert_template 'show' |
|
38 | 38 | assert_tag :tag => 'h1', :content => /CookBook documentation/ |
|
39 | 39 | |
|
40 | 40 | # child_pages macro |
|
41 | 41 | assert_tag :ul, :attributes => { :class => 'pages-hierarchy' }, |
|
42 | 42 | :child => { :tag => 'li', |
|
43 | 43 | :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' }, |
|
44 | 44 | :content => 'Page with an inline image' } } |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def test_show_page_with_name |
|
48 | 48 | get :show, :project_id => 1, :id => 'Another_page' |
|
49 | 49 | assert_response :success |
|
50 | 50 | assert_template 'show' |
|
51 | 51 | assert_tag :tag => 'h1', :content => /Another page/ |
|
52 | 52 | # Included page with an inline image |
|
53 | 53 | assert_tag :tag => 'p', :content => /This is an inline image/ |
|
54 | 54 | assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3', |
|
55 | 55 | :alt => 'This is a logo' } |
|
56 | 56 | end |
|
57 | 57 | |
|
58 | 58 | def test_show_with_sidebar |
|
59 | 59 | page = Project.find(1).wiki.pages.new(:title => 'Sidebar') |
|
60 | 60 | page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar') |
|
61 | 61 | page.save! |
|
62 | 62 | |
|
63 | 63 | get :show, :project_id => 1, :id => 'Another_page' |
|
64 | 64 | assert_response :success |
|
65 | 65 | assert_tag :tag => 'div', :attributes => {:id => 'sidebar'}, |
|
66 | 66 | :content => /Side bar content for test_show_with_sidebar/ |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def test_show_unexistent_page_without_edit_right |
|
70 | 70 | get :show, :project_id => 1, :id => 'Unexistent page' |
|
71 | 71 | assert_response 404 |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def test_show_unexistent_page_with_edit_right |
|
75 | 75 | @request.session[:user_id] = 2 |
|
76 | 76 | get :show, :project_id => 1, :id => 'Unexistent page' |
|
77 | 77 | assert_response :success |
|
78 | 78 | assert_template 'edit' |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def test_create_page |
|
82 | 82 | @request.session[:user_id] = 2 |
|
83 | 83 | put :update, :project_id => 1, |
|
84 | 84 | :id => 'New page', |
|
85 | 85 | :content => {:comments => 'Created the page', |
|
86 | 86 | :text => "h1. New page\n\nThis is a new page", |
|
87 | 87 | :version => 0} |
|
88 | 88 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page' |
|
89 | 89 | page = Project.find(1).wiki.find_page('New page') |
|
90 | 90 | assert !page.new_record? |
|
91 | 91 | assert_not_nil page.content |
|
92 | 92 | assert_equal 'Created the page', page.content.comments |
|
93 | 93 | end |
|
94 | 94 | |
|
95 | 95 | def test_create_page_with_attachments |
|
96 | 96 | @request.session[:user_id] = 2 |
|
97 | 97 | assert_difference 'WikiPage.count' do |
|
98 | 98 | assert_difference 'Attachment.count' do |
|
99 | 99 | put :update, :project_id => 1, |
|
100 | 100 | :id => 'New page', |
|
101 | 101 | :content => {:comments => 'Created the page', |
|
102 | 102 | :text => "h1. New page\n\nThis is a new page", |
|
103 | 103 | :version => 0}, |
|
104 | 104 | :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}} |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | page = Project.find(1).wiki.find_page('New page') |
|
108 | 108 | assert_equal 1, page.attachments.count |
|
109 | 109 | assert_equal 'testfile.txt', page.attachments.first.filename |
|
110 | 110 | end |
|
111 | 111 | |
|
112 | 112 | def test_update_page |
|
113 | 113 | @request.session[:user_id] = 2 |
|
114 | 114 | assert_no_difference 'WikiPage.count' do |
|
115 | 115 | assert_no_difference 'WikiContent.count' do |
|
116 | 116 | assert_difference 'WikiContent::Version.count' do |
|
117 | 117 | put :update, :project_id => 1, |
|
118 | 118 | :id => 'Another_page', |
|
119 | 119 | :content => { |
|
120 | 120 | :comments => "my comments", |
|
121 | 121 | :text => "edited", |
|
122 | 122 | :version => 1 |
|
123 | 123 | } |
|
124 | 124 | end |
|
125 | 125 | end |
|
126 | 126 | end |
|
127 | 127 | assert_redirected_to '/projects/ecookbook/wiki/Another_page' |
|
128 | 128 | |
|
129 | 129 | page = Wiki.find(1).pages.find_by_title('Another_page') |
|
130 | 130 | assert_equal "edited", page.content.text |
|
131 | 131 | assert_equal 2, page.content.version |
|
132 | 132 | assert_equal "my comments", page.content.comments |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def test_update_page_with_failure |
|
136 | 136 | @request.session[:user_id] = 2 |
|
137 | 137 | assert_no_difference 'WikiPage.count' do |
|
138 | 138 | assert_no_difference 'WikiContent.count' do |
|
139 | 139 | assert_no_difference 'WikiContent::Version.count' do |
|
140 | 140 | put :update, :project_id => 1, |
|
141 | 141 | :id => 'Another_page', |
|
142 | 142 | :content => { |
|
143 | 143 | :comments => 'a' * 300, # failure here, comment is too long |
|
144 | 144 | :text => 'edited', |
|
145 | 145 | :version => 1 |
|
146 | 146 | } |
|
147 | 147 | end |
|
148 | 148 | end |
|
149 | 149 | end |
|
150 | 150 | assert_response :success |
|
151 | 151 | assert_template 'edit' |
|
152 | 152 | |
|
153 | 153 | assert_error_tag :descendant => {:content => /Comment is too long/} |
|
154 | 154 | assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => 'edited' |
|
155 | 155 | assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'} |
|
156 | 156 | end |
|
157 | 157 | |
|
158 | def test_update_stale_page_should_not_raise_an_error | |
|
159 | @request.session[:user_id] = 2 | |
|
160 | c = Wiki.find(1).find_page('Another_page').content | |
|
161 | c.text = 'Previous text' | |
|
162 | c.save! | |
|
163 | assert_equal 2, c.version | |
|
164 | ||
|
165 | assert_no_difference 'WikiPage.count' do | |
|
166 | assert_no_difference 'WikiContent.count' do | |
|
167 | assert_no_difference 'WikiContent::Version.count' do | |
|
168 | put :update, :project_id => 1, | |
|
169 | :id => 'Another_page', | |
|
170 | :content => { | |
|
171 | :comments => 'My comments', | |
|
172 | :text => 'Text should not be lost', | |
|
173 | :version => 1 | |
|
174 | } | |
|
175 | end | |
|
176 | end | |
|
177 | end | |
|
178 | assert_response :success | |
|
179 | assert_template 'edit' | |
|
180 | assert_tag :div, | |
|
181 | :attributes => { :class => /error/ }, | |
|
182 | :content => /Data has been updated by another user/ | |
|
183 | assert_tag 'textarea', | |
|
184 | :attributes => { :name => 'content[text]' }, | |
|
185 | :content => /Text should not be lost/ | |
|
186 | assert_tag 'input', | |
|
187 | :attributes => { :name => 'content[comments]', :value => 'My comments' } | |
|
188 | ||
|
189 | c.reload | |
|
190 | assert_equal 'Previous text', c.text | |
|
191 | assert_equal 2, c.version | |
|
192 | end | |
|
193 | ||
|
158 | 194 | def test_preview |
|
159 | 195 | @request.session[:user_id] = 2 |
|
160 | 196 | xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation', |
|
161 | 197 | :content => { :comments => '', |
|
162 | 198 | :text => 'this is a *previewed text*', |
|
163 | 199 | :version => 3 } |
|
164 | 200 | assert_response :success |
|
165 | 201 | assert_template 'common/_preview' |
|
166 | 202 | assert_tag :tag => 'strong', :content => /previewed text/ |
|
167 | 203 | end |
|
168 | 204 | |
|
169 | 205 | def test_preview_new_page |
|
170 | 206 | @request.session[:user_id] = 2 |
|
171 | 207 | xhr :post, :preview, :project_id => 1, :id => 'New page', |
|
172 | 208 | :content => { :text => 'h1. New page', |
|
173 | 209 | :comments => '', |
|
174 | 210 | :version => 0 } |
|
175 | 211 | assert_response :success |
|
176 | 212 | assert_template 'common/_preview' |
|
177 | 213 | assert_tag :tag => 'h1', :content => /New page/ |
|
178 | 214 | end |
|
179 | 215 | |
|
180 | 216 | def test_history |
|
181 | 217 | get :history, :project_id => 1, :id => 'CookBook_documentation' |
|
182 | 218 | assert_response :success |
|
183 | 219 | assert_template 'history' |
|
184 | 220 | assert_not_nil assigns(:versions) |
|
185 | 221 | assert_equal 3, assigns(:versions).size |
|
186 | 222 | assert_select "input[type=submit][name=commit]" |
|
187 | 223 | end |
|
188 | 224 | |
|
189 | 225 | def test_history_with_one_version |
|
190 | 226 | get :history, :project_id => 1, :id => 'Another_page' |
|
191 | 227 | assert_response :success |
|
192 | 228 | assert_template 'history' |
|
193 | 229 | assert_not_nil assigns(:versions) |
|
194 | 230 | assert_equal 1, assigns(:versions).size |
|
195 | 231 | assert_select "input[type=submit][name=commit]", false |
|
196 | 232 | end |
|
197 | 233 | |
|
198 | 234 | def test_diff |
|
199 | 235 | get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1 |
|
200 | 236 | assert_response :success |
|
201 | 237 | assert_template 'diff' |
|
202 | 238 | assert_tag :tag => 'span', :attributes => { :class => 'diff_in'}, |
|
203 | 239 | :content => /updated/ |
|
204 | 240 | end |
|
205 | 241 | |
|
206 | 242 | def test_annotate |
|
207 | 243 | get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2 |
|
208 | 244 | assert_response :success |
|
209 | 245 | assert_template 'annotate' |
|
210 | 246 | # Line 1 |
|
211 | 247 | assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1' }, |
|
212 | 248 | :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/ }, |
|
213 | 249 | :child => { :tag => 'td', :content => /h1\. CookBook documentation/ } |
|
214 | 250 | # Line 2 |
|
215 | 251 | assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '2' }, |
|
216 | 252 | :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/ }, |
|
217 | 253 | :child => { :tag => 'td', :content => /Some updated \[\[documentation\]\] here/ } |
|
218 | 254 | end |
|
219 | 255 | |
|
220 | 256 | def test_get_rename |
|
221 | 257 | @request.session[:user_id] = 2 |
|
222 | 258 | get :rename, :project_id => 1, :id => 'Another_page' |
|
223 | 259 | assert_response :success |
|
224 | 260 | assert_template 'rename' |
|
225 | 261 | assert_tag 'option', |
|
226 | 262 | :attributes => {:value => ''}, |
|
227 | 263 | :content => '', |
|
228 | 264 | :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}} |
|
229 | 265 | assert_no_tag 'option', |
|
230 | 266 | :attributes => {:selected => 'selected'}, |
|
231 | 267 | :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}} |
|
232 | 268 | end |
|
233 | 269 | |
|
234 | 270 | def test_get_rename_child_page |
|
235 | 271 | @request.session[:user_id] = 2 |
|
236 | 272 | get :rename, :project_id => 1, :id => 'Child_1' |
|
237 | 273 | assert_response :success |
|
238 | 274 | assert_template 'rename' |
|
239 | 275 | assert_tag 'option', |
|
240 | 276 | :attributes => {:value => ''}, |
|
241 | 277 | :content => '', |
|
242 | 278 | :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}} |
|
243 | 279 | assert_tag 'option', |
|
244 | 280 | :attributes => {:value => '2', :selected => 'selected'}, |
|
245 | 281 | :content => /Another page/, |
|
246 | 282 | :parent => { |
|
247 | 283 | :tag => 'select', |
|
248 | 284 | :attributes => {:name => 'wiki_page[parent_id]'} |
|
249 | 285 | } |
|
250 | 286 | end |
|
251 | 287 | |
|
252 | 288 | def test_rename_with_redirect |
|
253 | 289 | @request.session[:user_id] = 2 |
|
254 | 290 | post :rename, :project_id => 1, :id => 'Another_page', |
|
255 | 291 | :wiki_page => { :title => 'Another renamed page', |
|
256 | 292 | :redirect_existing_links => 1 } |
|
257 | 293 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page' |
|
258 | 294 | wiki = Project.find(1).wiki |
|
259 | 295 | # Check redirects |
|
260 | 296 | assert_not_nil wiki.find_page('Another page') |
|
261 | 297 | assert_nil wiki.find_page('Another page', :with_redirect => false) |
|
262 | 298 | end |
|
263 | 299 | |
|
264 | 300 | def test_rename_without_redirect |
|
265 | 301 | @request.session[:user_id] = 2 |
|
266 | 302 | post :rename, :project_id => 1, :id => 'Another_page', |
|
267 | 303 | :wiki_page => { :title => 'Another renamed page', |
|
268 | 304 | :redirect_existing_links => "0" } |
|
269 | 305 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page' |
|
270 | 306 | wiki = Project.find(1).wiki |
|
271 | 307 | # Check that there's no redirects |
|
272 | 308 | assert_nil wiki.find_page('Another page') |
|
273 | 309 | end |
|
274 | 310 | |
|
275 | 311 | def test_rename_with_parent_assignment |
|
276 | 312 | @request.session[:user_id] = 2 |
|
277 | 313 | post :rename, :project_id => 1, :id => 'Another_page', |
|
278 | 314 | :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' } |
|
279 | 315 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page' |
|
280 | 316 | assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent |
|
281 | 317 | end |
|
282 | 318 | |
|
283 | 319 | def test_rename_with_parent_unassignment |
|
284 | 320 | @request.session[:user_id] = 2 |
|
285 | 321 | post :rename, :project_id => 1, :id => 'Child_1', |
|
286 | 322 | :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' } |
|
287 | 323 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1' |
|
288 | 324 | assert_nil WikiPage.find_by_title('Child_1').parent |
|
289 | 325 | end |
|
290 | 326 | |
|
291 | 327 | def test_destroy_child |
|
292 | 328 | @request.session[:user_id] = 2 |
|
293 | 329 | delete :destroy, :project_id => 1, :id => 'Child_1' |
|
294 | 330 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
295 | 331 | end |
|
296 | 332 | |
|
297 | 333 | def test_destroy_parent |
|
298 | 334 | @request.session[:user_id] = 2 |
|
299 | 335 | assert_no_difference('WikiPage.count') do |
|
300 | 336 | delete :destroy, :project_id => 1, :id => 'Another_page' |
|
301 | 337 | end |
|
302 | 338 | assert_response :success |
|
303 | 339 | assert_template 'destroy' |
|
304 | 340 | end |
|
305 | 341 | |
|
306 | 342 | def test_destroy_parent_with_nullify |
|
307 | 343 | @request.session[:user_id] = 2 |
|
308 | 344 | assert_difference('WikiPage.count', -1) do |
|
309 | 345 | delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify' |
|
310 | 346 | end |
|
311 | 347 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
312 | 348 | assert_nil WikiPage.find_by_id(2) |
|
313 | 349 | end |
|
314 | 350 | |
|
315 | 351 | def test_destroy_parent_with_cascade |
|
316 | 352 | @request.session[:user_id] = 2 |
|
317 | 353 | assert_difference('WikiPage.count', -3) do |
|
318 | 354 | delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy' |
|
319 | 355 | end |
|
320 | 356 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
321 | 357 | assert_nil WikiPage.find_by_id(2) |
|
322 | 358 | assert_nil WikiPage.find_by_id(5) |
|
323 | 359 | end |
|
324 | 360 | |
|
325 | 361 | def test_destroy_parent_with_reassign |
|
326 | 362 | @request.session[:user_id] = 2 |
|
327 | 363 | assert_difference('WikiPage.count', -1) do |
|
328 | 364 | delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1 |
|
329 | 365 | end |
|
330 | 366 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
331 | 367 | assert_nil WikiPage.find_by_id(2) |
|
332 | 368 | assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent |
|
333 | 369 | end |
|
334 | 370 | |
|
335 | 371 | def test_index |
|
336 | 372 | get :index, :project_id => 'ecookbook' |
|
337 | 373 | assert_response :success |
|
338 | 374 | assert_template 'index' |
|
339 | 375 | pages = assigns(:pages) |
|
340 | 376 | assert_not_nil pages |
|
341 | 377 | assert_equal Project.find(1).wiki.pages.size, pages.size |
|
342 | 378 | assert_equal pages.first.content.updated_on, pages.first.updated_on |
|
343 | 379 | |
|
344 | 380 | assert_tag :ul, :attributes => { :class => 'pages-hierarchy' }, |
|
345 | 381 | :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' }, |
|
346 | 382 | :content => 'CookBook documentation' }, |
|
347 | 383 | :child => { :tag => 'ul', |
|
348 | 384 | :child => { :tag => 'li', |
|
349 | 385 | :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' }, |
|
350 | 386 | :content => 'Page with an inline image' } } } }, |
|
351 | 387 | :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' }, |
|
352 | 388 | :content => 'Another page' } } |
|
353 | 389 | end |
|
354 | 390 | |
|
355 | 391 | context "GET :export" do |
|
356 | 392 | context "with an authorized user to export the wiki" do |
|
357 | 393 | setup do |
|
358 | 394 | @request.session[:user_id] = 2 |
|
359 | 395 | get :export, :project_id => 'ecookbook' |
|
360 | 396 | end |
|
361 | 397 | |
|
362 | 398 | should_respond_with :success |
|
363 | 399 | should_assign_to :pages |
|
364 | 400 | should_respond_with_content_type "text/html" |
|
365 | 401 | should "export all of the wiki pages to a single html file" do |
|
366 | 402 | assert_select "a[name=?]", "CookBook_documentation" |
|
367 | 403 | assert_select "a[name=?]", "Another_page" |
|
368 | 404 | assert_select "a[name=?]", "Page_with_an_inline_image" |
|
369 | 405 | end |
|
370 | 406 | |
|
371 | 407 | end |
|
372 | 408 | |
|
373 | 409 | context "with an unauthorized user" do |
|
374 | 410 | setup do |
|
375 | 411 | get :export, :project_id => 'ecookbook' |
|
376 | 412 | |
|
377 | 413 | should_respond_with :redirect |
|
378 | 414 | should_redirect_to('wiki index') { {:action => 'show', :project_id => @project, :id => nil} } |
|
379 | 415 | end |
|
380 | 416 | end |
|
381 | 417 | end |
|
382 | 418 | |
|
383 | 419 | context "GET :date_index" do |
|
384 | 420 | setup do |
|
385 | 421 | get :date_index, :project_id => 'ecookbook' |
|
386 | 422 | end |
|
387 | 423 | |
|
388 | 424 | should_respond_with :success |
|
389 | 425 | should_assign_to :pages |
|
390 | 426 | should_assign_to :pages_by_date |
|
391 | 427 | should_render_template 'wiki/date_index' |
|
392 | 428 | |
|
393 | 429 | end |
|
394 | 430 | |
|
395 | 431 | def test_not_found |
|
396 | 432 | get :show, :project_id => 999 |
|
397 | 433 | assert_response 404 |
|
398 | 434 | end |
|
399 | 435 | |
|
400 | 436 | def test_protect_page |
|
401 | 437 | page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page') |
|
402 | 438 | assert !page.protected? |
|
403 | 439 | @request.session[:user_id] = 2 |
|
404 | 440 | post :protect, :project_id => 1, :id => page.title, :protected => '1' |
|
405 | 441 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page' |
|
406 | 442 | assert page.reload.protected? |
|
407 | 443 | end |
|
408 | 444 | |
|
409 | 445 | def test_unprotect_page |
|
410 | 446 | page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation') |
|
411 | 447 | assert page.protected? |
|
412 | 448 | @request.session[:user_id] = 2 |
|
413 | 449 | post :protect, :project_id => 1, :id => page.title, :protected => '0' |
|
414 | 450 | assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation' |
|
415 | 451 | assert !page.reload.protected? |
|
416 | 452 | end |
|
417 | 453 | |
|
418 | 454 | def test_show_page_with_edit_link |
|
419 | 455 | @request.session[:user_id] = 2 |
|
420 | 456 | get :show, :project_id => 1 |
|
421 | 457 | assert_response :success |
|
422 | 458 | assert_template 'show' |
|
423 | 459 | assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' } |
|
424 | 460 | end |
|
425 | 461 | |
|
426 | 462 | def test_show_page_without_edit_link |
|
427 | 463 | @request.session[:user_id] = 4 |
|
428 | 464 | get :show, :project_id => 1 |
|
429 | 465 | assert_response :success |
|
430 | 466 | assert_template 'show' |
|
431 | 467 | assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' } |
|
432 | 468 | end |
|
433 | 469 | |
|
434 | 470 | def test_edit_unprotected_page |
|
435 | 471 | # Non members can edit unprotected wiki pages |
|
436 | 472 | @request.session[:user_id] = 4 |
|
437 | 473 | get :edit, :project_id => 1, :id => 'Another_page' |
|
438 | 474 | assert_response :success |
|
439 | 475 | assert_template 'edit' |
|
440 | 476 | end |
|
441 | 477 | |
|
442 | 478 | def test_edit_protected_page_by_nonmember |
|
443 | 479 | # Non members can't edit protected wiki pages |
|
444 | 480 | @request.session[:user_id] = 4 |
|
445 | 481 | get :edit, :project_id => 1, :id => 'CookBook_documentation' |
|
446 | 482 | assert_response 403 |
|
447 | 483 | end |
|
448 | 484 | |
|
449 | 485 | def test_edit_protected_page_by_member |
|
450 | 486 | @request.session[:user_id] = 2 |
|
451 | 487 | get :edit, :project_id => 1, :id => 'CookBook_documentation' |
|
452 | 488 | assert_response :success |
|
453 | 489 | assert_template 'edit' |
|
454 | 490 | end |
|
455 | 491 | |
|
456 | 492 | def test_history_of_non_existing_page_should_return_404 |
|
457 | 493 | get :history, :project_id => 1, :id => 'Unknown_page' |
|
458 | 494 | assert_response 404 |
|
459 | 495 | end |
|
460 | 496 | end |
General Comments 0
You need to be logged in to leave comments.
Login now