diff options
Diffstat (limited to 'engine/tests/objects/entities.php')
| -rw-r--r-- | engine/tests/objects/entities.php | 232 |
1 files changed, 192 insertions, 40 deletions
diff --git a/engine/tests/objects/entities.php b/engine/tests/objects/entities.php index 56c8f7947..bac72079e 100644 --- a/engine/tests/objects/entities.php +++ b/engine/tests/objects/entities.php @@ -29,8 +29,8 @@ class ElggCoreEntityTest extends ElggCoreUnitTest { $test_attributes['guid'] = NULL; $test_attributes['type'] = NULL; $test_attributes['subtype'] = NULL; - $test_attributes['owner_guid'] = get_loggedin_userid(); - $test_attributes['container_guid'] = get_loggedin_userid(); + $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; @@ -89,25 +89,25 @@ class ElggCoreEntityTest extends ElggCoreUnitTest { $this->assertFalse(isset($this->entity->non_existent)); // create metadata - $this->assertTrue($this->entity->non_existent = 'testing'); + $this->entity->existent = 'testing'; + $this->assertIdentical($this->entity->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'); + $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['non_existent'], 'testing'); + $this->assertIdentical($metadata['existent'], array('testing')); } public function testElggEnityGetAndSetAnnotations() { $this->assertFalse(array_key_exists('non_existent', $this->entity->expose_annotations())); - $this->assertFalse($this->entity->getAnnotations('non_existent')); + $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'), '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 @@ -118,22 +118,23 @@ class ElggCoreEntityTest extends ElggCoreUnitTest { $this->assertIsA($annotations[0], 'ElggAnnotation'); $this->assertIdentical($annotations[0]->name, 'non_existent'); $this->assertEqual($this->entity->countAnnotations('non_existent'), 1); - - $this->assertIdentical($annotations, get_annotations($this->entity->getGUID())); - $this->assertIdentical($annotations, get_annotations($this->entity->getGUID(), 'site')); - $this->assertIdentical($annotations, get_annotations($this->entity->getGUID(), 'site', 'testing')); - $this->assertIdentical(FALSE, get_annotations($this->entity->getGUID(), 'site', 'fail')); + + $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->clearAnnotations()); + $this->assertTrue($this->entity->deleteAnnotations()); $this->assertEqual($this->entity->countAnnotations('non_existent'), 0); - - $this->assertIdentical(FALSE, get_annotations($this->entity->getGUID())); - $this->assertIdentical(FALSE, get_annotations($this->entity->getGUID(), 'site')); - $this->assertIdentical(FALSE, get_annotations($this->entity->getGUID(), 'site', 'testing')); + + $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() { @@ -177,7 +178,7 @@ class ElggCoreEntityTest extends ElggCoreUnitTest { $this->AssertEqual($this->entity->get('non_existent'), 'testing'); // clean up with delete - $this->assertTrue($this->entity->delete()); + $this->assertIdentical(true, $this->entity->delete()); } public function testElggEntityDisableAndEnable() { @@ -186,45 +187,103 @@ class ElggCoreEntityTest extends ElggCoreUnitTest { // ensure enabled $this->assertTrue($this->entity->isEnabled()); - // false on disable + // 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 delte a non-existent metadata - $this->assertFalse($this->entity->clearMetaData('important')); - - // let's add the meatadata - $this->assertTrue($this->entity->important = 'indeed!'); - $this->assertTrue($this->entity->less_important = 'true, too!'); + // 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 http://trac.elgg.org/ticket/2273 - $this->assertFalse($this->entity->clearMetaData('impotent')); + // @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->clearMetaData('important')); - $this->assertEqual($this->entity->important, ''); - + $this->assertTrue($this->entity->deleteMetadata('important')); + $this->assertNull($this->entity->important); + // get rid of all metadata - $this->assertTrue($this->entity->clearMetaData()); - $this->assertEqual($this->entity->less_important, ''); - + $this->assertTrue($this->entity->deleteMetadata()); + $this->assertNull($this->entity->less_important); + // clean up database $this->assertTrue($this->entity->delete()); } @@ -244,8 +303,101 @@ class ElggCoreEntityTest extends ElggCoreUnitTest { $this->assertIdentical($exportables, $this->entity->getExportableValues()); } - protected function save_entity($type='site') - { + 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); } |
