You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

wiki_controller_test.rb 41KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class WikiControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
  21. :enabled_modules, :wikis, :wiki_pages, :wiki_contents,
  22. :wiki_content_versions, :attachments,
  23. :issues, :issue_statuses, :trackers
  24. def setup
  25. User.current = nil
  26. end
  27. def test_show_start_page
  28. get :show, :params => {:project_id => 'ecookbook'}
  29. assert_response :success
  30. assert_select 'h1', :text => /CookBook documentation/
  31. # child_pages macro
  32. assert_select 'ul.pages-hierarchy>li>a[href=?]', '/projects/ecookbook/wiki/Page_with_an_inline_image',
  33. :text => 'Page with an inline image'
  34. end
  35. def test_export_link
  36. Role.anonymous.add_permission! :export_wiki_pages
  37. get :show, :params => {:project_id => 'ecookbook'}
  38. assert_response :success
  39. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation.txt'
  40. end
  41. def test_show_page_with_name
  42. get :show, :params => {:project_id => 1, :id => 'Another_page'}
  43. assert_response :success
  44. assert_select 'h1', :text => /Another page/
  45. # Included page with an inline image
  46. assert_select 'p', :text => /This is an inline image/
  47. assert_select 'img[src=?][alt=?]', '/attachments/download/3/logo.gif', 'This is a logo'
  48. end
  49. def test_show_old_version
  50. with_settings :default_language => 'en' do
  51. get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'}
  52. end
  53. assert_response :success
  54. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/1', :text => /Previous/
  55. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/diff', :text => /diff/
  56. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/3', :text => /Next/
  57. end
  58. def test_show_old_version_with_attachments
  59. page = WikiPage.find(4)
  60. assert page.attachments.any?
  61. content = page.content
  62. content.text = "update"
  63. content.save!
  64. get :show, :params => {:project_id => 'ecookbook', :id => page.title, :version => '1'}
  65. assert_response :success
  66. end
  67. def test_show_old_version_without_permission_should_be_denied
  68. Role.anonymous.remove_permission! :view_wiki_edits
  69. get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'}
  70. assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Fprojects%2Fecookbook%2Fwiki%2FCookBook_documentation%2F2'
  71. end
  72. def test_show_first_version
  73. with_settings :default_language => 'en' do
  74. get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '1'}
  75. end
  76. assert_response :success
  77. assert_select 'a', :text => /Previous/, :count => 0
  78. assert_select 'a', :text => /diff/, :count => 0
  79. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => /Next/
  80. end
  81. def test_show_redirected_page
  82. WikiRedirect.create!(:wiki_id => 1, :title => 'Old_title', :redirects_to => 'Another_page')
  83. get :show, :params => {:project_id => 'ecookbook', :id => 'Old_title'}
  84. assert_redirected_to '/projects/ecookbook/wiki/Another_page'
  85. end
  86. def test_show_with_sidebar
  87. page = Project.find(1).wiki.pages.new(:title => 'Sidebar')
  88. page.content = WikiContent.new(:text => 'Side bar content for test_show_with_sidebar')
  89. page.save!
  90. get :show, :params => {:project_id => 1, :id => 'Another_page'}
  91. assert_response :success
  92. assert_select 'div#sidebar', :text => /Side bar content for test_show_with_sidebar/
  93. end
  94. def test_show_should_display_section_edit_links
  95. @request.session[:user_id] = 2
  96. get :show, :params => {:project_id => 1, :id => 'Page with sections'}
  97. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=1', 0
  98. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
  99. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=3'
  100. end
  101. def test_show_current_version_should_display_section_edit_links
  102. @request.session[:user_id] = 2
  103. get :show, :params => {:project_id => 1, :id => 'Page with sections', :version => 3}
  104. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=2'
  105. end
  106. def test_show_old_version_should_not_display_section_edit_links
  107. @request.session[:user_id] = 2
  108. get :show, :params => {:project_id => 1, :id => 'Page with sections', :version => 2}
  109. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Page_with_sections/edit?section=2', 0
  110. end
  111. def test_show_unexistent_page_without_edit_right
  112. get :show, :params => {:project_id => 1, :id => 'Unexistent page'}
  113. assert_response 404
  114. end
  115. def test_show_unexistent_page_with_edit_right
  116. @request.session[:user_id] = 2
  117. get :show, :params => {:project_id => 1, :id => 'Unexistent page'}
  118. assert_response :success
  119. assert_select 'textarea[name=?]', 'content[text]'
  120. end
  121. def test_show_specific_version_of_an_unexistent_page_without_edit_right
  122. get :show, :params => {:project_id => 1, :id => 'Unexistent page', :version => 1}
  123. assert_response 404
  124. end
  125. def test_show_unexistent_page_with_parent_should_preselect_parent
  126. @request.session[:user_id] = 2
  127. get :show, :params => {:project_id => 1, :id => 'Unexistent page', :parent => 'Another_page'}
  128. assert_response :success
  129. assert_select 'select[name=?] option[value="2"][selected=selected]', 'wiki_page[parent_id]'
  130. end
  131. def test_show_should_not_show_history_without_permission
  132. Role.anonymous.remove_permission! :view_wiki_edits
  133. get :show, :params => {:project_id => 1, :id => 'Page with sections', :version => 2}
  134. assert_response 302
  135. end
  136. def test_show_page_without_content_should_display_the_edit_form
  137. @request.session[:user_id] = 2
  138. WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
  139. get :show, :params => {:project_id => 1, :id => 'NoContent'}
  140. assert_response :success
  141. assert_select 'textarea[name=?]', 'content[text]'
  142. end
  143. def test_show_protected_page_shoud_show_locked_badge
  144. @request.session[:user_id] = 2
  145. get :show, :params => {:project_id => 1, :id => 'CookBook_documentation'}
  146. assert_select 'p.wiki-update-info' do
  147. assert_select 'span.badge.badge-status-locked'
  148. end
  149. end
  150. def test_get_new
  151. @request.session[:user_id] = 2
  152. get :new, :params => {:project_id => 'ecookbook'}
  153. assert_response :success
  154. assert_select 'input[name=?]', 'title'
  155. end
  156. def test_get_new_xhr
  157. @request.session[:user_id] = 2
  158. get :new, :params => {:project_id => 'ecookbook'}, :xhr => true
  159. assert_response :success
  160. assert_include 'Unallowed characters', response.body
  161. end
  162. def test_post_new_with_valid_title_should_redirect_to_edit
  163. @request.session[:user_id] = 2
  164. post :new, :params => {:project_id => 'ecookbook', :title => 'New Page'}
  165. assert_redirected_to '/projects/ecookbook/wiki/New_Page'
  166. end
  167. def test_post_new_xhr_with_valid_title_should_redirect_to_edit
  168. @request.session[:user_id] = 2
  169. post :new, :params => {:project_id => 'ecookbook', :title => 'New Page'}, :xhr => true
  170. assert_response :success
  171. assert_equal 'window.location = "/projects/ecookbook/wiki/New_Page"', response.body
  172. end
  173. def test_post_new_should_redirect_to_edit_with_parent
  174. @request.session[:user_id] = 2
  175. post :new, :params => {:project_id => 'ecookbook', :title => 'New_Page', :parent => 'Child_1'}
  176. assert_redirected_to '/projects/ecookbook/wiki/New_Page?parent=Child_1'
  177. end
  178. def test_post_new_with_invalid_title_should_display_errors
  179. @request.session[:user_id] = 2
  180. post :new, :params => {:project_id => 'ecookbook', :title => 'Another page'}
  181. assert_response :success
  182. assert_select_error 'Title has already been taken'
  183. end
  184. def test_post_new_with_protected_title_should_display_errors
  185. Role.find(1).remove_permission!(:protect_wiki_pages)
  186. @request.session[:user_id] = 2
  187. post :new, :params => {:project_id => 'ecookbook', :title => 'Sidebar'}
  188. assert_response :success
  189. assert_select_error /Title/
  190. end
  191. def test_post_new_xhr_with_invalid_title_should_display_errors
  192. @request.session[:user_id] = 2
  193. post :new, :params => {:project_id => 'ecookbook', :title => 'Another page'}, :xhr => true
  194. assert_response :success
  195. assert_include 'Title has already been taken', response.body
  196. end
  197. def test_create_page
  198. @request.session[:user_id] = 2
  199. assert_difference 'WikiPage.count' do
  200. assert_difference 'WikiContent.count' do
  201. put :update, :params => {
  202. :project_id => 1,
  203. :id => 'New page',
  204. :content => {
  205. :comments => 'Created the page',
  206. :text => "h1. New page\n\nThis is a new page",
  207. :version => 0
  208. }
  209. }
  210. end
  211. end
  212. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'New_page'
  213. page = Project.find(1).wiki.find_page('New page')
  214. assert !page.new_record?
  215. assert_not_nil page.content
  216. assert_nil page.parent
  217. assert_equal 'Created the page', page.content.comments
  218. end
  219. def test_create_page_with_attachments
  220. set_tmp_attachments_directory
  221. @request.session[:user_id] = 2
  222. assert_difference 'WikiPage.count' do
  223. assert_difference 'Attachment.count' do
  224. put :update, :params => {
  225. :project_id => 1,
  226. :id => 'New page',
  227. :content => {
  228. :comments => 'Created the page',
  229. :text => "h1. New page\n\nThis is a new page",
  230. :version => 0
  231. },
  232. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
  233. }
  234. end
  235. end
  236. page = Project.find(1).wiki.find_page('New page')
  237. assert_equal 1, page.attachments.count
  238. assert_equal 'testfile.txt', page.attachments.first.filename
  239. end
  240. def test_create_page_with_parent
  241. @request.session[:user_id] = 2
  242. assert_difference 'WikiPage.count' do
  243. put :update, :params => {
  244. :project_id => 1,
  245. :id => 'New page',
  246. :content => {
  247. :text => "h1. New page\n\nThis is a new page",
  248. :version => 0
  249. },
  250. :wiki_page => {:parent_id => 2}
  251. }
  252. end
  253. page = Project.find(1).wiki.find_page('New page')
  254. assert_equal WikiPage.find(2), page.parent
  255. end
  256. def test_edit_page
  257. @request.session[:user_id] = 2
  258. get :edit, :params => {:project_id => 'ecookbook', :id => 'Another_page'}
  259. assert_response :success
  260. assert_select 'textarea[name=?]', 'content[text]',
  261. :text => WikiPage.find_by_title('Another_page').content.text
  262. end
  263. def test_edit_section
  264. @request.session[:user_id] = 2
  265. get :edit, :params => {:project_id => 'ecookbook', :id => 'Page_with_sections', :section => 2}
  266. assert_response :success
  267. page = WikiPage.find_by_title('Page_with_sections')
  268. section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
  269. assert_select 'textarea[name=?]', 'content[text]', :text => section
  270. assert_select 'input[name=section][type=hidden][value="2"]'
  271. assert_select 'input[name=section_hash][type=hidden][value=?]', hash
  272. end
  273. def test_edit_invalid_section_should_respond_with_404
  274. @request.session[:user_id] = 2
  275. get :edit, :params => {:project_id => 'ecookbook', :id => 'Page_with_sections', :section => 10}
  276. assert_response 404
  277. end
  278. def test_update_page
  279. @request.session[:user_id] = 2
  280. assert_no_difference 'WikiPage.count' do
  281. assert_no_difference 'WikiContent.count' do
  282. assert_difference 'WikiContentVersion.count' do
  283. put :update, :params => {
  284. :project_id => 1,
  285. :id => 'Another_page',
  286. :content => {
  287. :comments => "my comments",
  288. :text => "edited",
  289. :version => 1
  290. }
  291. }
  292. end
  293. end
  294. end
  295. assert_redirected_to '/projects/ecookbook/wiki/Another_page'
  296. page = Wiki.find(1).pages.find_by_title('Another_page')
  297. assert_equal "edited", page.content.text
  298. assert_equal 2, page.content.version
  299. assert_equal "my comments", page.content.comments
  300. end
  301. def test_update_page_with_parent
  302. @request.session[:user_id] = 2
  303. assert_no_difference 'WikiPage.count' do
  304. assert_no_difference 'WikiContent.count' do
  305. assert_difference 'WikiContentVersion.count' do
  306. put :update, :params => {
  307. :project_id => 1,
  308. :id => 'Another_page',
  309. :content => {
  310. :comments => "my comments",
  311. :text => "edited",
  312. :version => 1
  313. },
  314. :wiki_page => {:parent_id => '1'}
  315. }
  316. end
  317. end
  318. end
  319. assert_redirected_to '/projects/ecookbook/wiki/Another_page'
  320. page = Wiki.find(1).pages.find_by_title('Another_page')
  321. assert_equal "edited", page.content.text
  322. assert_equal 2, page.content.version
  323. assert_equal "my comments", page.content.comments
  324. assert_equal WikiPage.find(1), page.parent
  325. end
  326. def test_update_page_with_failure
  327. @request.session[:user_id] = 2
  328. assert_no_difference 'WikiPage.count' do
  329. assert_no_difference 'WikiContent.count' do
  330. assert_no_difference 'WikiContentVersion.count' do
  331. put :update, :params => {
  332. :project_id => 1,
  333. :id => 'Another_page',
  334. :content => {
  335. :comments => 'a' * 1300, # failure here, comment is too long
  336. :text => 'edited'
  337. },
  338. :wiki_page => {
  339. :parent_id => ""
  340. }
  341. }
  342. end
  343. end
  344. end
  345. assert_response :success
  346. assert_select_error /Comment is too long/
  347. assert_select 'textarea#content_text', :text => "edited"
  348. assert_select 'input#content_version[value="1"]'
  349. end
  350. def test_update_page_with_parent_change_only_should_not_create_content_version
  351. @request.session[:user_id] = 2
  352. assert_no_difference 'WikiPage.count' do
  353. assert_no_difference 'WikiContent.count' do
  354. assert_no_difference 'WikiContentVersion.count' do
  355. put :update, :params => {
  356. :project_id => 1,
  357. :id => 'Another_page',
  358. :content => {
  359. :comments => '',
  360. :text => Wiki.find(1).find_page('Another_page').content.text,
  361. :version => 1
  362. },
  363. :wiki_page => {:parent_id => '1'}
  364. }
  365. end
  366. end
  367. end
  368. page = Wiki.find(1).pages.find_by_title('Another_page')
  369. assert_equal 1, page.content.version
  370. assert_equal WikiPage.find(1), page.parent
  371. end
  372. def test_update_page_with_attachments_only_should_not_create_content_version
  373. set_tmp_attachments_directory
  374. @request.session[:user_id] = 2
  375. assert_no_difference 'WikiPage.count' do
  376. assert_no_difference 'WikiContent.count' do
  377. assert_no_difference 'WikiContentVersion.count' do
  378. assert_difference 'Attachment.count' do
  379. put :update, :params => {
  380. :project_id => 1,
  381. :id => 'Another_page',
  382. :content => {
  383. :comments => '',
  384. :text => Wiki.find(1).find_page('Another_page').content.text,
  385. :version => 1
  386. },
  387. :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}}
  388. }
  389. end
  390. end
  391. end
  392. end
  393. page = Wiki.find(1).pages.find_by_title('Another_page')
  394. assert_equal 1, page.content.version
  395. end
  396. def test_update_with_deleted_attachment_ids
  397. set_tmp_attachments_directory
  398. @request.session[:user_id] = 2
  399. page = WikiPage.find(4)
  400. attachment = page.attachments.first
  401. assert_difference 'Attachment.count', -1 do
  402. put :update, :params => {
  403. :project_id => page.wiki.project.id,
  404. :id => page.title,
  405. :content => {
  406. :comments => 'delete file',
  407. :text => 'edited'
  408. },
  409. :wiki_page => {:deleted_attachment_ids => [attachment.id]}
  410. }
  411. end
  412. page.reload
  413. refute_includes page.attachments, attachment
  414. end
  415. def test_update_with_deleted_attachment_ids_and_failure_should_preserve_selected_attachments
  416. set_tmp_attachments_directory
  417. @request.session[:user_id] = 2
  418. page = WikiPage.find(4)
  419. attachment = page.attachments.first
  420. assert_no_difference 'Attachment.count' do
  421. put :update, :params => {
  422. :project_id => page.wiki.project.id,
  423. :id => page.title,
  424. :content => {
  425. :comments => 'a' * 1300, # failure here, comment is too long
  426. :text => 'edited'
  427. },
  428. :wiki_page => {:deleted_attachment_ids => [attachment.id]}
  429. }
  430. end
  431. page.reload
  432. assert_includes page.attachments, attachment
  433. end
  434. def test_update_stale_page_should_not_raise_an_error
  435. @request.session[:user_id] = 2
  436. c = Wiki.find(1).find_page('Another_page').content
  437. c.text = 'Previous text'
  438. c.save!
  439. assert_equal 2, c.version
  440. assert_no_difference 'WikiPage.count' do
  441. assert_no_difference 'WikiContent.count' do
  442. assert_no_difference 'WikiContentVersion.count' do
  443. put :update, :params => {
  444. :project_id => 1,
  445. :id => 'Another_page',
  446. :content => {
  447. :comments => 'My comments',
  448. :text => 'Text should not be lost',
  449. :version => 1
  450. }
  451. }
  452. end
  453. end
  454. end
  455. assert_response :success
  456. assert_select 'div.error', :text => /Data has been updated by another user/
  457. assert_select 'textarea[name=?]', 'content[text]', :text => /Text should not be lost/
  458. assert_select 'input[name=?][value=?]', 'content[comments]', 'My comments'
  459. c.reload
  460. assert_equal 'Previous text', c.text
  461. assert_equal 2, c.version
  462. end
  463. def test_update_page_without_content_should_create_content
  464. @request.session[:user_id] = 2
  465. page = WikiPage.create!(:title => 'NoContent', :wiki => Project.find(1).wiki)
  466. assert_no_difference 'WikiPage.count' do
  467. assert_difference 'WikiContent.count' do
  468. put :update, :params => {
  469. :project_id => 1,
  470. :id => 'NoContent',
  471. :content => {:text => 'Some content'}
  472. }
  473. assert_response 302
  474. end
  475. end
  476. assert_equal 'Some content', page.reload.content.text
  477. end
  478. def test_update_section
  479. @request.session[:user_id] = 2
  480. page = WikiPage.find_by_title('Page_with_sections')
  481. section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
  482. text = page.content.text
  483. assert_no_difference 'WikiPage.count' do
  484. assert_no_difference 'WikiContent.count' do
  485. assert_difference 'WikiContentVersion.count' do
  486. put :update, :params => {
  487. :project_id => 1,
  488. :id => 'Page_with_sections',
  489. :content => {
  490. :text => "New section content",
  491. :version => 3
  492. },
  493. :section => 2,
  494. :section_hash => hash
  495. }
  496. end
  497. end
  498. end
  499. assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
  500. assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.reload.content.text
  501. end
  502. def test_update_section_should_allow_stale_page_update
  503. @request.session[:user_id] = 2
  504. page = WikiPage.find_by_title('Page_with_sections')
  505. section, hash = Redmine::WikiFormatting::Textile::Formatter.new(page.content.text).get_section(2)
  506. text = page.content.text
  507. assert_no_difference 'WikiPage.count' do
  508. assert_no_difference 'WikiContent.count' do
  509. assert_difference 'WikiContentVersion.count' do
  510. put :update, :params => {
  511. :project_id => 1,
  512. :id => 'Page_with_sections',
  513. :content => {
  514. :text => "New section content",
  515. :version => 2 # Current version is 3
  516. },
  517. :section => 2,
  518. :section_hash => hash
  519. }
  520. end
  521. end
  522. end
  523. assert_redirected_to '/projects/ecookbook/wiki/Page_with_sections#section-2'
  524. page.reload
  525. assert_equal Redmine::WikiFormatting::Textile::Formatter.new(text).update_section(2, "New section content"), page.content.text
  526. assert_equal 4, page.content.version
  527. end
  528. def test_update_section_should_not_allow_stale_section_update
  529. @request.session[:user_id] = 2
  530. assert_no_difference 'WikiPage.count' do
  531. assert_no_difference 'WikiContent.count' do
  532. assert_no_difference 'WikiContentVersion.count' do
  533. put :update, :params => {
  534. :project_id => 1,
  535. :id => 'Page_with_sections',
  536. :content => {
  537. :comments => 'My comments',
  538. :text => "Text should not be lost",
  539. :version => 3
  540. },
  541. :section => 2,
  542. :section_hash => Digest::MD5.hexdigest("wrong hash")
  543. }
  544. end
  545. end
  546. end
  547. assert_response :success
  548. assert_select 'div.error', :text => /Data has been updated by another user/
  549. assert_select 'textarea[name=?]', 'content[text]', :text => /Text should not be lost/
  550. assert_select 'input[name=?][value=?]', 'content[comments]', 'My comments'
  551. end
  552. def test_preview
  553. @request.session[:user_id] = 2
  554. post :preview, :params => {
  555. :project_id => 1,
  556. :id => 'CookBook_documentation',
  557. :content => {
  558. :comments => '',
  559. :text => 'this is a *previewed text*',
  560. :version => 3
  561. }
  562. }, :xhr => true
  563. assert_response :success
  564. assert_select 'strong', :text => /previewed text/
  565. end
  566. def test_preview_new_page
  567. @request.session[:user_id] = 2
  568. post :preview, :params => {
  569. :project_id => 1,
  570. :id => 'New page',
  571. :content => {
  572. :text => 'h1. New page',
  573. :comments => '',
  574. :version => 0
  575. }
  576. }, :xhr => true
  577. assert_response :success
  578. assert_select 'h1', :text => /New page/
  579. end
  580. def test_history
  581. @request.session[:user_id] = 2
  582. get :history, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation'}
  583. assert_response :success
  584. assert_select 'table.wiki-page-versions tbody' do
  585. assert_select 'tr', 3
  586. end
  587. assert_select "input[type=submit][name=commit]"
  588. assert_select 'td' do
  589. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => '2'
  590. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2/annotate', :text => 'Annotate'
  591. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation/2', :text => 'Delete'
  592. end
  593. end
  594. def test_history_with_one_version
  595. @request.session[:user_id] = 2
  596. get :history, :params => {:project_id => 'ecookbook', :id => 'Another_page'}
  597. assert_response :success
  598. assert_select 'table.wiki-page-versions tbody' do
  599. assert_select 'tr', 1
  600. end
  601. assert_select "input[type=submit][name=commit]", false
  602. assert_select 'td' do
  603. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => '1'
  604. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1/annotate', :text => 'Annotate'
  605. assert_select 'a[href=?]', '/projects/ecookbook/wiki/Another_page/1', :text => 'Delete', :count => 0
  606. end
  607. end
  608. def test_diff
  609. content = WikiPage.find(1).content
  610. assert_difference 'WikiContentVersion.count', 2 do
  611. content.text = "Line removed\nThis is a sample text for testing diffs"
  612. content.save!
  613. content.text = "This is a sample text for testing diffs\nLine added"
  614. content.save!
  615. end
  616. get :diff, :params => {
  617. :project_id => 1, :id => 'CookBook_documentation',
  618. :version => content.version,
  619. :version_from => (content.version - 1)
  620. }
  621. assert_response :success
  622. assert_select 'span.diff_out', :text => 'Line removed'
  623. assert_select 'span.diff_in', :text => 'Line added'
  624. end
  625. def test_diff_with_invalid_version_should_respond_with_404
  626. get :diff, :params => {
  627. :project_id => 1, :id => 'CookBook_documentation',
  628. :version => '99'
  629. }
  630. assert_response 404
  631. end
  632. def test_diff_with_invalid_version_from_should_respond_with_404
  633. get :diff, :params => {
  634. :project_id => 1, :id => 'CookBook_documentation',
  635. :version => '99',
  636. :version_from => '98'
  637. }
  638. assert_response 404
  639. end
  640. def test_annotate
  641. get :annotate, :params => {
  642. :project_id => 1, :id => 'CookBook_documentation',
  643. :version => 2
  644. }
  645. assert_response :success
  646. # Line 1
  647. assert_select 'table.annotate tr:nth-child(1)' do
  648. assert_select 'th.line-num', :text => '1'
  649. assert_select 'td.author', :text => /Redmine Admin/
  650. assert_select 'td', :text => /h1\. CookBook documentation v2/
  651. end
  652. # Line 2
  653. assert_select 'table.annotate tr:nth-child(2)' do
  654. assert_select 'th.line-num', :text => '2'
  655. assert_select 'td.author', :text => /John Smith/
  656. end
  657. # Line 5
  658. assert_select 'table.annotate tr:nth-child(5)' do
  659. assert_select 'th.line-num', :text => '5'
  660. assert_select 'td.author', :text => /Redmine Admin/
  661. assert_select 'td', :text => /Some updated \[\[documentation\]\] here/
  662. end
  663. end
  664. def test_annotate_with_invalid_version_should_respond_with_404
  665. get :annotate, :params => {
  666. :project_id => 1, :id => 'CookBook_documentation',
  667. :version => '99'
  668. }
  669. assert_response 404
  670. end
  671. def test_get_rename
  672. @request.session[:user_id] = 2
  673. get :rename, :params => {:project_id => 1, :id => 'Another_page'}
  674. assert_response :success
  675. assert_select 'select[name=?]', 'wiki_page[parent_id]' do
  676. assert_select 'option[value=""]', :text => ''
  677. assert_select 'option[selected=selected]', 0
  678. end
  679. end
  680. def test_get_rename_child_page
  681. @request.session[:user_id] = 2
  682. get :rename, :params => {:project_id => 1, :id => 'Child_1'}
  683. assert_response :success
  684. assert_select 'select[name=?]', 'wiki_page[parent_id]' do
  685. assert_select 'option[value=""]', :text => ''
  686. assert_select 'option[value="2"][selected=selected]', :text => /Another page/
  687. end
  688. end
  689. def test_rename_with_redirect
  690. @request.session[:user_id] = 2
  691. post :rename, :params => {
  692. :project_id => 1,
  693. :id => 'Another_page',
  694. :wiki_page => {
  695. :title => 'Another renamed page',
  696. :redirect_existing_links => 1
  697. }
  698. }
  699. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
  700. wiki = Project.find(1).wiki
  701. # Check redirects
  702. assert_not_nil wiki.find_page('Another page')
  703. assert_nil wiki.find_page('Another page', :with_redirect => false)
  704. end
  705. def test_rename_without_redirect
  706. @request.session[:user_id] = 2
  707. post :rename, :params => {
  708. :project_id => 1,
  709. :id => 'Another_page',
  710. :wiki_page => {
  711. :title => 'Another renamed page',
  712. :redirect_existing_links => "0"
  713. }
  714. }
  715. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_renamed_page'
  716. wiki = Project.find(1).wiki
  717. # Check that there's no redirects
  718. assert_nil wiki.find_page('Another page')
  719. end
  720. def test_rename_with_parent_assignment
  721. @request.session[:user_id] = 2
  722. post :rename, :params => {
  723. :project_id => 1,
  724. :id => 'Another_page',
  725. :wiki_page => {
  726. :title => 'Another page',
  727. :redirect_existing_links => "0",
  728. :parent_id => '4'
  729. }
  730. }
  731. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
  732. assert_equal WikiPage.find(4), WikiPage.find_by_title('Another_page').parent
  733. end
  734. def test_rename_with_parent_unassignment
  735. @request.session[:user_id] = 2
  736. post :rename, :params => {
  737. :project_id => 1,
  738. :id => 'Child_1',
  739. :wiki_page => {
  740. :title => 'Child 1',
  741. :redirect_existing_links => "0",
  742. :parent_id => ''
  743. }
  744. }
  745. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Child_1'
  746. assert_nil WikiPage.find_by_title('Child_1').parent
  747. end
  748. def test_get_rename_should_show_target_projects_list
  749. @request.session[:user_id] = 2
  750. project = Project.find(5)
  751. project.enable_module! :wiki
  752. get :rename, :params => {:project_id => 1, :id => 'Another_page'}
  753. assert_response :success
  754. assert_select 'select[name=?]', 'wiki_page[wiki_id]' do
  755. assert_select 'option', 2
  756. assert_select 'option[value=?][selected=selected]', '1', :text => /eCookbook/
  757. assert_select 'option[value=?]', project.wiki.id.to_s, :text => /#{project.name}/
  758. end
  759. end
  760. def test_rename_with_move
  761. @request.session[:user_id] = 2
  762. project = Project.find(5)
  763. project.enable_module! :wiki
  764. post :rename, :params => {
  765. :project_id => 1,
  766. :id => 'Another_page',
  767. :wiki_page => {
  768. :wiki_id => project.wiki.id.to_s,
  769. :title => 'Another renamed page',
  770. :redirect_existing_links => 1
  771. }
  772. }
  773. assert_redirected_to '/projects/private-child/wiki/Another_renamed_page'
  774. page = WikiPage.find(2)
  775. assert_equal project.wiki.id, page.wiki_id
  776. end
  777. def test_rename_as_start_page
  778. @request.session[:user_id] = 2
  779. post :rename, :params => {
  780. :project_id => 'ecookbook',
  781. :id => 'Another_page',
  782. :wiki_page => {
  783. :wiki_id => '1',
  784. :title => 'Another_page',
  785. :redirect_existing_links => '1',
  786. :is_start_page => '1'
  787. }
  788. }
  789. assert_redirected_to '/projects/ecookbook/wiki/Another_page'
  790. wiki = Wiki.find(1)
  791. assert_equal 'Another_page', wiki.start_page
  792. end
  793. def test_destroy_a_page_without_children_should_not_ask_confirmation
  794. @request.session[:user_id] = 2
  795. delete :destroy, :params => {:project_id => 1, :id => 'Child_2'}
  796. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  797. end
  798. def test_destroy_parent_should_ask_confirmation
  799. @request.session[:user_id] = 2
  800. assert_no_difference('WikiPage.count') do
  801. delete :destroy, :params => {:project_id => 1, :id => 'Another_page'}
  802. end
  803. assert_response :success
  804. assert_select 'form' do
  805. assert_select 'input[name=todo][value=nullify]'
  806. assert_select 'input[name=todo][value=destroy]'
  807. assert_select 'input[name=todo][value=reassign]'
  808. end
  809. end
  810. def test_destroy_parent_with_nullify_should_delete_parent_only
  811. @request.session[:user_id] = 2
  812. assert_difference('WikiPage.count', -1) do
  813. delete :destroy, :params => {:project_id => 1, :id => 'Another_page', :todo => 'nullify'}
  814. end
  815. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  816. assert_nil WikiPage.find_by_id(2)
  817. end
  818. def test_destroy_parent_with_cascade_should_delete_descendants
  819. @request.session[:user_id] = 2
  820. assert_difference('WikiPage.count', -4) do
  821. delete :destroy, :params => {:project_id => 1, :id => 'Another_page', :todo => 'destroy'}
  822. end
  823. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  824. assert_nil WikiPage.find_by_id(2)
  825. assert_nil WikiPage.find_by_id(5)
  826. end
  827. def test_destroy_parent_with_reassign
  828. @request.session[:user_id] = 2
  829. assert_difference('WikiPage.count', -1) do
  830. delete :destroy, :params => {:project_id => 1, :id => 'Another_page', :todo => 'reassign', :reassign_to_id => 1}
  831. end
  832. assert_redirected_to :action => 'index', :project_id => 'ecookbook'
  833. assert_nil WikiPage.find_by_id(2)
  834. assert_equal WikiPage.find(1), WikiPage.find_by_id(5).parent
  835. end
  836. def test_destroy_version
  837. @request.session[:user_id] = 2
  838. assert_difference 'WikiContentVersion.count', -1 do
  839. assert_no_difference 'WikiContent.count' do
  840. assert_no_difference 'WikiPage.count' do
  841. delete :destroy_version, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 2}
  842. assert_redirected_to '/projects/ecookbook/wiki/CookBook_documentation/history'
  843. end
  844. end
  845. end
  846. end
  847. def test_destroy_invalid_version_should_respond_with_404
  848. @request.session[:user_id] = 2
  849. assert_no_difference 'WikiContentVersion.count' do
  850. assert_no_difference 'WikiContent.count' do
  851. assert_no_difference 'WikiPage.count' do
  852. delete :destroy_version, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => 99}
  853. end
  854. end
  855. end
  856. assert_response 404
  857. end
  858. def test_index
  859. get :index, :params => {:project_id => 'ecookbook'}
  860. assert_response :success
  861. assert_select 'ul.pages-hierarchy' do
  862. assert_select 'li', Project.find(1).wiki.pages.count
  863. end
  864. assert_select 'ul.pages-hierarchy' do
  865. assert_select 'li' do
  866. assert_select 'a[href=?]', '/projects/ecookbook/wiki/CookBook_documentation', :text => 'CookBook documentation'
  867. assert_select 'ul li a[href=?]', '/projects/ecookbook/wiki/Page_with_an_inline_image', :text => 'Page with an inline image'
  868. end
  869. assert_select 'li a[href=?]', '/projects/ecookbook/wiki/Another_page', :text => 'Another page'
  870. end
  871. end
  872. def test_index_should_include_atom_link
  873. get :index, :params => {:project_id => 'ecookbook'}
  874. assert_select 'a[href=?]', '/projects/ecookbook/activity.atom?show_wiki_edits=1'
  875. end
  876. def test_export_to_html
  877. @request.session[:user_id] = 2
  878. get :export, :params => {:project_id => 'ecookbook'}
  879. assert_response :success
  880. assert_equal "text/html", @response.content_type
  881. assert_select "a[name=?]", "CookBook_documentation"
  882. assert_select "a[name=?]", "Another_page"
  883. assert_select "a[name=?]", "Page_with_an_inline_image"
  884. end
  885. def test_export_to_pdf
  886. @request.session[:user_id] = 2
  887. get :export, :params => {:project_id => 'ecookbook', :format => 'pdf'}
  888. assert_response :success
  889. assert_equal 'application/pdf', @response.content_type
  890. assert_equal 'attachment; filename="ecookbook.pdf"', @response.headers['Content-Disposition']
  891. assert @response.body.starts_with?('%PDF')
  892. end
  893. def test_export_without_permission_should_be_denied
  894. @request.session[:user_id] = 2
  895. Role.find_by_name('Manager').remove_permission! :export_wiki_pages
  896. get :export, :params => {:project_id => 'ecookbook'}
  897. assert_response 403
  898. end
  899. def test_date_index
  900. get :date_index, :params => {:project_id => 'ecookbook'}
  901. assert_response :success
  902. assert_select 'a[href=?]', '/projects/ecookbook/activity.atom?show_wiki_edits=1'
  903. end
  904. def test_not_found
  905. get :show, :params => {:project_id => 999}
  906. assert_response 404
  907. end
  908. def test_protect_page
  909. page = WikiPage.find_by_wiki_id_and_title(1, 'Another_page')
  910. assert !page.protected?
  911. @request.session[:user_id] = 2
  912. post :protect, :params => {:project_id => 1, :id => page.title, :protected => '1'}
  913. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'Another_page'
  914. assert page.reload.protected?
  915. end
  916. def test_unprotect_page
  917. page = WikiPage.find_by_wiki_id_and_title(1, 'CookBook_documentation')
  918. assert page.protected?
  919. @request.session[:user_id] = 2
  920. post :protect, :params => {:project_id => 1, :id => page.title, :protected => '0'}
  921. assert_redirected_to :action => 'show', :project_id => 'ecookbook', :id => 'CookBook_documentation'
  922. assert !page.reload.protected?
  923. end
  924. def test_show_page_with_edit_link
  925. @request.session[:user_id] = 2
  926. get :show, :params => {:project_id => 1}
  927. assert_response :success
  928. assert_select 'a[href=?]', '/projects/1/wiki/CookBook_documentation/edit'
  929. end
  930. def test_show_page_without_edit_link
  931. @request.session[:user_id] = 4
  932. get :show, :params => {:project_id => 1}
  933. assert_response :success
  934. assert_select 'a[href=?]', '/projects/1/wiki/CookBook_documentation/edit', 0
  935. end
  936. def test_show_pdf
  937. @request.session[:user_id] = 2
  938. get :show, :params => {:project_id => 1, :format => 'pdf'}
  939. assert_response :success
  940. assert_equal 'application/pdf', @response.content_type
  941. assert_equal 'attachment; filename="CookBook_documentation.pdf"',
  942. @response.headers['Content-Disposition']
  943. end
  944. def test_show_html
  945. @request.session[:user_id] = 2
  946. get :show, :params => {:project_id => 1, :format => 'html'}
  947. assert_response :success
  948. assert_equal 'text/html', @response.content_type
  949. assert_equal 'attachment; filename="CookBook_documentation.html"',
  950. @response.headers['Content-Disposition']
  951. assert_select 'h1', :text => /CookBook documentation/
  952. end
  953. def test_show_versioned_html
  954. @request.session[:user_id] = 2
  955. get :show, :params => {:project_id => 1, :format => 'html', :version => 2}
  956. assert_response :success
  957. assert_equal 'text/html', @response.content_type
  958. assert_equal 'attachment; filename="CookBook_documentation.html"',
  959. @response.headers['Content-Disposition']
  960. assert_select 'h1', :text => /CookBook documentation v2/
  961. end
  962. def test_show_txt
  963. @request.session[:user_id] = 2
  964. get :show, :params => {:project_id => 1, :format => 'txt'}
  965. assert_response :success
  966. assert_equal 'text/plain', @response.content_type
  967. assert_equal 'attachment; filename="CookBook_documentation.txt"',
  968. @response.headers['Content-Disposition']
  969. assert_include 'h1. CookBook documentation', @response.body
  970. end
  971. def test_show_versioned_txt
  972. @request.session[:user_id] = 2
  973. get :show, :params => {:project_id => 1, :format => 'txt', :version => 2}
  974. assert_response :success
  975. assert_equal 'text/plain', @response.content_type
  976. assert_equal 'attachment; filename="CookBook_documentation.txt"',
  977. @response.headers['Content-Disposition']
  978. assert_include 'h1. CookBook documentation v2', @response.body
  979. end
  980. def test_show_filename_should_be_uri_encoded_for_ms_browsers
  981. @request.session[:user_id] = 2
  982. title = 'Этика_менеджмента'
  983. %w|pdf html txt|.each do |format|
  984. # Non-MS browsers
  985. @request.user_agent = ""
  986. get :show, :params => {:project_id => 1, :id => title, :format => format}
  987. assert_response :success
  988. assert_equal "attachment; filename=\"#{title}.#{format}\"",
  989. @response.headers['Content-Disposition']
  990. # Microsoft's browsers: filename should be URI encoded
  991. @request.user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063'
  992. get :show, :params => {:project_id => 1, :id => title, :format => format}
  993. assert_response :success
  994. filename = Addressable::URI.encode("#{title}.#{format}")
  995. assert_equal "attachment; filename=\"#{filename}\"",
  996. @response.headers['Content-Disposition']
  997. end
  998. end
  999. def test_edit_unprotected_page
  1000. # Non members can edit unprotected wiki pages
  1001. @request.session[:user_id] = 4
  1002. get :edit, :params => {:project_id => 1, :id => 'Another_page'}
  1003. assert_response :success
  1004. end
  1005. def test_edit_protected_page_by_nonmember
  1006. # Non members cannot edit protected wiki pages
  1007. @request.session[:user_id] = 4
  1008. get :edit, :params => {:project_id => 1, :id => 'CookBook_documentation'}
  1009. assert_response 403
  1010. end
  1011. def test_edit_protected_page_by_member
  1012. @request.session[:user_id] = 2
  1013. get :edit, :params => {:project_id => 1, :id => 'CookBook_documentation'}
  1014. assert_response :success
  1015. end
  1016. def test_history_of_non_existing_page_should_return_404
  1017. get :history, :params => {:project_id => 1, :id => 'Unknown_page'}
  1018. assert_response 404
  1019. end
  1020. def test_add_attachment
  1021. set_tmp_attachments_directory
  1022. @request.session[:user_id] = 2
  1023. assert_difference 'Attachment.count' do
  1024. post :add_attachment, :params => {
  1025. :project_id => 1,
  1026. :id => 'CookBook_documentation',
  1027. :attachments => {
  1028. '1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain'), 'description' => 'test file'}
  1029. }
  1030. }
  1031. end
  1032. attachment = Attachment.order('id DESC').first
  1033. assert_equal Wiki.find(1).find_page('CookBook_documentation'), attachment.container
  1034. end
  1035. def test_old_version_should_have_robot_exclusion_tag
  1036. @request.session[:user_id] = 2
  1037. # Discourage search engines from indexing old versions
  1038. get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation', :version => '2'}
  1039. assert_response :success
  1040. assert_select 'head>meta[name="robots"][content=?]', 'noindex,follow,noarchive'
  1041. # No robots meta tag in the current version
  1042. get :show, :params => {:project_id => 'ecookbook', :id => 'CookBook_documentation'}
  1043. assert_response :success
  1044. assert_select 'head>meta[name="robots"]', false
  1045. end
  1046. end