public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
technoweenie (author)
Mon Mar 10 23:51:29 -0700 2008
commit  2c6ad21d8f0e91dc89f7a3e8426d6bb5deb2b0b3
tree    64515c9d37ce88130e07c834130bcd1cd1869028
parent  667a1c0b0e37ac5da7c1e6ca629b118084e6b6eb
mephisto / test / unit / article_test.rb
100644 212 lines (176 sloc) 8.128 kb
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
require File.dirname(__FILE__) + '/../test_helper'
 
class ArticleTest < Test::Unit::TestCase
  fixtures :contents, :users, :sections, :sites, :assigned_sections
 
  def test_find_next
    assert_equal contents(:another).next, contents(:welcome)
    assert_equal contents(:another).next(sections(:home)), contents(:welcome)
    assert_equal contents(:another).next(sections(:about)), nil
    assert_equal contents(:welcome).next(sections(:about)), contents(:about)
    assert_equal contents(:article_1_only_in_page_section).next, contents(:article_2_only_in_page_section)
    assert_equal contents(:article_1_only_in_page_section).next(sections(:paged_section)), contents(:article_2_only_in_page_section)
    assert_equal contents(:article_2_only_in_page_section).next, nil
    assert_equal contents(:article_2_only_in_page_section).next(sections(:paged_section)), nil
  end
 
  def test_should_find_previous
    assert_equal contents(:another).previous, nil
    assert_equal contents(:another).previous(sections(:home)), nil
    assert_equal contents(:another).previous(sections(:about)), nil
    assert_equal contents(:welcome).previous(sections(:home)), contents(:another)
    assert_not_equal contents(:another).previous(sections(:cupcake_home)), contents(:at_beginning_of_next_month)
    assert_equal contents(:article_2_only_in_page_section).previous, contents(:article_1_only_in_page_section)
    assert_equal contents(:article_2_only_in_page_section).previous(sections(:paged_section)), contents(:article_1_only_in_page_section)
    assert_equal contents(:article_1_only_in_page_section).previous, nil
    assert_equal contents(:article_1_only_in_page_section).previous(sections(:paged_section)), nil
  end
  
  def test_should_create_permalink
    a = create_article :title => 'This IS a Tripped out title!!.!1 (well/ not really)', :body => 'foo'
    assert_equal 'this-is-a-tripped-out-title-1-well-not-really', a.permalink
  end
 
  def test_should_set_permalink
    a = create_article :title => 'This IS a Tripped out title!!.!1 (well/ not really)', :body => 'foo', :permalink => 'trippy'
    assert_equal 'trippy', a.permalink
  end
  
  def test_permalink_for_strange_article_title
    title = '////// meph1sto r0x ! \\\\\\'
    assert_equal 'meph1sto-r0x', Article.permalink_for(title)
  end
  
  def test_permalink_with_non_ascii_chars
    assert_equal 'acegiklnu', Article.permalink_for('āčēģīķļņū')
  end
 
  def test_should_pass_changed_attributes_down_to_comments
    contents(:welcome).update_attributes(:title => 'foo bar', :published_at => Time.utc(2000, 1, 1), :permalink => 'foo-bar')
    assert_equal 'foo bar', contents(:welcome_comment).title
    assert_equal Time.utc(2000, 1, 1), contents(:welcome_comment).published_at
    assert_equal 'foo-bar', contents(:welcome_comment).permalink
  end
 
  def test_full_permalink
    date = 3.days.ago.utc
    assert_equal ['', date.year, date.month, date.day, 'welcome-to-mephisto'].join('/'), contents(:welcome).full_permalink
  end
 
  def test_should_show_published_status
    assert !Article.new(:published_at => 5.minutes.ago).published?
    assert contents(:welcome).published?
    assert contents(:future).published?
  end
 
  def test_should_show_pending_status
    assert !contents(:welcome).pending?
    assert contents(:future).pending?
  end
 
  def test_should_show_status
    assert_equal :published, contents(:welcome).status
    assert_equal :pending, contents(:future).status
  end
 
  def test_should_cache_redcloth
    a = Article.create :title => 'This IS a Tripped out title!!!1 (well not really)', :user => users(:quentin), :excerpt => '*foo*', :body => '_bar_', :site_id => 1
    assert_equal '<p><strong>foo</strong></p>', a.excerpt_html
    assert_equal '<p><em>bar</em></p>', a.body_html
  end
 
  def test_should_save_filter_from_user
    a = Article.new
    assert_nil a.filter
    a.set_default_filter_from(users(:quentin))
    assert_equal 'textile_filter', a.filter
  end
 
  def test_should_cache_bluecloth
    a = Article.create :title => 'simple Title', :user => users(:arthur), :body => "# bar\n\nfoo", :filter => 'markdown_filter', :site_id => 1
    assert_equal "<h1>bar</h1>\n\n<p>foo</p>", a.body_html
  end
 
  def test_should_create_article_version
    assert_difference Article::Version, :count, 2 do
      Article.create :title => 'This IS a Tripped out title!!!1 (well not really)', :body => 'foo', :user_id => 1, :site_id => 1
      contents(:welcome).update_attributes :title => 'whoo!'
    end
  end
 
  def test_should_not_create_article_version_for_useless_changes
    contents(:welcome).body_html = 'nope'
    assert !contents(:welcome).save_version?
  end
 
  def test_comment_expiration_date_on_draft
    a = create_fake_article
    a.published_at = nil
    assert_equal (Time.now.utc+10.days).to_i, a.comments_expired_at.utc.to_i
  end
 
  def test_comment_expiration_date
    a = create_fake_article
    assert_equal((a.published_at + 10.days), a.comments_expired_at)
  end
 
  def test_comment_expiry
    a = create_fake_article(5.days.from_now.utc)
    assert !a.accept_comments?
    a.published_at = 5.days.ago.utc
    assert a.accept_comments?
    a.comment_age = 2
    assert !a.accept_comments?
    a.published_at = 5.years.ago.utc
    assert !a.accept_comments?
    a.comment_age = 0
    assert a.accept_comments?
  end
 
  def test_should_set_published_to_utc
    a = create_article :body => 'body', :published_at => Time.now
    assert a.published_at.utc?
  end
 
  def test_should_find_deleted_user
    assert_equal User.find_with_deleted(3), contents(:another).user
  end
 
  def test_should_set_tags
    assert_equal '', contents(:welcome).tag
    assert_difference Tagging, :count, 2 do
      contents(:welcome).update_attribute :tag, 'ruby, rails'
    end
    assert_equal 'rails, ruby', contents(:welcome).reload.tag
  end
 
  def test_should_set_tags_upon_article_creation
    a = nil
    assert_difference Tagging, :count, 2 do
      a = create_article :tag => 'ruby, rails', :body => 'foo'
      assert_valid a
    end
    assert_equal 'rails, ruby', a.reload.tag
  end
 
  def test_should_find_article_by_permalink
    assert_equal contents(:welcome), sites(:first).articles.find_by_permalink(:id => contents(:welcome).id)
    assert_equal contents(:welcome), sites(:first).articles.find_by_permalink(:permalink => contents(:welcome).permalink)
    assert_equal contents(:welcome), sites(:first).articles.find_by_permalink(:year => contents(:welcome).year, :permalink => contents(:welcome).permalink)
  end
 
  def test_should_find_all_in_month
    two_months_ago = Time.now.utc.advance(:months => -2)
    articles = Article.find_all_in_month(two_months_ago.year, two_months_ago.month)
 
    assert_equal 3, articles.length
    [:at_beginning_of_month, :at_end_of_month, :at_middle_of_month].each do |article|
      assert articles.include?(contents(article))
    end
  end
 
  protected
    def create_article(options = {})
      Article.create options.reverse_merge(:user_id => 1, :site_id => 1, :title => 'foo')
    end
    
    def create_fake_article(time = 5.days.ago.utc)
      returning Article.new(:comment_age => 10, :published_at => time.utc) do |a|
        def a.new_record?() false ; end
      end
    end
end
 
class ArticleFilterEditTest < Test::Unit::TestCase
  fixtures :contents, :users, :sections, :sites
 
  def setup
    @old_time = contents(:welcome_comment).updated_at
    assert_equal 'textile_filter', contents(:welcome).filter
    contents(:welcome).filter = 'markdown_filter'
    contents(:welcome).save!
  end
 
  def test_should_clear_filter
    contents(:welcome).filter = ''
    contents(:welcome).save!
    assert_equal '', contents(:welcome).filter
  end
 
  def test_should_modify_filter
    assert_equal 'markdown_filter', contents(:welcome).reload.filter
  end
 
  def test_should_modify_filter_and_leave_comments_alone
    assert_equal 'textile_filter', contents(:welcome_comment).reload.filter
  end
 
  def test_should_modify_filter_and_not_modify_comment_timestamps
    assert_equal @old_time, contents(:welcome_comment).reload.updated_at
  end
end