-
Notifications
You must be signed in to change notification settings - Fork 16
/
ancestry_test.rb
223 lines (172 loc) · 5.8 KB
/
ancestry_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
require 'test_helper'
class AncestryTest < Zena::Unit::TestCase
context 'On an object with a fullpath' do
setup do
login(:ant)
end
subject do
secure(Node) { nodes(:status) }
end
context 'on fullpath_as_title' do
should 'return path with titles' do
assert_equal ["projects list", "Clean Water project", "Etat des travaux"], subject.fullpath_as_title
end
should 'return double dot from rel_path' do
assert_equal '(../Clean Water project/Etat des travaux)', subject.pseudo_id(nodes(:wiki), :relative_path)
end
end # on fullpath_as_title
context 'on short_path' do
should 'return last two parent titles' do
assert_equal ["..", "Clean Water project", "Etat des travaux"], subject.short_path
end
end # on short_path
context 'and custom_base set' do
subject do
secure(Node) { nodes(:cleanWater) }
end
should 'set basepath' do
assert_equal fullpath(:projects, :cleanWater), subject.basepath
end
should 'set basepath in children' do
assert_equal fullpath(:projects, :cleanWater), nodes(:status).basepath
end
end # and custom_base set
context 'with a secret parent' do
subject do
secure(Node) { nodes(:talk) }
end
should 'replace parent by star on fullpath_as_title' do
assert_equal ["projects list", "*", "Talk"], subject.fullpath_as_title
end
should 'replace parent by star on short_path' do
assert_equal ["..", "*", "Talk"], subject.short_path
end
end # with a secret parent
end # On an object with a fullpath
context 'Moving an object' do
setup do
login(:lion)
subject.update_attributes(:parent_id => nodes_id(:lion))
end
subject do
secure(Node) { nodes(:secret) }
end
should 'rebuild fullpath in new parent' do
assert_equal fullpath(:zena, :people, :lion, :secret), subject.fullpath
end
should 'rebuild children fullpath' do
assert_equal fullpath(:zena, :people, :lion, :secret, :talk), nodes(:talk).fullpath
assert_equal fullpath(:zena, :people, :lion, :secret, :proposition), nodes(:proposition).fullpath
end
context 'with custom_base set' do
subject do
secure(Node) { nodes(:cleanWater) }
end
should 'rebuild basepath in new parent' do
assert_equal fullpath(:people, :lion, :cleanWater), subject.basepath
end
should 'rebuild children basepath' do
assert_equal fullpath(:people, :lion, :cleanWater), nodes(:status).basepath
end
end # with custom_base set
end # Moving an object
context 'Finding an object from a path' do
setup do
login(:lion)
end
subject do
secure(Node) { Node.find_by_path('projects list/Secret/Talk')}
end
should 'find object' do
assert_equal nodes_id(:talk), subject.id
end
context 'without access to secret parent' do
setup do
login(:anon)
end
should 'not find object' do
assert_nil subject
end
end # without access to secret parent
context 'without access to the object' do
setup do
login(:anon)
end
subject do
secure(Node) { Node.find_by_path('projects list/Secret') }
end
should 'not find object' do
assert_nil subject
end
end # without access to the object
end # Finding an object
context 'A node with ancestors' do
subject do
secure(Node) { nodes(:cleanWater) }
end
should 'return list of ancestors on ancestors' do
assert_equal nodes_zip(:zena, :projects), subject.ancestors.map(&:zip)
end
should 'return true on is_ancestor in parent' do
assert nodes(:projects).is_ancestor?(subject)
assert nodes(:zena).is_ancestor?(subject)
end
should 'return true on is_ancestor in self' do
assert subject.is_ancestor?(subject)
end
context 'with a zip starting like subject' do
setup do
subject.fullpath = '22'
@node = secure(Node) { nodes(:people) }
@node.fullpath = '2234'
end
should 'not return true on is_ancestor' do
assert !subject.is_ancestor?(@node)
end
end # with an zip starting like subject
should 'not return true on is_ancestor in foreign' do
assert subject.is_ancestor?(subject)
end
context 'with a secret parent' do
subject do
secure(Node) { nodes(:talk) }
end
should 'skip secret parent in ancestors' do
assert_equal nodes_zip(:zena, :projects), subject.ancestors.map(&:zip)
end
end # with a secret parent
end # A node with ancestors
context 'A visitor with write access' do
setup do
login(:tiger)
end
context 'creating a node' do
subject do
secure(Node) { Node.create(:parent_id => nodes_id(:projects), :title => 'Kyma')}
end
should 'build fullpath' do
assert_equal [nodes_zip(:zena), nodes_zip(:projects), subject.zip].join('/'), subject.fullpath
end
should 'build basepath' do
err subject
assert_equal [nodes_zip(:zena), nodes_zip(:projects), subject.zip].join('/'), subject.fullpath
end
end # creating a node
end # A visitor with write access
context 'A node in an ancestry loop' do
setup do
login(:ant)
Node.connection.execute "UPDATE nodes SET parent_id = #{nodes_id(:status)} WHERE id = #{nodes_id(:cleanWater)}"
end
subject do
secure(Node) { nodes(:lake_jpg) }
end
should 'raise Invalid record on site rebuild_fullpath' do
assert_raise(Zena::InvalidRecord) { current_site.rebuild_fullpath(subject.parent_id) }
end
end # A node in an ancestry loop
private
def fullpath(*args)
args.map {|sym| nodes_zip(sym).to_s}.join('/')
end
end