##// END OF EJS Templates
Merged r9895 from trunk....
Jean-Philippe Lang -
r9773:6488210ec1e7
parent child
Show More
@@ -1,72 +1,73
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 module Redmine
18 module Redmine
19 module Helpers
19 module Helpers
20 class Diff
20 class Diff
21 include ERB::Util
21 include ERB::Util
22 include ActionView::Helpers::TagHelper
22 include ActionView::Helpers::TagHelper
23 include ActionView::Helpers::TextHelper
23 include ActionView::Helpers::TextHelper
24 attr_reader :diff, :words
24 attr_reader :diff, :words
25
25
26 def initialize(content_to, content_from)
26 def initialize(content_to, content_from)
27 @words = content_to.to_s.split(/(\s+)/)
27 @words = content_to.to_s.split(/(\s+)/)
28 @words = @words.select {|word| word != ' '}
28 @words = @words.select {|word| word != ' '}
29 words_from = content_from.to_s.split(/(\s+)/)
29 words_from = content_from.to_s.split(/(\s+)/)
30 words_from = words_from.select {|word| word != ' '}
30 words_from = words_from.select {|word| word != ' '}
31 @diff = words_from.diff @words
31 @diff = words_from.diff @words
32 end
32 end
33
33
34 def to_html
34 def to_html
35 words = self.words.collect{|word| h(word)}
35 words = self.words.collect{|word| h(word)}
36 words_add = 0
36 words_add = 0
37 words_del = 0
37 words_del = 0
38 dels = 0
38 dels = 0
39 del_off = 0
39 del_off = 0
40 diff.diffs.each do |diff|
40 diff.diffs.each do |diff|
41 add_at = nil
41 add_at = nil
42 add_to = nil
42 add_to = nil
43 del_at = nil
43 del_at = nil
44 deleted = ""
44 deleted = ""
45 diff.each do |change|
45 diff.each do |change|
46 pos = change[1]
46 pos = change[1]
47 if change[0] == "+"
47 if change[0] == "+"
48 add_at = pos + dels unless add_at
48 add_at = pos + dels unless add_at
49 add_to = pos + dels
49 add_to = pos + dels
50 words_add += 1
50 words_add += 1
51 else
51 else
52 del_at = pos unless del_at
52 del_at = pos unless del_at
53 deleted << ' ' + h(change[2])
53 deleted << ' ' unless deleted.empty?
54 deleted << h(change[2])
54 words_del += 1
55 words_del += 1
55 end
56 end
56 end
57 end
57 if add_at
58 if add_at
58 words[add_at] = '<span class="diff_in">' + words[add_at]
59 words[add_at] = '<span class="diff_in">'.html_safe + words[add_at]
59 words[add_to] = words[add_to] + '</span>'
60 words[add_to] = words[add_to] + '</span>'.html_safe
60 end
61 end
61 if del_at
62 if del_at
62 words.insert del_at - del_off + dels + words_add, '<span class="diff_out">' + deleted + '</span>'
63 words.insert del_at - del_off + dels + words_add, '<span class="diff_out">'.html_safe + deleted + '</span>'.html_safe
63 dels += 1
64 dels += 1
64 del_off += words_del
65 del_off += words_del
65 words_del = 0
66 words_del = 0
66 end
67 end
67 end
68 end
68 words.join(' ').html_safe
69 words.join(' ').html_safe
69 end
70 end
70 end
71 end
71 end
72 end
72 end
73 end
@@ -1,845 +1,853
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
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
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.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19 require 'wiki_controller'
19 require 'wiki_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class WikiController; def rescue_action(e) raise e end; end
22 class WikiController; def rescue_action(e) raise e end; end
23
23
24 class WikiControllerTest < ActionController::TestCase
24 class WikiControllerTest < ActionController::TestCase
25 fixtures :projects, :users, :roles, :members, :member_roles,
25 fixtures :projects, :users, :roles, :members, :member_roles,
26 :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
26 :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
27 :wiki_content_versions, :attachments
27 :wiki_content_versions, :attachments
28
28
29 def setup
29 def setup
30 @controller = WikiController.new
30 @controller = WikiController.new
31 @request = ActionController::TestRequest.new
31 @request = ActionController::TestRequest.new
32 @response = ActionController::TestResponse.new
32 @response = ActionController::TestResponse.new
33 User.current = nil
33 User.current = nil
34 end
34 end
35
35
36 def test_show_start_page
36 def test_show_start_page
37 get :show, :project_id => 'ecookbook'
37 get :show, :project_id => 'ecookbook'
38 assert_response :success
38 assert_response :success
39 assert_template 'show'
39 assert_template 'show'
40 assert_tag :tag => 'h1', :content => /CookBook documentation/
40 assert_tag :tag => 'h1', :content => /CookBook documentation/
41
41
42 # child_pages macro
42 # child_pages macro
43 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
43 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
44 :child => { :tag => 'li',
44 :child => { :tag => 'li',
45 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
45 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
46 :content => 'Page with an inline image' } }
46 :content => 'Page with an inline image' } }
47 end
47 end
48
48
49 def test_export_link
49 def test_export_link
50 Role.anonymous.add_permission! :export_wiki_pages
50 Role.anonymous.add_permission! :export_wiki_pages
51 get :show, :project_id => 'ecookbook'
51 get :show, :project_id => 'ecookbook'
52 assert_response :success
52 assert_response :success
53 assert_tag 'a', :attributes => {:href => '/projects/ecookbook/wiki/CookBook_documentation.txt'}
53 assert_tag 'a', :attributes => {:href => '/projects/ecookbook/wiki/CookBook_documentation.txt'}
54 end
54 end
55
55
56 def test_show_page_with_name
56 def test_show_page_with_name
57 get :show, :project_id => 1, :id => 'Another_page'
57 get :show, :project_id => 1, :id => 'Another_page'
58 assert_response :success
58 assert_response :success
59 assert_template 'show'
59 assert_template 'show'
60 assert_tag :tag => 'h1', :content => /Another page/
60 assert_tag :tag => 'h1', :content => /Another page/
61 # Included page with an inline image
61 # Included page with an inline image
62 assert_tag :tag => 'p', :content => /This is an inline image/
62 assert_tag :tag => 'p', :content => /This is an inline image/
63 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
63 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
64 :alt => 'This is a logo' }
64 :alt => 'This is a logo' }
65 end
65 end
66
66
67 def test_show_redirected_page
67 def test_show_redirected_page
68 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
68 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
69
69
70 get :show, :project_id => 'ecookbook', :id => 'Old_title'
70 get :show, :project_id => 'ecookbook', :id => 'Old_title'
71 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
71 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
72 end
72 end
73
73
74 def test_show_with_sidebar
74 def test_show_with_sidebar
75 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
75 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
76 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
76 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
77 page.save!
77 page.save!
78
78
79 get :show, :project_id => 1, :id => 'Another_page'
79 get :show, :project_id => 1, :id => 'Another_page'
80 assert_response :success
80 assert_response :success
81 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
81 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
82 :content => /Side bar content for test_show_with_sidebar/
82 :content => /Side bar content for test_show_with_sidebar/
83 end
83 end
84
84
85 def test_show_should_display_section_edit_links
85 def test_show_should_display_section_edit_links
86 @request.session[:user_id] = 2
86 @request.session[:user_id] = 2
87 get :show, :project_id => 1, :id => 'Page with sections'
87 get :show, :project_id => 1, :id => 'Page with sections'
88 assert_no_tag 'a', :attributes => {
88 assert_no_tag 'a', :attributes => {
89 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=1'
89 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=1'
90 }
90 }
91 assert_tag 'a', :attributes => {
91 assert_tag 'a', :attributes => {
92 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
92 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
93 }
93 }
94 assert_tag 'a', :attributes => {
94 assert_tag 'a', :attributes => {
95 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
95 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
96 }
96 }
97 end
97 end
98
98
99 def test_show_current_version_should_display_section_edit_links
99 def test_show_current_version_should_display_section_edit_links
100 @request.session[:user_id] = 2
100 @request.session[:user_id] = 2
101 get :show, :project_id => 1, :id => 'Page with sections', :version => 3
101 get :show, :project_id => 1, :id => 'Page with sections', :version => 3
102
102
103 assert_tag 'a', :attributes => {
103 assert_tag 'a', :attributes => {
104 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
104 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
105 }
105 }
106 end
106 end
107
107
108 def test_show_old_version_should_not_display_section_edit_links
108 def test_show_old_version_should_not_display_section_edit_links
109 @request.session[:user_id] = 2
109 @request.session[:user_id] = 2
110 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
110 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
111
111
112 assert_no_tag 'a', :attributes => {
112 assert_no_tag 'a', :attributes => {
113 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
113 :href => '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
114 }
114 }
115 end
115 end
116
116
117 def test_show_unexistent_page_without_edit_right
117 def test_show_unexistent_page_without_edit_right
118 get :show, :project_id => 1, :id => 'Unexistent page'
118 get :show, :project_id => 1, :id => 'Unexistent page'
119 assert_response 404
119 assert_response 404
120 end
120 end
121
121
122 def test_show_unexistent_page_with_edit_right
122 def test_show_unexistent_page_with_edit_right
123 @request.session[:user_id] = 2
123 @request.session[:user_id] = 2
124 get :show, :project_id => 1, :id => 'Unexistent page'
124 get :show, :project_id => 1, :id => 'Unexistent page'
125 assert_response :success
125 assert_response :success
126 assert_template 'edit'
126 assert_template 'edit'
127 end
127 end
128
128
129 def test_show_unexistent_page_with_parent_should_preselect_parent
129 def test_show_unexistent_page_with_parent_should_preselect_parent
130 @request.session[:user_id] = 2
130 @request.session[:user_id] = 2
131 get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'
131 get :show, :project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'
132 assert_response :success
132 assert_response :success
133 assert_template 'edit'
133 assert_template 'edit'
134 assert_tag 'select', :attributes => {:name => 'wiki_page[parent_id]'},
134 assert_tag 'select', :attributes => {:name => 'wiki_page[parent_id]'},
135 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}}
135 :child => {:tag => 'option', :attributes => {:value => '2', :selected => 'selected'}}
136 end
136 end
137
137
138 def test_show_should_not_show_history_without_permission
138 def test_show_should_not_show_history_without_permission
139 Role.anonymous.remove_permission! :view_wiki_edits
139 Role.anonymous.remove_permission! :view_wiki_edits
140 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
140 get :show, :project_id => 1, :id => 'Page with sections', :version => 2
141
141
142 assert_response 302
142 assert_response 302
143 end
143 end
144
144
145 def test_create_page
145 def test_create_page
146 @request.session[:user_id] = 2
146 @request.session[:user_id] = 2
147 assert_difference 'WikiPage.count' do
147 assert_difference 'WikiPage.count' do
148 assert_difference 'WikiContent.count' do
148 assert_difference 'WikiContent.count' do
149 put :update, :project_id => 1,
149 put :update, :project_id => 1,
150 :id => 'New page',
150 :id => 'New page',
151 :content => {:comments => 'Created the page',
151 :content => {:comments => 'Created the page',
152 :text => "h1. New page\n\nThis is a new page",
152 :text => "h1. New page\n\nThis is a new page",
153 :version => 0}
153 :version => 0}
154 end
154 end
155 end
155 end
156 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
156 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
157 page = Project.find(1).wiki.find_page('New page')
157 page = Project.find(1).wiki.find_page('New page')
158 assert !page.new_record?
158 assert !page.new_record?
159 assert_not_nil page.content
159 assert_not_nil page.content
160 assert_nil page.parent
160 assert_nil page.parent
161 assert_equal 'Created the page', page.content.comments
161 assert_equal 'Created the page', page.content.comments
162 end
162 end
163
163
164 def test_create_page_with_attachments
164 def test_create_page_with_attachments
165 @request.session[:user_id] = 2
165 @request.session[:user_id] = 2
166 assert_difference 'WikiPage.count' do
166 assert_difference 'WikiPage.count' do
167 assert_difference 'Attachment.count' do
167 assert_difference 'Attachment.count' do
168 put :update, :project_id => 1,
168 put :update, :project_id => 1,
169 :id => 'New page',
169 :id => 'New page',
170 :content => {:comments => 'Created the page',
170 :content => {:comments => 'Created the page',
171 :text => "h1. New page\n\nThis is a new page",
171 :text => "h1. New page\n\nThis is a new page",
172 :version => 0},
172 :version => 0},
173 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
173 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
174 end
174 end
175 end
175 end
176 page = Project.find(1).wiki.find_page('New page')
176 page = Project.find(1).wiki.find_page('New page')
177 assert_equal 1, page.attachments.count
177 assert_equal 1, page.attachments.count
178 assert_equal 'testfile.txt', page.attachments.first.filename
178 assert_equal 'testfile.txt', page.attachments.first.filename
179 end
179 end
180
180
181 def test_create_page_with_parent
181 def test_create_page_with_parent
182 @request.session[:user_id] = 2
182 @request.session[:user_id] = 2
183 assert_difference 'WikiPage.count' do
183 assert_difference 'WikiPage.count' do
184 put :update, :project_id => 1, :id => 'New page',
184 put :update, :project_id => 1, :id => 'New page',
185 :content => {:text => "h1. New page\n\nThis is a new page", :version => 0},
185 :content => {:text => "h1. New page\n\nThis is a new page", :version => 0},
186 :wiki_page => {:parent_id => 2}
186 :wiki_page => {:parent_id => 2}
187 end
187 end
188 page = Project.find(1).wiki.find_page('New page')
188 page = Project.find(1).wiki.find_page('New page')
189 assert_equal WikiPage.find(2), page.parent
189 assert_equal WikiPage.find(2), page.parent
190 end
190 end
191
191
192 def test_edit_page
192 def test_edit_page
193 @request.session[:user_id] = 2
193 @request.session[:user_id] = 2
194 get :edit, :project_id => 'ecookbook', :id => 'Another_page'
194 get :edit, :project_id => 'ecookbook', :id => 'Another_page'
195
195
196 assert_response :success
196 assert_response :success
197 assert_template 'edit'
197 assert_template 'edit'
198
198
199 assert_tag 'textarea',
199 assert_tag 'textarea',
200 :attributes => { :name => 'content[text]' },
200 :attributes => { :name => 'content[text]' },
201 :content => "\n"+WikiPage.find_by_title('Another_page').content.text
201 :content => "\n"+WikiPage.find_by_title('Another_page').content.text
202 end
202 end
203
203
204 def test_edit_section
204 def test_edit_section
205 @request.session[:user_id] = 2
205 @request.session[:user_id] = 2
206 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2
206 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2
207
207
208 assert_response :success
208 assert_response :success
209 assert_template 'edit'
209 assert_template 'edit'
210
210
211 page = WikiPage.find_by_title('Page_with_sections')
211 page = WikiPage.find_by_title('Page_with_sections')
212 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
212 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
213
213
214 assert_tag 'textarea',
214 assert_tag 'textarea',
215 :attributes => { :name => 'content[text]' },
215 :attributes => { :name => 'content[text]' },
216 :content => "\n"+section
216 :content => "\n"+section
217 assert_tag 'input',
217 assert_tag 'input',
218 :attributes => { :name => 'section', :type => 'hidden', :value => '2' }
218 :attributes => { :name => 'section', :type => 'hidden', :value => '2' }
219 assert_tag 'input',
219 assert_tag 'input',
220 :attributes => { :name => 'section_hash', :type => 'hidden', :value => hash }
220 :attributes => { :name => 'section_hash', :type => 'hidden', :value => hash }
221 end
221 end
222
222
223 def test_edit_invalid_section_should_respond_with_404
223 def test_edit_invalid_section_should_respond_with_404
224 @request.session[:user_id] = 2
224 @request.session[:user_id] = 2
225 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10
225 get :edit, :project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10
226
226
227 assert_response 404
227 assert_response 404
228 end
228 end
229
229
230 def test_update_page
230 def test_update_page
231 @request.session[:user_id] = 2
231 @request.session[:user_id] = 2
232 assert_no_difference 'WikiPage.count' do
232 assert_no_difference 'WikiPage.count' do
233 assert_no_difference 'WikiContent.count' do
233 assert_no_difference 'WikiContent.count' do
234 assert_difference 'WikiContent::Version.count' do
234 assert_difference 'WikiContent::Version.count' do
235 put :update, :project_id => 1,
235 put :update, :project_id => 1,
236 :id => 'Another_page',
236 :id => 'Another_page',
237 :content => {
237 :content => {
238 :comments => "my comments",
238 :comments => "my comments",
239 :text => "edited",
239 :text => "edited",
240 :version => 1
240 :version => 1
241 }
241 }
242 end
242 end
243 end
243 end
244 end
244 end
245 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
245 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
246
246
247 page = Wiki.find(1).pages.find_by_title('Another_page')
247 page = Wiki.find(1).pages.find_by_title('Another_page')
248 assert_equal "edited", page.content.text
248 assert_equal "edited", page.content.text
249 assert_equal 2, page.content.version
249 assert_equal 2, page.content.version
250 assert_equal "my comments", page.content.comments
250 assert_equal "my comments", page.content.comments
251 end
251 end
252
252
253 def test_update_page_with_parent
253 def test_update_page_with_parent
254 @request.session[:user_id] = 2
254 @request.session[:user_id] = 2
255 assert_no_difference 'WikiPage.count' do
255 assert_no_difference 'WikiPage.count' do
256 assert_no_difference 'WikiContent.count' do
256 assert_no_difference 'WikiContent.count' do
257 assert_difference 'WikiContent::Version.count' do
257 assert_difference 'WikiContent::Version.count' do
258 put :update, :project_id => 1,
258 put :update, :project_id => 1,
259 :id => 'Another_page',
259 :id => 'Another_page',
260 :content => {
260 :content => {
261 :comments => "my comments",
261 :comments => "my comments",
262 :text => "edited",
262 :text => "edited",
263 :version => 1
263 :version => 1
264 },
264 },
265 :wiki_page => {:parent_id => '1'}
265 :wiki_page => {:parent_id => '1'}
266 end
266 end
267 end
267 end
268 end
268 end
269 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
269 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
270
270
271 page = Wiki.find(1).pages.find_by_title('Another_page')
271 page = Wiki.find(1).pages.find_by_title('Another_page')
272 assert_equal "edited", page.content.text
272 assert_equal "edited", page.content.text
273 assert_equal 2, page.content.version
273 assert_equal 2, page.content.version
274 assert_equal "my comments", page.content.comments
274 assert_equal "my comments", page.content.comments
275 assert_equal WikiPage.find(1), page.parent
275 assert_equal WikiPage.find(1), page.parent
276 end
276 end
277
277
278 def test_update_page_with_failure
278 def test_update_page_with_failure
279 @request.session[:user_id] = 2
279 @request.session[:user_id] = 2
280 assert_no_difference 'WikiPage.count' do
280 assert_no_difference 'WikiPage.count' do
281 assert_no_difference 'WikiContent.count' do
281 assert_no_difference 'WikiContent.count' do
282 assert_no_difference 'WikiContent::Version.count' do
282 assert_no_difference 'WikiContent::Version.count' do
283 put :update, :project_id => 1,
283 put :update, :project_id => 1,
284 :id => 'Another_page',
284 :id => 'Another_page',
285 :content => {
285 :content => {
286 :comments => 'a' * 300, # failure here, comment is too long
286 :comments => 'a' * 300, # failure here, comment is too long
287 :text => 'edited',
287 :text => 'edited',
288 :version => 1
288 :version => 1
289 }
289 }
290 end
290 end
291 end
291 end
292 end
292 end
293 assert_response :success
293 assert_response :success
294 assert_template 'edit'
294 assert_template 'edit'
295
295
296 assert_error_tag :descendant => {:content => /Comment is too long/}
296 assert_error_tag :descendant => {:content => /Comment is too long/}
297 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => "\nedited"
297 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => "\nedited"
298 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
298 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
299 end
299 end
300
300
301 def test_update_page_with_parent_change_only_should_not_create_content_version
301 def test_update_page_with_parent_change_only_should_not_create_content_version
302 @request.session[:user_id] = 2
302 @request.session[:user_id] = 2
303 assert_no_difference 'WikiPage.count' do
303 assert_no_difference 'WikiPage.count' do
304 assert_no_difference 'WikiContent.count' do
304 assert_no_difference 'WikiContent.count' do
305 assert_no_difference 'WikiContent::Version.count' do
305 assert_no_difference 'WikiContent::Version.count' do
306 put :update, :project_id => 1,
306 put :update, :project_id => 1,
307 :id => 'Another_page',
307 :id => 'Another_page',
308 :content => {
308 :content => {
309 :comments => '',
309 :comments => '',
310 :text => Wiki.find(1).find_page('Another_page').content.text,
310 :text => Wiki.find(1).find_page('Another_page').content.text,
311 :version => 1
311 :version => 1
312 },
312 },
313 :wiki_page => {:parent_id => '1'}
313 :wiki_page => {:parent_id => '1'}
314 end
314 end
315 end
315 end
316 end
316 end
317 page = Wiki.find(1).pages.find_by_title('Another_page')
317 page = Wiki.find(1).pages.find_by_title('Another_page')
318 assert_equal 1, page.content.version
318 assert_equal 1, page.content.version
319 assert_equal WikiPage.find(1), page.parent
319 assert_equal WikiPage.find(1), page.parent
320 end
320 end
321
321
322 def test_update_page_with_attachments_only_should_not_create_content_version
322 def test_update_page_with_attachments_only_should_not_create_content_version
323 @request.session[:user_id] = 2
323 @request.session[:user_id] = 2
324 assert_no_difference 'WikiPage.count' do
324 assert_no_difference 'WikiPage.count' do
325 assert_no_difference 'WikiContent.count' do
325 assert_no_difference 'WikiContent.count' do
326 assert_no_difference 'WikiContent::Version.count' do
326 assert_no_difference 'WikiContent::Version.count' do
327 assert_difference 'Attachment.count' do
327 assert_difference 'Attachment.count' do
328 put :update, :project_id => 1,
328 put :update, :project_id => 1,
329 :id => 'Another_page',
329 :id => 'Another_page',
330 :content => {
330 :content => {
331 :comments => '',
331 :comments => '',
332 :text => Wiki.find(1).find_page('Another_page').content.text,
332 :text => Wiki.find(1).find_page('Another_page').content.text,
333 :version => 1
333 :version => 1
334 },
334 },
335 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
335 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
336 end
336 end
337 end
337 end
338 end
338 end
339 end
339 end
340 page = Wiki.find(1).pages.find_by_title('Another_page')
340 page = Wiki.find(1).pages.find_by_title('Another_page')
341 assert_equal 1, page.content.version
341 assert_equal 1, page.content.version
342 end
342 end
343
343
344 def test_update_stale_page_should_not_raise_an_error
344 def test_update_stale_page_should_not_raise_an_error
345 @request.session[:user_id] = 2
345 @request.session[:user_id] = 2
346 c = Wiki.find(1).find_page('Another_page').content
346 c = Wiki.find(1).find_page('Another_page').content
347 c.text = 'Previous text'
347 c.text = 'Previous text'
348 c.save!
348 c.save!
349 assert_equal 2, c.version
349 assert_equal 2, c.version
350
350
351 assert_no_difference 'WikiPage.count' do
351 assert_no_difference 'WikiPage.count' do
352 assert_no_difference 'WikiContent.count' do
352 assert_no_difference 'WikiContent.count' do
353 assert_no_difference 'WikiContent::Version.count' do
353 assert_no_difference 'WikiContent::Version.count' do
354 put :update, :project_id => 1,
354 put :update, :project_id => 1,
355 :id => 'Another_page',
355 :id => 'Another_page',
356 :content => {
356 :content => {
357 :comments => 'My comments',
357 :comments => 'My comments',
358 :text => 'Text should not be lost',
358 :text => 'Text should not be lost',
359 :version => 1
359 :version => 1
360 }
360 }
361 end
361 end
362 end
362 end
363 end
363 end
364 assert_response :success
364 assert_response :success
365 assert_template 'edit'
365 assert_template 'edit'
366 assert_tag :div,
366 assert_tag :div,
367 :attributes => { :class => /error/ },
367 :attributes => { :class => /error/ },
368 :content => /Data has been updated by another user/
368 :content => /Data has been updated by another user/
369 assert_tag 'textarea',
369 assert_tag 'textarea',
370 :attributes => { :name => 'content[text]' },
370 :attributes => { :name => 'content[text]' },
371 :content => /Text should not be lost/
371 :content => /Text should not be lost/
372 assert_tag 'input',
372 assert_tag 'input',
373 :attributes => { :name => 'content[comments]', :value => 'My comments' }
373 :attributes => { :name => 'content[comments]', :value => 'My comments' }
374
374
375 c.reload
375 c.reload
376 assert_equal 'Previous text', c.text
376 assert_equal 'Previous text', c.text
377 assert_equal 2, c.version
377 assert_equal 2, c.version
378 end
378 end
379
379
380 def test_update_section
380 def test_update_section
381 @request.session[:user_id] = 2
381 @request.session[:user_id] = 2
382 page = WikiPage.find_by_title('Page_with_sections')
382 page = WikiPage.find_by_title('Page_with_sections')
383 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
383 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
384 text = page.content.text
384 text = page.content.text
385
385
386 assert_no_difference 'WikiPage.count' do
386 assert_no_difference 'WikiPage.count' do
387 assert_no_difference 'WikiContent.count' do
387 assert_no_difference 'WikiContent.count' do
388 assert_difference 'WikiContent::Version.count' do
388 assert_difference 'WikiContent::Version.count' do
389 put :update, :project_id => 1, :id => 'Page_with_sections',
389 put :update, :project_id => 1, :id => 'Page_with_sections',
390 :content => {
390 :content => {
391 :text => "New section content",
391 :text => "New section content",
392 :version => 3
392 :version => 3
393 },
393 },
394 :section => 2,
394 :section => 2,
395 :section_hash => hash
395 :section_hash => hash
396 end
396 end
397 end
397 end
398 end
398 end
399 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
399 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
400 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
400 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
401 end
401 end
402
402
403 def test_update_section_should_allow_stale_page_update
403 def test_update_section_should_allow_stale_page_update
404 @request.session[:user_id] = 2
404 @request.session[:user_id] = 2
405 page = WikiPage.find_by_title('Page_with_sections')
405 page = WikiPage.find_by_title('Page_with_sections')
406 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
406 section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
407 text = page.content.text
407 text = page.content.text
408
408
409 assert_no_difference 'WikiPage.count' do
409 assert_no_difference 'WikiPage.count' do
410 assert_no_difference 'WikiContent.count' do
410 assert_no_difference 'WikiContent.count' do
411 assert_difference 'WikiContent::Version.count' do
411 assert_difference 'WikiContent::Version.count' do
412 put :update, :project_id => 1, :id => 'Page_with_sections',
412 put :update, :project_id => 1, :id => 'Page_with_sections',
413 :content => {
413 :content => {
414 :text => "New section content",
414 :text => "New section content",
415 :version => 2 # Current version is 3
415 :version => 2 # Current version is 3
416 },
416 },
417 :section => 2,
417 :section => 2,
418 :section_hash => hash
418 :section_hash => hash
419 end
419 end
420 end
420 end
421 end
421 end
422 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
422 assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections'
423 page.reload
423 page.reload
424 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
424 assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
425 assert_equal 4, page.content.version
425 assert_equal 4, page.content.version
426 end
426 end
427
427
428 def test_update_section_should_not_allow_stale_section_update
428 def test_update_section_should_not_allow_stale_section_update
429 @request.session[:user_id] = 2
429 @request.session[:user_id] = 2
430
430
431 assert_no_difference 'WikiPage.count' do
431 assert_no_difference 'WikiPage.count' do
432 assert_no_difference 'WikiContent.count' do
432 assert_no_difference 'WikiContent.count' do
433 assert_no_difference 'WikiContent::Version.count' do
433 assert_no_difference 'WikiContent::Version.count' do
434 put :update, :project_id => 1, :id => 'Page_with_sections',
434 put :update, :project_id => 1, :id => 'Page_with_sections',
435 :content => {
435 :content => {
436 :comments => 'My comments',
436 :comments => 'My comments',
437 :text => "Text should not be lost",
437 :text => "Text should not be lost",
438 :version => 3
438 :version => 3
439 },
439 },
440 :section => 2,
440 :section => 2,
441 :section_hash => Digest::MD5.hexdigest("wrong hash")
441 :section_hash => Digest::MD5.hexdigest("wrong hash")
442 end
442 end
443 end
443 end
444 end
444 end
445 assert_response :success
445 assert_response :success
446 assert_template 'edit'
446 assert_template 'edit'
447 assert_tag :div,
447 assert_tag :div,
448 :attributes => { :class => /error/ },
448 :attributes => { :class => /error/ },
449 :content => /Data has been updated by another user/
449 :content => /Data has been updated by another user/
450 assert_tag 'textarea',
450 assert_tag 'textarea',
451 :attributes => { :name => 'content[text]' },
451 :attributes => { :name => 'content[text]' },
452 :content => /Text should not be lost/
452 :content => /Text should not be lost/
453 assert_tag 'input',
453 assert_tag 'input',
454 :attributes => { :name => 'content[comments]', :value => 'My comments' }
454 :attributes => { :name => 'content[comments]', :value => 'My comments' }
455 end
455 end
456
456
457 def test_preview
457 def test_preview
458 @request.session[:user_id] = 2
458 @request.session[:user_id] = 2
459 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
459 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
460 :content => { :comments => '',
460 :content => { :comments => '',
461 :text => 'this is a *previewed text*',
461 :text => 'this is a *previewed text*',
462 :version => 3 }
462 :version => 3 }
463 assert_response :success
463 assert_response :success
464 assert_template 'common/_preview'
464 assert_template 'common/_preview'
465 assert_tag :tag => 'strong', :content => /previewed text/
465 assert_tag :tag => 'strong', :content => /previewed text/
466 end
466 end
467
467
468 def test_preview_new_page
468 def test_preview_new_page
469 @request.session[:user_id] = 2
469 @request.session[:user_id] = 2
470 xhr :post, :preview, :project_id => 1, :id => 'New page',
470 xhr :post, :preview, :project_id => 1, :id => 'New page',
471 :content => { :text => 'h1. New page',
471 :content => { :text => 'h1. New page',
472 :comments => '',
472 :comments => '',
473 :version => 0 }
473 :version => 0 }
474 assert_response :success
474 assert_response :success
475 assert_template 'common/_preview'
475 assert_template 'common/_preview'
476 assert_tag :tag => 'h1', :content => /New page/
476 assert_tag :tag => 'h1', :content => /New page/
477 end
477 end
478
478
479 def test_history
479 def test_history
480 get :history, :project_id => 1, :id => 'CookBook_documentation'
480 get :history, :project_id => 1, :id => 'CookBook_documentation'
481 assert_response :success
481 assert_response :success
482 assert_template 'history'
482 assert_template 'history'
483 assert_not_nil assigns(:versions)
483 assert_not_nil assigns(:versions)
484 assert_equal 3, assigns(:versions).size
484 assert_equal 3, assigns(:versions).size
485 assert_select "input[type=submit][name=commit]"
485 assert_select "input[type=submit][name=commit]"
486 end
486 end
487
487
488 def test_history_with_one_version
488 def test_history_with_one_version
489 get :history, :project_id => 1, :id => 'Another_page'
489 get :history, :project_id => 1, :id => 'Another_page'
490 assert_response :success
490 assert_response :success
491 assert_template 'history'
491 assert_template 'history'
492 assert_not_nil assigns(:versions)
492 assert_not_nil assigns(:versions)
493 assert_equal 1, assigns(:versions).size
493 assert_equal 1, assigns(:versions).size
494 assert_select "input[type=submit][name=commit]", false
494 assert_select "input[type=submit][name=commit]", false
495 end
495 end
496
496
497 def test_diff
497 def test_diff
498 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1
498 content = WikiPage.find(1).content
499 assert_difference 'WikiContent::Version.count', 2 do
500 content.text = "Line removed\nThis is a sample text for testing diffs"
501 content.save!
502 content.text = "This is a sample text for testing diffs\nLine added"
503 content.save!
504 end
505
506 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => content.version, :version_from => (content.version - 1)
499 assert_response :success
507 assert_response :success
500 assert_template 'diff'
508 assert_template 'diff'
501 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
509 assert_select 'span.diff_out', :text => 'Line removed'
502 :content => /updated/
510 assert_select 'span.diff_in', :text => 'Line added'
503 end
511 end
504
512
505 def test_annotate
513 def test_annotate
506 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
514 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
507 assert_response :success
515 assert_response :success
508 assert_template 'annotate'
516 assert_template 'annotate'
509
517
510 # Line 1
518 # Line 1
511 assert_tag :tag => 'tr', :child => {
519 assert_tag :tag => 'tr', :child => {
512 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
520 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
513 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
521 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
514 :tag => 'td', :content => /h1\. CookBook documentation/
522 :tag => 'td', :content => /h1\. CookBook documentation/
515 }
523 }
516 }
524 }
517 }
525 }
518
526
519 # Line 5
527 # Line 5
520 assert_tag :tag => 'tr', :child => {
528 assert_tag :tag => 'tr', :child => {
521 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
529 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
522 :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/, :sibling => {
530 :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/, :sibling => {
523 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
531 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
524 }
532 }
525 }
533 }
526 }
534 }
527 end
535 end
528
536
529 def test_get_rename
537 def test_get_rename
530 @request.session[:user_id] = 2
538 @request.session[:user_id] = 2
531 get :rename, :project_id => 1, :id => 'Another_page'
539 get :rename, :project_id => 1, :id => 'Another_page'
532 assert_response :success
540 assert_response :success
533 assert_template 'rename'
541 assert_template 'rename'
534 assert_tag 'option',
542 assert_tag 'option',
535 :attributes => {:value => ''},
543 :attributes => {:value => ''},
536 :content => '',
544 :content => '',
537 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
545 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
538 assert_no_tag 'option',
546 assert_no_tag 'option',
539 :attributes => {:selected => 'selected'},
547 :attributes => {:selected => 'selected'},
540 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
548 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
541 end
549 end
542
550
543 def test_get_rename_child_page
551 def test_get_rename_child_page
544 @request.session[:user_id] = 2
552 @request.session[:user_id] = 2
545 get :rename, :project_id => 1, :id => 'Child_1'
553 get :rename, :project_id => 1, :id => 'Child_1'
546 assert_response :success
554 assert_response :success
547 assert_template 'rename'
555 assert_template 'rename'
548 assert_tag 'option',
556 assert_tag 'option',
549 :attributes => {:value => ''},
557 :attributes => {:value => ''},
550 :content => '',
558 :content => '',
551 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
559 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
552 assert_tag 'option',
560 assert_tag 'option',
553 :attributes => {:value => '2', :selected => 'selected'},
561 :attributes => {:value => '2', :selected => 'selected'},
554 :content => /Another page/,
562 :content => /Another page/,
555 :parent => {
563 :parent => {
556 :tag => 'select',
564 :tag => 'select',
557 :attributes => {:name => 'wiki_page[parent_id]'}
565 :attributes => {:name => 'wiki_page[parent_id]'}
558 }
566 }
559 end
567 end
560
568
561 def test_rename_with_redirect
569 def test_rename_with_redirect
562 @request.session[:user_id] = 2
570 @request.session[:user_id] = 2
563 post :rename, :project_id => 1, :id => 'Another_page',
571 post :rename, :project_id => 1, :id => 'Another_page',
564 :wiki_page => { :title => 'Another renamed page',
572 :wiki_page => { :title => 'Another renamed page',
565 :redirect_existing_links => 1 }
573 :redirect_existing_links => 1 }
566 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
574 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
567 wiki = Project.find(1).wiki
575 wiki = Project.find(1).wiki
568 # Check redirects
576 # Check redirects
569 assert_not_nil wiki.find_page('Another page')
577 assert_not_nil wiki.find_page('Another page')
570 assert_nil wiki.find_page('Another page', :with_redirect => false)
578 assert_nil wiki.find_page('Another page', :with_redirect => false)
571 end
579 end
572
580
573 def test_rename_without_redirect
581 def test_rename_without_redirect
574 @request.session[:user_id] = 2
582 @request.session[:user_id] = 2
575 post :rename, :project_id => 1, :id => 'Another_page',
583 post :rename, :project_id => 1, :id => 'Another_page',
576 :wiki_page => { :title => 'Another renamed page',
584 :wiki_page => { :title => 'Another renamed page',
577 :redirect_existing_links => "0" }
585 :redirect_existing_links => "0" }
578 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
586 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
579 wiki = Project.find(1).wiki
587 wiki = Project.find(1).wiki
580 # Check that there's no redirects
588 # Check that there's no redirects
581 assert_nil wiki.find_page('Another page')
589 assert_nil wiki.find_page('Another page')
582 end
590 end
583
591
584 def test_rename_with_parent_assignment
592 def test_rename_with_parent_assignment
585 @request.session[:user_id] = 2
593 @request.session[:user_id] = 2
586 post :rename, :project_id => 1, :id => 'Another_page',
594 post :rename, :project_id => 1, :id => 'Another_page',
587 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
595 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
588 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
596 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
589 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
597 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
590 end
598 end
591
599
592 def test_rename_with_parent_unassignment
600 def test_rename_with_parent_unassignment
593 @request.session[:user_id] = 2
601 @request.session[:user_id] = 2
594 post :rename, :project_id => 1, :id => 'Child_1',
602 post :rename, :project_id => 1, :id => 'Child_1',
595 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
603 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
596 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
604 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
597 assert_nil WikiPage.find_by_title('Child_1').parent
605 assert_nil WikiPage.find_by_title('Child_1').parent
598 end
606 end
599
607
600 def test_destroy_child
608 def test_destroy_child
601 @request.session[:user_id] = 2
609 @request.session[:user_id] = 2
602 delete :destroy, :project_id => 1, :id => 'Child_1'
610 delete :destroy, :project_id => 1, :id => 'Child_1'
603 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
611 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
604 end
612 end
605
613
606 def test_destroy_parent
614 def test_destroy_parent
607 @request.session[:user_id] = 2
615 @request.session[:user_id] = 2
608 assert_no_difference('WikiPage.count') do
616 assert_no_difference('WikiPage.count') do
609 delete :destroy, :project_id => 1, :id => 'Another_page'
617 delete :destroy, :project_id => 1, :id => 'Another_page'
610 end
618 end
611 assert_response :success
619 assert_response :success
612 assert_template 'destroy'
620 assert_template 'destroy'
613 end
621 end
614
622
615 def test_destroy_parent_with_nullify
623 def test_destroy_parent_with_nullify
616 @request.session[:user_id] = 2
624 @request.session[:user_id] = 2
617 assert_difference('WikiPage.count', -1) do
625 assert_difference('WikiPage.count', -1) do
618 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
626 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
619 end
627 end
620 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
628 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
621 assert_nil WikiPage.find_by_id(2)
629 assert_nil WikiPage.find_by_id(2)
622 end
630 end
623
631
624 def test_destroy_parent_with_cascade
632 def test_destroy_parent_with_cascade
625 @request.session[:user_id] = 2
633 @request.session[:user_id] = 2
626 assert_difference('WikiPage.count', -3) do
634 assert_difference('WikiPage.count', -3) do
627 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
635 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
628 end
636 end
629 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
637 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
630 assert_nil WikiPage.find_by_id(2)
638 assert_nil WikiPage.find_by_id(2)
631 assert_nil WikiPage.find_by_id(5)
639 assert_nil WikiPage.find_by_id(5)
632 end
640 end
633
641
634 def test_destroy_parent_with_reassign
642 def test_destroy_parent_with_reassign
635 @request.session[:user_id] = 2
643 @request.session[:user_id] = 2
636 assert_difference('WikiPage.count', -1) do
644 assert_difference('WikiPage.count', -1) do
637 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
645 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
638 end
646 end
639 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
647 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
640 assert_nil WikiPage.find_by_id(2)
648 assert_nil WikiPage.find_by_id(2)
641 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
649 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
642 end
650 end
643
651
644 def test_index
652 def test_index
645 get :index, :project_id => 'ecookbook'
653 get :index, :project_id => 'ecookbook'
646 assert_response :success
654 assert_response :success
647 assert_template 'index'
655 assert_template 'index'
648 pages = assigns(:pages)
656 pages = assigns(:pages)
649 assert_not_nil pages
657 assert_not_nil pages
650 assert_equal Project.find(1).wiki.pages.size, pages.size
658 assert_equal Project.find(1).wiki.pages.size, pages.size
651 assert_equal pages.first.content.updated_on, pages.first.updated_on
659 assert_equal pages.first.content.updated_on, pages.first.updated_on
652
660
653 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
661 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
654 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
662 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
655 :content => 'CookBook documentation' },
663 :content => 'CookBook documentation' },
656 :child => { :tag => 'ul',
664 :child => { :tag => 'ul',
657 :child => { :tag => 'li',
665 :child => { :tag => 'li',
658 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
666 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
659 :content => 'Page with an inline image' } } } },
667 :content => 'Page with an inline image' } } } },
660 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
668 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
661 :content => 'Another page' } }
669 :content => 'Another page' } }
662 end
670 end
663
671
664 def test_index_should_include_atom_link
672 def test_index_should_include_atom_link
665 get :index, :project_id => 'ecookbook'
673 get :index, :project_id => 'ecookbook'
666 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
674 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
667 end
675 end
668
676
669 def test_export_to_html
677 def test_export_to_html
670 @request.session[:user_id] = 2
678 @request.session[:user_id] = 2
671 get :export, :project_id => 'ecookbook'
679 get :export, :project_id => 'ecookbook'
672
680
673 assert_response :success
681 assert_response :success
674 assert_not_nil assigns(:pages)
682 assert_not_nil assigns(:pages)
675 assert assigns(:pages).any?
683 assert assigns(:pages).any?
676 assert_equal "text/html", @response.content_type
684 assert_equal "text/html", @response.content_type
677
685
678 assert_select "a[name=?]", "CookBook_documentation"
686 assert_select "a[name=?]", "CookBook_documentation"
679 assert_select "a[name=?]", "Another_page"
687 assert_select "a[name=?]", "Another_page"
680 assert_select "a[name=?]", "Page_with_an_inline_image"
688 assert_select "a[name=?]", "Page_with_an_inline_image"
681 end
689 end
682
690
683 def test_export_to_pdf
691 def test_export_to_pdf
684 @request.session[:user_id] = 2
692 @request.session[:user_id] = 2
685 get :export, :project_id => 'ecookbook', :format => 'pdf'
693 get :export, :project_id => 'ecookbook', :format => 'pdf'
686
694
687 assert_response :success
695 assert_response :success
688 assert_not_nil assigns(:pages)
696 assert_not_nil assigns(:pages)
689 assert assigns(:pages).any?
697 assert assigns(:pages).any?
690 assert_equal 'application/pdf', @response.content_type
698 assert_equal 'application/pdf', @response.content_type
691 assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
699 assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
692 assert @response.body.starts_with?('%PDF')
700 assert @response.body.starts_with?('%PDF')
693 end
701 end
694
702
695 def test_export_without_permission_should_be_denied
703 def test_export_without_permission_should_be_denied
696 @request.session[:user_id] = 2
704 @request.session[:user_id] = 2
697 Role.find_by_name('Manager').remove_permission! :export_wiki_pages
705 Role.find_by_name('Manager').remove_permission! :export_wiki_pages
698 get :export, :project_id => 'ecookbook'
706 get :export, :project_id => 'ecookbook'
699
707
700 assert_response 403
708 assert_response 403
701 end
709 end
702
710
703 def test_date_index
711 def test_date_index
704 get :date_index, :project_id => 'ecookbook'
712 get :date_index, :project_id => 'ecookbook'
705
713
706 assert_response :success
714 assert_response :success
707 assert_template 'date_index'
715 assert_template 'date_index'
708 assert_not_nil assigns(:pages)
716 assert_not_nil assigns(:pages)
709 assert_not_nil assigns(:pages_by_date)
717 assert_not_nil assigns(:pages_by_date)
710
718
711 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
719 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
712 end
720 end
713
721
714 def test_not_found
722 def test_not_found
715 get :show, :project_id => 999
723 get :show, :project_id => 999
716 assert_response 404
724 assert_response 404
717 end
725 end
718
726
719 def test_protect_page
727 def test_protect_page
720 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
728 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
721 assert !page.protected?
729 assert !page.protected?
722 @request.session[:user_id] = 2
730 @request.session[:user_id] = 2
723 post :protect, :project_id => 1, :id => page.title, :protected => '1'
731 post :protect, :project_id => 1, :id => page.title, :protected => '1'
724 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
732 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
725 assert page.reload.protected?
733 assert page.reload.protected?
726 end
734 end
727
735
728 def test_unprotect_page
736 def test_unprotect_page
729 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
737 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
730 assert page.protected?
738 assert page.protected?
731 @request.session[:user_id] = 2
739 @request.session[:user_id] = 2
732 post :protect, :project_id => 1, :id => page.title, :protected => '0'
740 post :protect, :project_id => 1, :id => page.title, :protected => '0'
733 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
741 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
734 assert !page.reload.protected?
742 assert !page.reload.protected?
735 end
743 end
736
744
737 def test_show_page_with_edit_link
745 def test_show_page_with_edit_link
738 @request.session[:user_id] = 2
746 @request.session[:user_id] = 2
739 get :show, :project_id => 1
747 get :show, :project_id => 1
740 assert_response :success
748 assert_response :success
741 assert_template 'show'
749 assert_template 'show'
742 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
750 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
743 end
751 end
744
752
745 def test_show_page_without_edit_link
753 def test_show_page_without_edit_link
746 @request.session[:user_id] = 4
754 @request.session[:user_id] = 4
747 get :show, :project_id => 1
755 get :show, :project_id => 1
748 assert_response :success
756 assert_response :success
749 assert_template 'show'
757 assert_template 'show'
750 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
758 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
751 end
759 end
752
760
753 def test_show_pdf
761 def test_show_pdf
754 @request.session[:user_id] = 2
762 @request.session[:user_id] = 2
755 get :show, :project_id => 1, :format => 'pdf'
763 get :show, :project_id => 1, :format => 'pdf'
756 assert_response :success
764 assert_response :success
757 assert_not_nil assigns(:page)
765 assert_not_nil assigns(:page)
758 assert_equal 'application/pdf', @response.content_type
766 assert_equal 'application/pdf', @response.content_type
759 assert_equal 'attachment; filename="CookBook_documentation.pdf"',
767 assert_equal 'attachment; filename="CookBook_documentation.pdf"',
760 @response.headers['Content-Disposition']
768 @response.headers['Content-Disposition']
761 end
769 end
762
770
763 def test_show_html
771 def test_show_html
764 @request.session[:user_id] = 2
772 @request.session[:user_id] = 2
765 get :show, :project_id => 1, :format => 'html'
773 get :show, :project_id => 1, :format => 'html'
766 assert_response :success
774 assert_response :success
767 assert_not_nil assigns(:page)
775 assert_not_nil assigns(:page)
768 assert_equal 'text/html', @response.content_type
776 assert_equal 'text/html', @response.content_type
769 assert_equal 'attachment; filename="CookBook_documentation.html"',
777 assert_equal 'attachment; filename="CookBook_documentation.html"',
770 @response.headers['Content-Disposition']
778 @response.headers['Content-Disposition']
771 assert_tag 'h1', :content => 'CookBook documentation'
779 assert_tag 'h1', :content => 'CookBook documentation'
772 end
780 end
773
781
774 def test_show_versioned_html
782 def test_show_versioned_html
775 @request.session[:user_id] = 2
783 @request.session[:user_id] = 2
776 get :show, :project_id => 1, :format => 'html', :version => 2
784 get :show, :project_id => 1, :format => 'html', :version => 2
777 assert_response :success
785 assert_response :success
778 assert_not_nil assigns(:content)
786 assert_not_nil assigns(:content)
779 assert_equal 2, assigns(:content).version
787 assert_equal 2, assigns(:content).version
780 assert_equal 'text/html', @response.content_type
788 assert_equal 'text/html', @response.content_type
781 assert_equal 'attachment; filename="CookBook_documentation.html"',
789 assert_equal 'attachment; filename="CookBook_documentation.html"',
782 @response.headers['Content-Disposition']
790 @response.headers['Content-Disposition']
783 assert_tag 'h1', :content => 'CookBook documentation'
791 assert_tag 'h1', :content => 'CookBook documentation'
784 end
792 end
785
793
786 def test_show_txt
794 def test_show_txt
787 @request.session[:user_id] = 2
795 @request.session[:user_id] = 2
788 get :show, :project_id => 1, :format => 'txt'
796 get :show, :project_id => 1, :format => 'txt'
789 assert_response :success
797 assert_response :success
790 assert_not_nil assigns(:page)
798 assert_not_nil assigns(:page)
791 assert_equal 'text/plain', @response.content_type
799 assert_equal 'text/plain', @response.content_type
792 assert_equal 'attachment; filename="CookBook_documentation.txt"',
800 assert_equal 'attachment; filename="CookBook_documentation.txt"',
793 @response.headers['Content-Disposition']
801 @response.headers['Content-Disposition']
794 assert_include 'h1. CookBook documentation', @response.body
802 assert_include 'h1. CookBook documentation', @response.body
795 end
803 end
796
804
797 def test_show_versioned_txt
805 def test_show_versioned_txt
798 @request.session[:user_id] = 2
806 @request.session[:user_id] = 2
799 get :show, :project_id => 1, :format => 'txt', :version => 2
807 get :show, :project_id => 1, :format => 'txt', :version => 2
800 assert_response :success
808 assert_response :success
801 assert_not_nil assigns(:content)
809 assert_not_nil assigns(:content)
802 assert_equal 2, assigns(:content).version
810 assert_equal 2, assigns(:content).version
803 assert_equal 'text/plain', @response.content_type
811 assert_equal 'text/plain', @response.content_type
804 assert_equal 'attachment; filename="CookBook_documentation.txt"',
812 assert_equal 'attachment; filename="CookBook_documentation.txt"',
805 @response.headers['Content-Disposition']
813 @response.headers['Content-Disposition']
806 assert_include 'h1. CookBook documentation', @response.body
814 assert_include 'h1. CookBook documentation', @response.body
807 end
815 end
808
816
809 def test_edit_unprotected_page
817 def test_edit_unprotected_page
810 # Non members can edit unprotected wiki pages
818 # Non members can edit unprotected wiki pages
811 @request.session[:user_id] = 4
819 @request.session[:user_id] = 4
812 get :edit, :project_id => 1, :id => 'Another_page'
820 get :edit, :project_id => 1, :id => 'Another_page'
813 assert_response :success
821 assert_response :success
814 assert_template 'edit'
822 assert_template 'edit'
815 end
823 end
816
824
817 def test_edit_protected_page_by_nonmember
825 def test_edit_protected_page_by_nonmember
818 # Non members can't edit protected wiki pages
826 # Non members can't edit protected wiki pages
819 @request.session[:user_id] = 4
827 @request.session[:user_id] = 4
820 get :edit, :project_id => 1, :id => 'CookBook_documentation'
828 get :edit, :project_id => 1, :id => 'CookBook_documentation'
821 assert_response 403
829 assert_response 403
822 end
830 end
823
831
824 def test_edit_protected_page_by_member
832 def test_edit_protected_page_by_member
825 @request.session[:user_id] = 2
833 @request.session[:user_id] = 2
826 get :edit, :project_id => 1, :id => 'CookBook_documentation'
834 get :edit, :project_id => 1, :id => 'CookBook_documentation'
827 assert_response :success
835 assert_response :success
828 assert_template 'edit'
836 assert_template 'edit'
829 end
837 end
830
838
831 def test_history_of_non_existing_page_should_return_404
839 def test_history_of_non_existing_page_should_return_404
832 get :history, :project_id => 1, :id => 'Unknown_page'
840 get :history, :project_id => 1, :id => 'Unknown_page'
833 assert_response 404
841 assert_response 404
834 end
842 end
835
843
836 def test_add_attachment
844 def test_add_attachment
837 @request.session[:user_id] = 2
845 @request.session[:user_id] = 2
838 assert_difference 'Attachment.count' do
846 assert_difference 'Attachment.count' do
839 post :add_attachment, :project_id => 1, :id => 'CookBook_documentation',
847 post :add_attachment, :project_id => 1, :id => 'CookBook_documentation',
840 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
848 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
841 end
849 end
842 attachment = Attachment.first(:order => 'id DESC')
850 attachment = Attachment.first(:order => 'id DESC')
843 assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
851 assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
844 end
852 end
845 end
853 end
General Comments 0
You need to be logged in to leave comments. Login now