diff options
Diffstat (limited to 'engine/tests/objects')
| -rw-r--r-- | engine/tests/objects/entities.php | 423 | ||||
| -rw-r--r-- | engine/tests/objects/filestore.php | 100 | ||||
| -rw-r--r-- | engine/tests/objects/objects.php | 306 | ||||
| -rw-r--r-- | engine/tests/objects/sites.php | 77 | ||||
| -rw-r--r-- | engine/tests/objects/users.php | 250 | 
5 files changed, 1156 insertions, 0 deletions
| diff --git a/engine/tests/objects/entities.php b/engine/tests/objects/entities.php new file mode 100644 index 000000000..bac72079e --- /dev/null +++ b/engine/tests/objects/entities.php @@ -0,0 +1,423 @@ +<?php +/** + * Elgg Test ElggEntities + * + * @package Elgg + * @subpackage Test + */ +class ElggCoreEntityTest extends ElggCoreUnitTest { +	/** +	 * Called before each test method. +	 */ +	public function setUp() { +		$this->entity = new ElggEntityTest(); +	} + +	/** +	 * Called after each test method. +	 */ +	public function tearDown() { +		$this->swallowErrors(); +		unset($this->entity); +	} + +	/** +	 * Tests the protected attributes +	 */ +	public function testElggEntityAttributes() { +		$test_attributes = array(); +		$test_attributes['guid'] = NULL; +		$test_attributes['type'] = NULL; +		$test_attributes['subtype'] = NULL; +		$test_attributes['owner_guid'] = elgg_get_logged_in_user_guid(); +		$test_attributes['container_guid'] = elgg_get_logged_in_user_guid(); +		$test_attributes['site_guid'] = NULL; +		$test_attributes['access_id'] = ACCESS_PRIVATE; +		$test_attributes['time_created'] = NULL; +		$test_attributes['time_updated'] = NULL; +		$test_attributes['last_action'] = NULL; +		$test_attributes['enabled'] = 'yes'; +		$test_attributes['tables_split'] = 1; +		$test_attributes['tables_loaded'] = 0; +		ksort($test_attributes); + +		$entity_attributes = $this->entity->expose_attributes(); +		ksort($entity_attributes); + +		$this->assertIdentical($entity_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->getOwnerGUID(), $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->entity->existent = 'testing'; +		$this->assertIdentical($this->entity->existent, 'testing'); + +		// check metadata set +		$this->assertTrue(isset($this->entity->existent)); +		$this->assertIdentical($this->entity->getMetaData('existent'), 'testing'); + +		// check internal metadata array +		$metadata = $this->entity->expose_metadata(); +		$this->assertIdentical($metadata['existent'], array('testing')); +	} + +	public function testElggEnityGetAndSetAnnotations() { +		$this->assertFalse(array_key_exists('non_existent', $this->entity->expose_annotations())); +		$this->assertIdentical($this->entity->getAnnotations('non_existent'), array()); + +		// set and check temp annotation +		$this->assertTrue($this->entity->annotate('non_existent', 'testing')); +		$this->assertIdentical($this->entity->getAnnotations('non_existent'), array('testing')); +		$this->assertTrue(array_key_exists('non_existent', $this->entity->expose_annotations())); + +		// save entity and check for annotation +		$this->entity->subtype = 'testing'; +		$this->save_entity(); +		$this->assertFalse(array_key_exists('non_existent', $this->entity->expose_annotations())); +		$annotations = $this->entity->getAnnotations('non_existent'); +		$this->assertIsA($annotations[0], 'ElggAnnotation'); +		$this->assertIdentical($annotations[0]->name, 'non_existent'); +		$this->assertEqual($this->entity->countAnnotations('non_existent'), 1); + +		$this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID()))); +		$this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'site'))); +		$this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'site', 'subtype' => 'testing'))); +		$this->assertIdentical(FALSE, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'site', 'subtype' => 'fail'))); + +		//  clear annotation +		$this->assertTrue($this->entity->deleteAnnotations()); +		$this->assertEqual($this->entity->countAnnotations('non_existent'), 0); + +		$this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID()))); +		$this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'site'))); +		$this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'site', 'subtype' => 'testing'))); + +		// clean up +		$this->assertTrue($this->entity->delete()); +		remove_subtype('site', 'testing'); +	} + +	public function testElggEntityCache() { +		global $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(), elgg_echo('InvalidParameterException:EntityTypeNotSet')); +		} + +		// set elements +		$this->entity->type = 'site'; +		$this->entity->non_existent = 'testing'; + +		// save +		$this->AssertEqual($this->entity->getGUID(), 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->assertIdentical(true, $this->entity->delete()); +	} + +	public function testElggEntityDisableAndEnable() { +		global $CONFIG; + +		// ensure enabled +		$this->assertTrue($this->entity->isEnabled()); + +		// false on disable because it's not saved yet. +		$this->assertFalse($this->entity->disable()); + +		// save and disable +		$this->save_entity(); + +		// add annotations and metadata to check if they're disabled. +		$annotation_id = create_annotation($this->entity->guid, 'test_annotation_' . rand(), 'test_value_' . rand()); +		$metadata_id = create_metadata($this->entity->guid, 'test_metadata_' . rand(), 'test_value_' . rand()); + +		$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'); + +		$annotation = get_data_row("SELECT * FROM {$CONFIG->dbprefix}annotations WHERE id = '$annotation_id'"); +		$this->assertIdentical($annotation->enabled, 'no'); + +		$metadata = get_data_row("SELECT * FROM {$CONFIG->dbprefix}metadata WHERE id = '$metadata_id'"); +		$this->assertIdentical($metadata->enabled, 'no'); + +		// re-enable for deletion to work +		$this->assertTrue($this->entity->enable()); + +		// check enabled +		// check annotations and metadata enabled. +		$entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$this->entity->guid}'"); +		$this->assertIdentical($entity->enabled, 'yes'); + +		$annotation = get_data_row("SELECT * FROM {$CONFIG->dbprefix}annotations WHERE id = '$annotation_id'"); +		$this->assertIdentical($annotation->enabled, 'yes'); + +		$metadata = get_data_row("SELECT * FROM {$CONFIG->dbprefix}metadata WHERE id = '$metadata_id'"); +		$this->assertIdentical($metadata->enabled, 'yes'); + +		$this->assertTrue($this->entity->delete()); +	} + +	public function testElggEntityRecursiveDisableAndEnable() { +		global $CONFIG; + +		$this->save_entity(); +		$obj1 = new ElggObject(); +		$obj1->container_guid = $this->entity->getGUID(); +		$obj1->save(); +		$obj2 = new ElggObject(); +		$obj2->container_guid = $this->entity->getGUID(); +		$obj2->save(); + +		// disable $obj2 before disabling the container +		$this->assertTrue($obj2->disable()); + +		// disable entities container by $this->entity +		$this->assertTrue($this->entity->disable()); +		$entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj1->guid}'"); +		$this->assertIdentical($entity->enabled, 'no'); + +		// enable entities that were disabled with the container (but not $obj2) +		$this->assertTrue($this->entity->enable()); +		$entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj1->guid}'"); +		$this->assertIdentical($entity->enabled, 'yes'); +		$entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj2->guid}'"); +		$this->assertIdentical($entity->enabled, 'no'); + +		// cleanup +		$this->assertTrue($obj2->enable()); +		$this->assertTrue($obj2->delete()); +		$this->assertTrue($obj1->delete()); +		$this->assertTrue($this->entity->delete()); +	} + +	public function testElggEntityMetadata() { +		// let's delete a non-existent metadata +		$this->assertFalse($this->entity->deleteMetadata('important')); + +		// let's add the metadata +		$this->entity->important = 'indeed!'; +		$this->assertIdentical('indeed!', $this->entity->important); +		$this->entity->less_important = 'true, too!'; +		$this->assertIdentical('true, too!', $this->entity->less_important); +		$this->save_entity(); + +		// test deleting incorrectly +		// @link https://github.com/elgg/elgg/issues/2273 +		$this->assertNull($this->entity->deleteMetadata('impotent')); +		$this->assertEqual($this->entity->important, 'indeed!'); + +		// get rid of one metadata +		$this->assertEqual($this->entity->important, 'indeed!'); +		$this->assertTrue($this->entity->deleteMetadata('important')); +		$this->assertNull($this->entity->important); + +		// get rid of all metadata +		$this->assertTrue($this->entity->deleteMetadata()); +		$this->assertNull($this->entity->less_important); + +		// clean up database +		$this->assertTrue($this->entity->delete()); +	} + +	public function testElggEntityExportables() { +		$exportables = array( +			'guid', +			'type', +			'subtype', +			'time_created', +			'time_updated', +			'container_guid', +			'owner_guid', +			'site_guid' +		); + +		$this->assertIdentical($exportables, $this->entity->getExportableValues()); +	} + +	public function testElggEntityMultipleMetadata() { +		foreach (array(false, true) as $save) { +			if ($save) { +				$this->save_entity(); +			} +			$md = array('brett', 'bryan', 'brad'); +			$name = 'test_md_' . rand(); + +			$this->entity->$name = $md; + +			$this->assertEqual($md, $this->entity->$name); + +			if ($save) { +				$this->assertTrue($this->entity->delete()); +			} +		} +	} + +	public function testElggEntitySingleElementArrayMetadata() { +		foreach (array(false, true) as $save) { +			if ($save) { +				$this->save_entity(); +			} +			$md = array('test'); +			$name = 'test_md_' . rand(); + +			$this->entity->$name = $md; + +			$this->assertEqual($md[0], $this->entity->$name); + +			if ($save) { +				$this->assertTrue($this->entity->delete()); +			} +		} +	} + +	public function testElggEntityAppendMetadata() { +		foreach (array(false, true) as $save) { +			if ($save) { +				$this->save_entity(); +			} +			$md = 'test'; +			$name = 'test_md_' . rand(); + +			$this->entity->$name = $md; +			$this->entity->setMetaData($name, 'test2', '', true); + +			$this->assertEqual(array('test', 'test2'), $this->entity->$name); + +			if ($save) { +				$this->assertTrue($this->entity->delete()); +			} +		} +	} + +	public function testElggEntitySingleElementArrayAppendMetadata() { +		foreach (array(false, true) as $save) { +			if ($save) { +				$this->save_entity(); +			} +			$md = 'test'; +			$name = 'test_md_' . rand(); + +			$this->entity->$name = $md; +			$this->entity->setMetaData($name, array('test2'), '', true); + +			$this->assertEqual(array('test', 'test2'), $this->entity->$name); + +			if ($save) { +				$this->assertTrue($this->entity->delete()); +			} +		} +	} + +	public function testElggEntityArrayAppendMetadata() { +		foreach (array(false, true) as $save) { +			if ($save) { +				$this->save_entity(); +			} +			$md = array('brett', 'bryan', 'brad'); +			$md2 = array('test1', 'test2', 'test3'); +			$name = 'test_md_' . rand(); + +			$this->entity->$name = $md; +			$this->entity->setMetaData($name, $md2, '', true); + +			$this->assertEqual(array_merge($md, $md2), $this->entity->$name); + +			if ($save) { +				$this->assertTrue($this->entity->delete()); +			} +		} +	} + +	protected function save_entity($type='site') { +		$this->entity->type = $type; +		$this->assertNotEqual($this->entity->save(), 0); +	} +} + +// ElggEntity is an abstract class with no abstact methods. +class ElggEntityTest extends ElggEntity { +	public function __construct() { +		$this->initializeAttributes(); +	} + +	public function expose_attributes() { +		return $this->attributes; +	} + +	public function expose_metadata() { +		return $this->temp_metadata; +	} + +	public function expose_annotations() { +		return $this->temp_annotations; +	} +} diff --git a/engine/tests/objects/filestore.php b/engine/tests/objects/filestore.php new file mode 100644 index 000000000..9732f0af4 --- /dev/null +++ b/engine/tests/objects/filestore.php @@ -0,0 +1,100 @@ +<?php +/** + * Elgg Test Skeleton + * + * @package Elgg + * @subpackage Test + */ +class ElggCoreFilestoreTest 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->filestore = new ElggDiskFilestoreTest(); +	} + +	/** +	 * Called after each test method. +	 */ +	public function tearDown() { +		// do not allow SimpleTest to interpret Elgg notices as exceptions +		$this->swallowErrors(); +		 +		unset($this->filestore); +	} + +	/** +	 * Called after each test object. +	 */ +	public function __destruct() { +		// all code should go above here +		parent::__destruct(); +	} + +	public function testFileMatrix() { +		global $CONFIG; +		 +		// create a test user +		$user = $this->createTestUser(); +		$created = date('Y/m/d', $user->time_created); +		 +		// check matrix with guid +		$guid_dir = $this->filestore->makeFileMatrix($user->guid); +		$this->assertIdentical($guid_dir, "$created/$user->guid/"); +		 +		// clean up user +		$user->delete(); +	} +	 +	public function testFilenameOnFilestore() { +		global $CONFIG; +		 +		// create a user to own the file +		$user = $this->createTestUser(); +		$created = date('Y/m/d', $user->time_created); +		 +		// setup a test file +		$file = new ElggFile(); +		$file->owner_guid = $user->guid; +		$file->setFilename('testing/filestore.txt'); +		$file->open('write'); +		$file->write('Testing!'); +		$this->assertTrue($file->close()); +		 +		// ensure filename and path is expected +		$filename = $file->getFilenameOnFilestore($file); +		$filepath = "$CONFIG->dataroot$created/$user->guid/testing/filestore.txt"; +		$this->assertIdentical($filename, $filepath); +		$this->assertTrue(file_exists($filepath)); +		 +		// ensure file removed on user delete +		$user->delete(); +		$this->assertFalse(file_exists($filepath)); +	} + + +	protected function createTestUser($username = 'fileTest') { +		$user = new ElggUser(); +		$user->username = $username; +		$guid = $user->save(); +		 +		// load user to have access to creation time +		return get_entity($guid); +	} +} + +class ElggDiskFilestoreTest extends ElggDiskFilestore { +	public function makeFileMatrix($guid) { +		return parent::makeFileMatrix($guid); +	} +} diff --git a/engine/tests/objects/objects.php b/engine/tests/objects/objects.php new file mode 100644 index 000000000..263ab2414 --- /dev/null +++ b/engine/tests/objects/objects.php @@ -0,0 +1,306 @@ +<?php +/** + * Elgg Test ElggObject + * + * @package Elgg + * @subpackage Test + */ +class ElggCoreObjectTest extends ElggCoreUnitTest { + +	/** +	 * Called before each test object. +	 */ +	public function __construct() { +		parent::__construct(); +	} + +	/** +	 * Called before each test method. +	 */ +	public function setUp() { +		$this->entity = new ElggObjectTest(); +	} + +	/** +	 * Called after each test method. +	 */ +	public function tearDown() { +		$this->swallowErrors(); +		unset($this->entity); +	} + +	/** +	 * Called after each test object. +	 */ +	public function __destruct() { +		parent::__destruct(); +	} + +	public function testElggObjectConstructor() { +		$attributes = array(); +		$attributes['guid'] = NULL; +		$attributes['type'] = 'object'; +		$attributes['subtype'] = NULL; +		$attributes['owner_guid'] = elgg_get_logged_in_user_guid(); +		$attributes['container_guid'] = elgg_get_logged_in_user_guid(); +		$attributes['site_guid'] = NULL; +		$attributes['access_id'] = ACCESS_PRIVATE; +		$attributes['time_created'] = NULL; +		$attributes['time_updated'] = NULL; +		$attributes['last_action'] = NULL; +		$attributes['enabled'] = 'yes'; +		$attributes['tables_split'] = 2; +		$attributes['tables_loaded'] = 0; +		$attributes['title'] = NULL; +		$attributes['description'] = NULL; +		ksort($attributes); + +		$entity_attributes = $this->entity->expose_attributes(); +		ksort($entity_attributes); + +		$this->assertIdentical($entity_attributes, $attributes); +	} + +	public function testElggObjectSave() { +		// new object +		$this->AssertEqual($this->entity->getGUID(), 0); +		$guid = $this->entity->save(); +		$this->AssertNotEqual($guid, 0); + +		$entity_row = $this->get_entity_row($guid); +		$this->assertIsA($entity_row, 'stdClass'); + +		// update existing object +		$this->entity->title = 'testing'; +		$this->entity->description = 'ElggObject'; +		$this->assertEqual($this->entity->save(), $guid); + +		$object_row = $this->get_object_row($guid); +		$this->assertIsA($object_row, 'stdClass'); +		$this->assertIdentical($object_row->title, 'testing'); +		$this->assertIdentical($object_row->description, 'ElggObject'); + +		// clean up +		$this->entity->delete(); +	} + +	public function testElggObjectLoad() { +		// fail on wrong type +		try { +			$error = new ElggObjectTest(elgg_get_logged_in_user_guid()); +			$this->assertTrue(FALSE); +		} catch (Exception $e) { +			$this->assertIsA($e, 'InvalidClassException'); +			$message = sprintf(elgg_echo('InvalidClassException:NotValidElggStar'), elgg_get_logged_in_user_guid(), 'ElggObject'); +			$this->assertIdentical($e->getMessage(), $message); +		} +	} + +	public function testElggObjectConstructorByGUID() { +		$guid = $this->entity->save(); + +		// load a new object using guid +		$entity = new ElggObjectTest($guid); +		$this->assertIdentical($this->entity, $entity); + +		// clean up +		$this->entity->delete(); +	} + +	public function testElggObjectClone() { +		$this->entity->title = 'testing'; +		$this->entity->description = 'ElggObject'; +		$this->entity->var1 = "test"; +		$this->entity->var2 = 1; +		$this->entity->var3 = true; +		$this->entity->save(); + +		// add tag array +		$tag_string = 'tag1, tag2, tag3'; +		$tagarray = string_to_tag_array($tag_string); +		$this->entity->tags = $tagarray; + +		// a cloned ElggEntity has the guid reset +		$object = clone $this->entity; +		$this->assertIdentical(0, (int)$object->guid); + +		// make sure attributes were copied over +		$this->assertIdentical($object->title, 'testing'); +		$this->assertIdentical($object->description, 'ElggObject'); + +		$guid = $object->save(); +		$this->assertTrue($guid !== 0); +		$this->assertTrue($guid !== $this->entity->guid); + +		// test that metadata was transfered +		$this->assertIdentical($this->entity->var1, $object->var1); +		$this->assertIdentical($this->entity->var2, $object->var2); +		$this->assertIdentical($this->entity->var3, $object->var3); +		$this->assertIdentical($this->entity->tags, $object->tags); + +		// clean up +		$object->delete(); +		$this->entity->delete(); +	} + +	public function testElggObjectContainer() { +		$this->assertEqual($this->entity->getContainerGUID(), elgg_get_logged_in_user_guid()); + +		// create and save to group +		$group = new ElggGroup(); +		$guid = $group->save(); +		$this->assertTrue($this->entity->setContainerGUID($guid)); + +		// check container +		$this->assertEqual($this->entity->getContainerGUID(), $guid); +		$this->assertIdentical($group, $this->entity->getContainerEntity()); + +		// clean up +		$group->delete(); +	} + +	public function testElggObjectExportables() { +		$exportables = array( +			'guid', +			'type', +			'subtype', +			'time_created', +			'time_updated', +			'container_guid', +			'owner_guid', +			'site_guid', +			'title', +			'description' +		); + +		$this->assertIdentical($exportables, $this->entity->getExportableValues()); +	} + +	public function xtestElggObjectAccessOverrides() { +		// set entity to private access with no owner. +		$entity = $this->entity; +		$entity->access_id = ACCESS_PRIVATE; +		$entity->owner_guid = 0; +		$this->assertTrue($entity->save()); +		$guid = $entity->getGUID(); + +		var_dump($guid); +		// try to grab entity +		$entity = false; +		$entity = get_entity($guid); +		var_dump($entity); +		$this->assertFalse($entity); + +		$old = elgg_set_ignore_access(true); +	} + +	// see https://github.com/elgg/elgg/issues/1196 +	public function testElggEntityRecursiveDisableWhenLoggedOut() { +		$e1 = new ElggObject(); +		$e1->access_id = ACCESS_PUBLIC; +		$e1->owner_guid = 0; +		$e1->container_guid = 0; +		$e1->save(); +		$guid1 = $e1->getGUID(); + +		$e2 = new ElggObject(); +		$e2->container_guid = $guid1; +		$e2->access_id = ACCESS_PUBLIC; +		$e2->owner_guid = 0; +		$e2->save(); +		$guid2 = $e2->getGUID(); + +		// fake being logged out +		$user = $_SESSION['user']; +		unset($_SESSION['user']); +		$ia = elgg_set_ignore_access(true); + +		$this->assertTrue(disable_entity($guid1, null, true)); + +		// "log in" original user +		$_SESSION['user'] = $user; +		elgg_set_ignore_access($ia); + +		$this->assertFalse(get_entity($guid1)); +		$this->assertFalse(get_entity($guid2)); + +		$db_prefix = get_config('dbprefix'); +		$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $guid1"; +		$r = get_data_row($q); +		$this->assertEqual('no', $r->enabled); + +		$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $guid2"; +		$r = get_data_row($q); +		$this->assertEqual('no', $r->enabled); + +		access_show_hidden_entities(true); +		delete_entity($guid1); +		delete_entity($guid2); +		access_show_hidden_entities(false); +	} + +	public function testElggRecursiveDelete() { +		$types = array('ElggGroup', 'ElggObject', 'ElggUser', 'ElggSite'); +		$db_prefix = elgg_get_config('dbprefix'); + +		foreach ($types as $type) { +			$parent = new $type(); +			$this->assertTrue($parent->save()); +			 +			$child = new ElggObject(); +			$child->container_guid = $parent->guid; +			$this->assertTrue($child->save()); + +			$grandchild = new ElggObject(); +			$grandchild->container_guid = $child->guid; +			$this->assertTrue($grandchild->save()); + +			$this->assertTrue($parent->delete(true)); + +			$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $parent->guid"; +			$r = get_data($q); +			$this->assertFalse($r); + +			$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $child->guid"; +			$r = get_data($q); +			$this->assertFalse($r); + +			$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $grandchild->guid"; +			$r = get_data($q); +			$this->assertFalse($r); +		} + +		// object that owns itself +		// can't check container_guid because of infinite loops in can_edit_entity() +		$obj = new ElggObject(); +		$obj->save(); +		$obj->owner_guid = $obj->guid; +		$obj->save(); + +		$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $obj->guid"; +		$r = get_data_row($q); +		$this->assertEqual($obj->guid, $r->owner_guid); + +		$this->assertTrue($obj->delete(true)); + +		$q = "SELECT * FROM {$db_prefix}entities WHERE guid = $obj->guid"; +		$r = get_data_row($q); +		$this->assertFalse($r); +	} + +	protected function get_object_row($guid) { +		global $CONFIG; +		return get_data_row("SELECT * FROM {$CONFIG->dbprefix}objects_entity WHERE guid='$guid'"); +	} + +	protected function get_entity_row($guid) { +		global $CONFIG; +		return get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid='$guid'"); +	} +} + +class ElggObjectTest extends ElggObject { +	public function expose_attributes() { +		return $this->attributes; +	} +} diff --git a/engine/tests/objects/sites.php b/engine/tests/objects/sites.php new file mode 100644 index 000000000..a01a661e3 --- /dev/null +++ b/engine/tests/objects/sites.php @@ -0,0 +1,77 @@ +<?php +/** + * Elgg Test ElggSite + * + * @package Elgg + * @subpackage Test + */ +class ElggCoreSiteTest extends ElggCoreUnitTest { + +	/** +	 * Called before each test object. +	 */ +	public function __construct() { +		parent::__construct(); +	} + +	/** +	 * Called before each test method. +	 */ +	public function setUp() { +		$this->site = new ElggSiteTest(); +	} + +	/** +	 * Called after each test method. +	 */ +	public function tearDown() { +		$this->swallowErrors(); +		unset($this->site); +	} + +	/** +	 * Called after each test object. +	 */ +	public function __destruct() { +		parent::__destruct(); +	} + +	public function testElggSiteConstructor() { +		$attributes = array(); +		$attributes['guid'] = NULL; +		$attributes['type'] = 'site'; +		$attributes['subtype'] = NULL; +		$attributes['owner_guid'] = elgg_get_logged_in_user_guid(); +		$attributes['container_guid'] = elgg_get_logged_in_user_guid(); +		$attributes['site_guid'] = NULL; +		$attributes['access_id'] = ACCESS_PRIVATE; +		$attributes['time_created'] = NULL; +		$attributes['time_updated'] = NULL; +		$attributes['last_action'] = NULL; +		$attributes['enabled'] = 'yes'; +		$attributes['tables_split'] = 2; +		$attributes['tables_loaded'] = 0; +		$attributes['name'] = NULL; +		$attributes['description'] = NULL; +		$attributes['url'] = NULL; +		ksort($attributes); + +		$entity_attributes = $this->site->expose_attributes(); +		ksort($entity_attributes); + +		$this->assertIdentical($entity_attributes, $attributes); +	} + +	public function testElggSiteSaveAndDelete() { +		$guid = $this->site->save(); +		$this->assertIsA($guid, 'int'); +		$this->assertTrue($guid > 0); +		$this->assertIdentical(true, $this->site->delete()); +	} +} + +class ElggSiteTest extends ElggSite { +	public function expose_attributes() { +		return $this->attributes; +	} +} diff --git a/engine/tests/objects/users.php b/engine/tests/objects/users.php new file mode 100644 index 000000000..8a1033ac4 --- /dev/null +++ b/engine/tests/objects/users.php @@ -0,0 +1,250 @@ +<?php +/** + * Elgg Test ElggUser + * + * @package Elgg + * @subpackage Test + */ +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(); +	} + +	public function testElggUserConstructor() { +		$attributes = array(); +		$attributes['guid'] = NULL; +		$attributes['type'] = 'user'; +		$attributes['subtype'] = NULL; +		$attributes['owner_guid'] = elgg_get_logged_in_user_guid(); +		$attributes['container_guid'] = elgg_get_logged_in_user_guid(); +		$attributes['site_guid'] = NULL; +		$attributes['access_id'] = ACCESS_PRIVATE; +		$attributes['time_created'] = NULL; +		$attributes['time_updated'] = NULL; +		$attributes['last_action'] = NULL; +		$attributes['enabled'] = 'yes'; +		$attributes['tables_split'] = 2; +		$attributes['tables_loaded'] = 0; +		$attributes['name'] = NULL; +		$attributes['username'] = NULL; +		$attributes['password'] = NULL; +		$attributes['salt'] = NULL; +		$attributes['email'] = NULL; +		$attributes['language'] = NULL; +		$attributes['code'] = NULL; +		$attributes['banned'] = 'no'; +		$attributes['admin'] = 'no'; +		$attributes['prev_last_action'] = NULL; +		$attributes['last_login'] = NULL; +		$attributes['prev_last_login'] = NULL; +		ksort($attributes); + +		$entity_attributes = $this->user->expose_attributes(); +		ksort($entity_attributes); + +		$this->assertIdentical($entity_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(elgg_get_logged_in_user_guid()); +		$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(elgg_get_logged_in_user_guid()); +		$user = new ElggUser($row); +		$this->assertIdentical($user, $_SESSION['user']); +	} + +	public function testElggUserConstructorByUsername() { +		$row = $this->fetchUser(elgg_get_logged_in_user_guid()); +		$user = new ElggUser($row->username); +		$this->assertIdentical($user, $_SESSION['user']); +	} + +	public function testElggUserSave() { +		// new object +		$this->AssertEqual($this->user->getGUID(), 0); +		$guid = $this->user->save(); +		$this->AssertNotEqual($guid, 0); + +		// clean up +		$this->user->delete(); +	} + +	public function testElggUserDelete() { +		$guid = $this->user->save(); + +		// delete object +		$this->assertIdentical(true, $this->user->delete()); + +		// check GUID not in database +		$this->assertFalse($this->fetchUser($guid)); +	} + +	public function testElggUserNameCache() { +		// issue https://github.com/elgg/elgg/issues/1305 + +		// very unlikely a user would have this username +		$name = (string)time(); +		$this->user->username = $name; + +		$guid = $this->user->save(); + +		$user = get_user_by_username($name); +		$user->delete(); +		$user = get_user_by_username($name); +		$this->assertFalse($user); +	} + +	public function testGetUserByUsernameAcceptsUrlEncoded() { +		$username = (string)time(); +		$this->user->username = $username; +		$guid = $this->user->save(); + +		// percent encode first letter +		$first_letter = $username[0]; +		$first_letter = str_pad('%' . dechex(ord($first_letter)), 2, '0', STR_PAD_LEFT); +		$username =   $first_letter . substr($username, 1); + +		$user = get_user_by_username($username); +		$this->assertTrue((bool) $user); +		$this->assertEqual($guid, $user->guid); + +		$this->user->delete(); +	} + +	public function testElggUserMakeAdmin() { +		global $CONFIG; + +		// need to save user to have a guid +		$guid = $this->user->save(); + +		$this->assertTrue($this->user->makeAdmin()); + +		$q = "SELECT admin FROM {$CONFIG->dbprefix}users_entity WHERE guid = $guid"; +		$r = mysql_query($q); + +		$admin = mysql_fetch_assoc($r); +		$this->assertEqual($admin['admin'], 'yes'); + +		$this->user->delete(); +	} + +	public function testElggUserRemoveAdmin() { +		global $CONFIG; + +		// need to save user to have a guid +		$guid = $this->user->save(); + +		$this->assertTrue($this->user->removeAdmin()); + +		$q = "SELECT admin FROM {$CONFIG->dbprefix}users_entity WHERE guid = $guid"; +		$r = mysql_query($q); + +		$admin = mysql_fetch_assoc($r); +		$this->assertEqual($admin['admin'], 'no'); + +		$this->user->delete(); +	} + +	public function testElggUserIsAdmin() { +		// need to grab a real user with a guid and everything. +		$guid = $this->user->save(); + +		$this->assertTrue($this->user->makeAdmin()); + +		// this is testing the function, not the SQL. +		// that's been tested above. +		$this->assertTrue($this->user->isAdmin()); + +		$this->user->delete(); +	} + +	public function testElggUserIsNotAdmin() { +		// need to grab a real user with a guid and everything. +		$guid = $this->user->save(); + +		$this->assertTrue($this->user->removeAdmin()); + +		// this is testing the function, not the SQL. +		// that's been tested above. +		$this->assertFalse($this->user->isAdmin()); + +		$this->user->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; +	} +} | 
