1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ORM\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\SapphireTest; |
6
|
|
|
use SilverStripe\ORM\Tests\DataObjectTest\Team; |
7
|
|
|
use SilverStripe\ORM\Tests\HasManyListTest\Company; |
8
|
|
|
use SilverStripe\ORM\Tests\HasManyListTest\CompanyCar; |
9
|
|
|
use SilverStripe\ORM\Tests\HasManyListTest\Employee; |
10
|
|
|
|
11
|
|
|
class HasManyListTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
protected static $fixture_file = [ |
15
|
|
|
'DataObjectTest.yml', // Borrow the model from DataObjectTest |
16
|
|
|
'HasManyListTest.yml', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
public static $extra_data_objects = [ |
20
|
|
|
Company::class, |
21
|
|
|
Employee::class, |
22
|
|
|
CompanyCar::class, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
public static function getExtraDataObjects() |
26
|
|
|
{ |
27
|
|
|
return array_merge( |
28
|
|
|
DataObjectTest::$extra_data_objects, |
29
|
|
|
ManyManyListTest::$extra_data_objects, |
30
|
|
|
static::$extra_data_objects |
31
|
|
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function testRelationshipEmptyOnNewRecords() |
35
|
|
|
{ |
36
|
|
|
// Relies on the fact that (unrelated) comments exist in the fixture file already |
37
|
|
|
$newTeam = new Team(); // has_many Comments |
38
|
|
|
$this->assertEquals([], $newTeam->Comments()->column('ID')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Test that related objects can be removed from a relation |
43
|
|
|
*/ |
44
|
|
|
public function testRemoveRelation() |
45
|
|
|
{ |
46
|
|
|
|
47
|
|
|
// Check that expected teams exist |
48
|
|
|
$list = Team::get(); |
49
|
|
|
$this->assertEquals( |
50
|
|
|
['Subteam 1', 'Subteam 2', 'Subteam 3', 'Team 1', 'Team 2', 'Team 3'], |
51
|
|
|
$list->sort('Title')->column('Title') |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
// Test that each team has the correct fans |
55
|
|
|
$team1 = $this->objFromFixture(DataObjectTest\Team::class, 'team1'); |
56
|
|
|
$team2 = $this->objFromFixture(DataObjectTest\Team::class, 'team2'); |
57
|
|
|
$this->assertEquals(['Bob', 'Joe'], $team1->Comments()->sort('Name')->column('Name')); |
|
|
|
|
58
|
|
|
$this->assertEquals(['Phil'], $team2->Comments()->sort('Name')->column('Name')); |
59
|
|
|
|
60
|
|
|
// Test that removing comments from unrelated team has no effect |
61
|
|
|
$team1comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1'); |
62
|
|
|
$team2comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3'); |
63
|
|
|
$team1->Comments()->remove($team2comment); |
64
|
|
|
$team2->Comments()->remove($team1comment); |
65
|
|
|
$this->assertEquals(['Bob', 'Joe'], $team1->Comments()->sort('Name')->column('Name')); |
66
|
|
|
$this->assertEquals(['Phil'], $team2->Comments()->sort('Name')->column('Name')); |
67
|
|
|
$this->assertEquals($team1->ID, $team1comment->TeamID); |
|
|
|
|
68
|
|
|
$this->assertEquals($team2->ID, $team2comment->TeamID); |
69
|
|
|
|
70
|
|
|
// Test that removing items from the related team resets the has_one relations on the fan |
71
|
|
|
$team1comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment1'); |
72
|
|
|
$team2comment = $this->objFromFixture(DataObjectTest\TeamComment::class, 'comment3'); |
73
|
|
|
$team1->Comments()->remove($team1comment); |
74
|
|
|
$team2->Comments()->remove($team2comment); |
75
|
|
|
$this->assertEquals(['Bob'], $team1->Comments()->sort('Name')->column('Name')); |
76
|
|
|
$this->assertEquals([], $team2->Comments()->sort('Name')->column('Name')); |
77
|
|
|
$this->assertEmpty($team1comment->TeamID); |
78
|
|
|
$this->assertEmpty($team2comment->TeamID); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testDefaultSortIsUsedOnList() |
82
|
|
|
{ |
83
|
|
|
/** @var Company $company */ |
84
|
|
|
$company = $this->objFromFixture(Company::class, 'silverstripe'); |
85
|
|
|
|
86
|
|
|
$this->assertListEquals([ |
87
|
|
|
['Make' => 'Ferrari'], |
88
|
|
|
['Make' => 'Jaguar'], |
89
|
|
|
['Make' => 'Lamborghini'], |
90
|
|
|
], $company->CompanyCars()); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testCanBeSortedDescending() |
94
|
|
|
{ |
95
|
|
|
/** @var Company $company */ |
96
|
|
|
$company = $this->objFromFixture(Company::class, 'silverstripe'); |
97
|
|
|
|
98
|
|
|
$this->assertListEquals([ |
99
|
|
|
['Make' => 'Lamborghini'], |
100
|
|
|
['Make' => 'Jaguar'], |
101
|
|
|
['Make' => 'Ferrari'], |
102
|
|
|
], $company->CompanyCars()->sort('"Make" DESC')); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testSortByModel() |
106
|
|
|
{ |
107
|
|
|
/** @var Company $company */ |
108
|
|
|
$company = $this->objFromFixture(Company::class, 'silverstripe'); |
109
|
|
|
|
110
|
|
|
$this->assertListEquals([ |
111
|
|
|
['Model' => 'Countach'], |
112
|
|
|
['Model' => 'E Type'], |
113
|
|
|
['Model' => 'F40'], |
114
|
|
|
], $company->CompanyCars()->sort('"Model" ASC')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testRemoveCallbackOnRemove() |
118
|
|
|
{ |
119
|
|
|
$removedIds = []; |
120
|
|
|
|
121
|
|
|
$base = $this->objFromFixture(Company::class, 'silverstripe'); |
122
|
|
|
$relation = $base->Employees(); |
|
|
|
|
123
|
|
|
$remove = $relation->First(); |
124
|
|
|
|
125
|
|
|
$relation->setRemoveCallback(function ($list, $ids) use (&$removedIds) { |
126
|
|
|
$removedIds = $ids; |
127
|
|
|
}); |
128
|
|
|
|
129
|
|
|
$relation->remove($remove); |
130
|
|
|
$this->assertEquals([$remove->ID], $removedIds); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testRemoveCallbackOnRemoveById() |
134
|
|
|
{ |
135
|
|
|
$removedIds = []; |
136
|
|
|
|
137
|
|
|
$base = $this->objFromFixture(Company::class, 'silverstripe'); |
138
|
|
|
$relation = $base->Employees(); |
139
|
|
|
$remove = $relation->First(); |
140
|
|
|
|
141
|
|
|
$relation->setRemoveCallback(function ($list, $ids) use (&$removedIds) { |
142
|
|
|
$removedIds = $ids; |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
$relation->removeByID($remove->ID); |
146
|
|
|
$this->assertEquals([$remove->ID], $removedIds); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|