Changeset 1675

Show
Ignore:
Timestamp:
03/15/08 09:51:55 (2 months ago)
Author:
pdcawley
Message:

Reinstated the old article tests in article_spec.rb.

Still short of article tests, I think, but every step in the right
direction is a good step.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/spec/models/article_spec.rb

    r1665 r1675  
    11require File.dirname(__FILE__) + '/../spec_helper' 
    22 
    3 describe "a valid Article" do 
    4   before(:each) do 
    5     @a = Article.new(:title => 'Test article', :body => 'body', 
    6                      :author => mock_model(User)) 
    7   end 
    8  
    9   it "should be valid" do 
    10     @a.should be_valid 
    11   end 
    12  
    13   it "should cast #published = '0' to false" do 
    14     @a.published = '0' 
    15     @a.published.should == false 
    16   end 
     3describe Article do 
     4  before do 
     5    @articles = [] 
     6  end 
     7 
     8  def assert_results_are(*expected) 
     9    assert_equal expected.size, @articles.size 
     10    expected.each do |i| 
     11      assert @articles.include?(i.is_a?(Symbol) ? contents(i) : i) 
     12    end 
     13  end 
     14 
     15  def test_content_fields 
     16    a = Article.new 
     17    assert_equal [:body, :extended], a.content_fields 
     18  end 
     19 
     20  def test_permalink_url 
     21    a = contents(:article3) 
     22    assert_equal 'http://myblog.net/articles/2004/06/01/article-3', a.permalink_url 
     23  end 
     24 
     25  def test_edit_url 
     26    a = contents(:article3) 
     27    assert_equal "http://myblog.net/admin/content/edit/#{a.id}", a.edit_url 
     28  end 
     29 
     30  def test_delete_url 
     31    a = contents(:article3) 
     32    assert_equal "http://myblog.net/admin/content/destroy/#{a.id}", a.delete_url 
     33  end 
     34 
     35  def test_feed_url 
     36    a = contents(:article3) 
     37    assert_equal "http://myblog.net/xml/atom10/article/#{a.id}/feed.xml", a.feed_url(:atom10) 
     38    assert_equal "http://myblog.net/xml/rss20/article/#{a.id}/feed.xml", a.feed_url(:rss20) 
     39  end 
     40 
     41  def test_create 
     42    a = Article.new 
     43    a.user_id = 1 
     44    a.body = "Foo" 
     45    a.title = "Zzz" 
     46    assert a.save 
     47 
     48    a.categories << Category.find(categories(:software).id) 
     49    assert_equal 1, a.categories.size 
     50 
     51    b = Article.find(a.id) 
     52    assert_equal 1, b.categories.size 
     53  end 
     54 
     55  def test_permalink 
     56    assert_equal( contents(:article3), Article.find_by_date(2004,06,01) ) 
     57    assert_equal( [contents(:article2), contents(:article1)], 
     58                  Article.find_all_by_date(2.days.ago.year) ) 
     59  end 
     60 
     61  def test_permalink_with_title 
     62    assert_equal( contents(:article3), 
     63                  Article.find_by_permalink(2004, 06, 01, "article-3") ) 
     64    assert_raises(ActiveRecord::RecordNotFound) do 
     65      Article.find_by_permalink(2005, 06, 01, "article-5") 
     66    end 
     67  end 
     68 
     69  def test_strip_title 
     70    assert_equal "article-3", "Article-3".to_url 
     71    assert_equal "article-3", "Article 3!?#".to_url 
     72    assert_equal "there-is-sex-in-my-violence", "There is Sex in my Violence!".to_url 
     73    assert_equal "article", "-article-".to_url 
     74    assert_equal "lorem-ipsum-dolor-sit-amet-consectetaur-adipisicing-elit", "Lorem ipsum dolor sit amet, consectetaur adipisicing elit".to_url 
     75    assert_equal "my-cats-best-friend", "My Cat's Best Friend".to_url 
     76  end 
     77 
     78  def test_perma_title 
     79    assert_equal "article-1", contents(:article1).stripped_title 
     80    assert_equal "article-2", contents(:article2).stripped_title 
     81    assert_equal "article-3", contents(:article3).stripped_title 
     82  end 
     83 
     84  def test_html_title 
     85    a = Article.new 
     86    a.title = "This <i>is</i> a <b>test</b>" 
     87    assert a.save 
     88 
     89    assert_equal 'this-is-a-test', a.permalink 
     90  end 
     91 
     92  def test_urls 
     93    urls = contents(:article4).html_urls 
     94    assert_equal ["http://www.example.com/public"], urls 
     95  end 
     96 
     97  ### XXX: Should we have a test here? 
     98  def test_send_pings 
     99  end 
     100 
     101  ### XXX: Should we have a test here? 
     102  def test_send_multiple_pings 
     103  end 
     104 
     105  def test_tags 
     106    a = Article.new(:title => 'Test tag article', 
     107                    :keywords => 'test tag tag stuff'); 
     108 
     109    assert_kind_of Article, a 
     110    assert_equal 0, a.tags.size 
     111 
     112    a.keywords_to_tags 
     113 
     114    assert_equal 3, a.tags.size 
     115    assert_equal ["test", "tag", "stuff"].sort , a.tags.collect {|t| t.name}.sort 
     116    assert a.save 
     117 
     118    a.keywords = 'tag bar stuff foo' 
     119    a.keywords_to_tags 
     120 
     121    assert_equal 4, a.tags.size 
     122    assert_equal ["foo", "bar", "tag", "stuff"].sort , a.tags.collect {|t| t.name}.sort 
     123 
     124    a.keywords='tag bar' 
     125    a.keywords_to_tags 
     126 
     127    assert_equal 2, a.tags.size 
     128 
     129    a.keywords='' 
     130    a.keywords_to_tags 
     131 
     132    assert_equal 0, a.tags.size 
     133 
     134    b = Article.new(:title => 'Tag Test 2', 
     135                    :keywords => 'tag test article one two three') 
     136 
     137    assert_kind_of Article,b 
     138    assert_equal 0, b.tags.size 
     139 
     140    c = Article.new(:title => 'Foo', :keywords => 'test "tag test" web2.0') 
     141    c.keywords_to_tags 
     142 
     143    assert_equal 3, c.tags.size 
     144    assert_equal ['test', 'tagtest', 'web2.0'].sort, c.tags.collect(&:name).sort 
     145  end 
     146 
     147  def test_find_published_by_tag_name 
     148    @articles = Tag.find_by_name(tags(:foo).name).published_articles 
     149 
     150    assert_results_are(:article1, :article2) 
     151  end 
     152 
     153 
     154  def test_find_published 
     155    @articles = Article.find_published 
     156    assert_results_are(:search_target, :article1, :article2, 
     157                       :article3, :inactive_article,:xmltest, 
     158                       :spammed_article) 
     159 
     160    @articles = Article.find_published(:all, 
     161                                                  :conditions => "title = 'Article 1!'") 
     162    assert_results_are :article1 
     163  end 
     164 
     165  def test_just_published_flag 
     166    art = Article.new(:title => 'title', 
     167                                   :body => 'body', 
     168                                   :published => true) 
     169    assert art.just_changed_published_status? 
     170    assert art.save 
     171 
     172    art = Article.find(art.id) 
     173    assert !art.just_changed_published_status? 
     174 
     175    art = Article.create!(:title => 'title2', 
     176                          :body => 'body', 
     177                          :published => false) 
     178 
     179    assert ! art.just_changed_published_status? 
     180  end 
     181 
     182  def test_future_publishing 
     183    assert_sets_trigger(Article.create!(:title => 'title', :body => 'body', 
     184                                        :published => true, 
     185                                        :published_at => Time.now + 4.seconds)) 
     186  end 
     187 
     188  def test_future_publishing_without_published_flag 
     189    assert_sets_trigger Article.create!(:title => 'title', :body => 'body', 
     190                                        :published_at => Time.now + 4.seconds) 
     191  end 
     192 
     193  def test_triggers_are_dependent 
     194    art = Article.create!(:title => 'title', :body => 'body', 
     195                          :published_at => Time.now + 1.hour) 
     196    assert_equal 1, Trigger.count 
     197    art.destroy 
     198    assert_equal 0, Trigger.count 
     199  end 
     200 
     201  def assert_sets_trigger(art) 
     202    assert_equal 1, Trigger.count 
     203    assert Trigger.find(:first, :conditions => ['pending_item_id = ?', art.id]) 
     204    sleep 4 
     205    Trigger.fire 
     206    art.reload 
     207    assert art.published 
     208  end 
     209 
     210  def test_find_published_by_category 
     211    Article.create!(:title      => "News from the future!", 
     212                    :body       => "The future is cool!", 
     213                    :keywords   => "future", 
     214                    :published_at => Time.now + 12.minutes) 
     215 
     216    @articles = Category.find_by_permalink('personal').published_articles 
     217    assert_results_are :article1, :article2, :article3 
     218 
     219    @articles = Category.find_by_permalink('software').published_articles 
     220    assert_results_are :article1 
     221  end 
     222 
     223  def test_find_published_by_nonexistent_category_raises_exception 
     224    assert_raises ActiveRecord::RecordNotFound do 
     225      Category.find_by_permalink('does-not-exist').published_articles 
     226    end 
     227  end 
     228 
     229  def test_destroy_file_upload_associations 
     230    a = contents(:article1) 
     231    assert_equal 2, a.resources.size 
     232    a.resources << resources(:resource3) 
     233    assert_equal 3, a.resources.size 
     234    a.destroy 
     235    assert_equal 0, Resource.find(:all, :conditions => "article_id = #{a.id}").size 
     236  end 
     237 
     238  def test_notifications 
     239    a = Article.new(:title => 'New Article', :body => 'Foo', :author => 'Tobi', :user => users(:tobi)) 
     240    assert a.save 
     241 
     242    assert_equal 2, a.notify_users.size 
     243    assert_equal ['bob', 'randomuser'], a.notify_users.collect {|u| u.login }.sort 
     244  end 
     245 
     246  def test_tags_on_update 
     247    contents(:article3).update_attribute :keywords, "my new tags" 
     248    assert_equal 3, contents(:article3).reload.tags.size 
     249    assert contents(:article3).tags.include?(Tag.find_by_name("new")) 
     250  end 
     251 
     252  # this also tests time_delta, indirectly 
     253  def test_find_all_by_date 
     254    feb28 = Article.new(:published => true) 
     255    mar1  = Article.new(:published => true) 
     256    mar2  = Article.new(:published => true) 
     257 
     258    feb28.title = "February 28" 
     259    mar1.title  = "March 1" 
     260    mar2.title  = "March 2" 
     261 
     262    feb28.created_at = feb28.published_at = "2004-02-28" 
     263    mar1.created_at  = mar1.published_at = "2004-03-01" 
     264    mar2.created_at  = mar2.published_at = "2004-03-02" 
     265 
     266    [feb28, mar1, mar2].each do |x| 
     267      x.state = :published 
     268      x.save 
     269    end 
     270 
     271    assert_equal(1, Article.find_all_by_date(2004,02).size) 
     272    assert_equal(2, Article.find_all_by_date(2004,03).size) 
     273    assert_equal(1, Article.find_all_by_date(2004,03,01).size) 
     274  end 
     275 
     276  def test_withdrawal 
     277    art = Article.find(contents(:article1).id) 
     278    assert   art.published? 
     279    assert ! art.withdrawn? 
     280    art.withdraw! 
     281    assert ! art.published? 
     282    assert   art.withdrawn? 
     283    art.reload 
     284    assert ! art.published? 
     285    assert   art.withdrawn? 
     286  end 
     287 
     288  def test_default_filter 
     289    a = Article.find(contents(:article1).id) 
     290    assert_equal 'textile', a.default_text_filter.name 
     291  end 
     292 
    17293end