123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 |
- <?php
- abstract class ElggCoreGetEntitiesBaseTest extends \ElggCoreUnitTest {
-
- protected $entities;
-
- protected $types;
-
- protected $subtypes;
-
- public function __construct() {
- elgg_set_ignore_access(true);
- $this->entities = array();
- $this->subtypes = array(
- 'object' => array(),
- 'user' => array(),
- 'group' => array(),
-
- );
-
- $this->types = array('object', 'user', 'group');
-
-
- for ($i=0; $i<5; $i++) {
- $subtype = 'test_object_subtype_' . rand();
- $e = new \ElggObject();
- $e->subtype = $subtype;
- $e->save();
- $this->entities[] = $e;
- $this->subtypes['object'][] = $subtype;
- }
-
- for ($i=0; $i<5; $i++) {
- $subtype = "test_user_subtype_" . rand();
- $e = new \ElggUser();
- $e->username = "test_user_" . rand();
- $e->subtype = $subtype;
- $e->save();
- $this->entities[] = $e;
- $this->subtypes['user'][] = $subtype;
- }
-
- for ($i=0; $i<5; $i++) {
- $subtype = "test_group_subtype_" . rand();
- $e = new \ElggGroup();
- $e->subtype = $subtype;
- $e->save();
- $this->entities[] = $e;
- $this->subtypes['group'][] = $subtype;
- }
- parent::__construct();
- }
-
- public function __destruct() {
- global $CONFIG;
- foreach ($this->entities as $e) {
- $e->delete();
- }
-
-
- $subtype_arr = array();
- foreach ($this->subtypes as $type => $subtypes) {
- foreach ($subtypes as $subtype) {
- remove_subtype($type, $subtype);
- }
- }
- parent::__destruct();
- }
-
-
- protected function getRandomValidTypes($num = 1) {
- $r = array();
- for ($i=1; $i<=$num; $i++) {
- do {
- $t = $this->types[array_rand($this->types)];
- } while (in_array($t, $r) && count($r) < count($this->types));
- $r[] = $t;
- }
- shuffle($r);
- return $r;
- }
-
- protected function getRandomValidSubtypes(array $types, $num = 1) {
- $r = array();
- for ($i=1; $i<=$num; $i++) {
- do {
-
- if ($i-1 < count($types)) {
- $type = $types[$i-1];
- } else {
- $type = $types[array_rand($types)];
- }
- $k = array_rand($this->subtypes[$type]);
- $t = $this->subtypes[$type][$k];
- } while (in_array($t, $r));
- $r[] = $t;
- }
- shuffle($r);
- return $r;
- }
-
- protected function getRandomInvalids($num = 1) {
- $r = array();
- for ($i=1; $i<=$num; $i++) {
- $r[] = 'random_invalid_' . rand();
- }
- return $r;
- }
-
- protected function getRandomMixedTypes($num = 2) {
- $have_valid = $have_invalid = false;
- $r = array();
-
- $valid_n = rand(1, $num-1);
- $r = array_merge($r, $this->getRandomValidTypes($valid_n));
- $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
- shuffle($r);
- return $r;
- }
-
- protected function getRandomMixedSubtypes(array $types, $num = 2) {
- $types_c = count($types);
- $r = array();
-
-
- for ($i=0; $i < $types_c && $num > 0; $i++) {
-
- if (true) {
- $type = $types[$i];
- $r = array_merge($r, $this->getRandomValidSubtypes(array($type), 1));
- $r = array_merge($r, $this->getRandomInvalids(1));
- $num -= 2;
- }
- }
- if ($num > 0) {
- $valid_n = rand(1, $num);
- $r = array_merge($r, $this->getRandomValidSubtypes($types, $valid_n));
- $r = array_merge($r, $this->getRandomInvalids($num - $valid_n));
- }
-
- return $r;
- }
- }
|