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

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