ElggCoreMetadataAPITest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Elgg Test metadata API
  4. *
  5. * @package Elgg
  6. * @subpackage Test
  7. */
  8. class ElggCoreMetadataAPITest extends \ElggCoreUnitTest {
  9. protected $metastrings;
  10. /**
  11. * Called before each test method.
  12. */
  13. public function setUp() {
  14. $this->metastrings = array();
  15. $this->object = new \ElggObject();
  16. }
  17. /**
  18. * Called after each test method.
  19. */
  20. public function tearDown() {
  21. unset($this->object);
  22. }
  23. public function testGetMetastringById() {
  24. foreach (array('metaUnitTest', 'metaunittest', 'METAUNITTEST') as $string) {
  25. // since there is no guarantee that metastrings are garbage collected
  26. // between unit test runs, we delete before testing
  27. $this->delete_metastrings($string);
  28. $this->create_metastring($string);
  29. }
  30. // lookup metastring id
  31. $cs_ids = elgg_get_metastring_id('metaUnitTest', true);
  32. $this->assertEqual($cs_ids, $this->metastrings['metaUnitTest']);
  33. // lookup all metastrings, ignoring case
  34. $cs_ids = elgg_get_metastring_id('metaUnitTest', false);
  35. $this->assertEqual(count($cs_ids), 3);
  36. $this->assertEqual(count($cs_ids), count($this->metastrings));
  37. foreach ($cs_ids as $string )
  38. {
  39. $this->assertTrue(in_array($string, $this->metastrings));
  40. }
  41. }
  42. public function testElggGetEntitiesFromMetadata() {
  43. global $CONFIG, $METASTRINGS_CACHE;
  44. $METASTRINGS_CACHE = array();
  45. $this->object->title = 'Meta Unit Test';
  46. $this->object->save();
  47. $this->create_metastring('metaUnitTest');
  48. $this->create_metastring('tested');
  49. // create_metadata returns id of metadata on success
  50. $this->assertNotEqual(false, create_metadata($this->object->guid, 'metaUnitTest', 'tested'));
  51. // check value with improper case
  52. $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'Tested', 'limit' => 10, 'metadata_case_sensitive' => true);
  53. $this->assertIdentical(array(), elgg_get_entities_from_metadata($options));
  54. // compare forced case with ignored case
  55. $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'tested', 'limit' => 10, 'metadata_case_sensitive' => true);
  56. $case_true = elgg_get_entities_from_metadata($options);
  57. $this->assertIsA($case_true, 'array');
  58. $options = array('metadata_names' => 'metaUnitTest', 'metadata_values' => 'Tested', 'limit' => 10, 'metadata_case_sensitive' => false);
  59. $case_false = elgg_get_entities_from_metadata($options);
  60. $this->assertIsA($case_false, 'array');
  61. $this->assertIdentical($case_true, $case_false);
  62. // clean up
  63. $this->object->delete();
  64. }
  65. public function testElggGetMetadataCount() {
  66. $this->object->title = 'Meta Unit Test';
  67. $this->object->save();
  68. $guid = $this->object->getGUID();
  69. create_metadata($guid, 'tested', 'tested1', 'text', 0, ACCESS_PUBLIC, true);
  70. create_metadata($guid, 'tested', 'tested2', 'text', 0, ACCESS_PUBLIC, true);
  71. $count = (int)elgg_get_metadata(array(
  72. 'metadata_names' => array('tested'),
  73. 'guid' => $guid,
  74. 'count' => true,
  75. ));
  76. $this->assertIdentical($count, 2);
  77. $this->object->delete();
  78. }
  79. public function testElggDeleteMetadata() {
  80. $e = new \ElggObject();
  81. $e->save();
  82. for ($i = 0; $i < 30; $i++) {
  83. $name = "test_metadata$i";
  84. $e->$name = rand(0, 10000);
  85. }
  86. $options = array(
  87. 'guid' => $e->getGUID(),
  88. 'limit' => 0,
  89. );
  90. $md = elgg_get_metadata($options);
  91. $this->assertIdentical(30, count($md));
  92. $this->assertTrue(elgg_delete_metadata($options));
  93. $md = elgg_get_metadata($options);
  94. $this->assertTrue(empty($md));
  95. $e->delete();
  96. }
  97. /**
  98. * https://github.com/Elgg/Elgg/issues/4867
  99. */
  100. public function testElggGetEntityMetadataWhereSqlWithFalseValue() {
  101. $pair = array('name' => 'test' , 'value' => false);
  102. $result = _elgg_get_entity_metadata_where_sql('e', 'metadata', null, null, $pair);
  103. $where = preg_replace( '/\s+/', ' ', $result['wheres'][0]);
  104. $this->assertTrue(strpos($where, "msn1.string = 'test' AND BINARY msv1.string = 0") > 0);
  105. $result = _elgg_get_entity_metadata_where_sql('e', 'metadata', array('test'), array(false));
  106. $where = preg_replace( '/\s+/', ' ', $result['wheres'][0]);
  107. $this->assertTrue(strpos($where, "msn.string IN ('test')) AND ( BINARY msv.string IN ('0')"));
  108. }
  109. // Make sure metadata with multiple values is correctly deleted when re-written
  110. // by another user
  111. // https://github.com/elgg/elgg/issues/2776
  112. public function test_elgg_metadata_multiple_values() {
  113. $u1 = new \ElggUser();
  114. $u1->username = rand();
  115. $u1->save();
  116. $u2 = new \ElggUser();
  117. $u2->username = rand();
  118. $u2->save();
  119. $obj = new \ElggObject();
  120. $obj->owner_guid = $u1->guid;
  121. $obj->container_guid = $u1->guid;
  122. $obj->access_id = ACCESS_PUBLIC;
  123. $obj->save();
  124. $md_values = array(
  125. 'one',
  126. 'two',
  127. 'three'
  128. );
  129. // need to fake different logins.
  130. // good times without mocking.
  131. $original_user = elgg_get_logged_in_user_entity();
  132. $_SESSION['user'] = $u1;
  133. elgg_set_ignore_access(false);
  134. // add metadata as one user
  135. $obj->test = $md_values;
  136. // check only these md exists
  137. $db_prefix = elgg_get_config('dbprefix');
  138. $q = "SELECT * FROM {$db_prefix}metadata WHERE entity_guid = $obj->guid";
  139. $data = get_data($q);
  140. $this->assertEqual(count($md_values), count($data));
  141. foreach ($data as $md_row) {
  142. $md = elgg_get_metadata_from_id($md_row->id);
  143. $this->assertTrue(in_array($md->value, $md_values));
  144. $this->assertEqual('test', $md->name);
  145. }
  146. // add md w/ same name as a different user
  147. $_SESSION['user'] = $u2;
  148. $md_values2 = array(
  149. 'four',
  150. 'five',
  151. 'six',
  152. 'seven'
  153. );
  154. $obj->test = $md_values2;
  155. $q = "SELECT * FROM {$db_prefix}metadata WHERE entity_guid = $obj->guid";
  156. $data = get_data($q);
  157. $this->assertEqual(count($md_values2), count($data));
  158. foreach ($data as $md_row) {
  159. $md = elgg_get_metadata_from_id($md_row->id);
  160. $this->assertTrue(in_array($md->value, $md_values2));
  161. $this->assertEqual('test', $md->name);
  162. }
  163. $_SESSION['user'] = $original_user;
  164. $obj->delete();
  165. $u1->delete();
  166. $u2->delete();
  167. }
  168. public function testDefaultOrderedById() {
  169. $ia = elgg_set_ignore_access(true);
  170. $obj = new ElggObject();
  171. $obj->owner_guid = elgg_get_site_entity()->guid;
  172. $obj->container_guid = elgg_get_site_entity()->guid;
  173. $obj->access_id = ACCESS_PUBLIC;
  174. $obj->save();
  175. $obj->test_md = [1, 2, 3];
  176. $time = time();
  177. $prefix = _elgg_services()->db->getTablePrefix();
  178. // reverse the times
  179. $mds = elgg_get_metadata([
  180. 'metadata_owner_guids' => $obj->guid,
  181. 'metadata_names' => 'test_md',
  182. 'order_by' => 'n_table.id ASC',
  183. ]);
  184. foreach ($mds as $i => $md) {
  185. update_data("
  186. UPDATE {$prefix}metadata
  187. SET time_created = " . ($time - $i) . "
  188. WHERE id = {$md->id}
  189. ");
  190. }
  191. // verify ID order
  192. $mds = elgg_get_metadata([
  193. 'guid' => $obj->guid,
  194. 'metadata_names' => 'test_md',
  195. ]);
  196. $md_values = array_map(function (ElggMetadata $md) {
  197. return (int)$md->value;
  198. }, $mds);
  199. $this->assertEqual($md_values, [1, 2, 3]);
  200. // ignore access bypasses the MD cache, so we try it both ways
  201. elgg_set_ignore_access(false);
  202. _elgg_services()->metadataCache->clear($obj->guid);
  203. $md_values = $obj->test_md;
  204. $this->assertEqual($md_values, [1, 2, 3]);
  205. elgg_set_ignore_access(true);
  206. _elgg_services()->metadataCache->clear($obj->guid);
  207. $md_values = $obj->test_md;
  208. $this->assertEqual($md_values, [1, 2, 3]);
  209. $obj->delete();
  210. elgg_set_ignore_access($ia);
  211. }
  212. protected function delete_metastrings($string) {
  213. global $CONFIG, $METASTRINGS_CACHE;
  214. $METASTRINGS_CACHE = array();
  215. $string = sanitise_string($string);
  216. _elgg_services()->db->deleteData("
  217. DELETE FROM {$CONFIG->dbprefix}metastrings WHERE string = BINARY '$string'
  218. ");
  219. }
  220. protected function create_metastring($string) {
  221. global $CONFIG, $METASTRINGS_CACHE;
  222. $METASTRINGS_CACHE = array();
  223. $string = sanitise_string($string);
  224. $id = _elgg_services()->db->insertData("
  225. INSERT INTO {$CONFIG->dbprefix}metastrings (string) VALUES ('$string')
  226. ");
  227. $this->metastrings[$string] = $id;
  228. }
  229. }