ElggObjectTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * Elgg Test \ElggObject
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggCoreObjectTest extends \ElggCoreUnitTest {
  9. /**
  10. * Called before each test object.
  11. */
  12. public function __construct() {
  13. parent::__construct();
  14. }
  15. /**
  16. * Called before each test method.
  17. */
  18. public function setUp() {
  19. $this->entity = new \ElggObjectTest();
  20. }
  21. /**
  22. * Called after each test method.
  23. */
  24. public function tearDown() {
  25. unset($this->entity);
  26. }
  27. /**
  28. * Called after each test object.
  29. */
  30. public function __destruct() {
  31. parent::__destruct();
  32. }
  33. public function testElggObjectConstructor() {
  34. $attributes = array();
  35. $attributes['guid'] = null;
  36. $attributes['type'] = 'object';
  37. $attributes['subtype'] = null;
  38. $attributes['owner_guid'] = elgg_get_logged_in_user_guid();
  39. $attributes['container_guid'] = elgg_get_logged_in_user_guid();
  40. $attributes['site_guid'] = null;
  41. $attributes['access_id'] = ACCESS_PRIVATE;
  42. $attributes['time_created'] = null;
  43. $attributes['time_updated'] = null;
  44. $attributes['last_action'] = null;
  45. $attributes['enabled'] = 'yes';
  46. $attributes['title'] = null;
  47. $attributes['description'] = null;
  48. ksort($attributes);
  49. $entity_attributes = $this->entity->expose_attributes();
  50. ksort($entity_attributes);
  51. $this->assertIdentical($entity_attributes, $attributes);
  52. }
  53. public function testElggObjectSave() {
  54. // new object
  55. $this->AssertEqual($this->entity->getGUID(), 0);
  56. $guid = $this->entity->save();
  57. $this->AssertNotEqual($guid, 0);
  58. $entity_row = $this->get_entity_row($guid);
  59. $this->assertIsA($entity_row, '\stdClass');
  60. // update existing object
  61. $this->entity->title = 'testing';
  62. $this->entity->description = '\ElggObject';
  63. $this->assertEqual($this->entity->save(), $guid);
  64. $object_row = $this->get_object_row($guid);
  65. $this->assertIsA($object_row, '\stdClass');
  66. $this->assertIdentical($object_row->title, 'testing');
  67. $this->assertIdentical($object_row->description, '\ElggObject');
  68. // clean up
  69. $this->entity->delete();
  70. }
  71. public function testElggConstructorWithGarbage() {
  72. try {
  73. $error = new \ElggObjectTest('garbage');
  74. $this->assertTrue(false);
  75. } catch (Exception $e) {
  76. $this->assertIsA($e, 'InvalidParameterException');
  77. }
  78. }
  79. public function testElggObjectClone() {
  80. $this->entity->title = 'testing';
  81. $this->entity->description = '\ElggObject';
  82. $this->entity->var1 = "test";
  83. $this->entity->var2 = 1;
  84. $this->entity->var3 = true;
  85. $this->entity->save();
  86. // add tag array
  87. $tag_string = 'tag1, tag2, tag3';
  88. $tagarray = string_to_tag_array($tag_string);
  89. $this->entity->tags = $tagarray;
  90. // a cloned \ElggEntity has the guid reset
  91. $object = clone $this->entity;
  92. $this->assertIdentical(0, (int)$object->guid);
  93. // make sure attributes were copied over
  94. $this->assertIdentical($object->title, 'testing');
  95. $this->assertIdentical($object->description, '\ElggObject');
  96. $guid = $object->save();
  97. $this->assertTrue($guid !== 0);
  98. $this->assertTrue($guid !== $this->entity->guid);
  99. // test that metadata was transfered
  100. $this->assertIdentical($this->entity->var1, $object->var1);
  101. $this->assertIdentical($this->entity->var2, $object->var2);
  102. $this->assertIdentical($this->entity->var3, $object->var3);
  103. $this->assertIdentical($this->entity->tags, $object->tags);
  104. // clean up
  105. $object->delete();
  106. $this->entity->delete();
  107. }
  108. public function testElggObjectContainer() {
  109. $this->assertEqual($this->entity->getContainerGUID(), elgg_get_logged_in_user_guid());
  110. // create and save to group
  111. $group = new \ElggGroup();
  112. $guid = $group->save();
  113. $this->assertTrue($this->entity->setContainerGUID($guid));
  114. // check container
  115. $this->assertEqual($this->entity->getContainerGUID(), $guid);
  116. $this->assertIdenticalEntities($group, $this->entity->getContainerEntity());
  117. // clean up
  118. $group->delete();
  119. }
  120. public function testElggObjectToObject() {
  121. $keys = array(
  122. 'guid',
  123. 'type',
  124. 'subtype',
  125. 'time_created',
  126. 'time_updated',
  127. 'container_guid',
  128. 'owner_guid',
  129. 'site_guid',
  130. 'url',
  131. 'read_access',
  132. 'title',
  133. 'description',
  134. 'tags',
  135. );
  136. $object = $this->entity->toObject();
  137. $object_keys = array_keys(get_object_vars($object));
  138. sort($keys);
  139. sort($object_keys);
  140. $this->assertIdentical($keys, $object_keys);
  141. }
  142. public function xtestElggObjectAccessOverrides() {
  143. // set entity to private access with no owner.
  144. $entity = $this->entity;
  145. $entity->access_id = ACCESS_PRIVATE;
  146. $entity->owner_guid = 0;
  147. $this->assertTrue($entity->save());
  148. $guid = $entity->getGUID();
  149. var_dump($guid);
  150. // try to grab entity
  151. $entity = false;
  152. $entity = get_entity($guid);
  153. var_dump($entity);
  154. $this->assertFalse($entity);
  155. $old = elgg_set_ignore_access(true);
  156. }
  157. // see https://github.com/elgg/elgg/issues/1196
  158. public function testElggEntityRecursiveDisableWhenLoggedOut() {
  159. $e1 = new \ElggObject();
  160. $e1->access_id = ACCESS_PUBLIC;
  161. $e1->owner_guid = 0;
  162. $e1->container_guid = 0;
  163. $e1->save();
  164. $guid1 = $e1->getGUID();
  165. $e2 = new \ElggObject();
  166. $e2->container_guid = $guid1;
  167. $e2->access_id = ACCESS_PUBLIC;
  168. $e2->owner_guid = 0;
  169. $e2->save();
  170. $guid2 = $e2->getGUID();
  171. // fake being logged out
  172. $user = $_SESSION['user'];
  173. unset($_SESSION['user']);
  174. $ia = elgg_set_ignore_access(true);
  175. $this->assertTrue($e1->disable(null, true));
  176. // "log in" original user
  177. $_SESSION['user'] = $user;
  178. elgg_set_ignore_access($ia);
  179. $this->assertFalse(get_entity($guid1));
  180. $this->assertFalse(get_entity($guid2));
  181. $db_prefix = get_config('dbprefix');
  182. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $guid1";
  183. $r = get_data_row($q);
  184. $this->assertEqual('no', $r->enabled);
  185. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $guid2";
  186. $r = get_data_row($q);
  187. $this->assertEqual('no', $r->enabled);
  188. access_show_hidden_entities(true);
  189. $e1->delete();
  190. $e2->delete();
  191. access_show_hidden_entities(false);
  192. }
  193. public function testElggRecursiveDelete() {
  194. $types = array('\ElggGroup', '\ElggObject', '\ElggUser', '\ElggSite');
  195. $db_prefix = elgg_get_config('dbprefix');
  196. foreach ($types as $type) {
  197. $parent = new $type();
  198. $this->assertTrue($parent->save());
  199. $child = new \ElggObject();
  200. $child->container_guid = $parent->guid;
  201. $this->assertTrue($child->save());
  202. $grandchild = new \ElggObject();
  203. $grandchild->container_guid = $child->guid;
  204. $this->assertTrue($grandchild->save());
  205. $this->assertTrue($parent->delete(true));
  206. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $parent->guid";
  207. $r = get_data($q);
  208. $this->assertFalse($r);
  209. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $child->guid";
  210. $r = get_data($q);
  211. $this->assertFalse($r);
  212. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $grandchild->guid";
  213. $r = get_data($q);
  214. $this->assertFalse($r);
  215. }
  216. // object that owns itself
  217. // can't check container_guid because of infinite loops in can_edit_entity()
  218. $obj = new \ElggObject();
  219. $obj->save();
  220. $obj->owner_guid = $obj->guid;
  221. $obj->save();
  222. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $obj->guid";
  223. $r = get_data_row($q);
  224. $this->assertEqual($obj->guid, $r->owner_guid);
  225. $this->assertTrue($obj->delete(true));
  226. $q = "SELECT * FROM {$db_prefix}entities WHERE guid = $obj->guid";
  227. $r = get_data_row($q);
  228. $this->assertFalse($r);
  229. }
  230. protected function get_object_row($guid) {
  231. global $CONFIG;
  232. return get_data_row("SELECT * FROM {$CONFIG->dbprefix}objects_entity WHERE guid='$guid'");
  233. }
  234. protected function get_entity_row($guid) {
  235. global $CONFIG;
  236. return get_data_row("SELECT * FROM {$CONFIG->dbprefix}entities WHERE guid='$guid'");
  237. }
  238. }
  239. class ElggObjectTest extends \ElggObject {
  240. public function expose_attributes() {
  241. return $this->attributes;
  242. }
  243. }