/
LoadedRevTests.pm
228 lines (193 loc) · 7.75 KB
/
LoadedRevTests.pm
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
224
225
226
227
228
# Tests that specifically target the ticklish area of rev number management.
# These tests acknowledge the possibility that loading a topic may not
# return the true revision number of the topic, but some cached number
# that may or may not be correct. They also verify that if
# the topic is *force loaded* with a specific revision (which may or may
# not be in the range of known revisions) that a "true" revision
# will be loaded.
#
# These tests are conducted at the Foswiki::Meta object level, so need
# to be run for each different store implementation.
#
package LoadedRevTests;
use strict;
use FoswikiFnTestCase;
our @ISA = ('FoswikiFnTestCase');
use Foswiki;
use Foswiki::Meta;
sub new {
my $self = shift()->SUPER::new(@_);
return $self;
}
# Topic has not been saved. Loaded rev should be undef *even after
# a load*
sub test_phantom_topic {
my $this = shift;
my $topicObject =
Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "PhantomTopic");
$this->assert_equals(undef, $topicObject->getLoadedRev());
$topicObject->load();
$this->assert_equals(undef, $topicObject->getLoadedRev());
$topicObject->load(1);
$this->assert_equals(undef, $topicObject->getLoadedRev());
$topicObject =
Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "PhantomTopic");
$this->assert_equals(undef, $topicObject->getLoadedRev());
}
# Topic has been saved. Loaded rev should be defined after a load,
# and if an out-of-range rev is loaded, it should be reigned back
# to the valid range.
sub test_good_topic {
my $this = shift;
my $topicObject =
Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "GoodTopic",
'Let there be light');
# We haven't loaded a rev yet, so the loaded rev should be undef
$this->assert_equals(undef, $topicObject->getLoadedRev());
# Now save. The loaded rev should be set.
$this->assert_equals(1, $topicObject->save());
$this->assert_equals(1, $topicObject->getLoadedRev());
# Create a new unloaded object for what we just saved
$topicObject =
Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "GoodTopic");
$this->assert_equals(undef, $topicObject->getLoadedRev());
$topicObject->load();
$this->assert_equals(1, $topicObject->getLoadedRev());
$topicObject =
Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "GoodTopic", 0);
$this->assert_equals(1, $topicObject->getLoadedRev());
$topicObject =
Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "GoodTopic", 1);
$this->assert_equals(1, $topicObject->getLoadedRev());
$topicObject =
Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "GoodTopic", 2);
$this->assert_equals(1, $topicObject->getLoadedRev());
}
# Save a topic with borked TOPICINFO. The TOPICINFO should be corrected
# during the save.
sub test_borked_TOPICINFO_save {
my $this = shift;
my $topicObject =
Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", <<SICK);
%META:TOPICINFO{version="3"}%
Houston, we may have a problem here
SICK
# We haven't loaded a rev yet, so the loaded rev should be undef
$this->assert_equals(undef, $topicObject->getLoadedRev());
$topicObject->save(forcenewrevision=>1);
# Now we *have* saved, and the rev should have been force-corrected
$this->assert_equals(1, $topicObject->getLoadedRev());
# Load it again to make sure
$topicObject =
Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO");
$this->assert_equals(1, $topicObject->getLoadedRev());
}
sub test_no_comma_v {
my $this = shift;
my $f;
open($f, '>',
"$Foswiki::cfg{DataDir}/$this->{test_web}/NoCommaV.txt")
|| return;
print $f <<WHEE;
%META:TOPICINFO{version="1.3"}%
Blue. No, Green!
WHEE
close($f);
my $topicObject =
Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "NoCommaV");
$this->assert_equals(1, $topicObject->getLoadedRev());
$topicObject =
Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "NoCommaV");
$topicObject->load(3);
# We asked for an out-of-range version; even though that's the rev no
# in the topic, it deosn't exist as a version so the loaded rev
# should rewind to the "true" version.
$this->assert_equals(1, $topicObject->getLoadedRev());
# Reload out-of-range
$topicObject = Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "NoCommaV");
$topicObject->load(4);
$this->assert_equals(1, $topicObject->getLoadedRev());
# Reload undef
$topicObject = Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "NoCommaV");
$topicObject->load();
$this->assert_equals(1, $topicObject->getLoadedRev());
# Reload 0
$topicObject = Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "NoCommaV");
$topicObject->load(0);
$this->assert_equals(1, $topicObject->getLoadedRev());
}
# Topic exists on disk, but the topic cache was saved by an external
# process and META:TOPICINFO is behind the latest topic in the DB.
# This case is specifically aimed at stores that decouple
# the revision history from the topic text.
# When the topic is first loaded, the version number will be imaginary.
sub test_borked_TOPICINFO_load_behind {
my $this = shift;
# Start by creating a topic with a valid rev no (1)
my $topicObject =
Foswiki::Meta->new(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", <<SICK);
Your grandmother smells of elderberries
SICK
$topicObject->save();
$this->assert_equals(1, $topicObject->getLoadedRev());
$topicObject->text('ere, Dennis, there some lovely muck over ere');
$topicObject->save(forcenewrevision => 1);
$this->assert_equals(2, $topicObject->getLoadedRev());
# Stomp the cache
my $f;
open($f, '>',
"$Foswiki::cfg{DataDir}/$this->{test_web}/BorkedTOPICINFO.txt")
|| return;
print $f <<SICK;
%META:TOPICINFO{version="1"}%
We are the knights who say Ni!
SICK
close($f);
# The load shouldn't access the history
$topicObject = Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO");
$this->assert_equals(1, $topicObject->getLoadedRev());
$this->assert_matches(qr/knights who say Ni/, $topicObject->text());
# Now if we load the latest, we will see a rev number of
# 1, but if we load any other rev we should see a correct rev
# number
# load explicit number
$topicObject = Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", 1);
$this->assert_equals(1, $topicObject->getLoadedRev());
$this->assert_matches(qr/elderberries/, $topicObject->text());
$topicObject = Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", 2);
$this->assert_equals(2, $topicObject->getLoadedRev());
$this->assert_matches(qr/lovely muck/, $topicObject->text());
# load latest rev
$topicObject = Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", 3);
$this->assert_equals(2, $topicObject->getLoadedRev());
$this->assert_matches(qr/lovely muck/, $topicObject->text());
$topicObject = Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", 0);
# Should ignore the TOPICINFO and return the "true" revision
$this->assert_equals(2, $topicObject->getLoadedRev());
# If we now save it, we should be back to corrected rev nos
$topicObject->save(forcenewrevision => 1);
$topicObject = Foswiki::Meta->load(
$this->{session}, $this->{test_web}, "BorkedTOPICINFO", 0);
$this->assert_equals(3, $topicObject->getLoadedRev());
}
1;