##// END OF EJS Templates
Merged r6206 from trunk....
Jean-Philippe Lang -
r6087:a8550ba8a633
parent child
Show More
@@ -1,217 +1,221
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 'diff'
18 require 'diff'
19 require 'enumerator'
19 require 'enumerator'
20
20
21 class WikiPage < ActiveRecord::Base
21 class WikiPage < ActiveRecord::Base
22 belongs_to :wiki
22 belongs_to :wiki
23 has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
23 has_one :content, :class_name => 'WikiContent', :foreign_key => 'page_id', :dependent => :destroy
24 acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
24 acts_as_attachable :delete_permission => :delete_wiki_pages_attachments
25 acts_as_tree :dependent => :nullify, :order => 'title'
25 acts_as_tree :dependent => :nullify, :order => 'title'
26
26
27 acts_as_watchable
27 acts_as_watchable
28 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
28 acts_as_event :title => Proc.new {|o| "#{l(:label_wiki)}: #{o.title}"},
29 :description => :text,
29 :description => :text,
30 :datetime => :created_on,
30 :datetime => :created_on,
31 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
31 :url => Proc.new {|o| {:controller => 'wiki', :action => 'show', :project_id => o.wiki.project, :id => o.title}}
32
32
33 acts_as_searchable :columns => ['title', 'text'],
33 acts_as_searchable :columns => ['title', 'text'],
34 :include => [{:wiki => :project}, :content],
34 :include => [{:wiki => :project}, :content],
35 :permission => :view_wiki_pages,
35 :permission => :view_wiki_pages,
36 :project_key => "#{Wiki.table_name}.project_id"
36 :project_key => "#{Wiki.table_name}.project_id"
37
37
38 attr_accessor :redirect_existing_links
38 attr_accessor :redirect_existing_links
39
39
40 validates_presence_of :title
40 validates_presence_of :title
41 validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
41 validates_format_of :title, :with => /^[^,\.\/\?\;\|\s]*$/
42 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
42 validates_uniqueness_of :title, :scope => :wiki_id, :case_sensitive => false
43 validates_associated :content
43 validates_associated :content
44
44
45 # eager load information about last updates, without loading text
45 # eager load information about last updates, without loading text
46 named_scope :with_updated_on, {
46 named_scope :with_updated_on, {
47 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
47 :select => "#{WikiPage.table_name}.*, #{WikiContent.table_name}.updated_on",
48 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
48 :joins => "LEFT JOIN #{WikiContent.table_name} ON #{WikiContent.table_name}.page_id = #{WikiPage.table_name}.id"
49 }
49 }
50
50
51 # Wiki pages that are protected by default
51 # Wiki pages that are protected by default
52 DEFAULT_PROTECTED_PAGES = %w(sidebar)
52 DEFAULT_PROTECTED_PAGES = %w(sidebar)
53
53
54 def after_initialize
54 def after_initialize
55 if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
55 if new_record? && DEFAULT_PROTECTED_PAGES.include?(title.to_s.downcase)
56 self.protected = true
56 self.protected = true
57 end
57 end
58 end
58 end
59
59
60 def visible?(user=User.current)
60 def visible?(user=User.current)
61 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
61 !user.nil? && user.allowed_to?(:view_wiki_pages, project)
62 end
62 end
63
63
64 def title=(value)
64 def title=(value)
65 value = Wiki.titleize(value)
65 value = Wiki.titleize(value)
66 @previous_title = read_attribute(:title) if @previous_title.blank?
66 @previous_title = read_attribute(:title) if @previous_title.blank?
67 write_attribute(:title, value)
67 write_attribute(:title, value)
68 end
68 end
69
69
70 def before_save
70 def before_save
71 self.title = Wiki.titleize(title)
71 self.title = Wiki.titleize(title)
72 # Manage redirects if the title has changed
72 # Manage redirects if the title has changed
73 if !@previous_title.blank? && (@previous_title != title) && !new_record?
73 if !@previous_title.blank? && (@previous_title != title) && !new_record?
74 # Update redirects that point to the old title
74 # Update redirects that point to the old title
75 wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
75 wiki.redirects.find_all_by_redirects_to(@previous_title).each do |r|
76 r.redirects_to = title
76 r.redirects_to = title
77 r.title == r.redirects_to ? r.destroy : r.save
77 r.title == r.redirects_to ? r.destroy : r.save
78 end
78 end
79 # Remove redirects for the new title
79 # Remove redirects for the new title
80 wiki.redirects.find_all_by_title(title).each(&:destroy)
80 wiki.redirects.find_all_by_title(title).each(&:destroy)
81 # Create a redirect to the new title
81 # Create a redirect to the new title
82 wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
82 wiki.redirects << WikiRedirect.new(:title => @previous_title, :redirects_to => title) unless redirect_existing_links == "0"
83 @previous_title = nil
83 @previous_title = nil
84 end
84 end
85 end
85 end
86
86
87 def before_destroy
87 def before_destroy
88 # Remove redirects to this page
88 # Remove redirects to this page
89 wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
89 wiki.redirects.find_all_by_redirects_to(title).each(&:destroy)
90 end
90 end
91
91
92 def pretty_title
92 def pretty_title
93 WikiPage.pretty_title(title)
93 WikiPage.pretty_title(title)
94 end
94 end
95
95
96 def content_for_version(version=nil)
96 def content_for_version(version=nil)
97 result = content.versions.find_by_version(version.to_i) if version
97 result = content.versions.find_by_version(version.to_i) if version
98 result ||= content
98 result ||= content
99 result
99 result
100 end
100 end
101
101
102 def diff(version_to=nil, version_from=nil)
102 def diff(version_to=nil, version_from=nil)
103 version_to = version_to ? version_to.to_i : self.content.version
103 version_to = version_to ? version_to.to_i : self.content.version
104 version_from = version_from ? version_from.to_i : version_to - 1
104 version_from = version_from ? version_from.to_i : version_to - 1
105 version_to, version_from = version_from, version_to unless version_from < version_to
105 version_to, version_from = version_from, version_to unless version_from < version_to
106
106
107 content_to = content.versions.find_by_version(version_to)
107 content_to = content.versions.find_by_version(version_to)
108 content_from = content.versions.find_by_version(version_from)
108 content_from = content.versions.find_by_version(version_from)
109
109
110 (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
110 (content_to && content_from) ? WikiDiff.new(content_to, content_from) : nil
111 end
111 end
112
112
113 def annotate(version=nil)
113 def annotate(version=nil)
114 version = version ? version.to_i : self.content.version
114 version = version ? version.to_i : self.content.version
115 c = content.versions.find_by_version(version)
115 c = content.versions.find_by_version(version)
116 c ? WikiAnnotate.new(c) : nil
116 c ? WikiAnnotate.new(c) : nil
117 end
117 end
118
118
119 def self.pretty_title(str)
119 def self.pretty_title(str)
120 (str && str.is_a?(String)) ? str.tr('_', ' ') : str
120 (str && str.is_a?(String)) ? str.tr('_', ' ') : str
121 end
121 end
122
122
123 def project
123 def project
124 wiki.project
124 wiki.project
125 end
125 end
126
126
127 def text
127 def text
128 content.text if content
128 content.text if content
129 end
129 end
130
130
131 def updated_on
131 def updated_on
132 unless @updated_on
132 unless @updated_on
133 if time = read_attribute(:updated_on)
133 if time = read_attribute(:updated_on)
134 # content updated_on was eager loaded with the page
134 # content updated_on was eager loaded with the page
135 @updated_on = Time.parse(time) rescue nil
135 @updated_on = Time.parse(time) rescue nil
136 else
136 else
137 @updated_on = content && content.updated_on
137 @updated_on = content && content.updated_on
138 end
138 end
139 end
139 end
140 @updated_on
140 @updated_on
141 end
141 end
142
142
143 # Returns true if usr is allowed to edit the page, otherwise false
143 # Returns true if usr is allowed to edit the page, otherwise false
144 def editable_by?(usr)
144 def editable_by?(usr)
145 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
145 !protected? || usr.allowed_to?(:protect_wiki_pages, wiki.project)
146 end
146 end
147
147
148 def attachments_deletable?(usr=User.current)
148 def attachments_deletable?(usr=User.current)
149 editable_by?(usr) && super(usr)
149 editable_by?(usr) && super(usr)
150 end
150 end
151
151
152 def parent_title
152 def parent_title
153 @parent_title || (self.parent && self.parent.pretty_title)
153 @parent_title || (self.parent && self.parent.pretty_title)
154 end
154 end
155
155
156 def parent_title=(t)
156 def parent_title=(t)
157 @parent_title = t
157 @parent_title = t
158 parent_page = t.blank? ? nil : self.wiki.find_page(t)
158 parent_page = t.blank? ? nil : self.wiki.find_page(t)
159 self.parent = parent_page
159 self.parent = parent_page
160 end
160 end
161
161
162 protected
162 protected
163
163
164 def validate
164 def validate
165 errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
165 errors.add(:parent_title, :invalid) if !@parent_title.blank? && parent.nil?
166 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
166 errors.add(:parent_title, :circular_dependency) if parent && (parent == self || parent.ancestors.include?(self))
167 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
167 errors.add(:parent_title, :not_same_project) if parent && (parent.wiki_id != wiki_id)
168 end
168 end
169 end
169 end
170
170
171 class WikiDiff < Redmine::Helpers::Diff
171 class WikiDiff < Redmine::Helpers::Diff
172 attr_reader :content_to, :content_from
172 attr_reader :content_to, :content_from
173
173
174 def initialize(content_to, content_from)
174 def initialize(content_to, content_from)
175 @content_to = content_to
175 @content_to = content_to
176 @content_from = content_from
176 @content_from = content_from
177 super(content_to.text, content_from.text)
177 super(content_to.text, content_from.text)
178 end
178 end
179 end
179 end
180
180
181 class WikiAnnotate
181 class WikiAnnotate
182 attr_reader :lines, :content
182 attr_reader :lines, :content
183
183
184 def initialize(content)
184 def initialize(content)
185 @content = content
185 @content = content
186 current = content
186 current = content
187 current_lines = current.text.split(/\r?\n/)
187 current_lines = current.text.split(/\r?\n/)
188 @lines = current_lines.collect {|t| [nil, nil, t]}
188 @lines = current_lines.collect {|t| [nil, nil, t]}
189 positions = []
189 positions = []
190 current_lines.size.times {|i| positions << i}
190 current_lines.size.times {|i| positions << i}
191 while (current.previous)
191 while (current.previous)
192 d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
192 d = current.previous.text.split(/\r?\n/).diff(current.text.split(/\r?\n/)).diffs.flatten
193 d.each_slice(3) do |s|
193 d.each_slice(3) do |s|
194 sign, line = s[0], s[1]
194 sign, line = s[0], s[1]
195 if sign == '+' && positions[line] && positions[line] != -1
195 if sign == '+' && positions[line] && positions[line] != -1
196 if @lines[positions[line]][0].nil?
196 if @lines[positions[line]][0].nil?
197 @lines[positions[line]][0] = current.version
197 @lines[positions[line]][0] = current.version
198 @lines[positions[line]][1] = current.author
198 @lines[positions[line]][1] = current.author
199 end
199 end
200 end
200 end
201 end
201 end
202 d.each_slice(3) do |s|
202 d.each_slice(3) do |s|
203 sign, line = s[0], s[1]
203 sign, line = s[0], s[1]
204 if sign == '-'
204 if sign == '-'
205 positions.insert(line, -1)
205 positions.insert(line, -1)
206 else
206 else
207 positions[line] = nil
207 positions[line] = nil
208 end
208 end
209 end
209 end
210 positions.compact!
210 positions.compact!
211 # Stop if every line is annotated
211 # Stop if every line is annotated
212 break unless @lines.detect { |line| line[0].nil? }
212 break unless @lines.detect { |line| line[0].nil? }
213 current = current.previous
213 current = current.previous
214 end
214 end
215 @lines.each { |line| line[0] ||= current.version }
215 @lines.each { |line|
216 line[0] ||= current.version
217 # if the last known version is > 1 (eg. history was cleared), we don't know the author
218 line[1] ||= current.author if current.version == 1
219 }
216 end
220 end
217 end
221 end
@@ -1,511 +1,521
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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, :enabled_modules, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, :attachments
25 fixtures :projects, :users, :roles, :members, :member_roles, :enabled_modules, :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions, :attachments
26
26
27 def setup
27 def setup
28 @controller = WikiController.new
28 @controller = WikiController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 User.current = nil
31 User.current = nil
32 end
32 end
33
33
34 def test_show_start_page
34 def test_show_start_page
35 get :show, :project_id => 'ecookbook'
35 get :show, :project_id => 'ecookbook'
36 assert_response :success
36 assert_response :success
37 assert_template 'show'
37 assert_template 'show'
38 assert_tag :tag => 'h1', :content => /CookBook documentation/
38 assert_tag :tag => 'h1', :content => /CookBook documentation/
39
39
40 # child_pages macro
40 # child_pages macro
41 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
41 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
42 :child => { :tag => 'li',
42 :child => { :tag => 'li',
43 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
43 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
44 :content => 'Page with an inline image' } }
44 :content => 'Page with an inline image' } }
45 end
45 end
46
46
47 def test_show_page_with_name
47 def test_show_page_with_name
48 get :show, :project_id => 1, :id => 'Another_page'
48 get :show, :project_id => 1, :id => 'Another_page'
49 assert_response :success
49 assert_response :success
50 assert_template 'show'
50 assert_template 'show'
51 assert_tag :tag => 'h1', :content => /Another page/
51 assert_tag :tag => 'h1', :content => /Another page/
52 # Included page with an inline image
52 # Included page with an inline image
53 assert_tag :tag => 'p', :content => /This is an inline image/
53 assert_tag :tag => 'p', :content => /This is an inline image/
54 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
54 assert_tag :tag => 'img', :attributes => { :src => '/attachments/download/3',
55 :alt => 'This is a logo' }
55 :alt => 'This is a logo' }
56 end
56 end
57
57
58 def test_show_redirected_page
58 def test_show_redirected_page
59 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
59 WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
60
60
61 get :show, :project_id => 'ecookbook', :id => 'Old_title'
61 get :show, :project_id => 'ecookbook', :id => 'Old_title'
62 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
62 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
63 end
63 end
64
64
65 def test_show_with_sidebar
65 def test_show_with_sidebar
66 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
66 page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
67 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
67 page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
68 page.save!
68 page.save!
69
69
70 get :show, :project_id => 1, :id => 'Another_page'
70 get :show, :project_id => 1, :id => 'Another_page'
71 assert_response :success
71 assert_response :success
72 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
72 assert_tag :tag => 'div', :attributes => {:id => 'sidebar'},
73 :content => /Side bar content for test_show_with_sidebar/
73 :content => /Side bar content for test_show_with_sidebar/
74 end
74 end
75
75
76 def test_show_unexistent_page_without_edit_right
76 def test_show_unexistent_page_without_edit_right
77 get :show, :project_id => 1, :id => 'Unexistent page'
77 get :show, :project_id => 1, :id => 'Unexistent page'
78 assert_response 404
78 assert_response 404
79 end
79 end
80
80
81 def test_show_unexistent_page_with_edit_right
81 def test_show_unexistent_page_with_edit_right
82 @request.session[:user_id] = 2
82 @request.session[:user_id] = 2
83 get :show, :project_id => 1, :id => 'Unexistent page'
83 get :show, :project_id => 1, :id => 'Unexistent page'
84 assert_response :success
84 assert_response :success
85 assert_template 'edit'
85 assert_template 'edit'
86 end
86 end
87
87
88 def test_create_page
88 def test_create_page
89 @request.session[:user_id] = 2
89 @request.session[:user_id] = 2
90 put :update, :project_id => 1,
90 put :update, :project_id => 1,
91 :id => 'New page',
91 :id => 'New page',
92 :content => {:comments => 'Created the page',
92 :content => {:comments => 'Created the page',
93 :text => "h1. New page\n\nThis is a new page",
93 :text => "h1. New page\n\nThis is a new page",
94 :version => 0}
94 :version => 0}
95 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
95 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
96 page = Project.find(1).wiki.find_page('New page')
96 page = Project.find(1).wiki.find_page('New page')
97 assert !page.new_record?
97 assert !page.new_record?
98 assert_not_nil page.content
98 assert_not_nil page.content
99 assert_equal 'Created the page', page.content.comments
99 assert_equal 'Created the page', page.content.comments
100 end
100 end
101
101
102 def test_create_page_with_attachments
102 def test_create_page_with_attachments
103 @request.session[:user_id] = 2
103 @request.session[:user_id] = 2
104 assert_difference 'WikiPage.count' do
104 assert_difference 'WikiPage.count' do
105 assert_difference 'Attachment.count' do
105 assert_difference 'Attachment.count' do
106 put :update, :project_id => 1,
106 put :update, :project_id => 1,
107 :id => 'New page',
107 :id => 'New page',
108 :content => {:comments => 'Created the page',
108 :content => {:comments => 'Created the page',
109 :text => "h1. New page\n\nThis is a new page",
109 :text => "h1. New page\n\nThis is a new page",
110 :version => 0},
110 :version => 0},
111 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
111 :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
112 end
112 end
113 end
113 end
114 page = Project.find(1).wiki.find_page('New page')
114 page = Project.find(1).wiki.find_page('New page')
115 assert_equal 1, page.attachments.count
115 assert_equal 1, page.attachments.count
116 assert_equal 'testfile.txt', page.attachments.first.filename
116 assert_equal 'testfile.txt', page.attachments.first.filename
117 end
117 end
118
118
119 def test_update_page
119 def test_update_page
120 @request.session[:user_id] = 2
120 @request.session[:user_id] = 2
121 assert_no_difference 'WikiPage.count' do
121 assert_no_difference 'WikiPage.count' do
122 assert_no_difference 'WikiContent.count' do
122 assert_no_difference 'WikiContent.count' do
123 assert_difference 'WikiContent::Version.count' do
123 assert_difference 'WikiContent::Version.count' do
124 put :update, :project_id => 1,
124 put :update, :project_id => 1,
125 :id => 'Another_page',
125 :id => 'Another_page',
126 :content => {
126 :content => {
127 :comments => "my comments",
127 :comments => "my comments",
128 :text => "edited",
128 :text => "edited",
129 :version => 1
129 :version => 1
130 }
130 }
131 end
131 end
132 end
132 end
133 end
133 end
134 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
134 assert_redirected_to '/projects/ecookbook/wiki/Another_page'
135
135
136 page = Wiki.find(1).pages.find_by_title('Another_page')
136 page = Wiki.find(1).pages.find_by_title('Another_page')
137 assert_equal "edited", page.content.text
137 assert_equal "edited", page.content.text
138 assert_equal 2, page.content.version
138 assert_equal 2, page.content.version
139 assert_equal "my comments", page.content.comments
139 assert_equal "my comments", page.content.comments
140 end
140 end
141
141
142 def test_update_page_with_failure
142 def test_update_page_with_failure
143 @request.session[:user_id] = 2
143 @request.session[:user_id] = 2
144 assert_no_difference 'WikiPage.count' do
144 assert_no_difference 'WikiPage.count' do
145 assert_no_difference 'WikiContent.count' do
145 assert_no_difference 'WikiContent.count' do
146 assert_no_difference 'WikiContent::Version.count' do
146 assert_no_difference 'WikiContent::Version.count' do
147 put :update, :project_id => 1,
147 put :update, :project_id => 1,
148 :id => 'Another_page',
148 :id => 'Another_page',
149 :content => {
149 :content => {
150 :comments => 'a' * 300, # failure here, comment is too long
150 :comments => 'a' * 300, # failure here, comment is too long
151 :text => 'edited',
151 :text => 'edited',
152 :version => 1
152 :version => 1
153 }
153 }
154 end
154 end
155 end
155 end
156 end
156 end
157 assert_response :success
157 assert_response :success
158 assert_template 'edit'
158 assert_template 'edit'
159
159
160 assert_error_tag :descendant => {:content => /Comment is too long/}
160 assert_error_tag :descendant => {:content => /Comment is too long/}
161 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => 'edited'
161 assert_tag :tag => 'textarea', :attributes => {:id => 'content_text'}, :content => 'edited'
162 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
162 assert_tag :tag => 'input', :attributes => {:id => 'content_version', :value => '1'}
163 end
163 end
164
164
165 def test_update_stale_page_should_not_raise_an_error
165 def test_update_stale_page_should_not_raise_an_error
166 @request.session[:user_id] = 2
166 @request.session[:user_id] = 2
167 c = Wiki.find(1).find_page('Another_page').content
167 c = Wiki.find(1).find_page('Another_page').content
168 c.text = 'Previous text'
168 c.text = 'Previous text'
169 c.save!
169 c.save!
170 assert_equal 2, c.version
170 assert_equal 2, c.version
171
171
172 assert_no_difference 'WikiPage.count' do
172 assert_no_difference 'WikiPage.count' do
173 assert_no_difference 'WikiContent.count' do
173 assert_no_difference 'WikiContent.count' do
174 assert_no_difference 'WikiContent::Version.count' do
174 assert_no_difference 'WikiContent::Version.count' do
175 put :update, :project_id => 1,
175 put :update, :project_id => 1,
176 :id => 'Another_page',
176 :id => 'Another_page',
177 :content => {
177 :content => {
178 :comments => 'My comments',
178 :comments => 'My comments',
179 :text => 'Text should not be lost',
179 :text => 'Text should not be lost',
180 :version => 1
180 :version => 1
181 }
181 }
182 end
182 end
183 end
183 end
184 end
184 end
185 assert_response :success
185 assert_response :success
186 assert_template 'edit'
186 assert_template 'edit'
187 assert_tag :div,
187 assert_tag :div,
188 :attributes => { :class => /error/ },
188 :attributes => { :class => /error/ },
189 :content => /Data has been updated by another user/
189 :content => /Data has been updated by another user/
190 assert_tag 'textarea',
190 assert_tag 'textarea',
191 :attributes => { :name => 'content[text]' },
191 :attributes => { :name => 'content[text]' },
192 :content => /Text should not be lost/
192 :content => /Text should not be lost/
193 assert_tag 'input',
193 assert_tag 'input',
194 :attributes => { :name => 'content[comments]', :value => 'My comments' }
194 :attributes => { :name => 'content[comments]', :value => 'My comments' }
195
195
196 c.reload
196 c.reload
197 assert_equal 'Previous text', c.text
197 assert_equal 'Previous text', c.text
198 assert_equal 2, c.version
198 assert_equal 2, c.version
199 end
199 end
200
200
201 def test_preview
201 def test_preview
202 @request.session[:user_id] = 2
202 @request.session[:user_id] = 2
203 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
203 xhr :post, :preview, :project_id => 1, :id => 'CookBook_documentation',
204 :content => { :comments => '',
204 :content => { :comments => '',
205 :text => 'this is a *previewed text*',
205 :text => 'this is a *previewed text*',
206 :version => 3 }
206 :version => 3 }
207 assert_response :success
207 assert_response :success
208 assert_template 'common/_preview'
208 assert_template 'common/_preview'
209 assert_tag :tag => 'strong', :content => /previewed text/
209 assert_tag :tag => 'strong', :content => /previewed text/
210 end
210 end
211
211
212 def test_preview_new_page
212 def test_preview_new_page
213 @request.session[:user_id] = 2
213 @request.session[:user_id] = 2
214 xhr :post, :preview, :project_id => 1, :id => 'New page',
214 xhr :post, :preview, :project_id => 1, :id => 'New page',
215 :content => { :text => 'h1. New page',
215 :content => { :text => 'h1. New page',
216 :comments => '',
216 :comments => '',
217 :version => 0 }
217 :version => 0 }
218 assert_response :success
218 assert_response :success
219 assert_template 'common/_preview'
219 assert_template 'common/_preview'
220 assert_tag :tag => 'h1', :content => /New page/
220 assert_tag :tag => 'h1', :content => /New page/
221 end
221 end
222
222
223 def test_history
223 def test_history
224 get :history, :project_id => 1, :id => 'CookBook_documentation'
224 get :history, :project_id => 1, :id => 'CookBook_documentation'
225 assert_response :success
225 assert_response :success
226 assert_template 'history'
226 assert_template 'history'
227 assert_not_nil assigns(:versions)
227 assert_not_nil assigns(:versions)
228 assert_equal 3, assigns(:versions).size
228 assert_equal 3, assigns(:versions).size
229 assert_select "input[type=submit][name=commit]"
229 assert_select "input[type=submit][name=commit]"
230 end
230 end
231
231
232 def test_history_with_one_version
232 def test_history_with_one_version
233 get :history, :project_id => 1, :id => 'Another_page'
233 get :history, :project_id => 1, :id => 'Another_page'
234 assert_response :success
234 assert_response :success
235 assert_template 'history'
235 assert_template 'history'
236 assert_not_nil assigns(:versions)
236 assert_not_nil assigns(:versions)
237 assert_equal 1, assigns(:versions).size
237 assert_equal 1, assigns(:versions).size
238 assert_select "input[type=submit][name=commit]", false
238 assert_select "input[type=submit][name=commit]", false
239 end
239 end
240
240
241 def test_diff
241 def test_diff
242 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1
242 get :diff, :project_id => 1, :id => 'CookBook_documentation', :version => 2, :version_from => 1
243 assert_response :success
243 assert_response :success
244 assert_template 'diff'
244 assert_template 'diff'
245 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
245 assert_tag :tag => 'span', :attributes => { :class => 'diff_in'},
246 :content => /updated/
246 :content => /updated/
247 end
247 end
248
248
249 def test_annotate
249 def test_annotate
250 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
250 get :annotate, :project_id => 1, :id => 'CookBook_documentation', :version => 2
251 assert_response :success
251 assert_response :success
252 assert_template 'annotate'
252 assert_template 'annotate'
253
253 # Line 1
254 # Line 1
254 assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1' },
255 assert_tag :tag => 'tr', :child => {
255 :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/ },
256 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '1', :sibling => {
256 :child => { :tag => 'td', :content => /h1\. CookBook documentation/ }
257 :tag => 'td', :attributes => {:class => 'author'}, :content => /John Smith/, :sibling => {
257 # Line 2
258 :tag => 'td', :content => /h1\. CookBook documentation/
258 assert_tag :tag => 'tr', :child => { :tag => 'th', :attributes => {:class => 'line-num'}, :content => '2' },
259 }
259 :child => { :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/ },
260 }
260 :child => { :tag => 'td', :content => /Some updated \[\[documentation\]\] here/ }
261 }
262
263 # Line 5
264 assert_tag :tag => 'tr', :child => {
265 :tag => 'th', :attributes => {:class => 'line-num'}, :content => '5', :sibling => {
266 :tag => 'td', :attributes => {:class => 'author'}, :content => /redMine Admin/, :sibling => {
267 :tag => 'td', :content => /Some updated \[\[documentation\]\] here/
268 }
269 }
270 }
261 end
271 end
262
272
263 def test_get_rename
273 def test_get_rename
264 @request.session[:user_id] = 2
274 @request.session[:user_id] = 2
265 get :rename, :project_id => 1, :id => 'Another_page'
275 get :rename, :project_id => 1, :id => 'Another_page'
266 assert_response :success
276 assert_response :success
267 assert_template 'rename'
277 assert_template 'rename'
268 assert_tag 'option',
278 assert_tag 'option',
269 :attributes => {:value => ''},
279 :attributes => {:value => ''},
270 :content => '',
280 :content => '',
271 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
281 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
272 assert_no_tag 'option',
282 assert_no_tag 'option',
273 :attributes => {:selected => 'selected'},
283 :attributes => {:selected => 'selected'},
274 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
284 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
275 end
285 end
276
286
277 def test_get_rename_child_page
287 def test_get_rename_child_page
278 @request.session[:user_id] = 2
288 @request.session[:user_id] = 2
279 get :rename, :project_id => 1, :id => 'Child_1'
289 get :rename, :project_id => 1, :id => 'Child_1'
280 assert_response :success
290 assert_response :success
281 assert_template 'rename'
291 assert_template 'rename'
282 assert_tag 'option',
292 assert_tag 'option',
283 :attributes => {:value => ''},
293 :attributes => {:value => ''},
284 :content => '',
294 :content => '',
285 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
295 :parent => {:tag => 'select', :attributes => {:name => 'wiki_page[parent_id]'}}
286 assert_tag 'option',
296 assert_tag 'option',
287 :attributes => {:value => '2', :selected => 'selected'},
297 :attributes => {:value => '2', :selected => 'selected'},
288 :content => /Another page/,
298 :content => /Another page/,
289 :parent => {
299 :parent => {
290 :tag => 'select',
300 :tag => 'select',
291 :attributes => {:name => 'wiki_page[parent_id]'}
301 :attributes => {:name => 'wiki_page[parent_id]'}
292 }
302 }
293 end
303 end
294
304
295 def test_rename_with_redirect
305 def test_rename_with_redirect
296 @request.session[:user_id] = 2
306 @request.session[:user_id] = 2
297 post :rename, :project_id => 1, :id => 'Another_page',
307 post :rename, :project_id => 1, :id => 'Another_page',
298 :wiki_page => { :title => 'Another renamed page',
308 :wiki_page => { :title => 'Another renamed page',
299 :redirect_existing_links => 1 }
309 :redirect_existing_links => 1 }
300 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
310 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
301 wiki = Project.find(1).wiki
311 wiki = Project.find(1).wiki
302 # Check redirects
312 # Check redirects
303 assert_not_nil wiki.find_page('Another page')
313 assert_not_nil wiki.find_page('Another page')
304 assert_nil wiki.find_page('Another page', :with_redirect => false)
314 assert_nil wiki.find_page('Another page', :with_redirect => false)
305 end
315 end
306
316
307 def test_rename_without_redirect
317 def test_rename_without_redirect
308 @request.session[:user_id] = 2
318 @request.session[:user_id] = 2
309 post :rename, :project_id => 1, :id => 'Another_page',
319 post :rename, :project_id => 1, :id => 'Another_page',
310 :wiki_page => { :title => 'Another renamed page',
320 :wiki_page => { :title => 'Another renamed page',
311 :redirect_existing_links => "0" }
321 :redirect_existing_links => "0" }
312 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
322 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
313 wiki = Project.find(1).wiki
323 wiki = Project.find(1).wiki
314 # Check that there's no redirects
324 # Check that there's no redirects
315 assert_nil wiki.find_page('Another page')
325 assert_nil wiki.find_page('Another page')
316 end
326 end
317
327
318 def test_rename_with_parent_assignment
328 def test_rename_with_parent_assignment
319 @request.session[:user_id] = 2
329 @request.session[:user_id] = 2
320 post :rename, :project_id => 1, :id => 'Another_page',
330 post :rename, :project_id => 1, :id => 'Another_page',
321 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
331 :wiki_page => { :title => 'Another page', :redirect_existing_links => "0", :parent_id => '4' }
322 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
332 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
323 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
333 assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
324 end
334 end
325
335
326 def test_rename_with_parent_unassignment
336 def test_rename_with_parent_unassignment
327 @request.session[:user_id] = 2
337 @request.session[:user_id] = 2
328 post :rename, :project_id => 1, :id => 'Child_1',
338 post :rename, :project_id => 1, :id => 'Child_1',
329 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
339 :wiki_page => { :title => 'Child 1', :redirect_existing_links => "0", :parent_id => '' }
330 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
340 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
331 assert_nil WikiPage.find_by_title('Child_1').parent
341 assert_nil WikiPage.find_by_title('Child_1').parent
332 end
342 end
333
343
334 def test_destroy_child
344 def test_destroy_child
335 @request.session[:user_id] = 2
345 @request.session[:user_id] = 2
336 delete :destroy, :project_id => 1, :id => 'Child_1'
346 delete :destroy, :project_id => 1, :id => 'Child_1'
337 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
347 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
338 end
348 end
339
349
340 def test_destroy_parent
350 def test_destroy_parent
341 @request.session[:user_id] = 2
351 @request.session[:user_id] = 2
342 assert_no_difference('WikiPage.count') do
352 assert_no_difference('WikiPage.count') do
343 delete :destroy, :project_id => 1, :id => 'Another_page'
353 delete :destroy, :project_id => 1, :id => 'Another_page'
344 end
354 end
345 assert_response :success
355 assert_response :success
346 assert_template 'destroy'
356 assert_template 'destroy'
347 end
357 end
348
358
349 def test_destroy_parent_with_nullify
359 def test_destroy_parent_with_nullify
350 @request.session[:user_id] = 2
360 @request.session[:user_id] = 2
351 assert_difference('WikiPage.count', -1) do
361 assert_difference('WikiPage.count', -1) do
352 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
362 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'nullify'
353 end
363 end
354 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
364 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
355 assert_nil WikiPage.find_by_id(2)
365 assert_nil WikiPage.find_by_id(2)
356 end
366 end
357
367
358 def test_destroy_parent_with_cascade
368 def test_destroy_parent_with_cascade
359 @request.session[:user_id] = 2
369 @request.session[:user_id] = 2
360 assert_difference('WikiPage.count', -3) do
370 assert_difference('WikiPage.count', -3) do
361 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
371 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'destroy'
362 end
372 end
363 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
373 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
364 assert_nil WikiPage.find_by_id(2)
374 assert_nil WikiPage.find_by_id(2)
365 assert_nil WikiPage.find_by_id(5)
375 assert_nil WikiPage.find_by_id(5)
366 end
376 end
367
377
368 def test_destroy_parent_with_reassign
378 def test_destroy_parent_with_reassign
369 @request.session[:user_id] = 2
379 @request.session[:user_id] = 2
370 assert_difference('WikiPage.count', -1) do
380 assert_difference('WikiPage.count', -1) do
371 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
381 delete :destroy, :project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1
372 end
382 end
373 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
383 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
374 assert_nil WikiPage.find_by_id(2)
384 assert_nil WikiPage.find_by_id(2)
375 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
385 assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
376 end
386 end
377
387
378 def test_index
388 def test_index
379 get :index, :project_id => 'ecookbook'
389 get :index, :project_id => 'ecookbook'
380 assert_response :success
390 assert_response :success
381 assert_template 'index'
391 assert_template 'index'
382 pages = assigns(:pages)
392 pages = assigns(:pages)
383 assert_not_nil pages
393 assert_not_nil pages
384 assert_equal Project.find(1).wiki.pages.size, pages.size
394 assert_equal Project.find(1).wiki.pages.size, pages.size
385 assert_equal pages.first.content.updated_on, pages.first.updated_on
395 assert_equal pages.first.content.updated_on, pages.first.updated_on
386
396
387 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
397 assert_tag :ul, :attributes => { :class => 'pages-hierarchy' },
388 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
398 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/CookBook_documentation' },
389 :content => 'CookBook documentation' },
399 :content => 'CookBook documentation' },
390 :child => { :tag => 'ul',
400 :child => { :tag => 'ul',
391 :child => { :tag => 'li',
401 :child => { :tag => 'li',
392 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
402 :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Page_with_an_inline_image' },
393 :content => 'Page with an inline image' } } } },
403 :content => 'Page with an inline image' } } } },
394 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
404 :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' },
395 :content => 'Another page' } }
405 :content => 'Another page' } }
396 end
406 end
397
407
398 def test_index_should_include_atom_link
408 def test_index_should_include_atom_link
399 get :index, :project_id => 'ecookbook'
409 get :index, :project_id => 'ecookbook'
400 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
410 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
401 end
411 end
402
412
403 context "GET :export" do
413 context "GET :export" do
404 context "with an authorized user to export the wiki" do
414 context "with an authorized user to export the wiki" do
405 setup do
415 setup do
406 @request.session[:user_id] = 2
416 @request.session[:user_id] = 2
407 get :export, :project_id => 'ecookbook'
417 get :export, :project_id => 'ecookbook'
408 end
418 end
409
419
410 should_respond_with :success
420 should_respond_with :success
411 should_assign_to :pages
421 should_assign_to :pages
412 should_respond_with_content_type "text/html"
422 should_respond_with_content_type "text/html"
413 should "export all of the wiki pages to a single html file" do
423 should "export all of the wiki pages to a single html file" do
414 assert_select "a[name=?]", "CookBook_documentation"
424 assert_select "a[name=?]", "CookBook_documentation"
415 assert_select "a[name=?]", "Another_page"
425 assert_select "a[name=?]", "Another_page"
416 assert_select "a[name=?]", "Page_with_an_inline_image"
426 assert_select "a[name=?]", "Page_with_an_inline_image"
417 end
427 end
418
428
419 end
429 end
420
430
421 context "with an unauthorized user" do
431 context "with an unauthorized user" do
422 setup do
432 setup do
423 get :export, :project_id => 'ecookbook'
433 get :export, :project_id => 'ecookbook'
424
434
425 should_respond_with :redirect
435 should_respond_with :redirect
426 should_redirect_to('wiki index') { {:action => 'show', :project_id => @project, :id => nil} }
436 should_redirect_to('wiki index') { {:action => 'show', :project_id => @project, :id => nil} }
427 end
437 end
428 end
438 end
429 end
439 end
430
440
431 context "GET :date_index" do
441 context "GET :date_index" do
432 setup do
442 setup do
433 get :date_index, :project_id => 'ecookbook'
443 get :date_index, :project_id => 'ecookbook'
434 end
444 end
435
445
436 should_respond_with :success
446 should_respond_with :success
437 should_assign_to :pages
447 should_assign_to :pages
438 should_assign_to :pages_by_date
448 should_assign_to :pages_by_date
439 should_render_template 'wiki/date_index'
449 should_render_template 'wiki/date_index'
440
450
441 should "include atom link" do
451 should "include atom link" do
442 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
452 assert_tag 'a', :attributes => { :href => '/projects/ecookbook/activity.atom?show_wiki_edits=1'}
443 end
453 end
444 end
454 end
445
455
446 def test_not_found
456 def test_not_found
447 get :show, :project_id => 999
457 get :show, :project_id => 999
448 assert_response 404
458 assert_response 404
449 end
459 end
450
460
451 def test_protect_page
461 def test_protect_page
452 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
462 page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
453 assert !page.protected?
463 assert !page.protected?
454 @request.session[:user_id] = 2
464 @request.session[:user_id] = 2
455 post :protect, :project_id => 1, :id => page.title, :protected => '1'
465 post :protect, :project_id => 1, :id => page.title, :protected => '1'
456 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
466 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
457 assert page.reload.protected?
467 assert page.reload.protected?
458 end
468 end
459
469
460 def test_unprotect_page
470 def test_unprotect_page
461 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
471 page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
462 assert page.protected?
472 assert page.protected?
463 @request.session[:user_id] = 2
473 @request.session[:user_id] = 2
464 post :protect, :project_id => 1, :id => page.title, :protected => '0'
474 post :protect, :project_id => 1, :id => page.title, :protected => '0'
465 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
475 assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
466 assert !page.reload.protected?
476 assert !page.reload.protected?
467 end
477 end
468
478
469 def test_show_page_with_edit_link
479 def test_show_page_with_edit_link
470 @request.session[:user_id] = 2
480 @request.session[:user_id] = 2
471 get :show, :project_id => 1
481 get :show, :project_id => 1
472 assert_response :success
482 assert_response :success
473 assert_template 'show'
483 assert_template 'show'
474 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
484 assert_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
475 end
485 end
476
486
477 def test_show_page_without_edit_link
487 def test_show_page_without_edit_link
478 @request.session[:user_id] = 4
488 @request.session[:user_id] = 4
479 get :show, :project_id => 1
489 get :show, :project_id => 1
480 assert_response :success
490 assert_response :success
481 assert_template 'show'
491 assert_template 'show'
482 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
492 assert_no_tag :tag => 'a', :attributes => { :href => '/projects/1/wiki/CookBook_documentation/edit' }
483 end
493 end
484
494
485 def test_edit_unprotected_page
495 def test_edit_unprotected_page
486 # Non members can edit unprotected wiki pages
496 # Non members can edit unprotected wiki pages
487 @request.session[:user_id] = 4
497 @request.session[:user_id] = 4
488 get :edit, :project_id => 1, :id => 'Another_page'
498 get :edit, :project_id => 1, :id => 'Another_page'
489 assert_response :success
499 assert_response :success
490 assert_template 'edit'
500 assert_template 'edit'
491 end
501 end
492
502
493 def test_edit_protected_page_by_nonmember
503 def test_edit_protected_page_by_nonmember
494 # Non members can't edit protected wiki pages
504 # Non members can't edit protected wiki pages
495 @request.session[:user_id] = 4
505 @request.session[:user_id] = 4
496 get :edit, :project_id => 1, :id => 'CookBook_documentation'
506 get :edit, :project_id => 1, :id => 'CookBook_documentation'
497 assert_response 403
507 assert_response 403
498 end
508 end
499
509
500 def test_edit_protected_page_by_member
510 def test_edit_protected_page_by_member
501 @request.session[:user_id] = 2
511 @request.session[:user_id] = 2
502 get :edit, :project_id => 1, :id => 'CookBook_documentation'
512 get :edit, :project_id => 1, :id => 'CookBook_documentation'
503 assert_response :success
513 assert_response :success
504 assert_template 'edit'
514 assert_template 'edit'
505 end
515 end
506
516
507 def test_history_of_non_existing_page_should_return_404
517 def test_history_of_non_existing_page_should_return_404
508 get :history, :project_id => 1, :id => 'Unknown_page'
518 get :history, :project_id => 1, :id => 'Unknown_page'
509 assert_response 404
519 assert_response 404
510 end
520 end
511 end
521 end
General Comments 0
You need to be logged in to leave comments. Login now