48 public function faker(
string $locale =
'en_US'): \Faker\Generator {
50 $this->
faker = Factory::create($locale);
53 $this->faker->addProvider(
new LocalImage($this->faker));
75 $email =
"noreply@{$this->getDomain()}";
80 if (count(explode(
'.',
$domain)) <= 1) {
93 return substr(sha1(microtime() . rand()), 0, 25);
107 $create = function () use ($properties,
$options) {
108 $properties[
'__faker'] =
true;
110 if (empty($properties[
'password'])) {
114 if (empty($properties[
'name'])) {
115 $properties[
'name'] = $this->
faker()->name;
118 if (empty($properties[
'username'])) {
122 if (empty($properties[
'email'])) {
123 $properties[
'email'] = $this->
getRandomEmail($properties[
'username']);
126 if (empty($properties[
'subtype'])) {
127 $properties[
'subtype'] =
'user';
144 if (!isset($properties[
'time_created'])) {
148 if (!empty($properties[
'time_created'])) {
149 $user->time_created = $properties[
'time_created'];
152 if (isset($properties[
'admin'])) {
153 if ($properties[
'admin']) {
156 $user->removeAdmin();
160 if (isset($properties[
'banned'])) {
161 if ($properties[
'banned']) {
162 $user->ban(
'Banned by seeder');
168 if (!isset($properties[
'validated'])) {
169 $properties[
'validated'] = $this->
faker()->boolean(80);
172 $user->setValidationStatus((
bool) $properties[
'validated'],
'seeder');
174 if (!
$user->isValidated()) {
175 $user->disable(
'seeder invalidation');
178 unset($properties[
'username']);
179 unset($properties[
'password']);
180 unset($properties[
'name']);
181 unset($properties[
'email']);
182 unset($properties[
'banned']);
183 unset($properties[
'admin']);
184 unset($properties[
'validated']);
186 $user->setNotificationSetting(
'email',
false);
187 $user->setNotificationSetting(
'site',
true);
195 $this->
log(
"Created new user {$user->getDisplayName()} [guid: {$user->guid}]");
203 $attr_log = print_r($properties,
true);
204 $this->
log(
"User creation failed with message {$e->getMessage()} [properties: $attr_log]");
213 while (!
$user instanceof \
ElggUser && $attempts < $this->MAX_ATTEMPTS) {
218 }
catch (\Exception $ex) {
224 throw new MaxAttemptsException(
"Unable to create a user after {$attempts} seeding attempts");
242 $create = function () use ($properties,
$options) {
243 $properties[
'__faker'] =
true;
245 if (!isset($properties[
'time_created'])) {
249 if (!isset($properties[
'access_id'])) {
253 if (!isset($properties[
'content_access_mode'])) {
257 if (!isset($properties[
'membership'])) {
261 if (empty($properties[
'name'])) {
262 $properties[
'name'] = $this->
faker()->sentence();
265 if (empty($properties[
'description'])) {
266 $properties[
'description'] = $this->
faker()->text($this->
faker()->numberBetween(500, 1000));
269 if (!isset($properties[
'owner_guid'])) {
279 $properties[
'owner_guid'] =
$user->guid;
286 if (!isset($properties[
'container_guid'])) {
287 $properties[
'container_guid'] = $properties[
'owner_guid'];
294 if (empty($properties[
'subtype'])) {
295 $properties[
'subtype'] =
'group';
301 foreach ($tool_options as $group_option) {
302 $prop_name = $group_option->mapMetadataName();
303 $prop_value = $group_option->mapMetadataValue();
304 $properties[$prop_name] = $prop_value;
307 if ($this->
faker()->
boolean(20)) {
308 $properties[
'featured_group'] =
'yes';
311 $group = new \ElggGroup();
333 $acl =
$group->getOwnedAccessCollection(
'group_acl');
335 $group->access_id = $acl->id;
343 'view' =>
'river/group/create',
344 'action_type' =>
'create',
345 'subject_guid' => $properties[
'owner_guid'],
346 'object_guid' =>
$group->guid,
347 'target_guid' => $properties[
'container_guid'],
348 'posted' =>
$group->time_created,
351 $this->
log(
"Created new group {$group->getDisplayName()} [guid: {$group->guid}]");
359 while (!
$group instanceof \
ElggGroup && $attempts < $this->MAX_ATTEMPTS) {
366 throw new MaxAttemptsException(
"Unable to create a group after {$attempts} seeding attempts");
383 $default_properties = [
385 'description' => true,
388 $properties = array_merge($default_properties, $properties);
390 $create =
function () use ($properties,
$options) {
391 $properties[
'__faker'] =
true;
393 if (!isset($properties[
'time_created'])) {
397 if ($properties[
'title'] ===
true) {
398 $properties[
'title'] = $this->faker()->sentence();
399 }
elseif ($properties[
'title'] ===
false) {
400 unset($properties[
'title']);
403 if ($properties[
'description'] ===
true) {
404 $properties[
'description'] = $this->
faker()->text($this->
faker()->numberBetween(500, 1000));
405 }
elseif ($properties[
'description'] ===
false) {
406 unset($properties[
'description']);
409 if (empty($properties[
'subtype'])) {
413 if ($properties[
'tags'] ===
true) {
414 $properties[
'tags'] = $this->
faker()->words(10);
415 }
elseif ($properties[
'tags'] ===
false) {
416 unset($properties[
'tags']);
419 if (!isset($properties[
'owner_guid'])) {
429 $properties[
'owner_guid'] =
$user->guid;
436 if (!isset($properties[
'container_guid'])) {
437 $properties[
'container_guid'] = $properties[
'owner_guid'];
444 if (!isset($properties[
'access_id'])) {
477 $type_str =
elgg_echo(
"item:object:{$object->getSubtype()}");
479 $this->
log(
"Created new item in {$type_str} {$object->getDisplayName()} [guid: {$object->guid}]");
494 throw new MaxAttemptsException(
"Unable to create an object after {$attempts} seeding attempts");
521 public function getRandomUser(array $exclude = [],
bool $allow_create =
true) {
529 'metadata_names' => [
'__faker'],
545 $profile_fields_config =
_elgg_services()->fields->get(
'user',
'user');
546 $profile_fields = [];
547 foreach ($profile_fields_config as
$field) {
554 'profile_fields' => $profile_fields,
575 'metadata_names' => [
'__faker'],
585 if (!empty($groups)) {
590 $profile_fields_config =
_elgg_services()->fields->get(
'group',
'group');
591 $profile_fields = [];
592 foreach ($profile_fields_config as
$field) {
601 'profile_fields' => $profile_fields,
622 return array_rand($access_array, 1);
634 $make =
function(
$name =
null) {
639 return implode(
'.', preg_split(
'/\W/',
$name));
668 $make =
function($base =
null) {
673 $validate =
function(
$email) {
683 while (!$validate(
$email)) {
781 $icon_location = $this->
faker()->image();
782 if (empty($icon_location)) {
789 $since = $this->create_since;
793 'view' =>
'river/user/default/profileiconupdate',
794 'action_type' =>
'update',
795 'subject_guid' =>
$entity->guid,
796 'object_guid' =>
$entity->guid,
797 'posted' => $this->getRandomCreationTimestamp(),
800 $this->create_since = $since;
825 $limit = $this->faker()->numberBetween(1, 20);
828 $since = $this->create_since;
833 $comment->owner_guid = $this->getRandomUser()->guid ?:
$entity->owner_guid;
835 $comment->description = $this->faker()->paragraph;
837 $comment->access_id = $entity->access_id;
845 $this->create_since = $since;
869 $limit = $this->faker()->numberBetween(1, 20);
872 while ($success <
$limit) {
890 public function log($msg, $level = LogLevel::NOTICE): void {
log($level, $message, array $context=[])
Log a message.
getEmailDomain()
Get valid domain for emails.
faker(string $locale='en_US')
Returns an instance of faker.
createUser(array $properties=[], array $options=[])
Create a new fake user.
createSite(array $properties=[])
Create a new fake site.
getRandomSubtype()
Returns random unique subtype.
trait Seeding
Seeding trait Can be used to easily create new random users, groups and objects in the database.
getRandomAccessId(?\ElggUser $user=null, ?\ElggEntity $container=null)
Get random access id.
createComments(\ElggEntity $entity, $limit=null)
Create comments/replies.
getRandomGroup(array $exclude=[], bool $allow_create=true)
Returns random fake group.
createObject(array $properties=[], array $options=[])
Create a new fake object.
createIcon(\ElggEntity $entity)
Create an icon for an entity.
getRandomEmail($base=null)
Generate a random valid email.
createGroup(array $properties=[], array $options=[])
Create a new fake group.
createLikes(\ElggEntity $entity, $limit=null)
Create likes.
getRandomUsername($name=null)
Generates a unique available and valid username.
getDomain()
Get site domain.
getRandomUser(array $exclude=[], bool $allow_create=true)
Returns random fake user.
populateMetadata(\ElggEntity $entity, array $fields=[], array $metadata=[])
Set random metadata.
$fields
Save the configuration of the security.txt contents.
if(! $user||! $user->canDelete()) $name
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
foreach( $paths as $path)
if(empty($user_guids)) $users
save()
Save this data to the appropriate database table.bool
const CONTENT_ACCESS_MODE_UNRESTRICTED
A collection of unique items.
Extends QueryBuilder with ORDER BY clauses.
Database abstraction query builder.
Provide images from a local folder for seeding.
Could not register a new user for whatever reason.
Base exception of exceptions in the Elgg system.
Thrown when the seeding has exceeded the max attempts for trying to create an \ElggEntity.
const ELGG_IGNORE_ACCESS
elgg_call() flags
const ELGG_HIDE_DISABLED_ENTITIES
const ELGG_SHOW_DISABLED_ENTITIES
const ELGG_VALUE_INTEGER
Value types.
if($who_can_change_language==='nobody') elseif($who_can_change_language==='admin_only' &&!elgg_is_admin_logged_in()) $options
if($email instanceof \Elgg\Email) $object
if($item instanceof \ElggEntity) elseif($item instanceof \ElggRiverItem) elseif($item instanceof \ElggRelationship) elseif(is_callable([ $item, 'getType']))
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
_elgg_services()
Get the global service provider.
elgg_call(int $flags, Closure $closure)
Calls a callable autowiring the arguments using public DI services and applying logic based on flags.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
elgg_get_write_access_array(int $user_guid=0, bool $flush=false, array $input_params=[])
Returns an array of access permissions that the user is allowed to save content with.
elgg_register_user(array $params=[])
Registers a user.
elgg_generate_password()
Generate a random 12 character clear text password.
get_user(int $guid)
Elgg users Functions to manage multiple or single users in an Elgg install.
get_entity(int $guid)
Loads and returns an entity object from a guid.
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
elgg_get_entity_class(string $type, string $subtype)
Return the class name registered as a constructor for an entity of a given type and subtype.
elgg_get_site_entity()
Get the current site entity.
elgg_entity_has_capability(string $type, string $subtype, string $capability, bool $default=false)
Checks if a capability is enabled for a specified type/subtype.
elgg_entity_exists(int $guid)
Does an entity exist?
elgg_echo(string $message_key, array $args=[], string $language='')
Elgg language module Functions to manage language and translations.
elgg_strtolower()
Wrapper function for mb_strtolower().
trait GroupHelpers
Group helpers for seeding.
getRandomGroupVisibility()
Returns random visibility value.
setCreateSince($since='now')
Set a time for entities to be created after.
getRandomCreationTimestamp()
Get a random timestamp between a lower and upper time.
getRandomGroupContentAccessMode()
Returns random content access mode value.
trait TimeHelpers
Trait to add time helpers.
getRandomGroupMembership()
Returns random membership mode.
if(!elgg_get_config('trash_enabled')) $group
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
elgg_create_river_item(array $options=[])
Elgg river.
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.