diff options
author | nickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2009-10-08 16:27:11 +0000 |
---|---|---|
committer | nickw <nickw@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2009-10-08 16:27:11 +0000 |
commit | 993ccb6d2433c6316e45dda23387f161c1f70e03 (patch) | |
tree | 82a2705bbfa8b4b336c355570487eb202e76706b | |
parent | 963f208e290420ad5e25a55905ed025a70aadc3d (diff) | |
download | elgg-993ccb6d2433c6316e45dda23387f161c1f70e03.tar.gz elgg-993ccb6d2433c6316e45dda23387f161c1f70e03.tar.bz2 |
Adding generic unit tests for ElggEntity.
git-svn-id: http://code.elgg.org/elgg/trunk@3516 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | engine/lib/entities.php | 26 | ||||
-rw-r--r-- | engine/tests/core/entities.php | 73 | ||||
-rw-r--r-- | engine/tests/objects/entities.php | 190 |
3 files changed, 209 insertions, 80 deletions
diff --git a/engine/lib/entities.php b/engine/lib/entities.php index 3eaea5136..779efb7af 100644 --- a/engine/lib/entities.php +++ b/engine/lib/entities.php @@ -5,9 +5,7 @@ * * @package Elgg * @subpackage Core - * @author Curverider Ltd <info@elgg.com> - * @link http://elgg.org/ */ @@ -149,6 +147,8 @@ * Q: Why are we not using __set overload here? * A: Because overload operators cause problems during subclassing, so we put the code here and * create overloads in subclasses. + * + * @todo Move "title" logic to applicable extending classes. * * @param string $name * @param mixed $value @@ -203,7 +203,9 @@ * @param string $name * @return mixed */ - function __get($name) { return $this->get($name); } + function __get($name) { + return $this->get($name); + } /** * Class member set overloading @@ -212,7 +214,9 @@ * @param mixed $value * @return mixed */ - function __set($name, $value) { return $this->set($name, $value); } + function __set($name, $value) { + return $this->set($name, $value); + } /** * Supporting isset. @@ -220,7 +224,14 @@ * @param string $name The name of the attribute or metadata. * @return bool */ - function __isset($name) { if ($this->$name!="") return true; else return false; } + function __isset($name) { + if ($this->$name!="") { + return true; + } + else { + return false; + } + } /** * Supporting unsetting of magic attributes. @@ -635,7 +646,8 @@ } else { - $this->attributes['guid'] = create_entity($this->attributes['type'], $this->attributes['subtype'], $this->attributes['owner_guid'], $this->attributes['access_id'], $this->attributes['site_guid'], $this->attributes['container_guid']); // Create a new entity (nb: using attribute array directly 'cos set function does something special!) + // Create a new entity (nb: using attribute array directly 'cos set function does something special!) + $this->attributes['guid'] = create_entity($this->attributes['type'], $this->attributes['subtype'], $this->attributes['owner_guid'], $this->attributes['access_id'], $this->attributes['site_guid'], $this->attributes['container_guid']); if (!$this->attributes['guid']) throw new IOException(elgg_echo('IOException:BaseEntitySaveFailed')); // Save any unsaved metadata TODO: How to capture extra information (access id etc) @@ -2775,7 +2787,7 @@ */ function entities_test($hook, $type, $value, $params) { global $CONFIG; - $params[] = $CONFIG->path . 'engine/tests/core/entities.php'; + $params[] = $CONFIG->path . 'engine/tests/objects/entities.php'; return $params; } diff --git a/engine/tests/core/entities.php b/engine/tests/core/entities.php deleted file mode 100644 index 37960c3af..000000000 --- a/engine/tests/core/entities.php +++ /dev/null @@ -1,73 +0,0 @@ -<?php -/** - * Elgg Test ElggEntities - * - * @package Elgg - * @subpackage Test - * @author Curverider Ltd - * @link http://elgg.org/ - */ -class ElggCoreEntityTest extends ElggCoreUnitTest { - /** - * Called before each test method. - */ - public function setUp() { - $this->entity = new ElggEntityTest(); - } - - /** - * Called after each test method. - */ - public function tearDown() { - unset($this->entity); - } - - /** - * Tests the protected attributes - */ - public function testElggEntityAttributes() { - $test_attributes = array(); - $test_attributes['guid'] = ''; - $test_attributes['type'] = ''; - $test_attributes['subtype'] = ''; - $test_attributes['owner_guid'] = get_loggedin_userid(); - $test_attributes['container_guid'] = get_loggedin_userid(); - $test_attributes['site_guid'] = 0; - $test_attributes['access_id'] = ACCESS_PRIVATE; - $test_attributes['time_created'] = ''; - $test_attributes['time_updated'] = ''; - $test_attributes['enabled'] = 'yes'; - $test_attributes['tables_split'] = 1; - $test_attributes['tables_loaded'] = 0; - - $this->assertIdentical($this->entity->expose_attributes(), $test_attributes); - } - - public function testElggEntityGetAndSet() { - $this->assertIdentical($this->entity->get('access_id'), ACCESS_PRIVATE); - $this->assertTrue($this->entity->set('access_id', ACCESS_PUBLIC)); - $this->assertIdentical($this->entity->get('access_id'), ACCESS_PUBLIC); - $this->assertIdentical($this->entity->access_id, ACCESS_PUBLIC); - unset($this->entity->access_id); - $this->assertIdentical($this->entity->access_id, ''); - - $this->assertFalse($this->entity->set('guid', 'error')); - - $this->assertNull($this->entity->get('non_existent')); - $this->assertFalse(isset($this->entity->non_existent)); - $this->assertTrue($this->entity->non_existent = 'test'); - $this->assertTrue(isset($this->entity->non_existent)); - $this->assertIdentical($this->entity->non_existent, 'test'); - } -} - -// ElggEntity is an abstract class with no abstact methods. -class ElggEntityTest extends ElggEntity { - public function __construct() { - $this->initialise_attributes(); - } - - public function expose_attributes() { - return $this->attributes; - } -} diff --git a/engine/tests/objects/entities.php b/engine/tests/objects/entities.php new file mode 100644 index 000000000..f13252871 --- /dev/null +++ b/engine/tests/objects/entities.php @@ -0,0 +1,190 @@ +<?php +/** + * Elgg Test ElggEntities + * + * @package Elgg + * @subpackage Test + * @author Curverider Ltd + * @link http://elgg.org/ + */ +class ElggCoreEntityTest extends ElggCoreUnitTest { + /** + * Called before each test method. + */ + public function setUp() { + $this->entity = new ElggEntityTest(); + } + + /** + * Called after each test method. + */ + public function tearDown() { + unset($this->entity); + } + + /** + * Tests the protected attributes + */ + public function testElggEntityAttributes() { + $test_attributes = array(); + $test_attributes['guid'] = ''; + $test_attributes['type'] = ''; + $test_attributes['subtype'] = ''; + $test_attributes['owner_guid'] = get_loggedin_userid(); + $test_attributes['container_guid'] = get_loggedin_userid(); + $test_attributes['site_guid'] = 0; + $test_attributes['access_id'] = ACCESS_PRIVATE; + $test_attributes['time_created'] = ''; + $test_attributes['time_updated'] = ''; + $test_attributes['enabled'] = 'yes'; + $test_attributes['tables_split'] = 1; + $test_attributes['tables_loaded'] = 0; + + $this->assertIdentical($this->entity->expose_attributes(), $test_attributes); + } + + public function testElggEntityGetAndSetBaseAttributes() { + // explicitly set and get access_id + $this->assertIdentical($this->entity->get('access_id'), ACCESS_PRIVATE); + $this->assertTrue($this->entity->set('access_id', ACCESS_PUBLIC)); + $this->assertIdentical($this->entity->get('access_id'), ACCESS_PUBLIC); + + // check internal attributes array + $attributes = $this->entity->expose_attributes(); + $this->assertIdentical($attributes['access_id'], ACCESS_PUBLIC); + + // implicitly set and get access_id + $this->entity->access_id = ACCESS_PRIVATE; + $this->assertIdentical($this->entity->access_id, ACCESS_PRIVATE); + + // unset access_id + unset($this->entity->access_id); + $this->assertIdentical($this->entity->access_id, ''); + + // unable to directly set guid + $this->assertFalse($this->entity->set('guid', 'error')); + $this->entity->guid = 'error'; + $this->assertNotEqual($this->entity->guid, 'error'); + + // fail on non-attribute + $this->assertNull($this->entity->get('non_existent')); + + // consider helper methods + $this->assertIdentical($this->entity->getGUID(), $this->entity->guid ); + $this->assertIdentical($this->entity->getType(), $this->entity->type ); + $this->assertIdentical($this->entity->getSubtype(), $this->entity->subtype ); + $this->assertIdentical($this->entity->getOwner(), $this->entity->owner_guid ); + $this->assertIdentical($this->entity->getAccessID(), $this->entity->access_id ); + $this->assertIdentical($this->entity->getTimeCreated(), $this->entity->time_created ); + $this->assertIdentical($this->entity->getTimeUpdated(), $this->entity->time_updated ); + } + + public function testElggEntityGetAndSetMetaData() { + // ensure metadata not set + $this->assertNull($this->entity->get('non_existent')); + $this->assertFalse(isset($this->entity->non_existent)); + + // create metadata + $this->assertTrue($this->entity->non_existent = 'testing'); + + // check metadata set + $this->assertTrue(isset($this->entity->non_existent)); + $this->assertIdentical($this->entity->non_existent, 'testing'); + $this->assertIdentical($this->entity->getMetaData('non_existent'), 'testing'); + + // check internal metadata array + $metadata = $this->entity->expose_metadata(); + $this->assertIdentical($metadata['non_existent'], 'testing'); + } + + public function testElggEntityCache() { + global $ENTITY_CACHE; + $ENTITY_CACHE = NULL; + + $this->assertNull($ENTITY_CACHE); + initialise_entity_cache(); + $this->assertIsA($ENTITY_CACHE, 'array'); + } + + public function testElggEntitySaveAndDelete() { + global $ENTITY_CACHE; + + // unable to delete with no guid + $this->assertFalse($this->entity->delete()); + + // error on save + try { + $this->entity->save(); + $this->assertTrue(FALSE); + } + catch (Exception $e) { + $this->assertIsA($e, 'InvalidParameterException'); + $this->assertIdentical($e->getMessage(), 'Entity type must be set.'); + } + + // set elements + $this->entity->type = 'site'; + $this->entity->non_existent = 'testing'; + + // save + $this->AssertEqual($guid, 0); + $guid = $this->entity->save(); + $this->AssertNotEqual($guid, 0); + + // check guid + $this->AssertEqual($this->entity->getGUID(), $guid); + $attributes = $this->entity->expose_attributes(); + $this->AssertEqual($attributes['guid'], $guid); + $this->AssertIdentical($ENTITY_CACHE[$guid], $this->entity); + + // check metadata + $metadata = $this->entity->expose_metadata(); + $this->AssertFalse(in_array('non_existent', $metadata)); + $this->AssertEqual($this->entity->get('non_existent'), 'testing'); + + // clean up with delete + $this->assertTrue($this->entity->delete()); + } + + public function testElggEntityDisableAndEnable() { + global $CONFIG; + + // ensure enabled + $this->assertTrue($this->entity->isEnabled()); + + // false on disable + $this->assertFalse($this->entity->disable()); + + // save and disable + $this->entity->type = 'site'; + $this->assertNotEqual($this->entity->save(), 0); + $this->assertTrue($this->entity->disable()); + + // ensure disabled by comparing directly with database + $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$this->entity->guid}'"); + $this->assertIdentical($entity->enabled, 'no'); + + // re-enable for deletion to work + $this->assertTrue($this->entity->enable()); + $this->assertTrue($this->entity->delete()); + } +} + +// ElggEntity is an abstract class with no abstact methods. +class ElggEntityTest extends ElggEntity { + public function __construct() { + $this->initialise_attributes(); + } + + public function load($guid) { + return parent::load($guid); + } + + public function expose_attributes() { + return $this->attributes; + } + + public function expose_metadata() { + return $this->temp_metadata; + } +} |