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.

changeset_test.rb 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. # encoding: utf-8
  2. #
  3. # Redmine - project management software
  4. # Copyright (C) 2006-2013 Jean-Philippe Lang
  5. #
  6. # This program is free software; you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation; either version 2
  9. # of the License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. require File.expand_path('../../test_helper', __FILE__)
  20. class ChangesetTest < ActiveSupport::TestCase
  21. fixtures :projects, :repositories,
  22. :issues, :issue_statuses, :issue_categories,
  23. :changesets, :changes,
  24. :enumerations,
  25. :custom_fields, :custom_values,
  26. :users, :members, :member_roles, :trackers,
  27. :enabled_modules, :roles
  28. def test_ref_keywords_any
  29. ActionMailer::Base.deliveries.clear
  30. Setting.commit_fix_status_id = IssueStatus.find(
  31. :first, :conditions => ["is_closed = ?", true]).id
  32. Setting.commit_fix_done_ratio = '90'
  33. Setting.commit_ref_keywords = '*'
  34. Setting.commit_fix_keywords = 'fixes , closes'
  35. c = Changeset.new(:repository => Project.find(1).repository,
  36. :committed_on => Time.now,
  37. :comments => 'New commit (#2). Fixes #1',
  38. :revision => '12345')
  39. assert c.save
  40. assert_equal [1, 2], c.issue_ids.sort
  41. fixed = Issue.find(1)
  42. assert fixed.closed?
  43. assert_equal 90, fixed.done_ratio
  44. assert_equal 1, ActionMailer::Base.deliveries.size
  45. end
  46. def test_ref_keywords
  47. Setting.commit_ref_keywords = 'refs'
  48. Setting.commit_fix_keywords = ''
  49. c = Changeset.new(:repository => Project.find(1).repository,
  50. :committed_on => Time.now,
  51. :comments => 'Ignores #2. Refs #1',
  52. :revision => '12345')
  53. assert c.save
  54. assert_equal [1], c.issue_ids.sort
  55. end
  56. def test_ref_keywords_any_only
  57. Setting.commit_ref_keywords = '*'
  58. Setting.commit_fix_keywords = ''
  59. c = Changeset.new(:repository => Project.find(1).repository,
  60. :committed_on => Time.now,
  61. :comments => 'Ignores #2. Refs #1',
  62. :revision => '12345')
  63. assert c.save
  64. assert_equal [1, 2], c.issue_ids.sort
  65. end
  66. def test_ref_keywords_any_with_timelog
  67. Setting.commit_ref_keywords = '*'
  68. Setting.commit_logtime_enabled = '1'
  69. {
  70. '2' => 2.0,
  71. '2h' => 2.0,
  72. '2hours' => 2.0,
  73. '15m' => 0.25,
  74. '15min' => 0.25,
  75. '3h15' => 3.25,
  76. '3h15m' => 3.25,
  77. '3h15min' => 3.25,
  78. '3:15' => 3.25,
  79. '3.25' => 3.25,
  80. '3.25h' => 3.25,
  81. '3,25' => 3.25,
  82. '3,25h' => 3.25,
  83. }.each do |syntax, expected_hours|
  84. c = Changeset.new(:repository => Project.find(1).repository,
  85. :committed_on => 24.hours.ago,
  86. :comments => "Worked on this issue #1 @#{syntax}",
  87. :revision => '520',
  88. :user => User.find(2))
  89. assert_difference 'TimeEntry.count' do
  90. c.scan_comment_for_issue_ids
  91. end
  92. assert_equal [1], c.issue_ids.sort
  93. time = TimeEntry.first(:order => 'id desc')
  94. assert_equal 1, time.issue_id
  95. assert_equal 1, time.project_id
  96. assert_equal 2, time.user_id
  97. assert_equal expected_hours, time.hours,
  98. "@#{syntax} should be logged as #{expected_hours} hours but was #{time.hours}"
  99. assert_equal Date.yesterday, time.spent_on
  100. assert time.activity.is_default?
  101. assert time.comments.include?('r520'),
  102. "r520 was expected in time_entry comments: #{time.comments}"
  103. end
  104. end
  105. def test_ref_keywords_closing_with_timelog
  106. Setting.commit_fix_status_id = IssueStatus.find(
  107. :first, :conditions => ["is_closed = ?", true]).id
  108. Setting.commit_ref_keywords = '*'
  109. Setting.commit_fix_keywords = 'fixes , closes'
  110. Setting.commit_logtime_enabled = '1'
  111. c = Changeset.new(:repository => Project.find(1).repository,
  112. :committed_on => Time.now,
  113. :comments => 'This is a comment. Fixes #1 @4.5, #2 @1',
  114. :user => User.find(2))
  115. assert_difference 'TimeEntry.count', 2 do
  116. c.scan_comment_for_issue_ids
  117. end
  118. assert_equal [1, 2], c.issue_ids.sort
  119. assert Issue.find(1).closed?
  120. assert Issue.find(2).closed?
  121. times = TimeEntry.all(:order => 'id desc', :limit => 2)
  122. assert_equal [1, 2], times.collect(&:issue_id).sort
  123. end
  124. def test_ref_keywords_any_line_start
  125. Setting.commit_ref_keywords = '*'
  126. c = Changeset.new(:repository => Project.find(1).repository,
  127. :committed_on => Time.now,
  128. :comments => '#1 is the reason of this commit',
  129. :revision => '12345')
  130. assert c.save
  131. assert_equal [1], c.issue_ids.sort
  132. end
  133. def test_ref_keywords_allow_brackets_around_a_issue_number
  134. Setting.commit_ref_keywords = '*'
  135. c = Changeset.new(:repository => Project.find(1).repository,
  136. :committed_on => Time.now,
  137. :comments => '[#1] Worked on this issue',
  138. :revision => '12345')
  139. assert c.save
  140. assert_equal [1], c.issue_ids.sort
  141. end
  142. def test_ref_keywords_allow_brackets_around_multiple_issue_numbers
  143. Setting.commit_ref_keywords = '*'
  144. c = Changeset.new(:repository => Project.find(1).repository,
  145. :committed_on => Time.now,
  146. :comments => '[#1 #2, #3] Worked on these',
  147. :revision => '12345')
  148. assert c.save
  149. assert_equal [1,2,3], c.issue_ids.sort
  150. end
  151. def test_commit_referencing_a_subproject_issue
  152. c = Changeset.new(:repository => Project.find(1).repository,
  153. :committed_on => Time.now,
  154. :comments => 'refs #5, a subproject issue',
  155. :revision => '12345')
  156. assert c.save
  157. assert_equal [5], c.issue_ids.sort
  158. assert c.issues.first.project != c.project
  159. end
  160. def test_commit_closing_a_subproject_issue
  161. with_settings :commit_fix_status_id => 5, :commit_fix_keywords => 'closes',
  162. :default_language => 'en' do
  163. issue = Issue.find(5)
  164. assert !issue.closed?
  165. assert_difference 'Journal.count' do
  166. c = Changeset.new(:repository => Project.find(1).repository,
  167. :committed_on => Time.now,
  168. :comments => 'closes #5, a subproject issue',
  169. :revision => '12345')
  170. assert c.save
  171. end
  172. assert issue.reload.closed?
  173. journal = Journal.first(:order => 'id DESC')
  174. assert_equal issue, journal.issue
  175. assert_include "Applied in changeset ecookbook:r12345.", journal.notes
  176. end
  177. end
  178. def test_commit_referencing_a_parent_project_issue
  179. # repository of child project
  180. r = Repository::Subversion.create!(
  181. :project => Project.find(3),
  182. :url => 'svn://localhost/test')
  183. c = Changeset.new(:repository => r,
  184. :committed_on => Time.now,
  185. :comments => 'refs #2, an issue of a parent project',
  186. :revision => '12345')
  187. assert c.save
  188. assert_equal [2], c.issue_ids.sort
  189. assert c.issues.first.project != c.project
  190. end
  191. def test_commit_referencing_a_project_with_commit_cross_project_ref_disabled
  192. r = Repository::Subversion.create!(
  193. :project => Project.find(3),
  194. :url => 'svn://localhost/test')
  195. with_settings :commit_cross_project_ref => '0' do
  196. c = Changeset.new(:repository => r,
  197. :committed_on => Time.now,
  198. :comments => 'refs #4, an issue of a different project',
  199. :revision => '12345')
  200. assert c.save
  201. assert_equal [], c.issue_ids
  202. end
  203. end
  204. def test_commit_referencing_a_project_with_commit_cross_project_ref_enabled
  205. r = Repository::Subversion.create!(
  206. :project => Project.find(3),
  207. :url => 'svn://localhost/test')
  208. with_settings :commit_cross_project_ref => '1' do
  209. c = Changeset.new(:repository => r,
  210. :committed_on => Time.now,
  211. :comments => 'refs #4, an issue of a different project',
  212. :revision => '12345')
  213. assert c.save
  214. assert_equal [4], c.issue_ids
  215. end
  216. end
  217. def test_text_tag_revision
  218. c = Changeset.new(:revision => '520')
  219. assert_equal 'r520', c.text_tag
  220. end
  221. def test_text_tag_revision_with_same_project
  222. c = Changeset.new(:revision => '520', :repository => Project.find(1).repository)
  223. assert_equal 'r520', c.text_tag(Project.find(1))
  224. end
  225. def test_text_tag_revision_with_different_project
  226. c = Changeset.new(:revision => '520', :repository => Project.find(1).repository)
  227. assert_equal 'ecookbook:r520', c.text_tag(Project.find(2))
  228. end
  229. def test_text_tag_revision_with_repository_identifier
  230. r = Repository::Subversion.create!(
  231. :project_id => 1,
  232. :url => 'svn://localhost/test',
  233. :identifier => 'documents')
  234. c = Changeset.new(:revision => '520', :repository => r)
  235. assert_equal 'documents|r520', c.text_tag
  236. assert_equal 'ecookbook:documents|r520', c.text_tag(Project.find(2))
  237. end
  238. def test_text_tag_hash
  239. c = Changeset.new(
  240. :scmid => '7234cb2750b63f47bff735edc50a1c0a433c2518',
  241. :revision => '7234cb2750b63f47bff735edc50a1c0a433c2518')
  242. assert_equal 'commit:7234cb2750b63f47bff735edc50a1c0a433c2518', c.text_tag
  243. end
  244. def test_text_tag_hash_with_same_project
  245. c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => Project.find(1).repository)
  246. assert_equal 'commit:7234cb27', c.text_tag(Project.find(1))
  247. end
  248. def test_text_tag_hash_with_different_project
  249. c = Changeset.new(:revision => '7234cb27', :scmid => '7234cb27', :repository => Project.find(1).repository)
  250. assert_equal 'ecookbook:commit:7234cb27', c.text_tag(Project.find(2))
  251. end
  252. def test_text_tag_hash_all_number
  253. c = Changeset.new(:scmid => '0123456789', :revision => '0123456789')
  254. assert_equal 'commit:0123456789', c.text_tag
  255. end
  256. def test_previous
  257. changeset = Changeset.find_by_revision('3')
  258. assert_equal Changeset.find_by_revision('2'), changeset.previous
  259. end
  260. def test_previous_nil
  261. changeset = Changeset.find_by_revision('1')
  262. assert_nil changeset.previous
  263. end
  264. def test_next
  265. changeset = Changeset.find_by_revision('2')
  266. assert_equal Changeset.find_by_revision('3'), changeset.next
  267. end
  268. def test_next_nil
  269. changeset = Changeset.find_by_revision('10')
  270. assert_nil changeset.next
  271. end
  272. def test_comments_should_be_converted_to_utf8
  273. proj = Project.find(3)
  274. # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
  275. str = "Texte encod\xe9 en ISO-8859-1."
  276. str.force_encoding("ASCII-8BIT") if str.respond_to?(:force_encoding)
  277. r = Repository::Bazaar.create!(
  278. :project => proj,
  279. :url => '/tmp/test/bazaar',
  280. :log_encoding => 'ISO-8859-1' )
  281. assert r
  282. c = Changeset.new(:repository => r,
  283. :committed_on => Time.now,
  284. :revision => '123',
  285. :scmid => '12345',
  286. :comments => str)
  287. assert( c.save )
  288. str_utf8 = "Texte encod\xc3\xa9 en ISO-8859-1."
  289. str_utf8.force_encoding("UTF-8") if str_utf8.respond_to?(:force_encoding)
  290. assert_equal str_utf8, c.comments
  291. end
  292. def test_invalid_utf8_sequences_in_comments_should_be_replaced_latin1
  293. proj = Project.find(3)
  294. # str = File.read("#{RAILS_ROOT}/test/fixtures/encoding/iso-8859-1.txt")
  295. str1 = "Texte encod\xe9 en ISO-8859-1."
  296. str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
  297. str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
  298. str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
  299. r = Repository::Bazaar.create!(
  300. :project => proj,
  301. :url => '/tmp/test/bazaar',
  302. :log_encoding => 'UTF-8' )
  303. assert r
  304. c = Changeset.new(:repository => r,
  305. :committed_on => Time.now,
  306. :revision => '123',
  307. :scmid => '12345',
  308. :comments => str1,
  309. :committer => str2)
  310. assert( c.save )
  311. assert_equal "Texte encod? en ISO-8859-1.", c.comments
  312. assert_equal "?a?b?c?d?e test", c.committer
  313. end
  314. def test_invalid_utf8_sequences_in_comments_should_be_replaced_ja_jis
  315. proj = Project.find(3)
  316. str = "test\xb5\xfetest\xb5\xfe"
  317. if str.respond_to?(:force_encoding)
  318. str.force_encoding('ASCII-8BIT')
  319. end
  320. r = Repository::Bazaar.create!(
  321. :project => proj,
  322. :url => '/tmp/test/bazaar',
  323. :log_encoding => 'ISO-2022-JP' )
  324. assert r
  325. c = Changeset.new(:repository => r,
  326. :committed_on => Time.now,
  327. :revision => '123',
  328. :scmid => '12345',
  329. :comments => str)
  330. assert( c.save )
  331. assert_equal "test??test??", c.comments
  332. end
  333. def test_comments_should_be_converted_all_latin1_to_utf8
  334. s1 = "\xC2\x80"
  335. s2 = "\xc3\x82\xc2\x80"
  336. s4 = s2.dup
  337. if s1.respond_to?(:force_encoding)
  338. s3 = s1.dup
  339. s1.force_encoding('ASCII-8BIT')
  340. s2.force_encoding('ASCII-8BIT')
  341. s3.force_encoding('ISO-8859-1')
  342. s4.force_encoding('UTF-8')
  343. assert_equal s3.encode('UTF-8'), s4
  344. end
  345. proj = Project.find(3)
  346. r = Repository::Bazaar.create!(
  347. :project => proj,
  348. :url => '/tmp/test/bazaar',
  349. :log_encoding => 'ISO-8859-1' )
  350. assert r
  351. c = Changeset.new(:repository => r,
  352. :committed_on => Time.now,
  353. :revision => '123',
  354. :scmid => '12345',
  355. :comments => s1)
  356. assert( c.save )
  357. assert_equal s4, c.comments
  358. end
  359. def test_invalid_utf8_sequences_in_paths_should_be_replaced
  360. proj = Project.find(3)
  361. str1 = "Texte encod\xe9 en ISO-8859-1"
  362. str2 = "\xe9a\xe9b\xe9c\xe9d\xe9e test"
  363. str1.force_encoding("UTF-8") if str1.respond_to?(:force_encoding)
  364. str2.force_encoding("ASCII-8BIT") if str2.respond_to?(:force_encoding)
  365. r = Repository::Bazaar.create!(
  366. :project => proj,
  367. :url => '/tmp/test/bazaar',
  368. :log_encoding => 'UTF-8' )
  369. assert r
  370. cs = Changeset.new(
  371. :repository => r,
  372. :committed_on => Time.now,
  373. :revision => '123',
  374. :scmid => '12345',
  375. :comments => "test")
  376. assert(cs.save)
  377. ch = Change.new(
  378. :changeset => cs,
  379. :action => "A",
  380. :path => str1,
  381. :from_path => str2,
  382. :from_revision => "345")
  383. assert(ch.save)
  384. assert_equal "Texte encod? en ISO-8859-1", ch.path
  385. assert_equal "?a?b?c?d?e test", ch.from_path
  386. end
  387. def test_comments_nil
  388. proj = Project.find(3)
  389. r = Repository::Bazaar.create!(
  390. :project => proj,
  391. :url => '/tmp/test/bazaar',
  392. :log_encoding => 'ISO-8859-1' )
  393. assert r
  394. c = Changeset.new(:repository => r,
  395. :committed_on => Time.now,
  396. :revision => '123',
  397. :scmid => '12345',
  398. :comments => nil,
  399. :committer => nil)
  400. assert( c.save )
  401. assert_equal "", c.comments
  402. assert_equal nil, c.committer
  403. if c.comments.respond_to?(:force_encoding)
  404. assert_equal "UTF-8", c.comments.encoding.to_s
  405. end
  406. end
  407. def test_comments_empty
  408. proj = Project.find(3)
  409. r = Repository::Bazaar.create!(
  410. :project => proj,
  411. :url => '/tmp/test/bazaar',
  412. :log_encoding => 'ISO-8859-1' )
  413. assert r
  414. c = Changeset.new(:repository => r,
  415. :committed_on => Time.now,
  416. :revision => '123',
  417. :scmid => '12345',
  418. :comments => "",
  419. :committer => "")
  420. assert( c.save )
  421. assert_equal "", c.comments
  422. assert_equal "", c.committer
  423. if c.comments.respond_to?(:force_encoding)
  424. assert_equal "UTF-8", c.comments.encoding.to_s
  425. assert_equal "UTF-8", c.committer.encoding.to_s
  426. end
  427. end
  428. def test_identifier
  429. c = Changeset.find_by_revision('1')
  430. assert_equal c.revision, c.identifier
  431. end
  432. end