1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dynamic\ManageableDataObject\Test\Extensions; |
4
|
|
|
|
5
|
|
|
use Dynamic\ManageableDataObject\Test\Model\SampleManageableDataObject; |
6
|
|
|
use Dynamic\ManageableDataObject\Test\Model\SampleManageableObjectPage; |
7
|
|
|
use Dynamic\ManageableDataObject\Test\Model\SampleManageableObjectPageController; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Dev\FunctionalTest; |
10
|
|
|
use SilverStripe\Security\Security; |
11
|
|
|
use SilverStripe\View\SSViewer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ManageableDataObjectExtensionTest |
15
|
|
|
*/ |
16
|
|
|
class ManageableDataObjectExtensionTest extends FunctionalTest |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected static $fixture_file = '../fixtures.yml'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected static $extra_dataobjects = [ |
28
|
|
|
SampleManageableDataObject::class, |
29
|
|
|
SampleManageableObjectPage::class, |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
*/ |
35
|
|
|
public function setUp() |
36
|
|
|
{ |
37
|
|
|
parent::setUp(); |
38
|
|
|
// Suppress themes |
39
|
|
|
SSViewer::config()->update('theme_enabled', false); |
40
|
|
|
$this->session()->set('readingMode', 'Stage.Stage'); |
41
|
|
|
$this->session()->set('unsecuredDraftSite', true); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Ensure any current member is logged out |
46
|
|
|
*/ |
47
|
|
|
public function logOut() |
48
|
|
|
{ |
49
|
|
|
if ($member = Security::getCurrentUser()) { |
|
|
|
|
50
|
|
|
Security::setCurrentUser(null); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
*/ |
57
|
|
|
public function testAdd() |
58
|
|
|
{ |
59
|
|
|
/** @var SampleManageableObjectPage $page */ |
60
|
|
|
$page = $this->objFromFixture(SampleManageableObjectPage::class, 'one'); |
61
|
|
|
|
62
|
|
|
$this->logInWithPermission('MDO_Create'); |
63
|
|
|
$response = $this->get($page->Link('add')); |
64
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
65
|
|
|
$this->logOut(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* |
70
|
|
|
*/ |
71
|
|
|
public function testEdit() |
72
|
|
|
{ |
73
|
|
|
/** @var SampleManageableDataObject $object */ |
74
|
|
|
$object = $this->objFromFixture(SampleManageableDataObject::class, 'one'); |
75
|
|
|
/** @var SampleManageableObjectPage $page */ |
76
|
|
|
$page = $this->objFromFixture(SampleManageableObjectPage::class, 'one'); |
77
|
|
|
|
78
|
|
|
$this->logInWithPermission('MDO_Edit'); |
79
|
|
|
$response = $this->get(Controller::join_links( |
80
|
|
|
$page->Link(), |
81
|
|
|
'edit', |
82
|
|
|
$object->ID |
83
|
|
|
)); |
84
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
85
|
|
|
$this->logOut(); |
86
|
|
|
|
87
|
|
|
$response2 = $this->get('/SampleManageableObjectPageController/edit/' . 0); |
88
|
|
|
$this->assertEquals(404, $response2->getStatusCode()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
*/ |
94
|
|
|
public function testDelete() |
95
|
|
|
{ |
96
|
|
|
/** @var SampleManageableObjectPage $page */ |
97
|
|
|
$page = $this->objFromFixture(SampleManageableObjectPage::class, 'one'); |
98
|
|
|
|
99
|
|
|
$newObject = SampleManageableDataObject::create(); |
100
|
|
|
$newObject->Title = 'Foo'; |
101
|
|
|
$newObject->write(); |
102
|
|
|
|
103
|
|
|
$id = $newObject->ID; |
104
|
|
|
|
105
|
|
|
$this->logInWithPermission('MDO_Delete'); |
106
|
|
|
|
107
|
|
|
$response404 = $this->get(Controller::join_links( |
108
|
|
|
$page->Link(), |
109
|
|
|
'delete', |
110
|
|
|
0 |
111
|
|
|
)); |
112
|
|
|
$this->assertEquals(404, $response404->getStatusCode()); |
113
|
|
|
|
114
|
|
|
$success = $this->get(Controller::join_links( |
115
|
|
|
$page->Link(), |
116
|
|
|
'delete', |
117
|
|
|
$id |
118
|
|
|
)); |
119
|
|
|
$this->assertEquals(200, $success->getStatusCode()); |
120
|
|
|
$this->assertNull(SampleManageableDataObject::get()->byID($id)); |
121
|
|
|
|
122
|
|
|
$this->logOut(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* |
127
|
|
|
*/ |
128
|
|
|
public function testDoSaveObject() |
129
|
|
|
{ |
130
|
|
|
/** @var SampleManageableObjectPage $page */ |
131
|
|
|
$page = $this->objFromFixture(SampleManageableObjectPage::class, 'one'); |
132
|
|
|
|
133
|
|
|
$this->logInWithPermission('MDO_Create'); |
134
|
|
|
|
135
|
|
|
$controller = SampleManageableObjectPageController::create( |
136
|
|
|
$this->objFromFixture(SampleManageableObjectPage::class, 'one') |
137
|
|
|
); |
138
|
|
|
$form = $controller->Form(); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$response = $this->get($page->Link('add')); |
141
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Submission successful'); |
142
|
|
|
|
143
|
|
|
$data = ['Title' => 'Foobar']; |
144
|
|
|
$responseSubmission = $this->submitForm( |
|
|
|
|
145
|
|
|
'ManageableDataObjectForm_Form', |
146
|
|
|
'action_doSaveObject', |
147
|
|
|
$data |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$record = SampleManageableDataObject::get()->filter($data)->first(); |
151
|
|
|
$this->assertInstanceOf(SampleManageableDataObject::class, $record); |
152
|
|
|
$this->assertTrue($record->exists()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|