Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 1f0b30e

Browse files
committed
ENH: refs #0340. Add testing for user deletion functionality
1 parent 7021db2 commit 1f0b30e

File tree

4 files changed

+130
-62
lines changed

4 files changed

+130
-62
lines changed

core/database/upgrade/3.2.1.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ public function pgsql()
1717
public function postUpgrade()
1818
{
1919
$user = new Zend_Session_Namespace('Auth_User');
20+
$id = $user && $user->Dao ? $user->Dao->getKey() : '1';
2021

2122
$modelLoader = new MIDAS_ModelLoader();
2223
$settingModel = $modelLoader->loadModel('Setting');
23-
$settingModel->setConfig('adminuser', $user->Dao->getKey());
24+
$settingModel->setConfig('adminuser', $id);
2425
}
2526
}
2627
?>

core/models/base/BitstreamModelBase.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ function delete($bitstream)
5959
{
6060
if(!$bitstream instanceof BitstreamDao)
6161
{
62-
throw new Zend_Exception("Error param.");
62+
throw new Zend_Exception('Must pass a bitstream dao');
6363
}
6464
$checksum = $bitstream->getChecksum();
6565
$path = $bitstream->getFullPath();
6666
$assetstore = $bitstream->getAssetstore();
6767
parent::delete($bitstream);
68-
if($assetstore->getType() != MIDAS_ASSETSTORE_REMOTE && $this->getByChecksum($checksum) == false)
68+
if(file_exists($path) && $assetstore->getType() != MIDAS_ASSETSTORE_REMOTE
69+
&& $this->getByChecksum($checksum) == false)
6970
{
7071
unlink($path);
7172
}

core/tests/controllers/UserControllerTest.php

Lines changed: 121 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ public function testRegisterAction()
7070
$this->dispatchUrI("/user/register");
7171

7272
$userDao = $this->User->getByEmail($this->params['email']);
73-
if($userDao == false)
74-
{
75-
$this->fail('Unable to register');
76-
}
73+
$this->assertTrue($userDao != false, 'Unable to register');
7774
}
7875

7976
/** test login*/
@@ -99,12 +96,8 @@ public function testLoginAction()
9996
$this->params['email'] = 'user1@user1.com';
10097
$this->params['password'] = 'test';
10198
$this->request->setMethod('POST');
102-
$this->dispatchUrI("/user/login");
103-
104-
if(strpos($this->getBody(), 'Test Pass') === false)
105-
{
106-
$this->fail('Unable to authenticate');
107-
}
99+
$this->dispatchUrI('/user/login');
100+
$this->assertTrue(strpos($this->getBody(), 'Test Pass') !== false, 'Unable to authenticate');
108101
}
109102

110103
/** test terms */
@@ -136,10 +129,7 @@ public function testRecoverpasswordAction()
136129
$this->dispatchUrI("/user/recoverpassword", null);
137130

138131
$userDao2 = $this->User->getByEmail($this->params['email']);
139-
if($userDao->getPassword() == $userDao2->getPassword())
140-
{
141-
$this->fail('Unable to change password');
142-
}
132+
$this->assertNotEquals($userDao->getPassword(), $userDao2->getPassword(), 'Unable to change password');
143133
$this->setupDatabase(array('default'));
144134
}
145135

@@ -149,10 +139,7 @@ public function testSettingsAction()
149139
$this->resetAll();
150140
$this->dispatchUrI("/user/settings", null, false);
151141
$body = $this->getBody();
152-
if(!empty($body))
153-
{
154-
$this->fail('Should return nothing');
155-
}
142+
$this->assertTrue(empty($body), 'Should return nothing');
156143

157144
$usersFile = $this->loadData('User', 'default');
158145
$userDao = $this->User->load($usersFile[0]->getKey());
@@ -170,10 +157,8 @@ public function testSettingsAction()
170157
$this->dispatchUrI("/user/settings", $userDao);
171158

172159
$userCheckDao = $this->User->getByEmail($userDao->getEmail());
173-
if($userDao->getPassword() == $userCheckDao->getPassword())
174-
{
175-
$this->fail('Unable to change password');
176-
}
160+
$this->assertNotEquals($userDao->getPassword(), $userCheckDao->getPassword(), 'Unable to change password');
161+
177162
$this->setupDatabase(array('default'));
178163

179164
$this->resetAll();
@@ -187,10 +172,7 @@ public function testSettingsAction()
187172
$this->dispatchUrI("/user/settings", $userDao);
188173

189174
$userCheckDao = $this->User->load($userDao->getKey());
190-
if($this->params['firstname'] != $userCheckDao->getFirstname())
191-
{
192-
$this->fail('Unable to change account information');
193-
}
175+
$this->assertEquals($this->params['firstname'], $userCheckDao->getFirstname(), 'Unable to change account information');
194176

195177
$this->resetAll();
196178
$this->params = array();
@@ -201,10 +183,7 @@ public function testSettingsAction()
201183
$userCheckDao = $this->User->load($userDao->getKey());
202184

203185
$thumbnail = $userCheckDao->getThumbnail();
204-
if(empty($thumbnail))
205-
{
206-
$this->fail('Unable to change avatar');
207-
}
186+
$this->assertTrue(!empty($thumbnail), 'Unable to change avatar');
208187

209188
$this->setupDatabase(array('default'));
210189
}
@@ -216,10 +195,7 @@ public function testManageAction()
216195
$this->dispatchUrI("/user/manage", null, false);
217196

218197
$body = $this->getBody();
219-
if(!empty($body))
220-
{
221-
$this->fail('The page should be empty');
222-
}
198+
$this->assertTrue(empty($body), 'The page should be empty');
223199

224200
$usersFile = $this->loadData('User', 'default');
225201
$userDao = $this->User->load($usersFile[0]->getKey());
@@ -258,53 +234,141 @@ public function testUserpageAction()
258234
public function testValidentryAction()
259235
{
260236
$this->resetAll();
261-
$this->dispatchUrI("/user/validentry");
262-
if(strpos($this->getBody(), 'false') === false)
263-
{
264-
$this->fail();
265-
}
237+
$this->dispatchUrI('/user/validentry');
238+
$this->assertTrue(strpos($this->getBody(), 'false') !== false);
266239

267240
$this->resetAll();
268241
$this->params = array();
269242
$this->params['entry'] = 'user1@user1.com';
270243
$this->params['type'] = 'dbuser';
271-
$this->dispatchUrI("/user/validentry");
272-
if(strpos($this->getBody(), 'true') === false)
273-
{
274-
$this->fail();
275-
}
244+
$this->dispatchUrI('/user/validentry');
245+
$this->assertTrue(strpos($this->getBody(), 'true') !== false);
276246

277247
$this->resetAll();
278248
$this->params = array();
279249
$this->params['entry'] = 'test_email_not_in_db';
280250
$this->params['type'] = 'dbuser';
281-
$this->dispatchUrI("/user/validentry");
282-
if(strpos($this->getBody(), 'false') === false)
283-
{
284-
$this->fail();
285-
}
251+
$this->dispatchUrI('/user/validentry');
252+
$this->assertTrue(strpos($this->getBody(), 'false') !== false);
286253

287254
$this->resetAll();
288255
$this->params = array();
289256
$this->params['entry'] = 'user1@user1.com';
290257
$this->params['type'] = 'login';
291258
$this->params['password'] = 'wrong_password';
292-
$this->dispatchUrI("/user/validentry");
293-
if(strpos($this->getBody(), 'false') === false)
294-
{
295-
$this->fail();
296-
}
259+
$this->dispatchUrI('/user/validentry');
260+
$this->assertTrue(strpos($this->getBody(), 'false') !== false);
297261

298262
$this->resetAll();
299263
$this->params = array();
300264
$this->params['entry'] = 'user1@user1.com';
301265
$this->params['type'] = 'login';
302266
$this->params['password'] = 'test';
303-
$this->dispatchUrI("/user/validentry");
304-
if(strpos($this->getBody(), 'true') === false)
267+
$this->dispatchUrI('/user/validentry');
268+
$this->assertTrue(strpos($this->getBody(), 'true') !== false);
269+
}
270+
271+
/** Test admin ability to delete a user */
272+
public function testDeleteUserAction()
273+
{
274+
$modelLoader = new MIDAS_ModelLoader();
275+
$settingModel = $modelLoader->loadModel('Setting');
276+
$adminuserSetting = $settingModel->getValueByName('adminuser');
277+
$usersFile = $this->loadData('User', 'default');
278+
$user1 = $this->User->load($usersFile[0]->getKey());
279+
$user2 = $this->User->load($usersFile[1]->getKey());
280+
$adminUser = $this->User->load($usersFile[2]->getKey());
281+
282+
// Render the delete dialog and make sure it has correct text
283+
$this->resetAll();
284+
$this->dispatchUrI('/user/deletedialog?userId='.$user1->getKey(), $adminUser);
285+
$this->assertQueryContentContains('#deleteDialogUserName', $user1->getFirstname().' '.$user1->getLastname());
286+
287+
// Should fail if we aren't logged in
288+
$this->resetAll();
289+
$this->dispatchUrI('/user/delete?userId='.$user1->getKey(), null, true);
290+
291+
// Should fail if we try to delete an admin user
292+
$this->resetAll();
293+
$this->dispatchUrI('/user/delete?userId='.$adminUser->getKey(), $adminUser, true);
294+
295+
// Should fail if a non admin user tries to delete a different user
296+
$this->resetAll();
297+
$this->dispatchUrI('/user/delete?userId='.$user2->getKey(), $user1, true);
298+
299+
$oldRevisions = $user1->getItemrevisions();
300+
$this->assertTrue(count($oldRevisions) > 0);
301+
$revisionKeys = array();
302+
foreach($oldRevisions as $oldRevision)
305303
{
306-
$this->fail();
304+
$this->assertEquals($oldRevision->getUserId(), $user1->getKey());
305+
$revisionKeys[] = $oldRevision->getKey();
307306
}
307+
// Delete user 1 as administrator
308+
$key = $user1->getKey();
309+
$this->resetAll();
310+
$this->dispatchUrI('/user/delete?userId='.$user1->getKey(), $adminUser);
311+
312+
// Make sure user record is now gone
313+
$this->assertFalse($this->User->load($key));
314+
315+
// Make sure all of user's revisions that weren't removed are now listed as uploaded by superadmin
316+
$revisionModel = $modelLoader->loadModel('ItemRevision');
317+
$revisionNotDeleted = false;
318+
foreach($revisionKeys as $revisionKey)
319+
{
320+
$revision = $revisionModel->load($revisionKey);
321+
$this->assertTrue($revision === false || $revision->getUserId() == $adminuserSetting);
322+
if($revision !== false)
323+
{
324+
$revisionNotDeleted = true;
325+
}
326+
}
327+
$this->assertTrue($revisionNotDeleted, 'At least one revision should not have been deleted');
308328
}
309329

330+
/** Test user's ability to delete himself */
331+
public function testDeleteSelfAction()
332+
{
333+
$modelLoader = new MIDAS_ModelLoader();
334+
$settingModel = $modelLoader->loadModel('Setting');
335+
$adminuserSetting = $settingModel->getValueByName('adminuser');
336+
$usersFile = $this->loadData('User', 'default');
337+
$user1 = $this->User->load($usersFile[0]->getKey());
338+
339+
// Render the delete dialog and make sure it has correct text for self-deletion
340+
$this->resetAll();
341+
$this->dispatchUrI('/user/deletedialog?userId='.$user1->getKey(), $user1);
342+
$this->assertTrue(strpos($this->getBody(), 'Are you sure you want to delete your user account?') !== false);
343+
344+
$oldRevisions = $user1->getItemrevisions();
345+
$this->assertTrue(count($oldRevisions) > 0);
346+
$revisionKeys = array();
347+
foreach($oldRevisions as $oldRevision)
348+
{
349+
$this->assertEquals($oldRevision->getUserId(), $user1->getKey());
350+
$revisionKeys[] = $oldRevision->getKey();
351+
}
352+
// Delete user 1 as user 1
353+
$key = $user1->getKey();
354+
$this->resetAll();
355+
$this->dispatchUrI('/user/delete?userId='.$user1->getKey(), $user1);
356+
357+
// Make sure user record is now gone
358+
$this->assertFalse($this->User->load($key));
359+
360+
// Make sure all of user's revisions that weren't removed are now listed as uploaded by superadmin
361+
$revisionModel = $modelLoader->loadModel('ItemRevision');
362+
$revisionNotDeleted = false;
363+
foreach($revisionKeys as $revisionKey)
364+
{
365+
$revision = $revisionModel->load($revisionKey);
366+
$this->assertTrue($revision === false || $revision->getUserId() == $adminuserSetting);
367+
if($revision !== false)
368+
{
369+
$revisionNotDeleted = true;
370+
}
371+
}
372+
$this->assertTrue($revisionNotDeleted, 'At least one revision should not have been deleted');
373+
}
310374
}

core/tests/databaseDataset/default.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@
6565
<itemrevision itemrevision_id="3" item_id="1" revision="2"
6666
date="2011-01-27 12:09:02" user_id='1' changes="Description 1" />
6767
<itempolicygroup item_id="1" group_id="0" policy='0' />
68-
<item2folder item_id="1" folder_id='1001' />
68+
<item2folder item_id="1" folder_id='1001' />
69+
<item2folder item_id="1" folder_id="1004" />
6970
<item item_id="2" name="name 2" description="Description 2" type="4" uuid='4e311fdf82007c245b07d8d6c4fcb4205f2621eb72751' />
7071
<itemrevision itemrevision_id="2" item_id="2" revision="1"
7172
date="2011-01-27 12:09:02" user_id='1' changes="Description 1" />
7273
<itempolicyuser item_id="2" user_id="1" policy='2' />
7374
<item2folder item_id="2" folder_id='1001' />
7475
<item item_id="3" name="name 3" description="Description 3" type="3" uuid='4e311fdf82007c245b07d8d6c4fcb4205f2621eb72752' />
75-
7676
<bitstream bitstream_id="1" itemrevision_id="3" name="foo.png" mimetype="image/png" sizebytes="64" checksum="f283bc88b24491ba85c65ba960642753"
7777
path="f2/83/f283bc88b24491ba85c65ba960642753" assetstore_id="1" date="2011-01-27 12:09:02" />
7878

@@ -113,4 +113,6 @@
113113
<!-- MIDAS_FEED_CREATE_REVISION -->
114114
<feed feed_id="10" date="2011-01-27 12:09:02" user_id='1' type="40"
115115
ressource="1" />
116+
117+
<setting name="adminuser" module="core" value="3" />
116118
</dataset>

0 commit comments

Comments
 (0)