cirandas.net

ref: master

test/unit/rss_feed_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
require_relative "../test_helper"

class RssFeedTest < ActiveSupport::TestCase

  should 'indicate the correct mime/type' do
    assert_equal 'text/xml', RssFeed.new.mime_type
  end

  should 'store settings in a hash serialized into body field' do
    feed = RssFeed.new
    assert_kind_of Hash, feed.body

    feed.body = {
      :feed_item_description => 'abstract',
      :search => 'parent_and_children',
    }
    feed.valid?
    refute feed.errors['body'.to_s].present?
  end

  should 'alias body as "settings"' do
    feed = RssFeed.new
    assert_same feed.body, feed.settings
  end

  should 'list recent articles of profile when top-level' do
    profile = create_user('testuser').person
    a1 = profile.articles.build(:name => 'article 1'); a1.save!
    a2 = profile.articles.build(:name => 'article 2'); a2.save!
    a3 = profile.articles.build(:name => 'article 3'); a3.save!

    feed = build(RssFeed, :name => 'testfeed')
    feed.profile = profile
    feed.save!

    rss = feed.data
    assert_match /<item><title>article 1<\/title>/, rss
    assert_match /<item><title>article 2<\/title>/, rss
    assert_match /<item><title>article 3<\/title>/, rss
  end

  should 'not list self' do
    profile = create_user('testuser').person
    a1 = profile.articles.build(:name => 'article 1'); a1.save!
    a2 = profile.articles.build(:name => 'article 2'); a2.save!
    a3 = profile.articles.build(:name => 'article 3'); a3.save!

    feed = build(RssFeed, :name => 'testfeed')
    feed.profile = profile
    feed.save!

    rss = feed.data
    assert_no_match /<item><title>testfeed<\/title>/, rss
  end

  should 'list recent article from parent article' do
    profile = create_user('testuser').person
    feed = RssFeed.new
    feed.expects(:profile).returns(profile).at_least_once
    array = []
    profile.expects(:last_articles).returns(array)
    feed.data
  end

  should "be able to search only children of feed's parent" do
    profile = create_user('testuser').person
    a1 = profile.articles.build(:name => 'article 1'); a1.save!
    a2 = profile.articles.build(:name => 'article 2'); a2.save!

    a3 = profile.articles.build(:name => 'article 3'); a3.save!
    a3_1 = a3.children.build(:name => 'article 3.1', :parent => a3, :profile => profile); a3_1.save!
    a3_2 = a3.children.build(:name => 'article 3.2', :parent => a3, :profile => profile); a3_2.save!
    a3_2_1 = a3_2.children.build(:name => 'article 3.2.1', :parent => a3_2, :profile => profile); a3_2_1.save!

    a3.reload
    feed = build(RssFeed, :name => 'testfeed')
    feed.parent = a3
    feed.profile = profile
    feed.include = 'parent_and_children'
    feed.save!

    rss = feed.data
    assert_match /<item><title>article 3<\/title>/, rss
    assert_match /<item><title>article 3\.1<\/title>/, rss
    assert_match /<item><title>article 3\.2<\/title>/, rss
    assert_match /<item><title>article 3\.2\.1<\/title>/, rss

    assert_no_match /<item><title>article 1<\/title>/, rss
    assert_no_match /<item><title>article 2<\/title>/, rss
  end

  should 'list blog posts with more recent first and respecting limit' do
    profile = create_user('testuser').person
    blog = create(Blog, :name => 'blog-test', :profile => profile)
    posts = []
    6.times do |i|
      posts << fast_create(TextArticle, :name => "post #{i}", :profile_id => profile.id, :parent_id => blog.id)
    end
    feed = blog.feed
    feed.limit = 5
    feed.save!

    assert_equal [posts[5], posts[4], posts[3],  posts[2], posts[1]], feed.fetch_articles
  end

  should 'list only published posts from blog' do
    profile = create_user('testuser').person
    blog = create(Blog, :name => 'blog-test', :profile => profile)
    posts = []
    5.times do |i|
      posts << fast_create(TextArticle, :name => "post #{i}", :profile_id => profile.id, :parent_id => blog.id)
    end
    posts[0].published = false
    posts[0].save!

    assert_equal [posts[4], posts[3], posts[2], posts[1]], blog.feed.fetch_articles
  end


  should 'provide link to profile' do
    profile = create_user('testuser').person
    feed = build(RssFeed, :name => 'testfeed')
    feed.profile = profile
    feed.save!

    assert_match "<link>http://#{profile.environment.default_hostname}/testuser/homepage</link>", feed.data
  end

  should 'provide link to each article' do
    profile = create_user('testuser').person
    art = profile.articles.build(:name => 'myarticle'); art.save!
    feed = build(RssFeed, :name => 'testfeed')
    feed.profile = profile
    feed.save!

    data = feed.data
    assert_match "<link>http://#{art.profile.environment.default_hostname}/testuser/myarticle</link>", data
    assert_match "<guid>http://#{art.profile.environment.default_hostname}/testuser/myarticle</guid>", data
  end

  should 'be able to indicate maximum number of items' do
    profile = create_user('testuser').person
    a1 = profile.articles.build(:name => 'article 1'); a1.save!
    a2 = profile.articles.build(:name => 'article 2'); a2.save!
    a3 = profile.articles.build(:name => 'article 3'); a3.save!

    feed = build(RssFeed, :name => 'testfeed')
    feed.profile = profile
    feed.save!

    feed.profile.expects(:last_articles).with(10).returns([]).once
    feed.data

    feed.limit = 5
    feed.profile.expects(:last_articles).with(5).returns([]).once
    feed.data
  end

  should 'limit should only accept integers' do
    feed = RssFeed.new
    feed.limit = 'text'
    assert_not_equal 'text', feed.limit
    feed.limit = 10
    assert_equal 10, feed.limit
  end

  should 'allow only parent_and_children and all as include setting' do
    feed = RssFeed.new
    feed.include = :something_else
    feed.valid?
    assert feed.errors[:include.to_s].present?

    feed.include = 'parent_and_children'
    feed.valid?
    refute feed.errors[:include.to_s].present?

    feed.include = 'all'
    feed.valid?
    refute feed.errors[:include.to_s].present?
  end

  should 'provide proper short description' do
    assert_kind_of String, RssFeed.short_description
  end

  should 'provide proper description' do
    assert_kind_of String, RssFeed.description
  end

  should 'provide the correct icon name' do
    assert_equal 'rss-feed', RssFeed.icon_name
  end

  should 'advertise is false before create' do
    profile = create_user('testuser').person
    feed = create(RssFeed, :name => 'testfeed', :profile => profile)
    refute feed.advertise?
  end

  should 'can display hits' do
    p = create_user('testuser').person
    a = create(RssFeed, :name => 'Test article', :profile => p)
    assert_equal false, a.can_display_hits?
  end

  should 'display the referenced body of a article published' do
    article = fast_create(TextArticle, :body => 'This is the content of the Sample Article.', :profile_id => fast_create(Person).id)
    profile = fast_create(Profile)
    blog = fast_create(Blog, :profile_id => profile.id)
    a = create(ApproveArticle, :name => 'test name', :article => article, :target => profile, :requestor => fast_create(Person))
    a.requestor.stubs(:notification_emails).returns(['random@example.org'])
    a.finish

    published_article = article.class.last
    published_article.parent = blog
    published_article.save

    feed = build(RssFeed, :parent => blog, :profile => profile)

    assert_match "This is the content of the Sample Article", feed.data
  end

  should 'display articles even within a private profile' do
    profile = create_user('testuser').person
    profile.public_profile = false
    profile.save!
    a1 = profile.articles.build(:name => 'article 1'); a1.save!
    a2 = profile.articles.build(:name => 'article 2'); a2.save!
    a3 = profile.articles.build(:name => 'article 3'); a3.save!

    feed = build(RssFeed, :name => 'testfeed')
    feed.profile = profile
    feed.save!

    rss = feed.data
    assert_match /<item><title>article 1<\/title>/, rss
    assert_match /<item><title>article 2<\/title>/, rss
    assert_match /<item><title>article 3<\/title>/, rss
  end

  should 'provide a non-nil to_html' do
    assert_not_nil RssFeed.new.to_html
  end

  should 'include posts from all languages' do
    profile = create_user('testuser').person
    blog = create(Blog, :name => 'blog-test', :profile => profile, :language => nil)
    blog.posts << en_post = fast_create(TextArticle, :name => "English", :profile_id => profile.id, :parent_id => blog.id, :published => true, :language => 'en')
    blog.posts << es_post = fast_create(TextArticle, :name => "Spanish", :profile_id => profile.id, :parent_id => blog.id, :published => true, :language => 'es')

    assert blog.feed.fetch_articles.include?(en_post)
    assert blog.feed.fetch_articles.include?(es_post)
  end

  should 'include only posts from some language' do
    profile = create_user('testuser').person
    blog = create(Blog, :name => 'blog-test', :profile => profile)
    blog.feed.update! :language => 'es'
    blog.posts << en_post = fast_create(TextArticle, :name => "English", :profile_id => profile.id, :parent_id => blog.id, :published => true, :language => 'en')
    blog.posts << es_post = fast_create(TextArticle, :name => "Spanish", :profile_id => profile.id, :parent_id => blog.id, :published => true, :language => 'es')

    assert_equal [es_post], blog.feed.fetch_articles
  end

  should 'a feed have the same privacy of its parent' do
    profile = create_user('testuser').person
    blog = create(Blog, :name => 'blog-test', :profile => profile)
    feed = blog.feed

    assert blog.published?
    assert feed.published?

    feed.published = false
    feed.save

    assert blog.published?
    assert feed.published?

    blog.published = false
    blog.save
    feed.reload

    assert !blog.published?
    assert !feed.published?

    feed.published = true
    feed.save

    assert !blog.published?
    assert !feed.published?
  end
end