ElggEntityTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. /**
  3. * Test \ElggEntity
  4. *
  5. */
  6. class ElggCoreEntityTest extends \ElggCoreUnitTest {
  7. /**
  8. * @var \ElggEntity
  9. */
  10. protected $entity;
  11. /**
  12. * Called before each test method.
  13. */
  14. public function setUp() {
  15. // use \ElggObject since \ElggEntity is an abstract class
  16. $this->entity = new \ElggObject();
  17. $this->entity->subtype = 'elgg_entity_test_subtype';
  18. $this->entity->save();
  19. }
  20. /**
  21. * Called after each test method.
  22. */
  23. public function tearDown() {
  24. if ($this->entity) {
  25. $this->entity->delete();
  26. }
  27. }
  28. public function __destruct() {
  29. parent::__destruct();
  30. remove_subtype('object', 'elgg_entity_test_subtype');
  31. }
  32. public function testSubtypePropertyReads() {
  33. $this->assertTrue($this->entity->save());
  34. $guid = $this->entity->guid;
  35. $subtype_prop = $this->entity->subtype;
  36. $this->assertIsA($subtype_prop, 'int');
  37. $this->assertEqual($subtype_prop, get_subtype_id('object', 'elgg_entity_test_subtype'));
  38. _elgg_invalidate_cache_for_entity($guid);
  39. $this->entity = null;
  40. $this->entity = get_entity($guid);
  41. $subtype_prop = $this->entity->subtype;
  42. $this->assertIsA($subtype_prop, 'int');
  43. $this->assertEqual($subtype_prop, get_subtype_id('object', 'elgg_entity_test_subtype'));
  44. }
  45. public function testGetSubtype() {
  46. $guid = $this->entity->guid;
  47. $this->assertEqual($this->entity->getSubtype(), 'elgg_entity_test_subtype');
  48. _elgg_invalidate_cache_for_entity($guid);
  49. $this->entity = null;
  50. $this->entity = get_entity($guid);
  51. $this->assertEqual($this->entity->getSubtype(), 'elgg_entity_test_subtype');
  52. }
  53. public function testSubtypeAddRemove() {
  54. $test_subtype = 'test_1389988642';
  55. $object = new \ElggObject();
  56. $object->subtype = $test_subtype;
  57. $object->save();
  58. $this->assertTrue(is_numeric(get_subtype_id('object', $test_subtype)));
  59. $object->delete();
  60. remove_subtype('object', $test_subtype);
  61. $this->assertFalse(get_subtype_id('object', $test_subtype));
  62. }
  63. public function testElggEntityGetAndSetAnnotations() {
  64. $this->assertIdentical($this->entity->getAnnotations(array('annotation_name' => 'non_existent')), array());
  65. // save entity and check for annotation
  66. $this->entity->annotate('non_existent', 'foo');
  67. $annotations = $this->entity->getAnnotations(array('annotation_name' => 'non_existent'));
  68. $this->assertIsA($annotations[0], '\ElggAnnotation');
  69. $this->assertIdentical($annotations[0]->name, 'non_existent');
  70. $this->assertEqual($this->entity->countAnnotations('non_existent'), 1);
  71. // @todo belongs in Annotations API test class
  72. $this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID())));
  73. $this->assertIdentical($annotations, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'object')));
  74. $this->assertIdentical(false, elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'object', 'subtype' => 'fail')));
  75. // clear annotation
  76. $this->assertTrue($this->entity->deleteAnnotations());
  77. $this->assertEqual($this->entity->countAnnotations('non_existent'), 0);
  78. // @todo belongs in Annotations API test class
  79. $this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID())));
  80. $this->assertIdentical(array(), elgg_get_annotations(array('guid' => $this->entity->getGUID(), 'type' => 'object')));
  81. }
  82. public function testElggEntitySaveAndDelete() {
  83. // check attributes populated during create()
  84. $time_minimum = time() - 5;
  85. $this->assertTrue($this->entity->time_created > $time_minimum);
  86. $this->assertTrue($this->entity->time_updated > $time_minimum);
  87. $this->assertEqual($this->entity->site_guid, elgg_get_site_entity()->guid);
  88. $this->assertEqual($this->entity->container_guid, elgg_get_logged_in_user_guid());
  89. }
  90. public function testElggEntityDisableAndEnable() {
  91. global $CONFIG;
  92. // add annotations and metadata to check if they're disabled.
  93. $annotation_id = create_annotation($this->entity->guid, 'test_annotation_' . rand(), 'test_value_' . rand());
  94. $metadata_id = create_metadata($this->entity->guid, 'test_metadata_' . rand(), 'test_value_' . rand());
  95. $this->assertTrue($this->entity->disable());
  96. // ensure disabled by comparing directly with database
  97. $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$this->entity->guid}'");
  98. $this->assertIdentical($entity->enabled, 'no');
  99. $annotation = get_data_row("SELECT * FROM {$CONFIG->dbprefix}annotations WHERE id = '$annotation_id'");
  100. $this->assertIdentical($annotation->enabled, 'no');
  101. $metadata = get_data_row("SELECT * FROM {$CONFIG->dbprefix}metadata WHERE id = '$metadata_id'");
  102. $this->assertIdentical($metadata->enabled, 'no');
  103. // re-enable for deletion to work
  104. $this->assertTrue($this->entity->enable());
  105. // check enabled
  106. // check annotations and metadata enabled.
  107. $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$this->entity->guid}'");
  108. $this->assertIdentical($entity->enabled, 'yes');
  109. $annotation = get_data_row("SELECT * FROM {$CONFIG->dbprefix}annotations WHERE id = '$annotation_id'");
  110. $this->assertIdentical($annotation->enabled, 'yes');
  111. $metadata = get_data_row("SELECT * FROM {$CONFIG->dbprefix}metadata WHERE id = '$metadata_id'");
  112. $this->assertIdentical($metadata->enabled, 'yes');
  113. $this->assertTrue($this->entity->delete());
  114. $this->entity = null;
  115. }
  116. public function testElggEntityRecursiveDisableAndEnable() {
  117. global $CONFIG;
  118. $obj1 = new \ElggObject();
  119. $obj1->container_guid = $this->entity->getGUID();
  120. $obj1->save();
  121. $obj2 = new \ElggObject();
  122. $obj2->container_guid = $this->entity->getGUID();
  123. $obj2->save();
  124. // disable $obj2 before disabling the container
  125. $this->assertTrue($obj2->disable());
  126. // disable entities container by $this->entity
  127. $this->assertTrue($this->entity->disable());
  128. $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj1->guid}'");
  129. $this->assertIdentical($entity->enabled, 'no');
  130. // enable entities that were disabled with the container (but not $obj2)
  131. $this->assertTrue($this->entity->enable());
  132. $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj1->guid}'");
  133. $this->assertIdentical($entity->enabled, 'yes');
  134. $entity = get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid = '{$obj2->guid}'");
  135. $this->assertIdentical($entity->enabled, 'no');
  136. // cleanup
  137. $this->assertTrue($obj2->enable());
  138. $this->assertTrue($obj2->delete());
  139. $this->assertTrue($obj1->delete());
  140. }
  141. public function testElggEntityMetadata() {
  142. // let's delete a non-existent metadata
  143. $this->assertFalse($this->entity->deleteMetadata('important'));
  144. // let's add the metadata
  145. $this->entity->important = 'indeed!';
  146. $this->assertIdentical('indeed!', $this->entity->important);
  147. $this->entity->less_important = 'true, too!';
  148. $this->assertIdentical('true, too!', $this->entity->less_important);
  149. // test deleting incorrectly
  150. // @link https://github.com/elgg/elgg/issues/2273
  151. $this->assertNull($this->entity->deleteMetadata('impotent'));
  152. $this->assertEqual($this->entity->important, 'indeed!');
  153. // get rid of one metadata
  154. $this->assertEqual($this->entity->important, 'indeed!');
  155. $this->assertTrue($this->entity->deleteMetadata('important'));
  156. $this->assertNull($this->entity->important);
  157. // get rid of all metadata
  158. $this->assertTrue($this->entity->deleteMetadata());
  159. $this->assertNull($this->entity->less_important);
  160. }
  161. public function testElggEntityMultipleMetadata() {
  162. foreach (array($this->entity, new \ElggObject()) as $obj) {
  163. $md = array('brett', 'bryan', 'brad');
  164. $name = 'test_md_' . rand();
  165. $obj->$name = $md;
  166. $this->assertEqual($md, $obj->$name);
  167. }
  168. }
  169. public function testElggEntitySingleElementArrayMetadata() {
  170. foreach (array($this->entity, new \ElggObject()) as $obj) {
  171. $md = array('test');
  172. $name = 'test_md_' . rand();
  173. $obj->$name = $md;
  174. $this->assertEqual($md[0], $obj->$name);
  175. }
  176. }
  177. public function testElggEntityAppendMetadata() {
  178. foreach (array($this->entity, new \ElggObject()) as $obj) {
  179. $md = 'test';
  180. $name = 'test_md_' . rand();
  181. $obj->$name = $md;
  182. $obj->setMetadata($name, 'test2', '', true);
  183. $this->assertEqual(array('test', 'test2'), $obj->$name);
  184. }
  185. }
  186. public function testElggEntitySingleElementArrayAppendMetadata() {
  187. foreach (array($this->entity, new \ElggObject()) as $obj) {
  188. $md = 'test';
  189. $name = 'test_md_' . rand();
  190. $obj->$name = $md;
  191. $obj->setMetadata($name, array('test2'), '', true);
  192. $this->assertEqual(array('test', 'test2'), $obj->$name);
  193. }
  194. }
  195. public function testElggEntityArrayAppendMetadata() {
  196. foreach (array($this->entity, new \ElggObject()) as $obj) {
  197. $md = array('brett', 'bryan', 'brad');
  198. $md2 = array('test1', 'test2', 'test3');
  199. $name = 'test_md_' . rand();
  200. $obj->$name = $md;
  201. $obj->setMetadata($name, $md2, '', true);
  202. $this->assertEqual(array_merge($md, $md2), $obj->$name);
  203. }
  204. }
  205. public function testElggEntityGetIconURL() {
  206. elgg_register_plugin_hook_handler('entity:icon:url', 'object', function ($hook, $type, $url, $params) {
  207. $size = (string) elgg_extract('size', $params);
  208. return "$size.jpg";
  209. }, 99999);
  210. $obj = new \ElggObject();
  211. $obj->save();
  212. // Test default size
  213. $this->assertEqual($obj->getIconURL(), elgg_normalize_url('medium.jpg'));
  214. // Test size
  215. $this->assertEqual($obj->getIconURL('small'), elgg_normalize_url('small.jpg'));
  216. // Test mixed params
  217. $this->assertEqual($obj->getIconURL('small'), $obj->getIconURL(array('size' => 'small')));
  218. // Test bad param
  219. $this->assertEqual($obj->getIconURL(new \stdClass), elgg_normalize_url('medium.jpg'));
  220. }
  221. public function testCanAnnotateDefault() {
  222. $object = new \ElggObject();
  223. $object->subtype = 'test_1389988642';
  224. $object->save();
  225. $this->assertTrue($object->canAnnotate());
  226. $user = elgg_get_logged_in_user_entity();
  227. elgg_get_session()->removeLoggedInUser();
  228. $this->assertFalse($object->canAnnotate());
  229. elgg_get_session()->setLoggedInUser($user);
  230. $object->delete();
  231. }
  232. public function testCanAnnotateCallsSpecificThenGenericHook() {
  233. $object = new \ElggObject();
  234. $object->subtype = 'test_1389988642';
  235. $object->save();
  236. elgg_register_plugin_hook_handler('permissions_check:annotate:foo', 'object', 'Elgg\Values::getFalse');
  237. $this->assertFalse($object->canAnnotate(0, 'foo'));
  238. // overrides
  239. elgg_register_plugin_hook_handler('permissions_check:annotate', 'object', 'Elgg\Values::getTrue');
  240. $this->assertTrue($object->canAnnotate());
  241. elgg_unregister_plugin_hook_handler('permissions_check:annotate:foo', 'object', 'Elgg\Values::getFalse');
  242. elgg_unregister_plugin_hook_handler('permissions_check:annotate', 'object', 'Elgg\Values::getTrue');
  243. $object->delete();
  244. }
  245. public function testCanAnnotateHookParams() {
  246. $object = new \ElggObject();
  247. $object->subtype = 'test_1389988642';
  248. $object->save();
  249. $call_params = [];
  250. $handler = function ($h, $t, $v, $p) use (&$call_params) {
  251. $call_params[] = $p;
  252. };
  253. elgg_register_plugin_hook_handler('permissions_check:annotate:foo', 'object', $handler);
  254. elgg_register_plugin_hook_handler('permissions_check:annotate', 'object', $handler);
  255. $object->canAnnotate(0, 'foo');
  256. $this->assertSame($call_params[0]['user']->guid, elgg_get_logged_in_user_guid());
  257. $this->assertSame($call_params[1]['user']->guid, elgg_get_logged_in_user_guid());
  258. $this->assertSame($call_params[0]['entity'], $object);
  259. $this->assertSame($call_params[1]['entity'], $object);
  260. $this->assertEqual($call_params[0]['annotation_name'], 'foo');
  261. $this->assertEqual($call_params[1]['annotation_name'], 'foo');
  262. elgg_unregister_plugin_hook_handler('permissions_check:annotate:foo', 'object', $handler);
  263. elgg_unregister_plugin_hook_handler('permissions_check:annotate', 'object', $handler);
  264. $object->delete();
  265. }
  266. public function testCanAnnotateDoesntCallSpecificThenGenericHookForEmptyString() {
  267. $object = new \ElggObject();
  268. $object->subtype = 'test_1389988642';
  269. $object->save();
  270. elgg_register_plugin_hook_handler('permissions_check:annotate:', 'object', 'Elgg\Values::getFalse');
  271. $this->assertTrue($object->canAnnotate());
  272. elgg_unregister_plugin_hook_handler('permissions_check:annotate:', 'object', 'Elgg\Values::getFalse');
  273. $object->delete();
  274. }
  275. public function testCreateWithContainerGuidEqualsZero() {
  276. $user = new \ElggUser();
  277. $user->save();
  278. $object = new \ElggObject();
  279. $object->owner_guid = $user->guid;
  280. $object->container_guid = 0;
  281. // If container_guid attribute is not updated with owner_guid attribute
  282. // ElggEntity::getContainerEntity() would return false
  283. // thus terminating save()
  284. $this->assertTrue($object->save());
  285. $this->assertEqual($user->guid, $object->getContainerGUID());
  286. $user->delete();
  287. }
  288. }