| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 | 
							- <?php
 
- /**
 
-  * Test \ElggEntity
 
-  *
 
-  */
 
- class ElggCoreEntityTest extends \ElggCoreUnitTest {
 
- 	/**
 
- 	 * @var \ElggEntity
 
- 	 */
 
- 	protected $entity;
 
- 	/**
 
- 	 * Called before each test method.
 
- 	 */
 
- 	public function setUp() {
 
- 		// use \ElggObject since \ElggEntity is an abstract class
 
- 		$this->entity = new \ElggObject();
 
- 		$this->entity->subtype = 'elgg_entity_test_subtype';
 
- 		$this->entity->save();
 
- 	}
 
- 	/**
 
- 	 * Called after each test method.
 
- 	 */
 
- 	public function tearDown() {
 
- 		if ($this->entity) {
 
- 			$this->entity->delete();
 
- 		}
 
- 	}
 
- 	public function __destruct() {
 
- 		parent::__destruct();
 
- 		remove_subtype('object', 'elgg_entity_test_subtype');
 
- 	}
 
- 	public function testSubtypePropertyReads() {
 
- 		$this->assertTrue($this->entity->save());
 
- 		$guid = $this->entity->guid;
 
- 		$subtype_prop = $this->entity->subtype;
 
- 		$this->assertIsA($subtype_prop, 'int');
 
- 		$this->assertEqual($subtype_prop, get_subtype_id('object', 'elgg_entity_test_subtype'));
 
- 		_elgg_invalidate_cache_for_entity($guid);
 
- 		$this->entity = null;
 
- 		$this->entity = get_entity($guid);
 
- 		$subtype_prop = $this->entity->subtype;
 
- 		$this->assertIsA($subtype_prop, 'int');
 
- 		$this->assertEqual($subtype_prop, get_subtype_id('object', 'elgg_entity_test_subtype'));
 
- 	}
 
- 	public function testGetSubtype() {
 
- 		$guid = $this->entity->guid;
 
- 		$this->assertEqual($this->entity->getSubtype(), 'elgg_entity_test_subtype');
 
- 		_elgg_invalidate_cache_for_entity($guid);
 
- 		$this->entity = null;
 
- 		$this->entity = get_entity($guid);
 
- 		$this->assertEqual($this->entity->getSubtype(), 'elgg_entity_test_subtype');
 
- 	}
 
- 	public function testSubtypeAddRemove() {
 
- 		$test_subtype = 'test_1389988642';
 
- 		$object = new \ElggObject();
 
- 		$object->subtype = $test_subtype;
 
- 		$object->save();
 
- 		$this->assertTrue(is_numeric(get_subtype_id('object', $test_subtype)));
 
- 		$object->delete();
 
- 		remove_subtype('object', $test_subtype);
 
- 		$this->assertFalse(get_subtype_id('object', $test_subtype));
 
- 	}
 
- 	public function testElggEntityGetAndSetAnnotations() {
 
- 		$this->assertIdentical($this->entity->getAnnotations(array('annotation_name' => 'non_existent')), array());
 
- 		// save entity and check for annotation
 
- 		$this->entity->annotate('non_existent', 'foo');
 
- 		$annotations = $this->entity->getAnnotations(array('annotation_name' => 'non_existent'));
 
- 		$this->assertIsA($annotations[0], '\ElggAnnotation');
 
- 		$this->assertIdentical($annotations[0]->name, 'non_existent');
 
- 		$this->assertEqual($this->entity->countAnnotations('non_existent'), 1);
 
- 		// @todo belongs in Annotations API test class
 
- 		$this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID())));
 
- 		$this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'object')));
 
- 		$this->assertIdentical(false, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'object', 'subtype' => 'fail')));
 
- 		//  clear annotation
 
- 		$this->assertTrue($this->entity->deleteAnnotations());
 
- 		$this->assertEqual($this->entity->countAnnotations('non_existent'), 0);
 
- 		// @todo belongs in Annotations API test class
 
- 		$this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID())));
 
- 		$this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'object')));
 
- 	}
 
- 	public function testElggEntitySaveAndDelete() {
 
- 		// check attributes populated during create()
 
- 		$time_minimum = time() - 5;
 
- 		$this->assertTrue($this->entity->time_created > $time_minimum);
 
- 		$this->assertTrue($this->entity->time_updated > $time_minimum);
 
- 		$this->assertEqual($this->entity->site_guid, elgg_get_site_entity()->guid);
 
- 		$this->assertEqual($this->entity->container_guid, elgg_get_logged_in_user_guid());
 
- 	}
 
- 	public function testElggEntityDisableAndEnable() {
 
- 		global $CONFIG;
 
- 		// 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());
 
- 		$this->entity = null;
 
- 	}
 
- 	public function testElggEntityRecursiveDisableAndEnable() {
 
- 		global $CONFIG;
 
- 		$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());
 
- 	}
 
- 	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);
 
- 		// 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);
 
- 	}
 
- 	public function testElggEntityMultipleMetadata() {
 
- 		foreach (array($this->entity, new \ElggObject()) as $obj) {
 
- 			$md = array('brett', 'bryan', 'brad');
 
- 			$name = 'test_md_' . rand();
 
- 			$obj->$name = $md;
 
- 			$this->assertEqual($md, $obj->$name);
 
- 		}
 
- 	}
 
- 	public function testElggEntitySingleElementArrayMetadata() {
 
- 		foreach (array($this->entity, new \ElggObject()) as $obj) {
 
- 			$md = array('test');
 
- 			$name = 'test_md_' . rand();
 
- 			$obj->$name = $md;
 
- 			$this->assertEqual($md[0], $obj->$name);
 
- 		}
 
- 	}
 
- 	public function testElggEntityAppendMetadata() {
 
- 		foreach (array($this->entity, new \ElggObject()) as $obj) {
 
- 			$md = 'test';
 
- 			$name = 'test_md_' . rand();
 
- 			$obj->$name = $md;
 
- 			$obj->setMetadata($name, 'test2', '', true);
 
- 			$this->assertEqual(array('test', 'test2'), $obj->$name);
 
- 		}
 
- 	}
 
- 	public function testElggEntitySingleElementArrayAppendMetadata() {
 
- 		foreach (array($this->entity, new \ElggObject()) as $obj) {
 
- 			$md = 'test';
 
- 			$name = 'test_md_' . rand();
 
- 			$obj->$name = $md;
 
- 			$obj->setMetadata($name, array('test2'), '', true);
 
- 			$this->assertEqual(array('test', 'test2'), $obj->$name);
 
- 		}
 
- 	}
 
- 	public function testElggEntityArrayAppendMetadata() {
 
- 		foreach (array($this->entity, new \ElggObject()) as $obj) {
 
- 			$md = array('brett', 'bryan', 'brad');
 
- 			$md2 = array('test1', 'test2', 'test3');
 
- 			$name = 'test_md_' . rand();
 
- 			$obj->$name = $md;
 
- 			$obj->setMetadata($name, $md2, '', true);
 
- 			$this->assertEqual(array_merge($md, $md2), $obj->$name);
 
- 		}
 
- 	}
 
- 	public function testElggEntityGetIconURL() {
 
- 		elgg_register_plugin_hook_handler('entity:icon:url', 'object', function ($hook, $type, $url, $params) {
 
- 			$size = (string) elgg_extract('size', $params);
 
- 			return "$size.jpg";
 
- 		}, 99999);
 
- 		$obj = new \ElggObject();
 
- 		$obj->save();
 
- 		// Test default size
 
- 		$this->assertEqual($obj->getIconURL(), elgg_normalize_url('medium.jpg'));
 
- 		// Test size
 
- 		$this->assertEqual($obj->getIconURL('small'), elgg_normalize_url('small.jpg'));
 
- 		// Test mixed params
 
- 		$this->assertEqual($obj->getIconURL('small'), $obj->getIconURL(array('size' => 'small')));
 
- 		// Test bad param
 
- 		$this->assertEqual($obj->getIconURL(new \stdClass), elgg_normalize_url('medium.jpg'));
 
- 	}
 
- 	public function testCanAnnotateDefault() {
 
- 		$object = new \ElggObject();
 
- 		$object->subtype = 'test_1389988642';
 
- 		$object->save();
 
- 		$this->assertTrue($object->canAnnotate());
 
- 		$user = elgg_get_logged_in_user_entity();
 
- 		elgg_get_session()->removeLoggedInUser();
 
- 		$this->assertFalse($object->canAnnotate());
 
- 		elgg_get_session()->setLoggedInUser($user);
 
- 		$object->delete();
 
- 	}
 
- 	public function testCanAnnotateCallsSpecificThenGenericHook() {
 
- 		$object = new \ElggObject();
 
- 		$object->subtype = 'test_1389988642';
 
- 		$object->save();
 
- 		elgg_register_plugin_hook_handler('permissions_check:annotate:foo', 'object', 'Elgg\Values::getFalse');
 
- 		$this->assertFalse($object->canAnnotate(0, 'foo'));
 
- 		// overrides
 
- 		elgg_register_plugin_hook_handler('permissions_check:annotate', 'object', 'Elgg\Values::getTrue');
 
- 		$this->assertTrue($object->canAnnotate());
 
- 		elgg_unregister_plugin_hook_handler('permissions_check:annotate:foo', 'object', 'Elgg\Values::getFalse');
 
- 		elgg_unregister_plugin_hook_handler('permissions_check:annotate', 'object', 'Elgg\Values::getTrue');
 
- 		$object->delete();
 
- 	}
 
- 	public function testCanAnnotateHookParams() {
 
- 		$object = new \ElggObject();
 
- 		$object->subtype = 'test_1389988642';
 
- 		$object->save();
 
- 		$call_params = [];
 
- 		$handler = function ($h, $t, $v, $p) use (&$call_params) {
 
- 			$call_params[] = $p;
 
- 		};
 
- 		elgg_register_plugin_hook_handler('permissions_check:annotate:foo', 'object', $handler);
 
- 		elgg_register_plugin_hook_handler('permissions_check:annotate', 'object', $handler);
 
- 		$object->canAnnotate(0, 'foo');
 
- 		$this->assertSame($call_params[0]['user']->guid, elgg_get_logged_in_user_guid());
 
- 		$this->assertSame($call_params[1]['user']->guid, elgg_get_logged_in_user_guid());
 
- 		$this->assertSame($call_params[0]['entity'], $object);
 
- 		$this->assertSame($call_params[1]['entity'], $object);
 
- 		$this->assertEqual($call_params[0]['annotation_name'], 'foo');
 
- 		$this->assertEqual($call_params[1]['annotation_name'], 'foo');
 
- 		elgg_unregister_plugin_hook_handler('permissions_check:annotate:foo', 'object', $handler);
 
- 		elgg_unregister_plugin_hook_handler('permissions_check:annotate', 'object', $handler);
 
- 		$object->delete();
 
- 	}
 
- 	public function testCanAnnotateDoesntCallSpecificThenGenericHookForEmptyString() {
 
- 		$object = new \ElggObject();
 
- 		$object->subtype = 'test_1389988642';
 
- 		$object->save();
 
- 		elgg_register_plugin_hook_handler('permissions_check:annotate:', 'object', 'Elgg\Values::getFalse');
 
- 		$this->assertTrue($object->canAnnotate());
 
- 		elgg_unregister_plugin_hook_handler('permissions_check:annotate:', 'object', 'Elgg\Values::getFalse');
 
- 		$object->delete();
 
- 	}
 
- 	public function testCreateWithContainerGuidEqualsZero() {
 
- 		$user = new \ElggUser();
 
- 		$user->save();
 
- 		$object = new \ElggObject();
 
- 		$object->owner_guid = $user->guid;
 
- 		$object->container_guid = 0;
 
- 		// If container_guid attribute is not updated with owner_guid attribute
 
- 		// ElggEntity::getContainerEntity() would return false
 
- 		// thus terminating save()
 
- 		$this->assertTrue($object->save());
 
- 		$this->assertEqual($user->guid, $object->getContainerGUID());
 
- 		$user->delete();
 
- 	}
 
- }
 
 
  |