ElggCoreGetEntitiesBaseTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Setup entities for getter tests
  4. */
  5. abstract class ElggCoreGetEntitiesBaseTest extends \ElggCoreUnitTest {
  6. /** @var array */
  7. protected $entities;
  8. /** @var array */
  9. protected $types;
  10. /** @var array */
  11. protected $subtypes;
  12. /**
  13. * Called before each test object.
  14. */
  15. public function __construct() {
  16. elgg_set_ignore_access(true);
  17. $this->entities = array();
  18. $this->subtypes = array(
  19. 'object' => array(),
  20. 'user' => array(),
  21. 'group' => array(),
  22. //'site' => array()
  23. );
  24. // sites are a bit wonky. Don't use them just now.
  25. $this->types = array('object', 'user', 'group');
  26. // create some fun objects to play with.
  27. // 5 with random subtypes
  28. for ($i=0; $i<5; $i++) {
  29. $subtype = 'test_object_subtype_' . rand();
  30. $e = new \ElggObject();
  31. $e->subtype = $subtype;
  32. $e->save();
  33. $this->entities[] = $e;
  34. $this->subtypes['object'][] = $subtype;
  35. }
  36. // and users
  37. for ($i=0; $i<5; $i++) {
  38. $subtype = "test_user_subtype_" . rand();
  39. $e = new \ElggUser();
  40. $e->username = "test_user_" . rand();
  41. $e->subtype = $subtype;
  42. $e->save();
  43. $this->entities[] = $e;
  44. $this->subtypes['user'][] = $subtype;
  45. }
  46. // and groups
  47. for ($i=0; $i<5; $i++) {
  48. $subtype = "test_group_subtype_" . rand();
  49. $e = new \ElggGroup();
  50. $e->subtype = $subtype;
  51. $e->save();
  52. $this->entities[] = $e;
  53. $this->subtypes['group'][] = $subtype;
  54. }
  55. parent::__construct();
  56. }
  57. /**
  58. * Called after each test object.
  59. */
  60. public function __destruct() {
  61. global $CONFIG;
  62. foreach ($this->entities as $e) {
  63. $e->delete();
  64. }
  65. // manually remove subtype entries since there is no way
  66. // to using the API.
  67. $subtype_arr = array();
  68. foreach ($this->subtypes as $type => $subtypes) {
  69. foreach ($subtypes as $subtype) {
  70. remove_subtype($type, $subtype);
  71. }
  72. }
  73. parent::__destruct();
  74. }
  75. /*************************************************
  76. * Helpers for getting random types and subtypes *
  77. *************************************************/
  78. /**
  79. * Get a random valid subtype
  80. *
  81. * @param int $num
  82. * @return array
  83. */
  84. protected function getRandomValidTypes($num = 1) {
  85. $r = array();
  86. for ($i=1; $i<=$num; $i++) {
  87. do {
  88. $t = $this->types[array_rand($this->types)];
  89. } while (in_array($t, $r) && count($r) < count($this->types));
  90. $r[] = $t;
  91. }
  92. shuffle($r);
  93. return $r;
  94. }
  95. /**
  96. * Get a random valid subtype (that we just created)
  97. *
  98. * @param array $type Type of objects to return valid subtypes for.
  99. * @param int $num of subtypes.
  100. *
  101. * @return array
  102. */
  103. protected function getRandomValidSubtypes(array $types, $num = 1) {
  104. $r = array();
  105. for ($i=1; $i<=$num; $i++) {
  106. do {
  107. // make sure at least one subtype of each type is returned.
  108. if ($i-1 < count($types)) {
  109. $type = $types[$i-1];
  110. } else {
  111. $type = $types[array_rand($types)];
  112. }
  113. $k = array_rand($this->subtypes[$type]);
  114. $t = $this->subtypes[$type][$k];
  115. } while (in_array($t, $r));
  116. $r[] = $t;
  117. }
  118. shuffle($r);
  119. return $r;
  120. }
  121. /**
  122. * Return an array of invalid strings for type or subtypes.
  123. *
  124. * @param int $num
  125. * @return string[]
  126. */
  127. protected function getRandomInvalids($num = 1) {
  128. $r = array();
  129. for ($i=1; $i<=$num; $i++) {
  130. $r[] = 'random_invalid_' . rand();
  131. }
  132. return $r;
  133. }
  134. /**
  135. * Get a mix of valid and invalid types
  136. *
  137. * @param int $num
  138. * @return array
  139. */
  140. protected function getRandomMixedTypes($num = 2) {
  141. $have_valid = $have_invalid = false;
  142. $r = array();
  143. // need at least one of each type.
  144. $valid_n = rand(1, $num-1);
  145. $r = array_merge($r, $this->getRandomValidTypes($valid_n));
  146. $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
  147. shuffle($r);
  148. return $r;
  149. }
  150. /**
  151. * Get random mix of valid and invalid subtypes for types given.
  152. *
  153. * @param array $types
  154. * @param int $num
  155. * @return array
  156. */
  157. protected function getRandomMixedSubtypes(array $types, $num = 2) {
  158. $types_c = count($types);
  159. $r = array();
  160. // this can be more efficient but I'm very sleepy...
  161. // want at least one of valid and invalid of each type sent.
  162. for ($i=0; $i < $types_c && $num > 0; $i++) {
  163. // make sure we have a valid and invalid for each type
  164. if (true) {
  165. $type = $types[$i];
  166. $r = array_merge($r, $this->getRandomValidSubtypes(array($type), 1));
  167. $r = array_merge($r, $this->getRandomInvalids(1));
  168. $num -= 2;
  169. }
  170. }
  171. if ($num > 0) {
  172. $valid_n = rand(1, $num);
  173. $r = array_merge($r, $this->getRandomValidSubtypes($types, $valid_n));
  174. $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
  175. }
  176. //shuffle($r);
  177. return $r;
  178. }
  179. }