1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\SS_List; |
6
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\Base; |
7
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\Belongs; |
8
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\HasMany; |
9
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\Hub; |
10
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\HubExtension; |
11
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\HubSub; |
12
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ManyMany; |
13
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ManyManyNoBelongs; |
14
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ManyManyThrough; |
15
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ManyManyThroughNoBelongs; |
16
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\Node; |
17
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\Polymorphic; |
18
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\SelfReferentialNode; |
19
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ThroughObject; |
20
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ThroughObjectPolymorphic; |
21
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ThroughObjectMMT; |
22
|
|
|
use SilverStripe\ORM\Tests\RelatedDataServiceTest\ThroughObjectMMTNB; |
23
|
|
|
use SilverStripe\Dev\SapphireTest; |
24
|
|
|
use SilverStripe\ORM\DataObject; |
25
|
|
|
use SilverStripe\ORM\Queries\SQLDelete; |
26
|
|
|
|
27
|
|
|
class RelatedDataServiceTest extends SapphireTest |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
protected $usesDatabase = true; |
31
|
|
|
|
32
|
|
|
// This static is required to get Config to populate |
33
|
|
|
// Config is looped within RelatedDataService::findAll() |
34
|
|
|
protected static $extra_data_objects = [ |
35
|
|
|
Base::class, |
36
|
|
|
Belongs::class, |
37
|
|
|
HasMany::class, |
38
|
|
|
Hub::class, |
39
|
|
|
HubSub::class, |
40
|
|
|
ManyMany::class, |
41
|
|
|
ManyManyNoBelongs::class, |
42
|
|
|
ManyManyThrough::class, |
43
|
|
|
ManyManyThroughNoBelongs::class, |
44
|
|
|
Node::class, |
45
|
|
|
Polymorphic::class, |
46
|
|
|
SelfReferentialNode::class, |
47
|
|
|
ThroughObject::class, |
48
|
|
|
ThroughObjectMMT::class, |
49
|
|
|
ThroughObjectMMTNB::class, |
50
|
|
|
ThroughObjectPolymorphic::class, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
// This is static is required to get the database tables to get created |
54
|
|
|
protected static $extra_dataobjects = [ |
55
|
|
|
Base::class, |
56
|
|
|
Belongs::class, |
57
|
|
|
HasMany::class, |
58
|
|
|
Hub::class, |
59
|
|
|
HubSub::class, |
60
|
|
|
ManyMany::class, |
61
|
|
|
ManyManyNoBelongs::class, |
62
|
|
|
ManyManyThrough::class, |
63
|
|
|
ManyManyThroughNoBelongs::class, |
64
|
|
|
Node::class, |
65
|
|
|
Polymorphic::class, |
66
|
|
|
SelfReferentialNode::class, |
67
|
|
|
ThroughObject::class, |
68
|
|
|
ThroughObjectMMT::class, |
69
|
|
|
ThroughObjectMMTNB::class, |
70
|
|
|
ThroughObjectPolymorphic::class, |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
public function testUnsaved() |
74
|
|
|
{ |
75
|
|
|
$myFile = new Node(); |
76
|
|
|
// don't write() |
77
|
|
|
$list = $myFile->findAllRelatedData(); |
78
|
|
|
$this->assertTrue($list instanceof SS_List); |
79
|
|
|
$this->assertSame(0, $list->count()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testUsageUnrelated() |
83
|
|
|
{ |
84
|
|
|
$myFile = new Node(); |
85
|
|
|
$myFile->write(); |
86
|
|
|
$myPage = new Hub(); |
87
|
|
|
$myPage->Title = 'Unrelated page'; |
88
|
|
|
$myPage->write(); |
89
|
|
|
$list = $myFile->findAllRelatedData(); |
90
|
|
|
$this->assertSame(0, $list->count()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testUsageHasOne() |
94
|
|
|
{ |
95
|
|
|
$pageTitle = 'My Page that has_one File'; |
96
|
|
|
$myFile = new Node(); |
97
|
|
|
$myFile->write(); |
98
|
|
|
$myPage = new Hub(); |
99
|
|
|
$myPage->Title = $pageTitle; |
100
|
|
|
$myPage->HO = $myFile; |
|
|
|
|
101
|
|
|
$myPage->write(); |
102
|
|
|
$list = $myFile->findAllRelatedData(); |
103
|
|
|
$this->assertSame(1, $list->count()); |
104
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testUsageHasOneHubExtension() |
108
|
|
|
{ |
109
|
|
|
// Add DataExtension and reset database so that tables + columns get added |
110
|
|
|
Hub::add_extension(HubExtension::class); |
111
|
|
|
DataObject::reset(); |
112
|
|
|
self::resetDBSchema(true, true); |
113
|
|
|
// |
114
|
|
|
$pageTitle = 'My Page that has_one File using HubExtension'; |
115
|
|
|
$myFile = new Node(); |
116
|
|
|
$myFile->write(); |
117
|
|
|
$myPage = new Hub(); |
118
|
|
|
$myPage->Title = $pageTitle; |
119
|
|
|
$myPage->ExtHO = $myFile; |
|
|
|
|
120
|
|
|
$myPage->write(); |
121
|
|
|
$list = $myFile->findAllRelatedData(); |
122
|
|
|
$this->assertSame(1, $list->count()); |
123
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testUsageHubSub() |
127
|
|
|
{ |
128
|
|
|
$pageTitle = 'My Sub Page'; |
129
|
|
|
$pageSubTitle = 'My SubTitle'; |
130
|
|
|
$myFile = new Node(); |
131
|
|
|
$myFile->write(); |
132
|
|
|
$myPage = new HubSub(); |
133
|
|
|
$myPage->Title = $pageTitle; |
134
|
|
|
$myPage->SubTitle = $pageSubTitle; |
|
|
|
|
135
|
|
|
$myPage->HO = $myFile; |
|
|
|
|
136
|
|
|
$myPage->write(); |
137
|
|
|
$list = $myFile->findAllRelatedData(); |
138
|
|
|
$this->assertSame(1, $list->count()); |
139
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
140
|
|
|
$this->assertSame($pageSubTitle, $list->first()->SubTitle); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testUsageHasOnePolymorphic() |
144
|
|
|
{ |
145
|
|
|
$pageTitle = 'My Page that has_one File polymorphic'; |
146
|
|
|
$myFile = new Node(); |
147
|
|
|
$myFile->write(); |
148
|
|
|
$myPage = new Hub(); |
149
|
|
|
$myPage->Title = $pageTitle; |
150
|
|
|
$myPage->Parent = $myFile; |
|
|
|
|
151
|
|
|
$myPage->write(); |
152
|
|
|
$list = $myFile->findAllRelatedData(); |
153
|
|
|
$this->assertSame(1, $list->count()); |
154
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function testUsageHasOnePolymorphicOnNode() |
158
|
|
|
{ |
159
|
|
|
$pageTitle = 'My Page that that belongs to a polymorphic File'; |
160
|
|
|
$myPage = new Hub(); |
161
|
|
|
$myPage->Title = $pageTitle; |
162
|
|
|
$myPage->write(); |
163
|
|
|
$myFile = new Polymorphic(); |
164
|
|
|
$myFile->Parent = $myPage; |
|
|
|
|
165
|
|
|
$myFile->write(); |
166
|
|
|
$list = $myFile->findAllRelatedData(); |
167
|
|
|
$this->assertSame(1, $list->count()); |
168
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testUsageHasMany() |
172
|
|
|
{ |
173
|
|
|
$pageTitle = 'My Page that has_many File'; |
174
|
|
|
$myPage = new Hub(); |
175
|
|
|
$myPage->Title = $pageTitle; |
176
|
|
|
$myPage->write(); |
177
|
|
|
$myFile = new HasMany(); |
178
|
|
|
$myFile->write(); |
179
|
|
|
$myPage->HM()->add($myFile); |
|
|
|
|
180
|
|
|
$list = $myFile->findAllRelatedData(); |
181
|
|
|
$this->assertSame(1, $list->count()); |
182
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function testUsageManyManyWithBelongs() |
186
|
|
|
{ |
187
|
|
|
$pageTitle = 'My Page that many_many File with belong_many_many Page'; |
188
|
|
|
$myPage = new Hub(); |
189
|
|
|
$myPage->Title = $pageTitle; |
190
|
|
|
$myPage->write(); |
191
|
|
|
$myFile = new Belongs(); |
192
|
|
|
$myFile->write(); |
193
|
|
|
$myPage->MMtoBMM()->add($myFile); |
|
|
|
|
194
|
|
|
$list = $myFile->findAllRelatedData(); |
195
|
|
|
$this->assertSame(1, $list->count()); |
196
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function testUsageManyManyWithoutBelongs() |
200
|
|
|
{ |
201
|
|
|
$pageTitle = 'My Page that many_many File without belong_many_many Page'; |
202
|
|
|
$myPage = new Hub(); |
203
|
|
|
$myPage->Title = $pageTitle; |
204
|
|
|
$myPage->write(); |
205
|
|
|
$myFile = new Node(); |
206
|
|
|
$myFile->write(); |
207
|
|
|
$myPage->MMtoNoBMM()->add($myFile); |
|
|
|
|
208
|
|
|
$list = $myFile->findAllRelatedData(); |
209
|
|
|
$this->assertSame(1, $list->count()); |
210
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function testUsageManyManyWithoutBelongsHubExtension() |
214
|
|
|
{ |
215
|
|
|
// Add DataExtension and reset database so that tables + columns get added |
216
|
|
|
Hub::add_extension(HubExtension::class); |
217
|
|
|
DataObject::reset(); |
218
|
|
|
self::resetDBSchema(true, true); |
219
|
|
|
// |
220
|
|
|
$pageTitle = 'My Page that many_many File without belong_many_many Page using HubExtension'; |
221
|
|
|
$myPage = new Hub(); |
222
|
|
|
$myPage->Title = $pageTitle; |
223
|
|
|
$myPage->write(); |
224
|
|
|
$myFile = new Node(); |
225
|
|
|
$myFile->write(); |
226
|
|
|
$myPage->ExtMMtoNoBMM()->add($myFile); |
|
|
|
|
227
|
|
|
$list = $myFile->findAllRelatedData(); |
228
|
|
|
$this->assertSame(1, $list->count()); |
229
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function testUsageManyManyWithoutBelongsOrphanedJoinTable() |
233
|
|
|
{ |
234
|
|
|
$pageTitle = 'My Page that many_many File without belong_many_many Page orphaned join table'; |
235
|
|
|
$myPage = new Hub(); |
236
|
|
|
$myPage->Title = $pageTitle; |
237
|
|
|
$myPage->write(); |
238
|
|
|
$myFile = new Node(); |
239
|
|
|
$myFile->write(); |
240
|
|
|
$myPage->MMtoNoBMM()->add($myFile); |
241
|
|
|
// manually delete Page record from database, leaving join table record intact |
242
|
|
|
SQLDelete::create('"TestOnly_RelatedDataServiceTest_Hub"', sprintf('"ID" = %s', $myPage->ID))->execute(); |
243
|
|
|
SQLDelete::create('"TestOnly_RelatedDataServiceTest_Base"', sprintf('"ID" = %s', $myPage->ID))->execute(); |
244
|
|
|
$list = $myFile->findAllRelatedData(); |
245
|
|
|
$this->assertSame(0, $list->count()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function testUsageBelongsManyMany() |
249
|
|
|
{ |
250
|
|
|
$pageTitle = 'My Page that belongs_many_many File with many_many Page'; |
251
|
|
|
$pageTitle2 = 'My other Page that belongs_many_many File with many_many Page'; |
252
|
|
|
$myPage = new Hub(); |
253
|
|
|
$myPage->Title = $pageTitle; |
254
|
|
|
$myPage->write(); |
255
|
|
|
$myPage2 = new Hub(); |
256
|
|
|
$myPage2->Title = $pageTitle2; |
257
|
|
|
$myPage2->write(); |
258
|
|
|
$myFile = new ManyMany(); |
259
|
|
|
$myFile->write(); |
260
|
|
|
// add from both pages from different directions |
261
|
|
|
$myPage->BMMtoMM()->add($myFile); |
|
|
|
|
262
|
|
|
$myFile->Hubs()->add($myPage2); |
|
|
|
|
263
|
|
|
$list = $myFile->findAllRelatedData(); |
264
|
|
|
$this->assertSame(2, $list->count()); |
265
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
266
|
|
|
$this->assertSame($pageTitle2, $list->last()->Title); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function testUsageManyManyThrough() |
270
|
|
|
{ |
271
|
|
|
$pageTitle = 'My Page that many_many_through File'; |
272
|
|
|
$myPage = new Hub(); |
273
|
|
|
$myPage->Title = $pageTitle; |
274
|
|
|
$myPage->write(); |
275
|
|
|
$myFile = new Node(); |
276
|
|
|
$myFile->write(); |
277
|
|
|
$myPage->MMT()->add($myFile); |
|
|
|
|
278
|
|
|
$list = $myFile->findAllRelatedData(); |
279
|
|
|
$this->assertSame(1, $list->count()); |
280
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function testUsageManyManyThroughPolymorphic() |
284
|
|
|
{ |
285
|
|
|
$pageTitle = 'My Page that many_many_through_parent_class File'; |
286
|
|
|
$myPage = new Hub(); |
287
|
|
|
$myPage->Title = $pageTitle; |
288
|
|
|
$myPage->write(); |
289
|
|
|
$myFile = new Node(); |
290
|
|
|
$myFile->write(); |
291
|
|
|
$myPage->MMTP()->add($myFile); |
|
|
|
|
292
|
|
|
$list = $myFile->findAllRelatedData(); |
293
|
|
|
$this->assertSame(1, $list->count()); |
294
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
public function testUsageFileManyManyWithoutPageBelongs() |
298
|
|
|
{ |
299
|
|
|
$pageTitle = 'My Page that not belongs_many_many File with many_many Page'; |
300
|
|
|
$myPage = new Hub(); |
301
|
|
|
$myPage->Title = $pageTitle; |
302
|
|
|
$myPage->write(); |
303
|
|
|
$myFile = new ManyManyNoBelongs(); |
304
|
|
|
$myFile->write(); |
305
|
|
|
$myFile->Hubs()->add($myPage); |
|
|
|
|
306
|
|
|
$list = $myFile->findAllRelatedData(); |
307
|
|
|
$this->assertSame(1, $list->count()); |
308
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
public function testUsageFileManyManyThroughWithPageBelongs() |
312
|
|
|
{ |
313
|
|
|
$pageTitle = 'My Page that many_many_belongs File with many_many_through Page'; |
314
|
|
|
$pageTitle2 = 'My other Page that many_many_belongs File with many_many_through Page'; |
315
|
|
|
$myPage = new Hub(); |
316
|
|
|
$myPage->Title = $pageTitle; |
317
|
|
|
$myPage->write(); |
318
|
|
|
$myPage2 = new Hub(); |
319
|
|
|
$myPage2->Title = $pageTitle2; |
320
|
|
|
$myPage2->write(); |
321
|
|
|
$myFile = new ManyManyThrough(); |
322
|
|
|
$myFile->write(); |
323
|
|
|
// add from both pages from different directions |
324
|
|
|
$myPage->BMMtoMMT()->add($myFile); |
|
|
|
|
325
|
|
|
$myFile->Hubs()->add($myPage2); |
|
|
|
|
326
|
|
|
$list = $myFile->findAllRelatedData(); |
327
|
|
|
$this->assertSame(2, $list->count()); |
328
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
329
|
|
|
$this->assertSame($pageTitle2, $list->last()->Title); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
public function testUsageFileManyManyThroughWithoutPageBelongs() |
333
|
|
|
{ |
334
|
|
|
$pageTitle = 'My Page that does not many_many_belongs File that many_many_through Page'; |
335
|
|
|
$myPage = new Hub(); |
336
|
|
|
$myPage->Title = $pageTitle; |
337
|
|
|
$myPage->write(); |
338
|
|
|
$myFile = new ManyManyThroughNoBelongs(); |
339
|
|
|
$myFile->write(); |
340
|
|
|
$myFile->Hubs()->add($myPage); |
|
|
|
|
341
|
|
|
$list = $myFile->findAllRelatedData(); |
342
|
|
|
$this->assertSame(1, $list->count()); |
343
|
|
|
$this->assertSame($pageTitle, $list->first()->Title); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
public function testSelfReferentialHasOne() |
347
|
|
|
{ |
348
|
|
|
$myFile = new SelfReferentialNode(); |
349
|
|
|
$myFile->Title = 'My self referential file has_one'; |
350
|
|
|
$myFile->write(); |
351
|
|
|
$myFile->HOA = $myFile; |
|
|
|
|
352
|
|
|
$myFile->HOB = $myFile; |
|
|
|
|
353
|
|
|
$myFile->write(); |
354
|
|
|
$list = $myFile->findAllRelatedData(); |
355
|
|
|
$this->assertSame(2, $list->count()); |
356
|
|
|
$this->assertSame($myFile->Title, $list->first()->Title); |
357
|
|
|
$this->assertTrue($list->first() instanceof SelfReferentialNode); |
358
|
|
|
$this->assertSame($myFile->Title, $list->last()->Title); |
359
|
|
|
$this->assertTrue($list->last() instanceof SelfReferentialNode); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function testSelfReferentialManyMany() |
363
|
|
|
{ |
364
|
|
|
$myFile = new SelfReferentialNode(); |
365
|
|
|
$myFile->Title = 'My self referential file many_many'; |
366
|
|
|
$myFile->write(); |
367
|
|
|
$myFile->MMA()->add($myFile); |
|
|
|
|
368
|
|
|
$myFile->MMB()->add($myFile); |
|
|
|
|
369
|
|
|
$list = $myFile->findAllRelatedData(); |
370
|
|
|
$this->assertSame(2, $list->count()); |
371
|
|
|
$this->assertSame($myFile->Title, $list->first()->Title); |
372
|
|
|
$this->assertTrue($list->first() instanceof SelfReferentialNode); |
373
|
|
|
$this->assertSame($myFile->Title, $list->last()->Title); |
374
|
|
|
$this->assertTrue($list->last() instanceof SelfReferentialNode); |
375
|
|
|
} |
376
|
|
|
} |
377
|
|
|
|