]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Creating a unit test for ElggUser
authornickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544>
Fri, 23 Oct 2009 20:14:26 +0000 (20:14 +0000)
committernickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544>
Fri, 23 Oct 2009 20:14:26 +0000 (20:14 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@3576 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/users.php
engine/tests/objects/users.php [new file with mode: 0644]

index 29a2dd42fd64832dfa7a3612c70d35ad893a7866..a15330501e544bb5e7ccfee494cb124bb237a7ec 100644 (file)
@@ -1535,3 +1535,13 @@ function users_settings_save() {
 //register actions *************************************************************
 register_elgg_event_handler('init','system','users_init',0);
 register_elgg_event_handler('pagesetup','system','users_pagesetup',0);
+
+/**
+ * Runs unit tests for ElggObject
+ */
+register_plugin_hook('unit_test', 'system', 'users_test');
+function users_test($hook, $type, $value, $params) {
+       global $CONFIG;
+       $value[] = "{$CONFIG->path}engine/tests/objects/users.php";
+       return $value;
+}
\ No newline at end of file
diff --git a/engine/tests/objects/users.php b/engine/tests/objects/users.php
new file mode 100644 (file)
index 0000000..5c88e4e
--- /dev/null
@@ -0,0 +1,157 @@
+<?php
+/**
+ * Elgg Test ElggUser
+ *
+ * @package Elgg
+ * @subpackage Test
+ * @author Curverider Ltd
+ * @link http://elgg.org/
+ */
+class ElggCoreUserTest extends ElggCoreUnitTest {
+
+       /**
+        * Called before each test object.
+        */
+       public function __construct() {
+               parent::__construct();
+               
+               // all code should come after here
+       }
+
+       /**
+        * Called before each test method.
+        */
+       public function setUp() {
+               $this->user = new ElggUserTest();
+       }
+
+       /**
+        * Called after each test method.
+        */
+       public function tearDown() {
+               // do not allow SimpleTest to interpret Elgg notices as exceptions
+               $this->swallowErrors();
+               
+               unset($this->user);
+       }
+
+       /**
+        * Called after each test object.
+        */
+       public function __destruct() {
+               // all code should go above here
+               parent::__destruct();
+       }
+
+       /**
+        * A basic test that will be called and fail.
+        */
+       public function testElggUserConstructor() {
+               $attributes = array();
+               $attributes['guid'] = '';
+               $attributes['type'] = 'user';
+               $attributes['subtype'] = '';
+               $attributes['owner_guid'] = get_loggedin_userid();
+               $attributes['container_guid'] = get_loggedin_userid();
+               $attributes['site_guid'] = 0;
+               $attributes['access_id'] = ACCESS_PRIVATE;
+               $attributes['time_created'] = '';
+               $attributes['time_updated'] = '';
+               $attributes['enabled'] = 'yes';
+               $attributes['tables_split'] = 2;
+               $attributes['tables_loaded'] = 0;
+               $attributes['name'] = '';
+               $attributes['username'] = '';
+               $attributes['password'] = '';
+               $attributes['salt'] = '';
+               $attributes['email'] = '';
+               $attributes['language'] = '';
+               $attributes['code'] = '';
+               $attributes['banned'] = 'no';
+               
+               $this->assertIdentical($this->user->expose_attributes(), $attributes);
+       }
+       
+       public function testElggUserLoad() {
+               // new object
+               $object = new ElggObject();
+               $this->AssertEqual($object->getGUID(), 0);
+               $guid = $object->save();
+               $this->AssertNotEqual($guid, 0);
+               
+               // fail on wrong type
+               try {
+                       $error = new ElggUserTest($guid);
+                       $this->assertTrue(FALSE);
+               } catch (Exception $e) {
+                       $this->assertIsA($e, 'InvalidClassException');
+                       $message = sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), $guid, 'ElggUser');
+                       $this->assertIdentical($e->getMessage(), $message);
+               }
+               
+               // clean up
+               $object->delete();
+       }
+       
+       public function testElggUserConstructorByGuid() {
+               $user = new ElggUser(get_loggedin_userid());
+               $this->assertIdentical($user, $_SESSION['user']);
+               
+               // fail with garbage
+               try {
+                       $error = new ElggUserTest(array('invalid'));
+                       $this->assertTrue(FALSE);
+               } catch (Exception $e) {
+                       $this->assertIsA($e, 'InvalidParameterException');
+                       $message = sprintf(elgg_echo('InvalidParameterException:UnrecognisedValue'));
+                       $this->assertIdentical($e->getMessage(), $message);
+               }
+       }
+       
+       public function testElggUserConstructorByDbRow() {
+               $row = $this->fetchUser(get_loggedin_userid());
+               $user = new ElggUser($row);
+               $this->assertIdentical($user, $_SESSION['user']);
+       }
+       
+       public function testElggUserConstructorByUsername() {
+               $row = $this->fetchUser(get_loggedin_userid());
+               $user = new ElggUser($row->username);
+               $this->assertIdentical($user, $_SESSION['user']);
+       }
+       
+       public function testElggUserConstructorByObject() {
+               $obj = new ElggUser(get_loggedin_userid());
+               $user = new ElggUser($obj);
+               $this->assertIdentical($obj, $user);
+               $this->assertIdentical($user, $_SESSION['user']);
+               
+               // fail on non-user object
+               $object = new ElggObject();
+               $object->save();
+               
+               try {
+                       $error = new ElggUserTest($object);
+                       $this->assertTrue(FALSE);
+               } catch (Exception $e) {
+                       $this->assertIsA($e, 'InvalidParameterException');
+                       $message = sprintf(elgg_echo('InvalidParameterException:NonElggUser'));
+                       $this->assertIdentical($e->getMessage(), $message);
+               }
+               
+               $object->delete();
+       }
+       
+       
+       protected function fetchUser($guid) {
+               global $CONFIG;
+               
+               return get_data_row("SELECT * FROM {$CONFIG->dbprefix}users_entity WHERE guid = '$guid'");
+       }
+}
+
+class ElggUserTest extends ElggUser {
+       public function expose_attributes() {
+               return $this->attributes;
+       }
+}