Commit 46f1339016b6ab52786ba332a78990f20da02e2b

Authored by Rodrigo Souto
1 parent 0945abec

rails3: fix article tests

PS: still breaking some tests due to action_tracker problem and html
generation on models problem.
Showing 1 changed file with 128 additions and 128 deletions   Show diff stats
test/unit/article_test.rb
... ... @@ -31,7 +31,7 @@ class ArticleTest < ActiveSupport::TestCase
31 31 end
32 32  
33 33 should 'limit length of names' do
34   - a = Article.new(:name => 'a'*151)
  34 + a = build(Article, :name => 'a'*151)
35 35 a.valid?
36 36 assert a.errors[:name.to_s].present?
37 37  
... ... @@ -41,7 +41,7 @@ class ArticleTest < ActiveSupport::TestCase
41 41 end
42 42  
43 43 should 'require value for slug and path if name is filled' do
44   - a = Article.new(:name => 'test article')
  44 + a = build(Article, :name => 'test article')
45 45 a.slug = nil
46 46 a.path = nil
47 47 a.valid?
... ... @@ -57,7 +57,7 @@ class ArticleTest < ActiveSupport::TestCase
57 57 end
58 58  
59 59 should 'act as versioned' do
60   - a = Article.create!(:name => 'my article', :body => 'my text', :profile_id => profile.id)
  60 + a = create(Article, :name => 'my article', :body => 'my text', :profile_id => profile.id)
61 61 assert_equal 1, a.versions(true).size
62 62 a.name = 'some other name'
63 63 a.save!
... ... @@ -65,7 +65,7 @@ class ArticleTest < ActiveSupport::TestCase
65 65 end
66 66  
67 67 should 'act as taggable' do
68   - a = Article.create!(:name => 'my article', :profile_id => profile.id)
  68 + a = create(Article, :name => 'my article', :profile_id => profile.id)
69 69 a.tag_list = ['one', 'two']
70 70 tags = a.tag_list.names
71 71 assert tags.include?('one')
... ... @@ -73,16 +73,17 @@ class ArticleTest < ActiveSupport::TestCase
73 73 end
74 74  
75 75 should 'act as filesystem' do
76   - a = Article.create!(:name => 'my article', :profile_id => profile.id)
77   - b = a.children.build(:name => 'child article', :profile_id => profile.id)
  76 + a = create(Article, :profile_id => profile.id)
  77 + b = create(Article, :profile_id => profile.id, :parent_id => a.id)
78 78 b.save!
79   - assert_equal 'my-article/child-article', b.path
  79 + assert_equal "#{a.slug}/#{b.slug}", b.path
80 80  
81 81 a = Article.find(a.id);
82 82 a.name = 'another name'
83 83 a.save!
  84 + b.reload
84 85  
85   - assert_equal 'another-name/child-article', Article.find(b.id).path
  86 + assert_equal "another-name/#{b.slug}", b.path
86 87 end
87 88  
88 89 should 'provide HTML version' do
... ... @@ -226,13 +227,14 @@ class ArticleTest < ActiveSupport::TestCase
226 227  
227 228 now = Time.now
228 229  
229   - first = p.articles.build(:name => 'first', :published => true, :created_at => now, :published_at => now); first.save!
230   - second = p.articles.build(:name => 'second', :published => true, :updated_at => now, :published_at => now + 1.second); second.save!
  230 + first = create(Article, :name => 'first', :published => true, :created_at => now, :published_at => now, :profile_id => p.id)
  231 + second = create(Article, :name => 'second', :published => true, :updated_at => now, :published_at => now + 1.second, :profile_id => p.id)
231 232  
232 233 assert_equal [ second, first ], Article.recent(2)
233 234  
234 235 Article.record_timestamps = false
235   - first.update_attributes!(:published_at => second.published_at + 1.second)
  236 + first.published_at = second.published_at + 1.second
  237 + first.save!
236 238 Article.record_timestamps = true
237 239  
238 240 assert_equal [ first, second ], Article.recent(2)
... ... @@ -242,7 +244,7 @@ class ArticleTest < ActiveSupport::TestCase
242 244 p = create_user('usr1').person
243 245 Article.destroy_all
244 246  
245   - first = UploadedFile.new(:profile => p, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')); first.save!
  247 + first = build(UploadedFile, :profile => p, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')); first.save!
246 248 second = fast_create(TextArticle, :profile_id => p.id, :name => 'second')
247 249  
248 250 assert_equal [ second ], Article.recent(nil)
... ... @@ -262,7 +264,7 @@ class ArticleTest < ActiveSupport::TestCase
262 264 p = create_user('usr1').person
263 265 Article.destroy_all
264 266 first = fast_create(Blog, :profile_id => p.id, :name => 'my blog', :advertise => true)
265   - second = p.articles.build(:name => 'second'); second.save!
  267 + second = create(Article, :name => 'second', :profile_id => p.id)
266 268  
267 269 assert_equal [ second ], Article.recent(nil)
268 270 end
... ... @@ -270,8 +272,8 @@ class ArticleTest < ActiveSupport::TestCase
270 272 should 'accept extra conditions to find recent' do
271 273 p = create_user('usr1').person
272 274 Article.destroy_all
273   - a1 = p.articles.create!(:name => 'first')
274   - a2 = p.articles.create!(:name => 'second')
  275 + a1 = create(Article, :name => 'first', :profile_id => p.id)
  276 + a2 = create(Article, :name => 'second', :profile_id => p.id)
275 277  
276 278 assert_equal [ a1 ], Article.recent(nil, :name => 'first')
277 279 end
... ... @@ -293,27 +295,24 @@ class ArticleTest < ActiveSupport::TestCase
293 295 end
294 296  
295 297 should 'provide a url to itself' do
296   - article = profile.articles.build(:name => 'myarticle')
297   - article.save!
298   -
  298 + article = create(Article, :name => 'myarticle', :profile_id => profile.id)
299 299 assert_equal(profile.url.merge(:page => ['myarticle']), article.url)
300 300 end
301 301  
302 302 should 'provide a url to itself having a parent topic' do
303   - parent = profile.articles.build(:name => 'parent'); parent.save!
304   - child = profile.articles.build(:name => 'child', :parent => parent); child.save!
  303 + parent = create(Article, :name => 'parent', :profile_id => profile.id)
  304 + child = create(Article, :name => 'child', :parent => parent, :profile_id => profile.id)
305 305  
306 306 assert_equal(profile.url.merge(:page => [ 'parent', 'child']), child.url)
307 307 end
308 308  
309 309 should 'associate with categories' do
310 310 env = Environment.default
311   - parent_cat = env.categories.build(:name => "parent category")
312   - parent_cat.save!
313   - c1 = env.categories.build(:name => "test category 1", :parent_id => parent_cat.id); c1.save!
314   - c2 = env.categories.build(:name => "test category 2"); c2.save!
  311 + parent_cat = create(Category, :name => "parent category", :environment_id => env.id)
  312 + c1 = create(Category, :name => "test category 1", :parent_id => parent_cat.id, :environment_id => env.id)
  313 + c2 = create(Category, :name => "test category 2", :environment_id => env.id)
315 314  
316   - article = profile.articles.build(:name => 'withcategories')
  315 + article = create(Article, :name => 'withcategories', :profile_id => profile.id)
317 316 article.save!
318 317  
319 318 article.add_category c1
... ... @@ -325,8 +324,7 @@ class ArticleTest < ActiveSupport::TestCase
325 324  
326 325 should 'remove comments when removing article' do
327 326 assert_no_difference Comment, :count do
328   - a = profile.articles.build(:name => 'test article')
329   - a.save!
  327 + a = create(Article, :name => 'test article', :profile_id => profile.id)
330 328  
331 329 assert_difference Comment, :count, 1 do
332 330 comment = a.comments.build
... ... @@ -346,10 +344,10 @@ class ArticleTest < ActiveSupport::TestCase
346 344 create(TextileArticle, :name => "art #{n}", :profile_id => profile.id)
347 345 end
348 346 first_article = profile.articles.first
349   - 2.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => first_article).save! }
  347 + 2.times { create(Comment, :title => 'test', :body => 'asdsad', :author => profile, :source => first_article).save! }
350 348  
351 349 last_article = profile.articles.last
352   - 4.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => last_article).save! }
  350 + 4.times { create(Comment, :title => 'test', :body => 'asdsad', :author => profile, :source => last_article).save! }
353 351 # should respect the order (more commented comes first)
354 352 assert_equal [last_article, first_article], profile.articles.most_commented(2)
355 353 end
... ... @@ -370,20 +368,21 @@ class ArticleTest < ActiveSupport::TestCase
370 368 should 'display to owner' do
371 369 # a person with private contents ...
372 370 person = create_user('testuser').person
373   - person.update_attributes!(:public_content => false)
  371 + person.public_content = false
  372 + person.save!
374 373  
375 374 # ... can see his own articles
376   - a = person.articles.create!(:name => 'test article')
  375 + a = create(Article, :name => 'test article', :profile_id => person.id)
377 376 assert_equal true, a.display_to?(person)
378 377 end
379 378  
380 379 should 'cache children count' do
381 380 owner = create_user('testuser').person
382   - art = owner.articles.build(:name => 'ytest'); art.save!
  381 + art = create(Article, :name => 'ytest', :profile_id => owner.id)
383 382  
384 383 # two children articles
385   - art.children.create!(:profile => owner, :name => 'c1')
386   - art.children.create!(:profile => owner, :name => 'c2')
  384 + create(Article, :profile => owner, :name => 'c1', :parent_id => art.id)
  385 + create(Article, :profile => owner, :name => 'c2', :parent_id => art.id)
387 386  
388 387 art.reload
389 388  
... ... @@ -393,12 +392,12 @@ class ArticleTest < ActiveSupport::TestCase
393 392 end
394 393  
395 394 should 'categorize in the entire category hierarchy' do
396   - c1 = Category.create!(:environment => Environment.default, :name => 'c1')
397   - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2')
398   - c3 = c2.children.create!(:environment => Environment.default, :name => 'c3')
  395 + c1 = create(Category, :environment => Environment.default, :name => 'c1')
  396 + c2 = create(Category, :environment => Environment.default, :name => 'c2', :parent_id => c1.id)
  397 + c3 = create(Category, :environment => Environment.default, :name => 'c3', :parent_id => c2.id)
399 398  
400 399 owner = create_user('testuser').person
401   - art = owner.articles.create!(:name => 'ytest')
  400 + art = create(Article, :name => 'ytest', :profile_id => owner.id)
402 401  
403 402 art.add_category(c3)
404 403  
... ... @@ -414,12 +413,12 @@ class ArticleTest < ActiveSupport::TestCase
414 413 end
415 414  
416 415 should 'redefine the entire category set at once' do
417   - c1 = Category.create!(:environment => Environment.default, :name => 'c1')
418   - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2')
419   - c3 = c2.children.create!(:environment => Environment.default, :name => 'c3')
420   - c4 = c1.children.create!(:environment => Environment.default, :name => 'c4')
  416 + c1 = create(Category, :environment => Environment.default, :name => 'c1')
  417 + c2 = create(Category, :environment => Environment.default, :name => 'c2', :parent_id => c1)
  418 + c3 = create(Category, :environment => Environment.default, :name => 'c3', :parent_id => c2)
  419 + c4 = create(Category, :environment => Environment.default, :name => 'c4', :parent_id => c1)
421 420 owner = create_user('testuser').person
422   - art = owner.articles.create!(:name => 'ytest')
  421 + art = create(Article, :name => 'ytest', :profile_id => owner.id)
423 422  
424 423 art.add_category(c4)
425 424  
... ... @@ -436,7 +435,7 @@ class ArticleTest < ActiveSupport::TestCase
436 435 c2 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c2')
437 436  
438 437 p = create_user('testinguser').person
439   - a = p.articles.create!(:name => 'test', :category_ids => [c1.id, c2.id])
  438 + a = create(Article, :name => 'test', :category_ids => [c1.id, c2.id], :profile_id => p)
440 439  
441 440 assert_equivalent [c1, c2], a.categories(true)
442 441 assert_includes a.categories_including_virtual(true), parent1
... ... @@ -444,10 +443,10 @@ class ArticleTest < ActiveSupport::TestCase
444 443  
445 444 should 'not add a category twice to article' do
446 445 c1 = fast_create(Category, :environment_id => Environment.default.id, :name => 'c1')
447   - c2 = c1.children.create!(:environment => Environment.default, :name => 'c2', :parent_id => c1.id)
448   - c3 = c1.children.create!(:environment => Environment.default, :name => 'c3', :parent_id => c1.id)
  446 + c2 = create(Category, :environment => Environment.default, :name => 'c2', :parent_id => c1.id)
  447 + c3 = create(Category, :environment => Environment.default, :name => 'c3', :parent_id => c1.id)
449 448 owner = create_user('testuser').person
450   - art = owner.articles.create!(:name => 'ytest')
  449 + art = create(Article, :name => 'ytest', :profile_id => owner)
451 450 art.category_ids = [c2,c3,c3].map(&:id)
452 451 assert_equal [c2, c3], art.categories(true)
453 452 assert_equal [c2, c1, c3], art.categories_including_virtual(true)
... ... @@ -538,7 +537,7 @@ class ArticleTest < ActiveSupport::TestCase
538 537  
539 538 should 'not allow friends of private person see the article' do
540 539 person = create_user('test_user').person
541   - article = Article.create!(:name => 'test article', :profile => person, :published => false)
  540 + article = create(Article, :name => 'test article', :profile => person, :published => false)
542 541 friend = create_user('test_friend').person
543 542 person.add_friend(friend)
544 543 person.save!
... ... @@ -559,7 +558,7 @@ class ArticleTest < ActiveSupport::TestCase
559 558  
560 559 should 'make a copy of the article as child of it' do
561 560 person = create_user('test_user').person
562   - a = person.articles.create!(:name => 'test article', :body => 'some text')
  561 + a = create(Article, :name => 'test article', :body => 'some text', :profile_id => person.id)
563 562 b = a.copy(:parent => a, :profile => a.profile)
564 563  
565 564 assert_includes a.children, b
... ... @@ -569,7 +568,7 @@ class ArticleTest < ActiveSupport::TestCase
569 568 should 'make a copy of the article to other profile' do
570 569 p1 = create_user('test_user1').person
571 570 p2 = create_user('test_user2').person
572   - a = p1.articles.create!(:name => 'test article', :body => 'some text')
  571 + a = create(Article, :name => 'test article', :body => 'some text', :profile_id => p1)
573 572 b = a.copy(:parent => a, :profile => p2)
574 573  
575 574 p2 = Person.find(p2.id)
... ... @@ -593,7 +592,7 @@ class ArticleTest < ActiveSupport::TestCase
593 592  
594 593 should 'load article under an old path' do
595 594 p = create_user('test_user').person
596   - a = p.articles.create(:name => 'old-name')
  595 + a = create(Article, :name => 'old-name', :profile_id => p.id)
597 596 old_path = a.explode_path
598 597 a.name = 'new-name'
599 598 a.save!
... ... @@ -605,7 +604,7 @@ class ArticleTest < ActiveSupport::TestCase
605 604  
606 605 should 'load new article name equal of another article old name' do
607 606 p = create_user('test_user').person
608   - a1 = p.articles.create!(:name => 'old-name')
  607 + a1 = create(Article, :name => 'old-name', :profile_id => p.id)
609 608 old_path = a1.explode_path
610 609 a1.name = 'new-name'
611 610 a1.save!
... ... @@ -618,11 +617,11 @@ class ArticleTest < ActiveSupport::TestCase
618 617  
619 618 should 'article with most recent version with the name must be loaded if no aritcle with the name' do
620 619 p = create_user('test_user').person
621   - a1 = p.articles.create!(:name => 'old-name')
  620 + a1 = create(Article, :name => 'old-name', :profile_id => p.id)
622 621 old_path = a1.explode_path
623 622 a1.name = 'new-name'
624 623 a1.save!
625   - a2 = p.articles.create!(:name => 'old-name')
  624 + a2 = create(Article, :name => 'old-name', :profile_id => p.id)
626 625 a2.name = 'other-new-name'
627 626 a2.save!
628 627  
... ... @@ -633,7 +632,7 @@ class ArticleTest < ActiveSupport::TestCase
633 632  
634 633 should 'not return an article of a different user' do
635 634 p1 = create_user('test_user').person
636   - a = p1.articles.create!(:name => 'old-name')
  635 + a = create(Article, :name => 'old-name', :profile_id => p1.id)
637 636 old_path = a.explode_path
638 637 a.name = 'new-name'
639 638 a.save!
... ... @@ -665,13 +664,13 @@ class ArticleTest < ActiveSupport::TestCase
665 664 end
666 665  
667 666 should 'has moderate comments false by default' do
668   - a = Article.create!(:name => 'my article', :body => 'my text', :profile_id => profile.id)
  667 + a = create(Article, :name => 'my article', :body => 'my text', :profile_id => profile.id)
669 668 a.reload
670 669 assert a.moderate_comments == false
671 670 end
672 671  
673 672 should 'save a article with moderate comments as true' do
674   - a = Article.create!(:name => 'my article', :body => 'my text', :profile_id => profile.id, :moderate_comments => true)
  673 + a = create(Article, :name => 'my article', :body => 'my text', :profile_id => profile.id, :moderate_comments => true)
675 674 a.reload
676 675 assert a.moderate_comments
677 676 end
... ... @@ -717,7 +716,7 @@ class ArticleTest < ActiveSupport::TestCase
717 716 end
718 717  
719 718 should 'return a view url when image' do
720   - image = UploadedFile.create!(:profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
  719 + image = create(UploadedFile, :profile => profile, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
721 720  
722 721 assert_equal image.url.merge(:view => true), image.view_url
723 722 end
... ... @@ -775,7 +774,7 @@ class ArticleTest < ActiveSupport::TestCase
775 774 end
776 775  
777 776 should 'ignore category with zero as id' do
778   - a = profile.articles.create!(:name => 'a test article')
  777 + a = create(Article, :name => 'a test article', :profile_id => profile.id)
779 778 c = fast_create(Category, :name => 'test category', :environment_id => profile.environment.id, :parent_id => 0)
780 779 a.category_ids = ['0', c.id, nil]
781 780 assert a.save
... ... @@ -788,25 +787,25 @@ class ArticleTest < ActiveSupport::TestCase
788 787 end
789 788  
790 789 should 'add owner on cache_key when has profile' do
791   - a = profile.articles.create!(:name => 'a test article')
  790 + a = create(Article, :name => 'a test article', :profile_id => profile.id)
792 791 assert_match(/-owner/, a.cache_key({}, profile))
793 792 end
794 793  
795 794 should 'not add owner on cache_key when has no profile' do
796   - a = profile.articles.create!(:name => 'a test article')
  795 + a = create(Article, :name => 'a test article', :profile_id => profile.id)
797 796 assert_no_match(/-owner/, a.cache_key({}))
798 797 end
799 798  
800 799 should 'add owner on cache_key when profile is community' do
801 800 c = fast_create(Community)
802   - a = c.articles.create!(:name => 'a test article')
  801 + a = create(Article, :name => 'a test article', :profile_id => c.id)
803 802 assert_match(/-owner/, a.cache_key({}, c))
804 803 end
805 804  
806 805 should 'allow author to edit if is publisher' do
807 806 c = fast_create(Community)
808 807 p = create_user_with_permission('test_user', 'publish_content', c)
809   - a = c.articles.create!(:name => 'a test article', :last_changed_by => p)
  808 + a = create(Article, :name => 'a test article', :last_changed_by => p, :profile_id => c.id)
810 809  
811 810 assert a.allow_post_content?(p)
812 811 end
... ... @@ -814,13 +813,13 @@ class ArticleTest < ActiveSupport::TestCase
814 813 should 'allow user with "Manage content" permissions to edit' do
815 814 c = fast_create(Community)
816 815 p = create_user_with_permission('test_user', 'post_content', c)
817   - a = c.articles.create!(:name => 'a test article')
  816 + a = create(Article, :name => 'a test article', :profile_id => c.id)
818 817  
819 818 assert a.allow_post_content?(p)
820 819 end
821 820  
822 821 should 'update slug from name' do
823   - article = Article.create!(:name => 'A test article', :profile_id => profile.id)
  822 + article = create(Article, :name => 'A test article', :profile_id => profile.id)
824 823 assert_equal 'a-test-article', article.slug
825 824 article.name = 'Changed name'
826 825 assert_equal 'changed-name', article.slug
... ... @@ -828,11 +827,11 @@ class ArticleTest < ActiveSupport::TestCase
828 827  
829 828 should 'find articles in a specific category' do
830 829 env = Environment.default
831   - parent_category = env.categories.create!(:name => "parent category")
832   - category_with_articles = env.categories.create!(:name => "Category with articles", :parent_id => parent_category.id)
833   - category_without_articles = env.categories.create!(:name => "Category without articles")
  830 + parent_category = create(Category, :name => "parent category", :environment_id => env.id)
  831 + category_with_articles = create(Category, :name => "Category with articles", :parent_id => parent_category.id, :environment_id => env.id)
  832 + category_without_articles = create(Category, :name => "Category without articles", :environment_id => env.id)
834 833  
835   - article_in_category = profile.articles.create!(:name => 'Article in category')
  834 + article_in_category = create(Article, :name => 'Article in category', :profile_id => profile.id)
836 835  
837 836 article_in_category.add_category(category_with_articles)
838 837  
... ... @@ -843,33 +842,33 @@ class ArticleTest < ActiveSupport::TestCase
843 842  
844 843 should 'has external_link attr' do
845 844 assert_nothing_raised NoMethodError do
846   - Article.new(:external_link => 'http://some.external.link')
  845 + build(Article, :external_link => 'http://some.external.link')
847 846 end
848 847 end
849 848  
850 849 should 'validates format of external_link' do
851   - article = Article.new(:external_link => 'http://invalid-url')
  850 + article = build(Article, :external_link => 'http://invalid-url')
852 851 article.valid?
853 852 assert_not_nil article.errors[:external_link]
854 853 end
855 854  
856 855 should 'put http in external_link' do
857   - article = Article.new(:external_link => 'url.without.http')
  856 + article = build(Article, :external_link => 'url.without.http')
858 857 assert_equal 'http://url.without.http', article.external_link
859 858 end
860 859  
861 860 should 'list only published articles' do
862 861 profile = fast_create(Person)
863 862  
864   - published = profile.articles.create(:name => 'Published', :published => true)
865   - unpublished = profile.articles.create(:name => 'Unpublished', :published => false)
  863 + published = create(Article, :name => 'Published', :published => true, :profile_id => profile.id)
  864 + unpublished = create(Article, :name => 'Unpublished', :published => false, :profile_id => profile.id)
866 865  
867 866 assert_equal [ published ], profile.articles.published
868 867 end
869 868  
870 869 should 'sanitize tags after save article' do
871 870 article = fast_create(Article, :slug => 'article-with-tags', :profile_id => profile.id)
872   - article.tags << Tag.new(:name => "TV Web w<script type='javascript'></script>")
  871 + article.tags << build(Tag, :name => "TV Web w<script type='javascript'></script>")
873 872 assert_match /[<>]/, article.tags.last.name
874 873 article.save!
875 874 assert_no_match /[<>]/, article.tags.last.name
... ... @@ -877,7 +876,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
877 876  
878 877 should 'strip HTML from tag names after save article' do
879 878 article = fast_create(Article, :slug => 'article-with-tags', :profile_id => profile.id)
880   - article.tags << Tag.new(:name => "TV Web w<script type=...")
  879 + article.tags << build(Tag, :name => "TV Web w<script type=...")
881 880 assert_match /</, article.tags.last.name
882 881 article.save!
883 882 assert_no_match /</, article.tags.last.name
... ... @@ -895,7 +894,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
895 894 person = fast_create(Person)
896 895 community = fast_create(Community)
897 896 article = fast_create(Article, :name => 'article name', :profile_id => person.id)
898   - a = ApproveArticle.create!(:article => article, :target => community, :requestor => profile)
  897 + a = create(ApproveArticle, :article => article, :target => community, :requestor => profile)
899 898 a.finish
900 899  
901 900 published = community.articles.find_by_name('article name')
... ... @@ -905,7 +904,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
905 904 end
906 905  
907 906 should 'remove script tags from name' do
908   - a = Article.new(:name => 'hello <script>alert(1)</script>')
  907 + a = build(Article, :name => 'hello <script>alert(1)</script>')
909 908 a.valid?
910 909  
911 910 assert_no_match(/<script>/, a.name)
... ... @@ -926,7 +925,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
926 925 end
927 926  
928 927 should 'return abstract as lead' do
929   - a = Article.new(:abstract => 'lead')
  928 + a = build(Article, :abstract => 'lead')
930 929 assert_equal 'lead', a.lead
931 930 end
932 931  
... ... @@ -937,13 +936,13 @@ class ArticleTest &lt; ActiveSupport::TestCase
937 936 end
938 937  
939 938 should 'return first paragraph as lead with empty but non-null abstract' do
940   - a = Article.new(:abstract => '')
  939 + a = build(Article, :abstract => '')
941 940 a.stubs(:first_paragraph).returns('<p>first</p>')
942 941 assert_equal '<p>first</p>', a.lead
943 942 end
944 943  
945 944 should 'return blank as lead when article has no paragraphs' do
946   - a = Article.new(:body => "<div>an article with content <em>but without</em> a paragraph</div>")
  945 + a = build(Article, :body => "<div>an article with content <em>but without</em> a paragraph</div>")
947 946 assert_equal '', a.lead
948 947 end
949 948  
... ... @@ -953,7 +952,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
953 952 end
954 953  
955 954 should 'remove html from short lead' do
956   - a = Article.new(:body => "<p>an article with html that should be <em>removed</em></p>")
  955 + a = build(Article, :body => "<p>an article with html that should be <em>removed</em></p>")
957 956 assert_equal 'an article with html that should be removed', a.short_lead
958 957 end
959 958  
... ... @@ -995,13 +994,13 @@ class ArticleTest &lt; ActiveSupport::TestCase
995 994  
996 995 should 'not notify activity by default on create' do
997 996 ActionTracker::Record.delete_all
998   - Article.create! :name => 'test', :profile_id => fast_create(Profile).id, :published => true
  997 + create Article, :name => 'test', :profile_id => fast_create(Profile).id, :published => true
999 998 assert_equal 0, ActionTracker::Record.count
1000 999 end
1001 1000  
1002 1001 should 'not notify activity by default on update' do
1003 1002 ActionTracker::Record.delete_all
1004   - a = Article.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
  1003 + a = create Article, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
1005 1004 a.name = 'foo'
1006 1005 a.save!
1007 1006 assert_equal 0, ActionTracker::Record.count
... ... @@ -1009,13 +1008,13 @@ class ArticleTest &lt; ActiveSupport::TestCase
1009 1008  
1010 1009 should 'not notify activity by default on destroy' do
1011 1010 ActionTracker::Record.delete_all
1012   - a = Article.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
  1011 + a = create Article, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
1013 1012 a.destroy
1014 1013 assert_equal 0, ActionTracker::Record.count
1015 1014 end
1016 1015  
1017 1016 should 'create activity' do
1018   - a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
  1017 + a = create TextileArticle, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
1019 1018 a.activity.destroy
1020 1019 assert_nil a.activity
1021 1020  
... ... @@ -1070,7 +1069,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1070 1069 member_1 = Person.first
1071 1070 community.add_member(member_1)
1072 1071  
1073   - article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => community.id
  1072 + article = create TinyMceArticle, :name => 'Tracked Article 1', :profile_id => community.id
1074 1073 first_activity = article.activity
1075 1074 assert_equal [first_activity], ActionTracker::Record.find_all_by_verb('create_article')
1076 1075  
... ... @@ -1080,7 +1079,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1080 1079 member_2 = fast_create(Person)
1081 1080 community.add_member(member_2)
1082 1081  
1083   - article2 = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => community.id
  1082 + article2 = create TinyMceArticle, :name => 'Tracked Article 2', :profile_id => community.id
1084 1083 second_activity = article2.activity
1085 1084 assert_equivalent [first_activity, second_activity], ActionTracker::Record.find_all_by_verb('create_article')
1086 1085  
... ... @@ -1106,14 +1105,14 @@ class ArticleTest &lt; ActiveSupport::TestCase
1106 1105 profile.add_friend(f1)
1107 1106  
1108 1107 UserStampSweeper.any_instance.expects(:current_user).returns(profile).at_least_once
1109   - article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => profile.id
  1108 + article = create TinyMceArticle, :name => 'Tracked Article 1', :profile_id => profile.id
1110 1109 assert_equal 1, ActionTracker::Record.find_all_by_verb('create_article').count
1111 1110 process_delayed_job_queue
1112 1111 assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(article.activity.id).count
1113 1112  
1114 1113 f2 = fast_create(Person)
1115 1114 profile.add_friend(f2)
1116   - article2 = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => profile.id
  1115 + article2 = create TinyMceArticle, :name => 'Tracked Article 2', :profile_id => profile.id
1117 1116 assert_equal 2, ActionTracker::Record.find_all_by_verb('create_article').count
1118 1117 process_delayed_job_queue
1119 1118 assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(article2.activity.id).count
... ... @@ -1378,7 +1377,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1378 1377  
1379 1378 should "the author_name returns the name of the article's author" do
1380 1379 author = fast_create(Person)
1381   - a = profile.articles.create!(:name => 'a test article', :last_changed_by => author)
  1380 + a = create(Article, :name => 'a test article', :last_changed_by => author, :profile_id => profile.id)
1382 1381 assert_equal author.name, a.author_name
1383 1382 author.destroy
1384 1383 a.reload
... ... @@ -1397,7 +1396,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1397 1396 should 'retrieve latest info from comment when has comments' do
1398 1397 forum = fast_create(Forum, :name => 'Forum test', :profile_id => profile.id)
1399 1398 post = fast_create(TextileArticle, :name => 'First post', :profile_id => profile.id, :parent_id => forum.id, :updated_at => Time.now)
1400   - post.comments << Comment.new(:name => 'Guest', :email => 'guest@example.com', :title => 'test comment', :body => 'hello!')
  1399 + post.comments << build(Comment, :name => 'Guest', :email => 'guest@example.com', :title => 'test comment', :body => 'hello!')
1401 1400 assert_equal post.comments.last.created_at, post.info_from_last_update[:date]
1402 1401 assert_equal "Guest", post.info_from_last_update[:author_name]
1403 1402 assert_nil post.info_from_last_update[:author_url]
... ... @@ -1463,7 +1462,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1463 1462  
1464 1463 should 'get images paths in article body' do
1465 1464 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
1466   - a = TinyMceArticle.new :profile => @profile
  1465 + a = build TinyMceArticle, :profile => @profile
1467 1466 a.body = 'Noosfero <img src="http://noosfero.com/test.png" /> test <img src="http://test.com/noosfero.png" />'
1468 1467 assert_includes a.body_images_paths, 'http://noosfero.com/test.png'
1469 1468 assert_includes a.body_images_paths, 'http://test.com/noosfero.png'
... ... @@ -1471,7 +1470,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1471 1470  
1472 1471 should 'get absolute images paths in article body' do
1473 1472 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
1474   - a = TinyMceArticle.new :profile => @profile
  1473 + a = build TinyMceArticle, :profile => @profile
1475 1474 a.body = 'Noosfero <img src="test.png" alt="Absolute" /> test <img src="/relative/path.png" />'
1476 1475 assert_includes a.body_images_paths, 'http://noosfero.org/test.png'
1477 1476 assert_includes a.body_images_paths, 'http://noosfero.org/relative/path.png'
... ... @@ -1479,20 +1478,20 @@ class ArticleTest &lt; ActiveSupport::TestCase
1479 1478  
1480 1479 should 'return empty if there are no images in article body' do
1481 1480 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
1482   - a = Event.new :profile => @profile
  1481 + a = build Event, :profile => @profile
1483 1482 a.body = 'Noosfero test'
1484 1483 assert_equal [], a.body_images_paths
1485 1484 end
1486 1485  
1487 1486 should 'return empty if body is nil' do
1488 1487 Environment.any_instance.stubs(:default_hostname).returns('noosfero.org')
1489   - a = Article.new :profile => @profile
  1488 + a = build Article, :profile => @profile
1490 1489 assert_equal [], a.body_images_paths
1491 1490 end
1492 1491  
1493 1492 should 'survive to a invalid src attribute while looking for images in body' do
1494   - domain = Environment.default.domains.first || Domain.new(:name => 'localhost')
1495   - article = Article.new(:body => "An article with invalid src in img tag <img src='path with spaces.png' />", :profile => @profile)
  1493 + domain = Environment.default.domains.first || build(Domain, :name => 'localhost')
  1494 + article = build(Article, :body => "An article with invalid src in img tag <img src='path with spaces.png' />", :profile => @profile)
1496 1495 assert_nothing_raised URI::InvalidURIError do
1497 1496 assert_equal ["http://#{profile.environment.default_hostname}/path%20with%20spaces.png"], article.body_images_paths
1498 1497 end
... ... @@ -1531,13 +1530,13 @@ class ArticleTest &lt; ActiveSupport::TestCase
1531 1530 end
1532 1531  
1533 1532 should "return 1 comment on label if the content has 1 comment" do
1534   - a = Article.new(:comments_count => 1)
  1533 + a = build(Article, :comments_count => 1)
1535 1534 assert_equal 1, a.comments_count
1536 1535 assert_equal "one comment", a.more_comments_label
1537 1536 end
1538 1537  
1539 1538 should "return number of comments on label if the content has more than one comment" do
1540   - a = Article.new(:comments_count => 4)
  1539 + a = build(Article, :comments_count => 4)
1541 1540 assert_equal 4, a.comments_count
1542 1541 assert_equal "4 comments", a.more_comments_label
1543 1542 end
... ... @@ -1549,13 +1548,13 @@ class ArticleTest &lt; ActiveSupport::TestCase
1549 1548 end
1550 1549  
1551 1550 should "return 1 view on label if the content has 1 view" do
1552   - a = Article.new(:hits => 1)
  1551 + a = build(Article, :hits => 1)
1553 1552 assert_equal 1, a.hits
1554 1553 assert_equal "one view", a.more_popular_label
1555 1554 end
1556 1555  
1557 1556 should "return number of views on label if the content has more than one view" do
1558   - a = Article.new(:hits => 4)
  1557 + a = build(Article, :hits => 4)
1559 1558 assert_equal 4, a.hits
1560 1559 assert_equal "4 views", a.more_popular_label
1561 1560 end
... ... @@ -1591,8 +1590,8 @@ class ArticleTest &lt; ActiveSupport::TestCase
1591 1590 end
1592 1591  
1593 1592 should 'remove all categorizations when destroyed' do
1594   - art = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
1595   - cat = Category.create!(:name => 'category 1', :environment_id => Environment.default.id)
  1593 + art = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id)
  1594 + cat = create(Category, :name => 'category 1', :environment_id => Environment.default.id)
1596 1595 art.add_category cat
1597 1596 art.destroy
1598 1597 assert cat.articles.reload.empty?
... ... @@ -1600,9 +1599,9 @@ class ArticleTest &lt; ActiveSupport::TestCase
1600 1599  
1601 1600 should 'show more popular articles' do
1602 1601 Article.destroy_all
1603   - art1 = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
1604   - art2 = Article.create!(:name => 'article 2', :profile_id => fast_create(Person).id)
1605   - art3 = Article.create!(:name => 'article 3', :profile_id => fast_create(Person).id)
  1602 + art1 = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id)
  1603 + art2 = create(Article, :name => 'article 2', :profile_id => fast_create(Person).id)
  1604 + art3 = create(Article, :name => 'article 3', :profile_id => fast_create(Person).id)
1606 1605  
1607 1606 art1.hits = 56; art1.save!
1608 1607 art3.hits = 92; art3.save!
... ... @@ -1612,11 +1611,11 @@ class ArticleTest &lt; ActiveSupport::TestCase
1612 1611 end
1613 1612  
1614 1613 should 'show if article is public' do
1615   - art1 = Article.create!(:name => 'article 1', :profile_id => fast_create(Person).id)
1616   - art2 = Article.create!(:name => 'article 2', :profile_id => fast_create(Person).id, :advertise => false)
1617   - art3 = Article.create!(:name => 'article 3', :profile_id => fast_create(Person).id, :published => false)
1618   - art4 = Article.create!(:name => 'article 4', :profile_id => fast_create(Person, :visible => false).id)
1619   - art5 = Article.create!(:name => 'article 5', :profile_id => fast_create(Person, :public_profile => false).id)
  1614 + art1 = create(Article, :name => 'article 1', :profile_id => fast_create(Person).id)
  1615 + art2 = create(Article, :name => 'article 2', :profile_id => fast_create(Person).id, :advertise => false)
  1616 + art3 = create(Article, :name => 'article 3', :profile_id => fast_create(Person).id, :published => false)
  1617 + art4 = create(Article, :name => 'article 4', :profile_id => fast_create(Person, :visible => false).id)
  1618 + art5 = create(Article, :name => 'article 5', :profile_id => fast_create(Person, :public_profile => false).id)
1620 1619  
1621 1620 articles = Article.public
1622 1621 assert_includes articles, art1
... ... @@ -1633,7 +1632,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1633 1632  
1634 1633 community.add_admin(admin)
1635 1634 community.add_member(member)
1636   - a = Article.new(:profile => community)
  1635 + a = build(Article, :profile => community)
1637 1636  
1638 1637 assert_equal false, a.allow_members_to_edit
1639 1638 assert_equal false, a.allow_edit?(member)
... ... @@ -1646,7 +1645,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1646 1645  
1647 1646 community.add_admin(admin)
1648 1647 community.add_member(member)
1649   - a = Article.new(:profile => community)
  1648 + a = build(Article, :profile => community)
1650 1649  
1651 1650 a.allow_members_to_edit = true
1652 1651  
... ... @@ -1681,20 +1680,20 @@ class ArticleTest &lt; ActiveSupport::TestCase
1681 1680 end
1682 1681  
1683 1682 should 'store first image in tracked action' do
1684   - a = TinyMceArticle.create! :name => 'Tracked Article', :body => '<p>Foo<img src="foo.png" />Bar</p>', :profile_id => profile.id
  1683 + a = create TinyMceArticle, :name => 'Tracked Article', :body => '<p>Foo<img src="foo.png" />Bar</p>', :profile_id => profile.id
1685 1684 assert_equal 'foo.png', ActionTracker::Record.last.get_first_image
1686 1685 end
1687 1686  
1688 1687 should 'be able to have a license' do
1689   - license = License.create!(:name => 'GPLv3', :environment => Environment.default)
1690   - article = Article.new(:license_id => license.id)
  1688 + license = create(License, :name => 'GPLv3', :environment => Environment.default)
  1689 + article = build(Article, :license_id => license.id)
1691 1690 assert_equal license, article.license
1692 1691 end
1693 1692  
1694 1693 should 'update path if parent is changed' do
1695   - f1 = Folder.create!(:name => 'Folder 1', :profile => profile)
1696   - f2 = Folder.create!(:name => 'Folder 2', :profile => profile)
1697   - article = TinyMceArticle.create!(:name => 'Sample Article', :parent_id => f1.id, :profile => profile)
  1694 + f1 = create(Folder, :name => 'Folder 1', :profile => profile)
  1695 + f2 = create(Folder, :name => 'Folder 2', :profile => profile)
  1696 + article = create(TinyMceArticle, :name => 'Sample Article', :parent_id => f1.id, :profile => profile)
1698 1697 assert_equal [f1.path,article.slug].join('/'), article.path
1699 1698  
1700 1699 article.parent = f2
... ... @@ -1705,12 +1704,13 @@ class ArticleTest &lt; ActiveSupport::TestCase
1705 1704 article.save!
1706 1705 assert_equal article.slug, article.path
1707 1706  
1708   - article.update_attributes({:parent_id => f2.id})
  1707 + article.parent = f2
  1708 + article.save!
1709 1709 assert_equal [f2.path,article.slug].join('/'), article.path
1710 1710 end
1711 1711  
1712 1712 should 'not allow parent as itself' do
1713   - article = Article.create!(:name => 'Sample Article', :profile => profile)
  1713 + article = create(Article, :name => 'Sample Article', :profile => profile)
1714 1714 article.parent = article
1715 1715 article.valid?
1716 1716  
... ... @@ -1718,9 +1718,9 @@ class ArticleTest &lt; ActiveSupport::TestCase
1718 1718 end
1719 1719  
1720 1720 should 'not allow cyclical paternity' do
1721   - a1 = Article.create!(:name => 'Sample Article 1', :profile => profile)
1722   - a2 = Article.create!(:name => 'Sample Article 2', :profile => profile, :parent => a1)
1723   - a3 = Article.create!(:name => 'Sample Article 3', :profile => profile, :parent => a2)
  1721 + a1 = create(Article, :name => 'Sample Article 1', :profile => profile)
  1722 + a2 = create(Article, :name => 'Sample Article 2', :profile => profile, :parent => a1)
  1723 + a3 = create(Article, :name => 'Sample Article 3', :profile => profile, :parent => a2)
1724 1724 a1.parent = a3
1725 1725 a1.valid?
1726 1726  
... ... @@ -1729,7 +1729,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1729 1729  
1730 1730 should 'set author_name before creating article if there is an author' do
1731 1731 author = fast_create(Person)
1732   - article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => author)
  1732 + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => author)
1733 1733 assert_equal author.name, article.author_name
1734 1734  
1735 1735 author_name = author.name
... ... @@ -1740,12 +1740,12 @@ class ArticleTest &lt; ActiveSupport::TestCase
1740 1740  
1741 1741 should "author_id return the author id of the article's author" do
1742 1742 author = fast_create(Person)
1743   - article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => author)
  1743 + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => author)
1744 1744 assert_equal author.id, article.author_id
1745 1745 end
1746 1746  
1747 1747 should "author_id return nil if there is no article's author" do
1748   - article = Article.create!(:name => 'Test', :profile => profile, :last_changed_by => nil)
  1748 + article = create(Article, :name => 'Test', :profile => profile, :last_changed_by => nil)
1749 1749 assert_nil article.author_id
1750 1750 end
1751 1751  
... ...