1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests\GridField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Core\Injector\Injector; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\Forms\Form; |
9
|
|
|
use SilverStripe\Forms\GridField\GridField; |
10
|
|
|
use SilverStripe\Forms\GridField\GridFieldAddNewButton; |
11
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
12
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Person; |
13
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\PeopleGroup; |
14
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Category; |
15
|
|
|
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\TestController; |
16
|
|
|
use SilverStripe\ORM\SS_List; |
17
|
|
|
|
18
|
|
|
class GridFieldAddNewButtonTest extends SapphireTest |
19
|
|
|
{ |
20
|
|
|
protected static $fixture_file = 'GridFieldDetailFormTest.yml'; |
21
|
|
|
|
22
|
|
|
protected static $extra_dataobjects = [ |
23
|
|
|
Person::class, |
24
|
|
|
PeopleGroup::class, |
25
|
|
|
Category::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
Injector::nest(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function tearDown() |
35
|
|
|
{ |
36
|
|
|
parent::tearDown(); |
37
|
|
|
Injector::unnest(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testButtonPassesParentContextToSingletonWhenRelationListIsUsed() |
41
|
|
|
{ |
42
|
|
|
$group = $this->objFromFixture(PeopleGroup::class, 'group'); |
43
|
|
|
$list = $group->People(); |
44
|
|
|
$this->mockSingleton(Person::class) |
45
|
|
|
->expects($this->once()) |
46
|
|
|
->method('canCreate') |
47
|
|
|
->with( |
48
|
|
|
$this->anything(), |
49
|
|
|
$this->callback(function ($arg) use ($group) { |
50
|
|
|
return isset($arg['Parent']) && $arg['Parent']->ID == $group->ID; |
51
|
|
|
}) |
52
|
|
|
); |
53
|
|
|
|
54
|
|
|
$this->mockButtonFragments($list, $group); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
View Code Duplication |
public function testButtonPassesNoParentContextToSingletonWhenRelationListIsNotUsed() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$group = $this->objFromFixture(PeopleGroup::class, 'group'); |
60
|
|
|
$this->mockSingleton(Person::class) |
61
|
|
|
->expects($this->once()) |
62
|
|
|
->method('canCreate') |
63
|
|
|
->with( |
64
|
|
|
$this->anything(), |
65
|
|
|
$this->callback(function ($arg) { |
66
|
|
|
return !isset($arg['Parent']); |
67
|
|
|
}) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$this->mockButtonFragments(Person::get(), $group); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
View Code Duplication |
public function testButtonPassesNoParentContextToSingletonWhenNoParentRecordExists() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$group = $this->objFromFixture(PeopleGroup::class, 'group'); |
76
|
|
|
$list = $group->People(); |
77
|
|
|
|
78
|
|
|
$this->mockSingleton(Person::class) |
79
|
|
|
->expects($this->once()) |
80
|
|
|
->method('canCreate') |
81
|
|
|
->with( |
82
|
|
|
$this->anything(), |
83
|
|
|
$this->callback(function ($arg) { |
84
|
|
|
return !isset($arg['Parent']); |
85
|
|
|
}) |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->mockButtonFragments($list, null); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function mockButtonFragments(SS_List $list, $parent = null) |
92
|
|
|
{ |
93
|
|
|
$form = Form::create( |
94
|
|
|
new TestController(), |
95
|
|
|
'test', |
96
|
|
|
FieldList::create( |
97
|
|
|
$fakeGrid = GridField::create( |
98
|
|
|
'dummy', |
99
|
|
|
'dummy', |
100
|
|
|
$list, |
101
|
|
|
new GridFieldConfig( |
102
|
|
|
$button = new GridFieldAddNewButton() |
|
|
|
|
103
|
|
|
) |
104
|
|
|
) |
105
|
|
|
), |
106
|
|
|
FieldList::create() |
107
|
|
|
); |
108
|
|
|
if ($parent) { |
109
|
|
|
$form->loadDataFrom($parent); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$button->getHTMLFragments($fakeGrid); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
protected function mockSingleton($class) |
116
|
|
|
{ |
117
|
|
|
$mock = $this->getMockBuilder($class) |
118
|
|
|
->setMethods(['canCreate']) |
119
|
|
|
->getMock(); |
120
|
|
|
Injector::inst()->registerService($mock, $class); |
121
|
|
|
|
122
|
|
|
return $mock; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.