PageRenderTime 33ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/plugins/rails-footnotes/test/footnotes_test.rb

http://github.com/stevenbristol/lovd-by-less
Ruby | 190 lines | 154 code | 33 blank | 3 comment | 0 complexity | 3dae1c17a981dd456ca125c87fb97ce8 MD5 | raw file
Possible License(s): MIT
  1. require File.dirname(__FILE__) + '/test_helper'
  2. require 'action_controller'
  3. require 'action_controller/test_case'
  4. require 'action_controller/test_process'
  5. class FootnotesController < ActionController::Base; attr_accessor :template, :performed_render; end
  6. module Footnotes::Notes
  7. class TestNote < AbstractNote
  8. def self.to_sym; :test; end
  9. def valid?; true; end
  10. end
  11. end
  12. class FootnotesTest < Test::Unit::TestCase
  13. def setup
  14. @controller = FootnotesController.new
  15. @controller.request = ActionController::TestRequest.new
  16. @controller.response = ActionController::TestResponse.new
  17. @controller.response.body = $html.dup
  18. Footnotes::Filter.notes = [ :test ]
  19. Footnotes::Filter.multiple_notes = false
  20. @footnotes = Footnotes::Filter.new(@controller)
  21. end
  22. def test_footnotes_controller
  23. index = @controller.response.body.index(/This is the HTML page/)
  24. assert_equal 334, index
  25. end
  26. def test_foonotes_included
  27. footnotes_perform!
  28. assert_not_equal $html, @controller.response.body
  29. end
  30. def test_footnotes_not_included_when_request_is_xhr
  31. @controller.request.env['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
  32. @controller.request.env['HTTP_ACCEPT'] = 'text/javascript, text/html, application/xml, text/xml, */*'
  33. footnotes_perform!
  34. assert_equal $html, @controller.response.body
  35. end
  36. def test_footnotes_not_included_when_content_type_is_javascript
  37. @controller.response.headers['Content-Type'] = 'text/javascript'
  38. footnotes_perform!
  39. assert_equal $html, @controller.response.body
  40. end
  41. def test_footnotes_included_when_content_type_is_html
  42. @controller.response.headers['Content-Type'] = 'text/html'
  43. footnotes_perform!
  44. assert_not_equal $html, @controller.response.body
  45. end
  46. def test_footnotes_included_when_content_type_is_nil
  47. footnotes_perform!
  48. assert_not_equal $html, @controller.response.body
  49. end
  50. def test_not_included_when_body_is_not_a_string
  51. @controller.response.body = Proc.new{ Time.now }
  52. @footnotes = Footnotes::Filter.new(@controller)
  53. assert_nothing_raised do
  54. footnotes_perform!
  55. end
  56. end
  57. def test_notes_are_initialized
  58. footnotes_perform!
  59. test_note = @footnotes.instance_variable_get('@notes').first
  60. assert 'Footnotes::Notes::TestNote', test_note.class
  61. assert :test, test_note.to_sym
  62. end
  63. def test_notes_links
  64. footnotes_perform!
  65. @footnotes.instance_variable_get('@notes').first.expects(:row).times(2)
  66. footnotes_perform!
  67. end
  68. def test_notes_fieldset
  69. footnotes_perform!
  70. @footnotes.instance_variable_get('@notes').first.expects(:fieldset?).times(3)
  71. footnotes_perform!
  72. end
  73. def test_multiple_notes
  74. Footnotes::Filter.multiple_notes = true
  75. footnotes_perform!
  76. @footnotes.instance_variable_get('@notes').first.expects(:fieldset?).times(2)
  77. footnotes_perform!
  78. end
  79. def test_notes_are_reset
  80. footnotes_perform!
  81. @footnotes.instance_variable_get('@notes').first.class.expects(:close!)
  82. @footnotes.send(:close!, @controller)
  83. end
  84. def test_links_helper
  85. note = Footnotes::Notes::TestNote.new
  86. note.expects(:title).times(2).returns(:title)
  87. assert_equal '<a href="#" onclick="">title</a>', @footnotes.send(:link_helper, note)
  88. note.expects(:link).times(1).returns(:link)
  89. assert_equal '<a href="link" onclick="">title</a>', @footnotes.send(:link_helper, note)
  90. end
  91. def test_links_helper_fieldset?
  92. note = Footnotes::Notes::TestNote.new
  93. note.expects(:title).times(1).returns(:title)
  94. note.expects(:fieldset?).times(1).returns(true)
  95. assert_equal '<a href="#" onclick="footnotes_toogle(\'test_debug_info\');return false;">title</a>', @footnotes.send(:link_helper, note)
  96. end
  97. def test_links_helper_onclick
  98. note = Footnotes::Notes::TestNote.new
  99. note.expects(:title).times(2).returns(:title)
  100. note.expects(:onclick).times(2).returns(:onclick)
  101. assert_equal '<a href="#" onclick="onclick">title</a>', @footnotes.send(:link_helper, note)
  102. note.expects(:fieldset?).times(1).returns(true)
  103. assert_equal '<a href="#" onclick="onclick">title</a>', @footnotes.send(:link_helper, note)
  104. end
  105. def test_insert_style
  106. @controller.response.body = "<head></head><split><body></body>"
  107. @footnotes = Footnotes::Filter.new(@controller)
  108. footnotes_perform!
  109. assert @controller.response.body.split('<split>').first.include?('<!-- Footnotes Style -->')
  110. end
  111. def test_insert_footnotes_inside_body
  112. @controller.response.body = "<head></head><split><body></body>"
  113. @footnotes = Footnotes::Filter.new(@controller)
  114. footnotes_perform!
  115. assert @controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->')
  116. end
  117. def test_insert_footnotes_inside_holder
  118. @controller.response.body = "<head></head><split><div id='footnotes_holder'></div>"
  119. @footnotes = Footnotes::Filter.new(@controller)
  120. footnotes_perform!
  121. assert @controller.response.body.split('<split>').last.include?('<!-- End Footnotes -->')
  122. end
  123. def test_insert_text
  124. @footnotes.send(:insert_text, :after, /<head>/, "Graffiti")
  125. after = " <head>Graffiti\n"
  126. assert_equal after, @controller.response.body.to_a[2]
  127. @footnotes.send(:insert_text, :before, /<\/body>/, "Notes")
  128. after = " Notes</body>\n"
  129. assert_equal after, @controller.response.body.to_a[12]
  130. end
  131. protected
  132. # First we make sure that footnotes will perform (long life to mocha!)
  133. # Then we call add_footnotes!
  134. #
  135. def footnotes_perform!
  136. @controller.template.expects(:instance_variable_get).returns(true)
  137. @controller.template.expects(:template_format).returns('html')
  138. @controller.performed_render = true
  139. @footnotes.add_footnotes!
  140. end
  141. end
  142. $html = <<HTML
  143. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  144. <html>
  145. <head>
  146. <title>HTML to XHTML Example: HTML page</title>
  147. <link rel="Stylesheet" href="htmltohxhtml.css" type="text/css" media="screen">
  148. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  149. </head>
  150. <body>
  151. <p>This is the HTML page. It works and is encoded just like any HTML page you
  152. have previously done. View <a href="htmltoxhtml2.htm">the XHTML version</a> of
  153. this page to view the difference between HTML and XHTML.</p>
  154. <p>You will be glad to know that no changes need to be made to any of your CSS files.</p>
  155. </body>
  156. </html>
  157. HTML