ElggEntityTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * This requires elgg_get_logged_in_user_guid() in session.php, the access
  4. * constants defined in entities.php, and elgg_normalize_url() in output.php
  5. */
  6. class ElggEntityTest extends \PHPUnit_Framework_TestCase {
  7. /** @var \ElggEntity */
  8. protected $obj;
  9. protected function setUp() {
  10. _elgg_services()->setValue('session', \ElggSession::getMock());
  11. $this->obj = $this->getMockForAbstractClass('\ElggEntity');
  12. $reflection = new ReflectionClass('\ElggEntity');
  13. $method = $reflection->getMethod('initializeAttributes');
  14. if (method_exists($method, 'setAccessible')) {
  15. $method->setAccessible(true);
  16. $method->invokeArgs($this->obj, array());
  17. }
  18. }
  19. public function testDefaultAttributes() {
  20. $this->assertEquals(null, $this->obj->guid);
  21. $this->assertEquals(null, $this->obj->type);
  22. $this->assertEquals(null, $this->obj->subtype);
  23. $this->assertEquals(elgg_get_logged_in_user_guid(), $this->obj->owner_guid);
  24. $this->assertEquals(elgg_get_logged_in_user_guid(), $this->obj->container_guid);
  25. $this->assertEquals(null, $this->obj->site_guid);
  26. $this->assertEquals(ACCESS_PRIVATE, $this->obj->access_id);
  27. $this->assertEquals(null, $this->obj->time_created);
  28. $this->assertEquals(null, $this->obj->time_updated);
  29. $this->assertEquals(null, $this->obj->last_action);
  30. $this->assertEquals('yes', $this->obj->enabled);
  31. }
  32. public function testSettingAndGettingAttribute() {
  33. // Note: before save() subtype returns string, int after
  34. // see https://github.com/Elgg/Elgg/issues/5920#issuecomment-25246298
  35. $this->obj->subtype = 'foo';
  36. $this->assertEquals('foo', $this->obj->subtype);
  37. }
  38. public function testSettingIntegerAttributes() {
  39. foreach (array('access_id', 'owner_guid', 'container_guid') as $name) {
  40. $this->obj->$name = '77';
  41. $this->assertSame(77, $this->obj->$name);
  42. }
  43. }
  44. public function testSettingUnsettableAttributes() {
  45. foreach (array('guid', 'time_updated', 'last_action') as $name) {
  46. $this->obj->$name = 'foo';
  47. $this->assertNotEquals('foo', $this->obj->$name);
  48. }
  49. }
  50. public function testSettingMetadataNoDatabase() {
  51. $this->obj->foo = 'test';
  52. $this->assertEquals('test', $this->obj->foo);
  53. $this->obj->foo = 'overwrite';
  54. $this->assertEquals('overwrite', $this->obj->foo);
  55. }
  56. public function testGettingNonexistentMetadataNoDatabase() {
  57. $this->assertNull($this->obj->foo);
  58. }
  59. public function testSimpleGetters() {
  60. $this->obj->type = 'foo';
  61. $this->obj->subtype = 'subtype';
  62. $this->obj->owner_guid = 77;
  63. $this->obj->access_id = 2;
  64. $this->obj->time_created = 123456789;
  65. $this->assertEquals($this->obj->getGUID(), $this->obj->guid );
  66. $this->assertEquals($this->obj->getType(), $this->obj->type );
  67. // Note: before save() subtype returns string, int after
  68. // see https://github.com/Elgg/Elgg/issues/5920#issuecomment-25246298
  69. $this->assertEquals($this->obj->getSubtype(), $this->obj->subtype );
  70. $this->assertEquals($this->obj->getOwnerGUID(), $this->obj->owner_guid );
  71. $this->assertEquals($this->obj->getAccessID(), $this->obj->access_id );
  72. $this->assertEquals($this->obj->getTimeCreated(), $this->obj->time_created );
  73. $this->assertEquals($this->obj->getTimeUpdated(), $this->obj->time_updated );
  74. }
  75. public function testUnsetAttribute() {
  76. $this->obj->access_id = 2;
  77. unset($this->obj->access_id);
  78. $this->assertEquals('', $this->obj->access_id);
  79. }
  80. /**
  81. * @expectedException InvalidParameterException
  82. */
  83. public function testSaveWithoutType() {
  84. $db = $this->getMock('\Elgg\Database',
  85. array('getData', 'getTablePrefix', 'sanitizeString'),
  86. array(),
  87. '',
  88. false
  89. );
  90. $db->expects($this->any())
  91. ->method('sanitizeString')
  92. ->will($this->returnArgument(0));
  93. _elgg_services()->setValue('db', $db);
  94. // requires type to be set
  95. $this->obj->save();
  96. }
  97. public function testIsEnabled() {
  98. $this->assertTrue($this->obj->isEnabled());
  99. }
  100. public function testDisableBeforeSaved() {
  101. // false on disable because it's not saved yet.
  102. $this->assertFalse($this->obj->disable());
  103. }
  104. public function testToObject() {
  105. $keys = array(
  106. 'guid',
  107. 'type',
  108. 'subtype',
  109. 'time_created',
  110. 'time_updated',
  111. 'container_guid',
  112. 'owner_guid',
  113. 'site_guid',
  114. 'url',
  115. 'read_access',
  116. );
  117. sort($keys);
  118. $object = $this->obj->toObject();
  119. $object_keys = array_keys(get_object_vars($object));
  120. sort($object_keys);
  121. $this->assertEquals($keys, $object_keys);
  122. }
  123. public function testLatLong() {
  124. // Coordinates for Elgg, Switzerland
  125. $lat = 47.483333;
  126. $long = 8.866667;
  127. $this->obj->setLatLong($lat, $long);
  128. $this->assertEquals($this->obj->getLatitude(), $lat);
  129. $this->assertEquals($this->obj->getLongitude(), $long);
  130. }
  131. }