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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 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 WikiContentTest < ActiveSupport::TestCase
  20. fixtures :projects, :enabled_modules,
  21. :users, :members, :member_roles, :roles,
  22. :email_addresses,
  23. :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
  24. def setup
  25. User.current = nil
  26. @wiki = Wiki.find(1)
  27. @page = @wiki.pages.first
  28. end
  29. def test_create
  30. page = WikiPage.new(:wiki => @wiki, :title => "Page")
  31. page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment")
  32. assert page.save
  33. page.reload
  34. content = page.content
  35. assert_kind_of WikiContent, content
  36. assert_equal 1, content.version
  37. assert_equal 1, content.versions.length
  38. assert_equal "Content text", content.text
  39. assert_equal "My comment", content.comments
  40. assert_equal User.find(1), content.author
  41. assert_equal content.text, content.versions.last.text
  42. end
  43. def test_create_should_send_email_notification
  44. ActionMailer::Base.deliveries.clear
  45. page = WikiPage.new(:wiki => @wiki, :title => "A new page")
  46. page.content = WikiContent.new(:text => "Content text", :author => User.find(1), :comments => "My comment")
  47. with_settings :default_language => 'en', :notified_events => %w(wiki_content_added) do
  48. assert page.save
  49. end
  50. assert_equal 2, ActionMailer::Base.deliveries.size
  51. ActionMailer::Base.deliveries.each do |mail|
  52. assert_include 'wiki page has been added', mail_body(mail)
  53. end
  54. end
  55. def test_update_should_be_versioned
  56. content = @page.content
  57. version_count = content.version
  58. content.text = "My new content"
  59. assert_difference 'WikiContentVersion.count' do
  60. assert content.save
  61. end
  62. content.reload
  63. assert_equal version_count+1, content.version
  64. assert_equal version_count+1, content.versions.length
  65. version = WikiContentVersion.order('id DESC').first
  66. assert_equal @page.id, version.page_id
  67. assert_equal '', version.compression
  68. assert_equal "My new content", version.data
  69. assert_equal "My new content", version.text
  70. end
  71. def test_update_with_gzipped_history
  72. with_settings :wiki_compression => 'gzip' do
  73. content = @page.content
  74. content.text = "My new content"
  75. assert_difference 'WikiContentVersion.count' do
  76. assert content.save
  77. end
  78. end
  79. version = WikiContentVersion.order('id DESC').first
  80. assert_equal @page.id, version.page_id
  81. assert_equal 'gzip', version.compression
  82. assert_not_equal "My new content", version.data
  83. assert_equal "My new content", version.text
  84. end
  85. def test_update_should_send_email_notification
  86. ActionMailer::Base.deliveries.clear
  87. content = @page.content
  88. content.text = "My new content"
  89. with_settings :notified_events => %w(wiki_content_updated), :default_language => 'en' do
  90. assert content.save
  91. end
  92. assert_equal 2, ActionMailer::Base.deliveries.size
  93. ActionMailer::Base.deliveries.each do |mail|
  94. assert_include 'wiki page has been updated', mail_body(mail)
  95. end
  96. end
  97. def test_fetch_history
  98. assert !@page.content.versions.empty?
  99. @page.content.versions.each do |version|
  100. assert_kind_of String, version.text
  101. end
  102. end
  103. def test_large_text_should_not_be_truncated_to_64k
  104. page = WikiPage.new(:wiki => @wiki, :title => "Big page")
  105. page.content = WikiContent.new(:text => "a" * 500.kilobyte, :author => User.find(1))
  106. assert page.save
  107. page.reload
  108. assert_equal 500.kilobyte, page.content.text.size
  109. end
  110. def test_current_version
  111. content = WikiContent.find(11)
  112. assert_equal true, content.current_version?
  113. assert_equal true, content.versions.order('version DESC').first.current_version?
  114. assert_equal false, content.versions.order('version ASC').first.current_version?
  115. end
  116. def test_previous_for_first_version_should_return_nil
  117. content = WikiContentVersion.find_by_page_id_and_version(1, 1)
  118. assert_nil content.previous
  119. end
  120. def test_previous_for_version_should_return_previous_version
  121. content = WikiContentVersion.find_by_page_id_and_version(1, 3)
  122. assert_not_nil content.previous
  123. assert_equal 2, content.previous.version
  124. end
  125. def test_previous_for_version_with_gap_should_return_previous_available_version
  126. WikiContentVersion.find_by_page_id_and_version(1, 2).destroy
  127. content = WikiContentVersion.find_by_page_id_and_version(1, 3)
  128. assert_not_nil content.previous
  129. assert_equal 1, content.previous.version
  130. end
  131. def test_next_for_last_version_should_return_nil
  132. content = WikiContentVersion.find_by_page_id_and_version(1, 3)
  133. assert_nil content.next
  134. end
  135. def test_next_for_version_should_return_next_version
  136. content = WikiContentVersion.find_by_page_id_and_version(1, 1)
  137. assert_not_nil content.next
  138. assert_equal 2, content.next.version
  139. end
  140. def test_next_for_version_with_gap_should_return_next_available_version
  141. WikiContentVersion.find_by_page_id_and_version(1, 2).destroy
  142. content = WikiContentVersion.find_by_page_id_and_version(1, 1)
  143. assert_not_nil content.next
  144. assert_equal 3, content.next.version
  145. end
  146. end