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

/plugins/resource_feeder/test/atom_feed_test.rb

https://github.com/bennett/communityengine
Ruby | 85 lines | 76 code | 9 blank | 0 comment | 0 complexity | 6ffe0e0b57e9d9a6b1b54396c1ad2c7e MD5 | raw file
  1. require File.dirname(__FILE__) + '/test_helper'
  2. class AtomFeedTest < Test::Unit::TestCase
  3. attr_reader :request
  4. def setup
  5. @request = OpenStruct.new
  6. @request.host_with_port = 'example.com'
  7. @records = Array.new(5).fill(Post.new)
  8. @records.each &:save
  9. end
  10. def test_default_atom_feed
  11. atom_feed_for @records
  12. assert_select 'feed' do
  13. assert_select '>title', 'Posts'
  14. assert_select '>id', "tag:#{request.host_with_port}:Posts"
  15. assert_select '>link' do
  16. assert_select "[rel='alternate']"
  17. assert_select "[type='text/html']"
  18. assert_select "[href='http://example.com/posts']"
  19. end
  20. assert_select 'entry', 5 do
  21. assert_select 'title', :text => 'feed title (title)'
  22. assert_select "content[type='html']", '&lt;p&gt;feed description (description)&lt;/p&gt;'
  23. assert_select 'id', "tag:#{request.host_with_port},#{@records.first.created_at.xmlschema}:#{'http://example.com/posts/1'}"
  24. assert_select 'published', @records.first.created_at.xmlschema
  25. assert_select 'updated', @records.first.created_at.xmlschema
  26. assert_select 'link' do
  27. assert_select "[rel='alternate']"
  28. assert_select "[type='text/html']"
  29. assert_select "[href='http://example.com/posts/1']"
  30. end
  31. end
  32. end
  33. end
  34. def test_should_allow_custom_feed_options
  35. atom_feed_for @records, :feed => { :title => 'Custom Posts', :link => '/posts', :description => 'stuff', :self => '/posts.atom' }
  36. assert_select 'feed>title', 'Custom Posts'
  37. assert_select "feed>link[href='/posts']"
  38. assert_select 'feed>subtitle', 'stuff'
  39. assert_select 'feed>link' do
  40. assert_select "[rel='self']"
  41. assert_select "[type='application/atom+xml']"
  42. assert_select "[href='/posts.atom']"
  43. end
  44. end
  45. def test_should_allow_custom_item_attributes
  46. atom_feed_for @records, :item => { :title => :name, :description => :body, :pub_date => :create_date, :link => :id }
  47. assert_select 'entry', 5 do
  48. assert_select 'title', :text => 'feed title (name)'
  49. assert_select "content[type='html']", '&lt;p&gt;feed description (body)&lt;/p&gt;'
  50. assert_select 'published', (@records.first.created_at - 5.minutes).xmlschema
  51. assert_select 'updated', (@records.first.created_at - 5.minutes).xmlschema
  52. assert_select 'id', "tag:#{request.host_with_port},#{(@records.first.created_at - 5.minutes).xmlschema}:1"
  53. assert_select 'link' do
  54. assert_select "[rel='alternate']"
  55. assert_select "[type='text/html']"
  56. assert_select "[href='1']"
  57. end
  58. end
  59. end
  60. def test_should_allow_custom_item_attribute_blocks
  61. atom_feed_for @records, :item => { :title => lambda { |r| r.name }, :description => lambda { |r| r.body }, :pub_date => lambda { |r| r.create_date },
  62. :link => lambda { |r| "/#{r.created_at.to_i}" }, :guid => lambda { |r| r.created_at.to_i } }
  63. assert_select 'entry', 5 do
  64. assert_select 'title', :text => 'feed title (name)'
  65. assert_select "content[type='html']", '&lt;p&gt;feed description (body)&lt;/p&gt;'
  66. assert_select 'published', (@records.first.created_at - 5.minutes).xmlschema
  67. assert_select 'updated', (@records.first.created_at - 5.minutes).xmlschema
  68. assert_select 'id', /:\d+$/
  69. assert_select 'link' do
  70. assert_select "[rel='alternate']"
  71. assert_select "[type='text/html']"
  72. assert_select "[href=?]", /^\/\d+$/
  73. end
  74. end
  75. end
  76. end