ElggCoreMetastringsTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /**
  3. * Elgg Metastrings test
  4. *
  5. * @package Elgg.Core
  6. * @subpackage Metastrings.Test
  7. */
  8. class ElggCoreMetastringsTest extends \ElggCoreUnitTest {
  9. public $metastringTypes = array('metadata', 'annotation');
  10. public $metastringTables = array(
  11. 'metadata' => 'metadata',
  12. 'annotation' => 'annotations',
  13. );
  14. /**
  15. * Called before each test object.
  16. */
  17. public function __construct() {
  18. parent::__construct();
  19. $this->metastrings = array();
  20. $this->object = new \ElggObject();
  21. $this->object->save();
  22. }
  23. public function createAnnotations($max = 1) {
  24. $annotations = array();
  25. for ($i=0; $i<$max; $i++) {
  26. $name = 'test_annotation_name' . rand();
  27. $value = 'test_annotation_value' . rand();
  28. $id = create_annotation($this->object->guid, $name, $value);
  29. $annotations[] = $id;
  30. }
  31. return $annotations;
  32. }
  33. public function createMetadata($max = 1) {
  34. $metadata = array();
  35. for ($i=0; $i<$max; $i++) {
  36. $name = 'test_metadata_name' . rand();
  37. $value = 'test_metadata_value' . rand();
  38. $id = create_metadata($this->object->guid, $name, $value);
  39. $metadata[] = $id;
  40. }
  41. return $metadata;
  42. }
  43. /**
  44. * Called after each test method.
  45. */
  46. public function tearDown() {
  47. access_show_hidden_entities(true);
  48. elgg_delete_annotations(array(
  49. 'guid' => $this->object->guid,
  50. ));
  51. access_show_hidden_entities(false);
  52. }
  53. /**
  54. * Called after each test object.
  55. */
  56. public function __destruct() {
  57. $this->object->delete();
  58. parent::__destruct();
  59. }
  60. public function testDeleteByID() {
  61. $db_prefix = elgg_get_config('dbprefix');
  62. $annotation = $this->createAnnotations(1);
  63. $metadata = $this->createMetadata(1);
  64. foreach ($this->metastringTypes as $type) {
  65. $id = ${$type}[0];
  66. $table = $db_prefix . $this->metastringTables[$type];
  67. $q = "SELECT * FROM $table WHERE id = $id";
  68. $test = get_data($q);
  69. $this->assertEqual($test[0]->id, $id);
  70. $this->assertTrue(_elgg_delete_metastring_based_object_by_id($id, $type));
  71. $this->assertIdentical(array(), get_data($q));
  72. }
  73. }
  74. public function testGetMetastringObjectFromID() {
  75. $db_prefix = elgg_get_config('dbprefix');
  76. $annotation = $this->createAnnotations(1);
  77. $metadata = $this->createMetadata(1);
  78. foreach ($this->metastringTypes as $type) {
  79. $id = ${$type}[0];
  80. $test = _elgg_get_metastring_based_object_from_id($id, $type);
  81. $this->assertEqual($id, $test->id);
  82. $this->assertTrue(_elgg_delete_metastring_based_object_by_id($id, $type));
  83. }
  84. }
  85. public function testGetMetastringObjectFromIDWithDisabledAnnotation() {
  86. $name = 'test_annotation_name' . rand();
  87. $value = 'test_annotation_value' . rand();
  88. $id = create_annotation($this->object->guid, $name, $value);
  89. $annotation = elgg_get_annotation_from_id($id);
  90. $this->assertTrue($annotation->disable());
  91. $test = _elgg_get_metastring_based_object_from_id($id, 'annotation');
  92. $this->assertEqual(false, $test);
  93. $prev = access_get_show_hidden_status();
  94. access_show_hidden_entities(true);
  95. $this->assertTrue(_elgg_delete_metastring_based_object_by_id($id, 'annotation'));
  96. access_show_hidden_entities($prev);
  97. }
  98. public function testGetMetastringBasedObjectWithDisabledAnnotation() {
  99. $name = 'test_annotation_name' . rand();
  100. $value = 'test_annotation_value' . rand();
  101. $id = create_annotation($this->object->guid, $name, $value);
  102. $annotation = elgg_get_annotation_from_id($id);
  103. $this->assertTrue($annotation->disable());
  104. $test = _elgg_get_metastring_based_objects(array(
  105. 'metastring_type' => 'annotations',
  106. 'guid' => $this->object->guid,
  107. ));
  108. $this->assertEqual(array(), $test);
  109. $prev = access_get_show_hidden_status();
  110. access_show_hidden_entities(true);
  111. $this->assertTrue(_elgg_delete_metastring_based_object_by_id($id, 'annotation'));
  112. access_show_hidden_entities($prev);
  113. }
  114. public function testEnableDisableByID() {
  115. $db_prefix = elgg_get_config('dbprefix');
  116. $annotation = $this->createAnnotations(1);
  117. $metadata = $this->createMetadata(1);
  118. foreach ($this->metastringTypes as $type) {
  119. $id = ${$type}[0];
  120. $table = $db_prefix . $this->metastringTables[$type];
  121. $q = "SELECT * FROM $table WHERE id = $id";
  122. $test = get_data($q);
  123. // disable
  124. $this->assertEqual($test[0]->enabled, 'yes');
  125. $this->assertTrue(_elgg_set_metastring_based_object_enabled_by_id($id, 'no', $type));
  126. $test = get_data($q);
  127. $this->assertEqual($test[0]->enabled, 'no');
  128. // enable
  129. $ashe = access_get_show_hidden_status();
  130. access_show_hidden_entities(true);
  131. $this->assertTrue(_elgg_set_metastring_based_object_enabled_by_id($id, 'yes', $type));
  132. $test = get_data($q);
  133. $this->assertEqual($test[0]->enabled, 'yes');
  134. access_show_hidden_entities($ashe);
  135. $this->assertTrue(_elgg_delete_metastring_based_object_by_id($id, $type));
  136. }
  137. }
  138. public function testKeepMeFromDeletingEverything() {
  139. foreach ($this->metastringTypes as $type) {
  140. $required = array(
  141. 'guid', 'guids'
  142. );
  143. switch ($type) {
  144. case 'metadata':
  145. $metadata_required = array(
  146. 'metadata_owner_guid', 'metadata_owner_guids',
  147. 'metadata_name', 'metadata_names',
  148. 'metadata_value', 'metadata_values'
  149. );
  150. $required = array_merge($required, $metadata_required);
  151. break;
  152. case 'annotation':
  153. $annotations_required = array(
  154. 'annotation_owner_guid', 'annotation_owner_guids',
  155. 'annotation_name', 'annotation_names',
  156. 'annotation_value', 'annotation_values'
  157. );
  158. $required = array_merge($required, $annotations_required);
  159. break;
  160. }
  161. $options = array();
  162. $this->assertFalse(_elgg_is_valid_options_for_batch_operation($options, $type));
  163. // limit alone isn't valid:
  164. $options = array('limit' => 10);
  165. $this->assertFalse(_elgg_is_valid_options_for_batch_operation($options, $type));
  166. foreach ($required as $key) {
  167. $options = array();
  168. $options[$key] = ELGG_ENTITIES_ANY_VALUE;
  169. $this->assertFalse(_elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = ELGG_ENTITIES_ANY_VALUE");
  170. $options[$key] = ELGG_ENTITIES_NO_VALUE;
  171. $this->assertFalse(_elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = ELGG_ENTITIES_NO_VALUE");
  172. $options[$key] = false;
  173. $this->assertFalse(_elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = bool false");
  174. $options[$key] = true;
  175. $this->assertTrue(_elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = bool true");
  176. $options[$key] = 'test';
  177. $this->assertTrue(_elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = 'test'");
  178. $options[$key] = array('test');
  179. $this->assertTrue(_elgg_is_valid_options_for_batch_operation($options, $type), "Sent $key = array('test')");
  180. }
  181. }
  182. }
  183. }