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_content_test.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class WikiContentTest < ActiveSupport::TestCase
  19. fixtures :projects, :enabled_modules,
  20. :users, :members, :member_roles, :roles,
  21. :email_addresses,
  22. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
  23. def setup
  24. User.current = nil
  25. @wiki = Wiki.find(1)
  26. @page = @wiki.pages.first
  27. end
  28. def test_create
  29. page = WikiPage.new(:wiki => @wiki, :title => "Page")
  30. page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment")
  31. assert page.save
  32. page.reload
  33. content = page.content
  34. assert_kind_of WikiContent, content
  35. assert_equal 1, content.version
  36. assert_equal 1, content.versions.length
  37. assert_equal "Content text", content.text
  38. assert_equal "My comment", content.comments
  39. assert_equal User.find(1), content.author
  40. assert_equal content.text, content.versions.last.text
  41. end
  42. def test_create_should_send_email_notification
  43. ActionMailer::Base.deliveries.clear
  44. page = WikiPage.new(:wiki => @wiki, :title => "A new page")
  45. page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment")
  46. with_settings :default_language => 'en', :notified_events => %w(wiki_content_added) do
  47. assert page.save
  48. end
  49. assert_equal 2, ActionMailer::Base.deliveries.size
  50. ActionMailer::Base.deliveries.each do |mail|
  51. assert_include 'wiki page has been added', mail_body(mail)
  52. end
  53. end
  54. def test_update_should_be_versioned
  55. content = @page.content
  56. version_count = content.version
  57. content.text = "My new content"
  58. assert_difference 'WikiContentVersion.count' do
  59. assert content.save
  60. end
  61. content.reload
  62. assert_equal version_count+1, content.version
  63. assert_equal version_count+1, content.versions.length
  64. version = WikiContentVersion.order('id DESC').first
  65. assert_equal @page.id, version.page_id
  66. assert_equal '', version.compression
  67. assert_equal "My new content", version.data
  68. assert_equal "My new content", version.text
  69. end
  70. def test_update_with_gzipped_history
  71. with_settings :wiki_compression => 'gzip' do
  72. content = @page.content
  73. content.text = "My new content"
  74. assert_difference 'WikiContentVersion.count' do
  75. assert content.save
  76. end
  77. end
  78. version = WikiContentVersion.order('id DESC').first
  79. assert_equal @page.id, version.page_id
  80. assert_equal 'gzip', version.compression
  81. assert_not_equal "My new content", version.data
  82. assert_equal "My new content", version.text
  83. end
  84. def test_update_should_send_email_notification
  85. ActionMailer::Base.deliveries.clear
  86. content = @page.content
  87. content.text = "My new content"
  88. with_settings :notified_events => %w(wiki_content_updated), :default_language => 'en' do
  89. assert content.save
  90. end
  91. assert_equal 2, ActionMailer::Base.deliveries.size
  92. ActionMailer::Base.deliveries.each do |mail|
  93. assert_include 'wiki page has been updated', mail_body(mail)
  94. end
  95. end
  96. def test_fetch_history
  97. assert !@page.content.versions.empty?
  98. @page.content.versions.each do |version|
  99. assert_kind_of String, version.text
  100. end
  101. end
  102. def test_large_text_should_not_be_truncated_to_64k
  103. page = WikiPage.new(:wiki => @wiki, :title => "Big page")
  104. page.content = WikiContent.new(:text => "a" * 500.kilobyte, :author => User.find(1))
  105. assert page.save
  106. page.reload
  107. assert_equal 500.kilobyte, page.content.text.size
  108. end
  109. def test_current_version
  110. content = WikiContent.find(11)
  111. assert_equal true, content.current_version?
  112. assert_equal true, content.versions.order('version DESC').first.current_version?
  113. assert_equal false, content.versions.order('version ASC').first.current_version?
  114. end
  115. def test_previous_for_first_version_should_return_nil
  116. content = WikiContentVersion.find_by_page_id_and_version(1, 1)
  117. assert_nil content.previous
  118. end
  119. def test_previous_for_version_should_return_previous_version
  120. content = WikiContentVersion.find_by_page_id_and_version(1, 3)
  121. assert_not_nil content.previous
  122. assert_equal 2, content.previous.version
  123. end
  124. def test_previous_for_version_with_gap_should_return_previous_available_version
  125. WikiContentVersion.find_by_page_id_and_version(1, 2).destroy
  126. content = WikiContentVersion.find_by_page_id_and_version(1, 3)
  127. assert_not_nil content.previous
  128. assert_equal 1, content.previous.version
  129. end
  130. def test_next_for_last_version_should_return_nil
  131. content = WikiContentVersion.find_by_page_id_and_version(1, 3)
  132. assert_nil content.next
  133. end
  134. def test_next_for_version_should_return_next_version
  135. content = WikiContentVersion.find_by_page_id_and_version(1, 1)
  136. assert_not_nil content.next
  137. assert_equal 2, content.next.version
  138. end
  139. def test_next_for_version_with_gap_should_return_next_available_version
  140. WikiContentVersion.find_by_page_id_and_version(1, 2).destroy
  141. content = WikiContentVersion.find_by_page_id_and_version(1, 1)
  142. assert_not_nil content.next
  143. assert_equal 3, content.next.version
  144. end
  145. end