PageRenderTime 102ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/test/unit/lib/content_block_test.rb

https://github.com/Doap/browsercms
Ruby | 333 lines | 284 code | 49 blank | 0 comment | 2 complexity | 94e2a0a8f793b8b1d7254438bc0046d9 MD5 | raw file
  1. require 'test_helper'
  2. class Cms::ApiTest < ActiveSupport::TestCase
  3. test "Find block from tablename without namespace" do
  4. assert_equal Cms::HtmlBlock, Cms::Acts::ContentBlock.model_for(:html_block)
  5. end
  6. test "Find block from tablename with namespace" do
  7. Cms.expects(:table_prefix).returns("cms_")
  8. assert_equal Cms::HtmlBlock, Cms::Acts::ContentBlock.model_for(:cms_html_block)
  9. end
  10. test "Find model for non-namespaced classes" do
  11. class ::Unscoped ; end
  12. model = Cms::Acts::ContentBlock.model_for(:unscoped)
  13. assert_equal ::Unscoped, model
  14. end
  15. test "Nonexistant class returns NilModel that still responds to defaults" do
  16. model = Cms::Acts::ContentBlock.model_for(:table_not_likely_to_exist)
  17. assert_equal Cms::Acts::ContentBlock::NilModel, model.class
  18. assert_equal "table_not_likely_to_exist_id", model.version_foreign_key
  19. end
  20. end
  21. class ContentBlockTest < ActiveSupport::TestCase
  22. def setup
  23. @block = Factory(:html_block, :name => "Test")
  24. end
  25. def test_publishing
  26. assert_equal "Draft", @block.status_name
  27. assert !@block.published?
  28. @block.publish
  29. assert @block.published?
  30. @block.publish_on_save = true
  31. @block.save
  32. assert @block.published?
  33. assert @block.update_attributes(:name => "Whatever")
  34. assert !@block.live?
  35. end
  36. def test_revision_comment_on_create
  37. assert_equal 'Created', @block.draft.version_comment
  38. end
  39. def test_revision_comment_on_update
  40. assert @block.update_attributes(:name => "Something Else", :content => "Whatever")
  41. assert_equal 'Changed content, name', @block.draft.version_comment
  42. end
  43. test "Updating a block without changing attributes shouldn't cause new save" do
  44. result = @block.update_attributes(:name => @block.name)
  45. assert_equal 1, @block.version, "Block should keep itself at version 1"
  46. assert_equal 1, @block.versions.size, "Should only have the one original version of the block"
  47. assert_equal 'Created', @block.draft.version_comment
  48. assert result , "Update with same attributes should still return true"
  49. end
  50. def test_custom_revision_comment
  51. assert @block.update_attributes(:name => "Something Else", :version_comment => "Something Changed")
  52. assert_equal "Something Changed", @block.draft.version_comment
  53. end
  54. end
  55. class SoftPublishingTest < ActiveSupport::TestCase
  56. def setup
  57. @block = Factory(:html_block, :name => "Test")
  58. end
  59. test "deleted? should return true for deleted records, false otherwise" do
  60. assert_equal false, @block.deleted?
  61. @block.destroy
  62. assert_equal true, @block.deleted?
  63. end
  64. test "Destroying a block should mark it as deleted, rather than remove it from the database" do
  65. @block.destroy
  66. found = Cms::HtmlBlock.find_by_sql("SELECT * FROM #{Cms::HtmlBlock.table_name} where id = #{@block.id}")
  67. assert_equal 1, found.size, "Should find one record"
  68. assert_not_nil found[0], "A block should still exist in the database"
  69. assert_equal true, found[0].deleted, "It's published flag should be true"
  70. end
  71. test "exists? should not return blocks marked as deleted" do
  72. @block.destroy
  73. assert_equal false, Cms::HtmlBlock.exists?(@block.id)
  74. assert_equal false, Cms::HtmlBlock.exists?(["name = ?", @block.name])
  75. end
  76. test "find by id should throw an exception for records marked as deleted" do
  77. @block.destroy
  78. assert_raise(ActiveRecord::RecordNotFound) { Cms::HtmlBlock.find(@b) }
  79. end
  80. test "dynamic finders (i.e. find_by_name) should not find deleted records" do
  81. @block.destroy
  82. assert_nil Cms::HtmlBlock.find_by_name(@block.name)
  83. end
  84. test "find with deleted returns all records even marked as deleted" do
  85. @block.destroy
  86. assert_not_nil Cms::HtmlBlock.find_with_deleted(@block.id)
  87. end
  88. test "Marking as deleted should create a new record in the versions table" do
  89. @block.destroy
  90. deleted_block = Cms::HtmlBlock.find_with_deleted(@block.id)
  91. assert_equal 2, deleted_block.versions.size
  92. assert_equal 2, deleted_block.version
  93. assert_equal 1, deleted_block.versions.first.version
  94. assert_equal 2, Cms::HtmlBlock::Version.count(:conditions => {:html_block_id => @block.id})
  95. end
  96. test "Count should exclude deleted records" do
  97. html_block_count = Cms::HtmlBlock.count
  98. @block.destroy
  99. assert_decremented html_block_count, Cms::HtmlBlock.count
  100. end
  101. test "count_with_deleted should return all records, even those marked as deleted" do
  102. original_count = Cms::HtmlBlock.count
  103. @block.destroy
  104. assert_equal original_count, Cms::HtmlBlock.count_with_deleted
  105. end
  106. def test_delete_all
  107. Cms::HtmlBlock.delete_all(["name = ?", @block.name])
  108. assert_raise(ActiveRecord::RecordNotFound) { Cms::HtmlBlock.find(@block.id) }
  109. assert Cms::HtmlBlock.find_with_deleted(@block.id).deleted?
  110. end
  111. end
  112. class VersionedContentBlock < ActiveSupport::TestCase
  113. def setup
  114. @block = Factory(:html_block, :name => "Versioned Content Block")
  115. end
  116. test "Calling publish! on a block should save it, and mark that block as published." do
  117. @block.publish!
  118. found = Cms::HtmlBlock.find(@block)
  119. assert_equal true, found.published?
  120. end
  121. test "Getting a block as of a particular version shouldn't be considered a 'new record'." do
  122. @block.update_attributes(:name=>"Changed", :publish_on_save=>true)
  123. @block.reload
  124. @v1 = @block.as_of_version(1)
  125. assert_equal false, @v1.new_record?, "Old versions of blocks aren't 'new' and shouldn't ever be resaved."
  126. end
  127. test 'Calling publish_on_save should not be sufficent to publish the block'do
  128. @block.publish_on_save = true
  129. @block.save
  130. found = Cms::HtmlBlock.find(@block)
  131. assert_equal 1, found.version
  132. end
  133. test "Setting the 'publish' flag on a block, along with any other change, and saving it should mark that block as published." do
  134. @block.publish_on_save = true
  135. @block.name = "Anything else"
  136. @block.save
  137. found = Cms::HtmlBlock.find(@block)
  138. assert_equal true, found.published?
  139. end
  140. def test_edit
  141. old_name = "Versioned Content Block"
  142. new_name = "New version of content block"
  143. @block.publish!
  144. @block.reload
  145. assert_equal @block.draft.name, old_name
  146. @block.name = new_name
  147. @block.save
  148. @block.reload
  149. assert_equal @block.draft.name, new_name
  150. @block.name = old_name
  151. @block.save
  152. @block.reload
  153. assert_equal @block.draft.name, old_name
  154. end
  155. def test_revert
  156. old_name = "Versioned Content Block"
  157. new_name = "New version of content block"
  158. @block.publish!
  159. @block.reload
  160. assert_equal @block.draft.name, old_name
  161. version = @block.version
  162. @block.name = new_name
  163. @block.save
  164. @block.reload
  165. assert_equal @block.draft.name, new_name
  166. @block.revert_to(version)
  167. @block.reload
  168. assert_equal @block.draft.name, old_name
  169. end
  170. end
  171. class VersionedContentBlockConnectedToAPageTest < ActiveSupport::TestCase
  172. def setup
  173. @page = Factory(:page, :section => root_section)
  174. @block = Factory(:html_block, :name => "Versioned Content Block")
  175. @page.create_connector(@block, "main")
  176. reset(:page, :block)
  177. end
  178. def test_editing_connected_to_an_unpublished_page
  179. page_version_count = Cms::Page::Version.count
  180. assert_equal 2, @page.versions.size, "Should be two versions of the page"
  181. assert !@page.published?, "The page should not be published yet."
  182. pages = Cms::Page.connected_to(:connectable => @block, :version => @block.version).all
  183. assert_equal [@page], pages, "The block should be connected to page"
  184. assert @block.update_attributes(:name => "something different")
  185. assert_equal false, @block.skip_callbacks
  186. assert_equal 2, @block.versions.size, "should be two versions of this block"
  187. reset(:page)
  188. assert !@page.published?
  189. assert_equal 3, @page.versions.size, "Should be three versions of the page."
  190. assert_equal 3, @page.draft.version, "Draft version of page should be updated to v3 since its connected to the updated block."
  191. assert_incremented page_version_count, Cms::Page::Version.count
  192. assert_match /^HtmlBlock #\d+ was Edited/, @page.draft.version_comment
  193. conns = @block.connectors.all(:order => 'id')
  194. assert_equal 2, conns.size
  195. assert_properties conns[0], :page => @page, :page_version => 2, :connectable => @block, :connectable_version => 1, :container => "main"
  196. assert_properties conns[1], :page => @page, :page_version => 3, :connectable => @block, :connectable_version => 2, :container => "main"
  197. end
  198. # Verify that when we have a block connected to a published page, the page should remain published.
  199. def test_editing_connected_to_a_published_page
  200. @page.publish!
  201. reset(:page, :block)
  202. assert @page.published?
  203. assert @block.update_attributes(:name => "something different", :publish_on_save => true)
  204. reset(:page)
  205. assert @page.published?
  206. end
  207. def test_deleting_when_connected_to_page
  208. @page.publish!
  209. reset(:page, :block)
  210. page_connector_count = @page.connectors.for_page_version(@page.draft.version).count
  211. page_version = @page.draft.version
  212. @block.destroy
  213. reset(:page)
  214. assert_decremented page_connector_count, @page.connectors.for_page_version(@page.draft.version).count
  215. assert_incremented page_version, @page.draft.version
  216. assert_match /^HtmlBlock #\d+ was Deleted/, @page.draft.version_comment
  217. end
  218. end
  219. class NonVersionedContentBlockConnectedToAPageTest < ActiveSupport::TestCase
  220. def setup
  221. @page = Factory(:page, :section => root_section)
  222. @block = Factory(:non_versioned_block, :name => "Non-Versioned Non-Publishable Content Block")
  223. @page.create_connector(@block, "main")
  224. reset(:page, :block)
  225. end
  226. def test_editing_connected_to_an_unpublished_page
  227. page_version_count = Cms::Page::Version.count
  228. assert_equal "Dynamic Portlet '#{@block.name}' was added to the 'main' container", @page.draft.version_comment
  229. assert !@page.published?
  230. assert @block.update_attributes(:name => "something different")
  231. reset(:page)
  232. assert 2, @page.version
  233. assert_equal page_version_count, Cms::Page::Version.count
  234. assert !@page.published?
  235. conns = Cms::Connector.for_connectable(@block).all(:order => 'id')
  236. assert_equal 1, conns.size
  237. assert_properties conns[0], :page => @page, :page_version => 2, :connectable => @block, :connectable_version => nil, :container => "main"
  238. end
  239. def test_editing_connected_to_a_published_page
  240. @page.publish!
  241. reset(:page)
  242. assert @page.published?
  243. assert @block.update_attributes(:name => "something different")
  244. reset(:page)
  245. assert @page.published?
  246. end
  247. end