1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\ORM\DataObject; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime; |
8
|
|
|
|
9
|
|
|
class DataObjectDuplicationTest extends SapphireTest |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected $usesDatabase = true; |
13
|
|
|
|
14
|
|
|
protected static $extra_dataobjects = array( |
15
|
|
|
DataObjectDuplicationTest\Class1::class, |
16
|
|
|
DataObjectDuplicationTest\Class2::class, |
17
|
|
|
DataObjectDuplicationTest\Class3::class, |
18
|
|
|
DataObjectDuplicationTest\Class4::class, |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
public function testDuplicate() |
22
|
|
|
{ |
23
|
|
|
DBDatetime::set_mock_now('2016-01-01 01:01:01'); |
24
|
|
|
$orig = new DataObjectDuplicationTest\Class1(); |
25
|
|
|
$orig->text = 'foo'; |
|
|
|
|
26
|
|
|
$orig->write(); |
27
|
|
|
DBDatetime::set_mock_now('2016-01-02 01:01:01'); |
28
|
|
|
$duplicate = $orig->duplicate(); |
29
|
|
|
$this->assertInstanceOf( |
30
|
|
|
DataObjectDuplicationTest\Class1::class, |
31
|
|
|
$duplicate, |
32
|
|
|
'Creates the correct type' |
33
|
|
|
); |
34
|
|
|
$this->assertNotEquals( |
35
|
|
|
$duplicate->ID, |
36
|
|
|
$orig->ID, |
37
|
|
|
'Creates a unique record' |
38
|
|
|
); |
39
|
|
|
$this->assertEquals( |
40
|
|
|
'foo', |
41
|
|
|
$duplicate->text, |
|
|
|
|
42
|
|
|
'Copies fields' |
43
|
|
|
); |
44
|
|
|
$this->assertEquals( |
45
|
|
|
2, |
46
|
|
|
DataObjectDuplicationTest\Class1::get()->Count(), |
47
|
|
|
'Only creates a single duplicate' |
48
|
|
|
); |
49
|
|
|
$this->assertEquals(DBDatetime::now()->Nice(), $duplicate->dbObject('Created')->Nice()); |
|
|
|
|
50
|
|
|
$this->assertNotEquals($orig->dbObject('Created')->Nice(), $duplicate->dbObject('Created')->Nice()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testDuplicateHasOne() |
54
|
|
|
{ |
55
|
|
|
$relationObj = new DataObjectDuplicationTest\Class1(); |
56
|
|
|
$relationObj->text = 'class1'; |
|
|
|
|
57
|
|
|
$relationObj->write(); |
58
|
|
|
|
59
|
|
|
$orig = new DataObjectDuplicationTest\Class2(); |
60
|
|
|
$orig->text = 'class2'; |
|
|
|
|
61
|
|
|
$orig->oneID = $relationObj->ID; |
|
|
|
|
62
|
|
|
$orig->write(); |
63
|
|
|
|
64
|
|
|
$duplicate = $orig->duplicate(); |
65
|
|
|
$this->assertEquals( |
66
|
|
|
$relationObj->ID, |
67
|
|
|
$duplicate->oneID, |
|
|
|
|
68
|
|
|
'Copies has_one relationship' |
69
|
|
|
); |
70
|
|
|
$this->assertEquals( |
71
|
|
|
2, |
72
|
|
|
DataObjectDuplicationTest\Class2::get()->Count(), |
73
|
|
|
'Only creates a single duplicate' |
74
|
|
|
); |
75
|
|
|
$this->assertEquals( |
76
|
|
|
1, |
77
|
|
|
DataObjectDuplicationTest\Class1::get()->Count(), |
78
|
|
|
'Does not create duplicate of has_one relationship' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function testDuplicateManyManyClasses() |
84
|
|
|
{ |
85
|
|
|
//create new test classes below |
86
|
|
|
$one = new DataObjectDuplicationTest\Class1(); |
87
|
|
|
$two = new DataObjectDuplicationTest\Class2(); |
88
|
|
|
$three = new DataObjectDuplicationTest\Class3(); |
89
|
|
|
|
90
|
|
|
//set some simple fields |
91
|
|
|
$text1 = "Test Text 1"; |
92
|
|
|
$text2 = "Test Text 2"; |
93
|
|
|
$text3 = "Test Text 3"; |
94
|
|
|
$one->text = $text1; |
|
|
|
|
95
|
|
|
$two->text = $text2; |
|
|
|
|
96
|
|
|
$three->text = $text3; |
|
|
|
|
97
|
|
|
|
98
|
|
|
//write the to DB |
99
|
|
|
$one->write(); |
100
|
|
|
$two->write(); |
101
|
|
|
$three->write(); |
102
|
|
|
|
103
|
|
|
//create relations |
104
|
|
|
$one->twos()->add($two); |
|
|
|
|
105
|
|
|
$one->threes()->add($three, array('TestExtra'=>'three')); |
|
|
|
|
106
|
|
|
|
107
|
|
|
$one = DataObject::get_by_id(DataObjectDuplicationTest\Class1::class, $one->ID); |
108
|
|
|
$two = DataObject::get_by_id(DataObjectDuplicationTest\Class2::class, $two->ID); |
109
|
|
|
$three = DataObject::get_by_id(DataObjectDuplicationTest\Class3::class, $three->ID); |
110
|
|
|
|
111
|
|
|
$this->assertCount(1, $one->twos()); |
|
|
|
|
112
|
|
|
$this->assertCount(1, $one->threes()); |
|
|
|
|
113
|
|
|
$this->assertCount(1, $three->ones()); |
|
|
|
|
114
|
|
|
|
115
|
|
|
//test duplication |
116
|
|
|
$oneCopy = $one->duplicate(true, true); |
117
|
|
|
$twoCopy = $two->duplicate(true, true); |
118
|
|
|
$threeCopy = $three->duplicate(true, true); |
119
|
|
|
|
120
|
|
|
$oneCopy = DataObject::get_by_id(DataObjectDuplicationTest\Class1::class, $oneCopy->ID); |
121
|
|
|
$twoCopy = DataObject::get_by_id(DataObjectDuplicationTest\Class2::class, $twoCopy->ID); |
122
|
|
|
$threeCopy = DataObject::get_by_id(DataObjectDuplicationTest\Class3::class, $threeCopy->ID); |
123
|
|
|
|
124
|
|
|
$this->assertNotNull($oneCopy, "Copy of 1 exists"); |
125
|
|
|
$this->assertNotNull($twoCopy, "Copy of 2 exists"); |
126
|
|
|
$this->assertNotNull($threeCopy, "Copy of 3 exists"); |
127
|
|
|
|
128
|
|
|
$this->assertEquals($text1, $oneCopy->text); |
|
|
|
|
129
|
|
|
$this->assertEquals($text2, $twoCopy->text); |
130
|
|
|
$this->assertEquals($text3, $threeCopy->text); |
131
|
|
|
|
132
|
|
|
$this->assertCount( |
133
|
|
|
0, |
134
|
|
|
$oneCopy->twos(), |
135
|
|
|
"Many-to-one relation not copied (has_many)" |
136
|
|
|
); |
137
|
|
|
$this->assertCount( |
138
|
|
|
2, |
139
|
|
|
$oneCopy->threes(), |
140
|
|
|
"Object has the correct number of relations" |
141
|
|
|
); |
142
|
|
|
$this->assertCount( |
143
|
|
|
2, |
144
|
|
|
$threeCopy->ones(), |
145
|
|
|
"Object has the correct number of relations" |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->assertEquals( |
149
|
|
|
$one->ID, |
150
|
|
|
$twoCopy->one()->ID, |
|
|
|
|
151
|
|
|
"Match between relation of copy and the original" |
152
|
|
|
); |
153
|
|
|
$this->assertCount( |
154
|
|
|
0, |
155
|
|
|
$oneCopy->twos(), |
156
|
|
|
"Many-to-one relation not copied (has_many)" |
157
|
|
|
); |
158
|
|
|
$this->assertContains( |
159
|
|
|
$three->ID, |
160
|
|
|
$oneCopy->threes()->column('ID'), |
|
|
|
|
161
|
|
|
"Match between relation of copy and the original" |
162
|
|
|
); |
163
|
|
|
$this->assertContains( |
164
|
|
|
$one->ID, |
165
|
|
|
$threeCopy->ones()->column('ID'), |
166
|
|
|
"Match between relation of copy and the original" |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$this->assertEquals( |
170
|
|
|
'three', |
171
|
|
|
$oneCopy->threes()->byID($three->ID)->TestExtra, |
|
|
|
|
172
|
|
|
"Match between extra field of copy and the original" |
173
|
|
|
); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function testDuplicateManyManyFiltered() |
177
|
|
|
{ |
178
|
|
|
$parent = new DataObjectDuplicationTest\Class4(); |
179
|
|
|
$parent->Title = 'Parent'; |
|
|
|
|
180
|
|
|
$parent->write(); |
181
|
|
|
|
182
|
|
|
$child = new DataObjectDuplicationTest\Class4(); |
183
|
|
|
$child->Title = 'Child'; |
184
|
|
|
$child->write(); |
185
|
|
|
|
186
|
|
|
$grandChild = new DataObjectDuplicationTest\Class4(); |
187
|
|
|
$grandChild->Title = 'GrandChild'; |
188
|
|
|
$grandChild->write(); |
189
|
|
|
|
190
|
|
|
$parent->Children()->add($child); |
191
|
|
|
$child->Children()->add($grandChild); |
192
|
|
|
|
193
|
|
|
// Duplcating $child should only duplicate grandchild |
194
|
|
|
$childDuplicate = $child->duplicate(true, 'many_many'); |
195
|
|
|
$this->assertEquals(0, $childDuplicate->Parents()->count()); |
196
|
|
|
$this->assertListEquals( |
197
|
|
|
[['Title' => 'GrandChild']], |
198
|
|
|
$childDuplicate->Children() |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
// Duplicate belongs_many_many only |
202
|
|
|
$belongsDuplicate = $child->duplicate(true, 'belongs_many_many'); |
203
|
|
|
$this->assertEquals(0, $belongsDuplicate->Children()->count()); |
204
|
|
|
$this->assertListEquals( |
205
|
|
|
[['Title' => 'Parent']], |
206
|
|
|
$belongsDuplicate->Parents() |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
// Duplicate all |
210
|
|
|
$allDuplicate = $child->duplicate(true, true); |
211
|
|
|
$this->assertListEquals( |
212
|
|
|
[['Title' => 'Parent']], |
213
|
|
|
$allDuplicate->Parents() |
214
|
|
|
); |
215
|
|
|
$this->assertListEquals( |
216
|
|
|
[['Title' => 'GrandChild']], |
217
|
|
|
$allDuplicate->Children() |
218
|
|
|
); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Test duplication of UnsavedRelations |
223
|
|
|
*/ |
224
|
|
|
public function testDuplicateUnsaved() |
225
|
|
|
{ |
226
|
|
|
$one = new DataObjectDuplicationTest\Class1(); |
227
|
|
|
$one->text = "Test Text 1"; |
|
|
|
|
228
|
|
|
$three = new DataObjectDuplicationTest\Class3(); |
229
|
|
|
$three->text = "Test Text 3"; |
|
|
|
|
230
|
|
|
$one->threes()->add($three); |
231
|
|
|
$this->assertListEquals( |
232
|
|
|
[['text' => 'Test Text 3']], |
233
|
|
|
$one->threes() |
234
|
|
|
); |
235
|
|
|
// Test duplicate |
236
|
|
|
$dupe = $one->duplicate(false, true); |
237
|
|
|
$this->assertEquals('Test Text 1', $dupe->text); |
|
|
|
|
238
|
|
|
$this->assertListEquals( |
239
|
|
|
[['text' => 'Test Text 3']], |
240
|
|
|
$dupe->threes() |
241
|
|
|
); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|