LeftAndMainTest.php
9.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* @package framework
* @subpackage tests
*/
class LeftAndMainTest extends FunctionalTest {
protected static $fixture_file = 'LeftAndMainTest.yml';
protected $extraDataObjects = array('LeftAndMainTest_Object');
protected $backupCss, $backupJs, $backupCombined;
public function setUp() {
parent::setUp();
// @todo fix controller stack problems and re-activate
//$this->autoFollowRedirection = false;
CMSMenu::populate_menu();
$this->backupCss = Config::inst()->get('LeftAndMain', 'extra_requirements_css');
$this->backupJs = Config::inst()->get('LeftAndMain', 'extra_requirements_javascript');
$this->backupCombined = Requirements::get_combined_files_enabled();
Config::inst()->update('LeftAndMain', 'extra_requirements_css', array(
FRAMEWORK_DIR . '/tests/assets/LeftAndMainTest.css'
));
Config::inst()->update('LeftAndMain', 'extra_requirements_javascript', array(
FRAMEWORK_DIR . '/tests/assets/LeftAndMainTest.js'
));
Requirements::set_combined_files_enabled(false);
}
public function tearDown() {
parent::tearDown();
Config::inst()->update('LeftAndMain', 'extra_requirements_css', $this->backupCss);
Config::inst()->update('LeftAndMain', 'extra_requirements_javascript', $this->backupJs);
Requirements::set_combined_files_enabled($this->backupCombined);
}
public function testExtraCssAndJavascript() {
$admin = $this->objFromFixture('Member', 'admin');
$this->session()->inst_set('loggedInAs', $admin->ID);
$response = $this->get('LeftAndMainTest_Controller');
$this->assertRegExp('/tests\/assets\/LeftAndMainTest.css/i', $response->getBody(),
"body should contain custom css");
$this->assertRegExp('/tests\/assets\/LeftAndMainTest.js/i', $response->getBody(),
"body should contain custom js");
}
/**
* Note: This test would typically rely on SiteTree and CMSMain, but is mocked by
* LeftAndMain_Controller and LeftAndMain_Object here to remove this dependency.
*/
public function testSaveTreeNodeSorting() {
$this->loginWithPermission('ADMIN');
// forcing sorting for non-MySQL
$rootPages = DataObject::get('LeftAndMainTest_Object', '"ParentID" = 0', '"ID"');
$siblingIDs = $rootPages->column('ID');
$page1 = $rootPages->offsetGet(0);
$page2 = $rootPages->offsetGet(1);
$page3 = $rootPages->offsetGet(2);
// Move page2 before page1
$siblingIDs[0] = $page2->ID;
$siblingIDs[1] = $page1->ID;
$data = array(
'SiblingIDs' => $siblingIDs,
'ID' => $page2->ID,
'ParentID' => 0
);
$response = $this->post('LeftAndMainTest_Controller/savetreenode', $data);
$this->assertEquals(200, $response->getStatusCode());
$page1 = DataObject::get_by_id('LeftAndMainTest_Object', $page1->ID, false);
$page2 = DataObject::get_by_id('LeftAndMainTest_Object', $page2->ID, false);
$page3 = DataObject::get_by_id('LeftAndMainTest_Object', $page3->ID, false);
$this->assertEquals(2, $page1->Sort, 'Page1 is sorted after Page2');
$this->assertEquals(1, $page2->Sort, 'Page2 is sorted before Page1');
$this->assertEquals(3, $page3->Sort, 'Sort order for other pages is unaffected');
}
public function testSaveTreeNodeParentID() {
$this->loginWithPermission('ADMIN');
$page1 = $this->objFromFixture('LeftAndMainTest_Object', 'page1');
$page2 = $this->objFromFixture('LeftAndMainTest_Object', 'page2');
$page3 = $this->objFromFixture('LeftAndMainTest_Object', 'page3');
$page31 = $this->objFromFixture('LeftAndMainTest_Object', 'page31');
$page32 = $this->objFromFixture('LeftAndMainTest_Object', 'page32');
// Move page2 into page3, between page3.1 and page 3.2
$siblingIDs = array(
$page31->ID,
$page2->ID,
$page32->ID
);
$data = array(
'SiblingIDs' => $siblingIDs,
'ID' => $page2->ID,
'ParentID' => $page3->ID
);
$response = $this->post('LeftAndMainTest_Controller/savetreenode', $data);
$this->assertEquals(200, $response->getStatusCode());
$page2 = DataObject::get_by_id('LeftAndMainTest_Object', $page2->ID, false);
$page31 = DataObject::get_by_id('LeftAndMainTest_Object', $page31->ID, false);
$page32 = DataObject::get_by_id('LeftAndMainTest_Object', $page32->ID, false);
$this->assertEquals($page3->ID, $page2->ParentID, 'Moved page gets new parent');
$this->assertEquals(1, $page31->Sort, 'Children pages before insertaion are unaffected');
$this->assertEquals(2, $page2->Sort, 'Moved page is correctly sorted');
$this->assertEquals(3, $page32->Sort, 'Children pages after insertion are resorted');
}
/**
* Check that all subclasses of leftandmain can be accessed
*/
public function testLeftAndMainSubclasses() {
$adminuser = $this->objFromFixture('Member','admin');
$this->session()->inst_set('loggedInAs', $adminuser->ID);
$menuItems = singleton('LeftAndMain')->MainMenu();
foreach($menuItems as $menuItem) {
$link = $menuItem->Link;
// don't test external links
if(preg_match('/^https?:\/\//',$link)) continue;
$response = $this->get($link);
$this->assertInstanceOf('SS_HTTPResponse', $response, "$link should return a response object");
$this->assertEquals(200, $response->getStatusCode(), "$link should return 200 status code");
// Check that a HTML page has been returned
$this->assertRegExp('/<html[^>]*>/i', $response->getBody(), "$link should contain <html> tag");
$this->assertRegExp('/<head[^>]*>/i', $response->getBody(), "$link should contain <head> tag");
$this->assertRegExp('/<body[^>]*>/i', $response->getBody(), "$link should contain <body> tag");
}
$this->session()->inst_set('loggedInAs', null);
}
public function testCanView() {
$adminuser = $this->objFromFixture('Member', 'admin');
$securityonlyuser = $this->objFromFixture('Member', 'securityonlyuser');
$allcmssectionsuser = $this->objFromFixture('Member', 'allcmssectionsuser');
$allValsFn = create_function('$obj', 'return $obj->getValue();');
// anonymous user
$this->session()->inst_set('loggedInAs', null);
$menuItems = singleton('LeftAndMain')->MainMenu(false);
$this->assertEquals(
array_map($allValsFn, $menuItems->column('Code')),
array(),
'Without valid login, members cant access any menu entries'
);
// restricted cms user
$this->session()->inst_set('loggedInAs', $securityonlyuser->ID);
$menuItems = singleton('LeftAndMain')->MainMenu(false);
$this->assertEquals(
array_map($allValsFn, $menuItems->column('Code')),
array('SecurityAdmin','Help'),
'Groups with limited access can only access the interfaces they have permissions for'
);
// all cms sections user
$this->session()->inst_set('loggedInAs', $allcmssectionsuser->ID);
$menuItems = singleton('LeftAndMain')->MainMenu(false);
$this->assertContains('SecurityAdmin',
array_map($allValsFn, $menuItems->column('Code')),
'Group with CMS_ACCESS_LeftAndMain permission can access all sections'
);
$this->assertContains('Help',
array_map($allValsFn, $menuItems->column('Code')),
'Group with CMS_ACCESS_LeftAndMain permission can access all sections'
);
// admin
$this->session()->inst_set('loggedInAs', $adminuser->ID);
$menuItems = singleton('LeftAndMain')->MainMenu(false);
$this->assertContains(
'SecurityAdmin',
array_map($allValsFn, $menuItems->column('Code')),
'Administrators can access Security Admin'
);
$this->session()->inst_set('loggedInAs', null);
}
/**
* Test {@see LeftAndMain::updatetreenodes}
*/
public function testUpdateTreeNodes() {
$page1 = $this->objFromFixture('LeftAndMainTest_Object', 'page1');
$page2 = $this->objFromFixture('LeftAndMainTest_Object', 'page2');
$page3 = $this->objFromFixture('LeftAndMainTest_Object', 'page3');
$page31 = $this->objFromFixture('LeftAndMainTest_Object', 'page31');
$page32 = $this->objFromFixture('LeftAndMainTest_Object', 'page32');
$this->logInWithPermission('ADMIN');
// Check page
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids='.$page1->ID);
$this->assertEquals(200, $result->getStatusCode());
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
$data = json_decode($result->getBody(), true);
$pageData = $data[$page1->ID];
$this->assertEquals(0, $pageData['ParentID']);
$this->assertEquals($page2->ID, $pageData['NextID']);
$this->assertEmpty($pageData['PrevID']);
// check subpage
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids='.$page31->ID);
$this->assertEquals(200, $result->getStatusCode());
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
$data = json_decode($result->getBody(), true);
$pageData = $data[$page31->ID];
$this->assertEquals($page3->ID, $pageData['ParentID']);
$this->assertEquals($page32->ID, $pageData['NextID']);
$this->assertEmpty($pageData['PrevID']);
// Multiple pages
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids='.$page1->ID.','.$page2->ID);
$this->assertEquals(200, $result->getStatusCode());
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
$data = json_decode($result->getBody(), true);
$this->assertEquals(2, count($data));
// Invalid IDs
$result = $this->get('LeftAndMainTest_Controller/updatetreenodes?ids=-3');
$this->assertEquals(200, $result->getStatusCode());
$this->assertEquals('text/json', $result->getHeader('Content-Type'));
$data = json_decode($result->getBody(), true);
$this->assertEquals(0, count($data));
}
}
/**
* @package framework
* @subpackage tests
*/
class LeftAndMainTest_Controller extends LeftAndMain implements TestOnly {
protected $template = 'BlankPage';
private static $tree_class = 'LeftAndMainTest_Object';
}
/**
* @package framework
* @subpackage tests
*/
class LeftAndMainTest_Object extends DataObject implements TestOnly {
private static $db = array(
'Title' => 'Varchar',
'URLSegment' => 'Varchar',
'Sort' => 'Int',
);
private static $extensions = array(
'Hierarchy'
);
public function CMSTreeClasses() {}
}