article_test.rb
4.02 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
require File.dirname(__FILE__) + '/../test_helper'
class ArticleTest < Test::Unit::TestCase
def setup
@profile = create_user('testing').person
end
attr_reader :profile
should 'have and require an associated profile' do
a = Article.new
a.valid?
assert a.errors.invalid?(:profile_id)
a.profile = profile
a.valid?
assert !a.errors.invalid?(:profile_id)
end
should 'require values for name, slug and path' do
a = Article.new
a.valid?
assert a.errors.invalid?(:name)
assert a.errors.invalid?(:slug)
assert a.errors.invalid?(:path)
a.name = 'my article'
a.valid?
assert !a.errors.invalid?(:name)
assert !a.errors.invalid?(:name)
assert !a.errors.invalid?(:path)
end
should 'act as versioned' do
a = Article.create!(:name => 'my article', :body => 'my text', :profile_id => profile.id)
assert_equal 1, a.versions(true).size
a.name = 'some other name'
a.save!
assert_equal 2, a.versions(true).size
end
should 'act as taggable' do
a = Article.create!(:name => 'my article', :profile_id => profile.id)
a.tag_list = ['one', 'two']
tags = a.tag_list.names
assert tags.include?('one')
assert tags.include?('two')
end
should 'act as filesystem' do
a = Article.create!(:name => 'my article', :profile_id => profile.id)
b = a.children.build(:name => 'child article', :profile_id => profile.id)
b.save!
assert_equal 'my-article/child-article', b.path
a.name = 'another name'
a.save!
assert_equal 'another-name/child-article', Article.find(b.id).path
end
should 'provide HTML version' do
profile = create_user('testinguser').person
a = Article.create!(:name => 'my article', :profile_id => profile.id)
a.expects(:body).returns('the body of the article')
assert_equal 'the body of the article', a.to_html
end
should 'inform the icon to be used' do
assert_equal 'text-html', Article.new.icon_name
end
should 'provide a (translatable) description' do
result = 'the description'
a = Article.new
a.expects(:_).returns(result)
assert_same result, a.mime_type_description
end
should 'not accept articles with same slug under the same level' do
# top level articles first
profile = create_user('testinguser').person
a1 = profile.articles.build(:name => 'test')
a1.save!
# cannot add another top level article with same slug
a2 = profile.articles.build(:name => 'test')
a2.valid?
assert a2.errors.invalid?(:slug)
# now create a child of a1
a3 = profile.articles.build(:name => 'test')
a3.parent = a1
a3.valid?
assert !a3.errors.invalid?(:slug)
a3.save!
# cannot add another child of a1 with same slug
a4 = profile.articles.build(:name => 'test')
a4.parent = a1
a4.valid?
assert a4.errors.invalid?(:slug)
end
should 'calculate public path' do
# top level
a = profile.articles.build(:name => 'aaa')
a.save!
assert_equal "/#{profile.identifier}/aaa", a.public_path
# child articles
b = profile.articles.build(:name => 'bbb')
b.parent = a
b.save!
assert_equal "/#{profile.identifier}/aaa/bbb", b.public_path
end
should 'record who did the last change' do
a = profile.articles.build(:name => 'test')
# must be a person
assert_raise ActiveRecord::AssociationTypeMismatch do
a.last_changed_by = Profile.new
end
assert_nothing_raised do
a.last_changed_by = Person.new
a.save!
end
end
should 'search for recent documents for a given profile' do
first = profile.articles.build(:name => 'first'); first.save!
second = profile.articles.build(:name => 'second'); second.save!
third = profile.articles.build(:name => 'third'); third.save!
forth = profile.articles.build(:name => 'forth'); forth.save!
fifth = profile.articles.build(:name => 'fifth'); fifth.save!
assert_equal [first,second,third], Article.recent(profile, 3)
assert_equal [first,second,third,forth,fifth], Article.recent(profile, 10)
end
end