1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Elgg\Database\Seeds; |
7
|
|
|
|
8
|
|
|
use ElggEntity; |
9
|
|
|
use ElggGroup; |
10
|
|
|
use ElggObject; |
11
|
|
|
use ElggUser; |
12
|
|
|
use Exception; |
13
|
|
|
use Faker\Factory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Seeding trait |
17
|
|
|
* Can be used to easily create new random users, groups and objects in the database |
18
|
|
|
* |
19
|
|
|
* @access private |
20
|
|
|
*/ |
21
|
|
|
trait Seeding { |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var int Max number of items to be created by the seed |
25
|
|
|
*/ |
26
|
|
|
protected $limit = 20; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Faker\Generator |
30
|
|
|
*/ |
31
|
|
|
protected $faker; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns an instance of faker |
35
|
|
|
* |
36
|
|
|
* @param string $locale Locale |
37
|
|
|
* |
38
|
|
|
* @return \Faker\Generator |
39
|
|
|
*/ |
40
|
|
|
public function faker($locale = 'en_US') { |
41
|
|
|
if (!isset($this->faker)) { |
42
|
|
|
$this->faker = Factory::create($locale); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this->faker; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get site domain |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function getDomain() { |
53
|
|
|
return elgg_get_site_entity()->getDomain(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get valid domain for emails |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getEmailDomain() { |
61
|
|
|
$email = elgg_get_site_entity()->email; |
62
|
|
|
if (!$email) { |
63
|
|
|
$email = "noreply@{$this->getDomain()}"; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
list(, $domain) = explode('@', $email); |
67
|
|
|
|
68
|
|
|
if (sizeof(explode('.', $domain)) <= 1) { |
69
|
|
|
$domain = 'example.net'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $domain; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create a new faker user |
77
|
|
|
* |
78
|
|
|
* @param array $attributes User entity attributes |
79
|
|
|
* @param array $metadata User entity metadata |
80
|
|
|
* |
81
|
|
|
* @return ElggUser |
82
|
|
|
*/ |
83
|
|
|
public function createUser(array $attributes = [], array $metadata = []) { |
84
|
|
|
|
85
|
|
|
$create = function () use ($attributes, $metadata) { |
86
|
|
|
$metadata['__faker'] = true; |
87
|
|
|
|
88
|
|
|
if (empty($attributes['password'])) { |
89
|
|
|
$attributes['password'] = generate_random_cleartext_password(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (empty($attributes['username'])) { |
93
|
|
|
$attributes['name'] = $this->faker()->name; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (empty($attributes['username'])) { |
97
|
|
|
$attributes['username'] = $this->getRandomUsername($attributes['name']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (empty($attributes['email'])) { |
101
|
|
|
$attributes['email'] = "{$attributes['username']}@{$this->getEmailDomain()}"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$user = false; |
105
|
|
|
|
106
|
|
|
try { |
107
|
|
|
$guid = register_user($attributes['username'], $attributes['password'], $attributes['name'], $attributes['email']); |
108
|
|
|
$user = get_entity($guid); |
|
|
|
|
109
|
|
|
/* @var $user ElggUser */ |
110
|
|
|
|
111
|
|
|
if (!$user) { |
112
|
|
|
throw new Exception("Unable to create new user with attributes: " . print_r($attributes, true)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (isset($attributes['admin'])) { |
116
|
|
|
if ($attributes['admin']) { |
117
|
|
|
$user->makeAdmin(); |
118
|
|
|
} else { |
119
|
|
|
$user->removeAdmin(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (isset($attributes['banned'])) { |
124
|
|
|
if ($attributes['banned']) { |
125
|
|
|
$user->ban('Banned by seeder'); |
126
|
|
|
} else { |
127
|
|
|
$user->unban('Unbanned by seeder'); |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
elgg_set_user_validation_status($guid, $this->faker()->boolean(), 'seeder'); |
|
|
|
|
132
|
|
|
|
133
|
|
|
$user->setNotificationSetting('email', false); |
134
|
|
|
$user->setNotificationSetting('site', true); |
135
|
|
|
|
136
|
|
|
$profile_fields = (array) elgg_get_config('profile_fields'); |
137
|
|
|
|
138
|
|
|
$user = $this->populateMetadata($user, $profile_fields, $metadata); |
139
|
|
|
|
140
|
|
|
$user->save(); |
141
|
|
|
|
142
|
|
|
$this->log("Created new user $user->name [guid: $user->guid]"); |
143
|
|
|
|
144
|
|
|
return $user; |
145
|
|
|
} catch (\RegistrationException $e) { |
146
|
|
|
if ($user && $user->guid) { |
147
|
|
|
$user->delete(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$attr_log = print_r($attributes, true); |
151
|
|
|
$this->log("User creation failed with message {$e->getMessage()} [attributes: $attr_log]"); |
152
|
|
|
|
153
|
|
|
return false; |
154
|
|
|
} |
155
|
|
|
}; |
156
|
|
|
|
157
|
|
|
$ia = elgg_set_ignore_access(true); |
158
|
|
|
|
159
|
|
|
$user = false; |
160
|
|
|
while (!$user instanceof \ElggUser) { |
161
|
|
|
$user = $create(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
elgg_set_ignore_access($ia); |
165
|
|
|
|
166
|
|
|
return $user; |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Create a new faker group |
172
|
|
|
* |
173
|
|
|
* @param array $attributes Group entity attributes |
174
|
|
|
* @param array $metadata Group entity metadata |
175
|
|
|
* |
176
|
|
|
* @return ElggGroup |
177
|
|
|
*/ |
178
|
|
|
public function createGroup(array $attributes = [], array $metadata = []) { |
179
|
|
|
|
180
|
|
|
$create = function () use ($attributes, $metadata) { |
181
|
|
|
$metadata['__faker'] = true; |
182
|
|
|
|
183
|
|
|
if (empty($attributes['access_id'])) { |
184
|
|
|
$attributes['access_id'] = ACCESS_PUBLIC; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (empty($metadata['content_access_mode'])) { |
188
|
|
|
$metadata['content_access_mode'] = ElggGroup::CONTENT_ACCESS_MODE_UNRESTRICTED; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if (empty($metadata['membership'])) { |
192
|
|
|
$metadata['membership'] = ACCESS_PUBLIC; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (empty($attributes['name'])) { |
196
|
|
|
$attributes['name'] = $this->faker()->sentence(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
View Code Duplication |
if (empty($attributes['description'])) { |
|
|
|
|
200
|
|
|
$attributes['description'] = $this->faker()->text($this->faker()->numberBetween(500, 1000)); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
if (empty($attributes['owner_guid'])) { |
204
|
|
|
$user = $this->getRandomUser(); |
205
|
|
|
if (!$user) { |
206
|
|
|
$user = $this->createUser(); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$attributes['owner_guid'] = $user->guid; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (empty($attributes['container_guid'])) { |
213
|
|
|
$attributes['container_guid'] = $attributes['owner_guid']; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
$owner = get_entity($attributes['owner_guid']); |
217
|
|
|
if (!$owner) { |
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$container = get_entity($attributes['container_guid']); |
222
|
|
|
if (!$container) { |
223
|
|
|
return false; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$tool_options = elgg_get_config('group_tool_options'); |
227
|
|
|
if ($tool_options) { |
228
|
|
|
foreach ($tool_options as $group_option) { |
229
|
|
|
$option_toggle_name = $group_option->name . "_enable"; |
230
|
|
|
$option_default = $group_option->default_on ? 'yes' : 'no'; |
231
|
|
|
$metadata[$option_toggle_name] = $option_default; |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
if ($this->faker()->boolean(20)) { |
236
|
|
|
$metadata['featured_group'] = 'yes'; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
$group = new ElggGroup(); |
240
|
|
|
foreach ($attributes as $name => $value) { |
241
|
|
|
$group->$name = $value; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
$profile_fields = (array) elgg_get_config('group'); |
245
|
|
|
$group = $this->populateMetadata($group, $profile_fields, $metadata); |
246
|
|
|
|
247
|
|
|
$group->save(); |
248
|
|
|
|
249
|
|
|
if ($group->access_id == ACCESS_PRIVATE) { |
250
|
|
|
$group->access_id = $group->group_acl; |
|
|
|
|
251
|
|
|
$group->save(); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$group->join(get_entity($attributes['owner_guid'])); |
255
|
|
|
|
256
|
|
|
elgg_create_river_item([ |
257
|
|
|
'view' => 'river/group/create', |
258
|
|
|
'action_type' => 'create', |
259
|
|
|
'subject_guid' => $owner->guid, |
260
|
|
|
'object_guid' => $group->guid, |
261
|
|
|
'target_guid' => $container->guid, |
262
|
|
|
]); |
263
|
|
|
|
264
|
|
|
$this->log("Created new group $group->name [guid: $group->guid]"); |
265
|
|
|
|
266
|
|
|
return $group; |
267
|
|
|
}; |
268
|
|
|
|
269
|
|
|
$ia = elgg_set_ignore_access(true); |
270
|
|
|
|
271
|
|
|
$group = false; |
272
|
|
|
while (!$group instanceof \ElggGroup) { |
273
|
|
|
$group = $create(); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
elgg_set_ignore_access($ia); |
277
|
|
|
|
278
|
|
|
return $group; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Create a new faker object |
283
|
|
|
* |
284
|
|
|
* @param array $attributes Object entity attributes |
285
|
|
|
* @param array $metadata Object entity metadata |
286
|
|
|
* |
287
|
|
|
* @return ElggObject |
288
|
|
|
*/ |
289
|
|
|
public function createObject(array $attributes = [], array $metadata = []) { |
290
|
|
|
|
291
|
|
|
$create = function () use ($attributes, $metadata) { |
292
|
|
|
$metadata['__faker'] = true; |
293
|
|
|
|
294
|
|
|
if (empty($attributes['title'])) { |
295
|
|
|
$attributes['title'] = $this->faker()->sentence(); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
View Code Duplication |
if (empty($attributes['description'])) { |
|
|
|
|
299
|
|
|
$attributes['description'] = $this->faker()->text($this->faker()->numberBetween(500, 1000)); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
if (empty($attributes['container_guid'])) { |
303
|
|
|
if ($this->faker()->boolean()) { |
304
|
|
|
$container = $this->getRandomGroup(); |
305
|
|
|
if (!$container) { |
306
|
|
|
$container = $this->createGroup(); |
307
|
|
|
} |
308
|
|
|
} else { |
309
|
|
|
$container = $this->getRandomUser(); |
310
|
|
|
if (!$container) { |
311
|
|
|
$container = $this->createUser(); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
$attributes['container_guid'] = $container->guid; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
if (empty($attributes['subtype'])) { |
319
|
|
|
$attributes['subtype'] = strtolower($this->faker()->word); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
if (empty($metadata['tags'])) { |
323
|
|
|
$metadata['tags'] = $this->faker()->words(10); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
if (empty($attributes['owner_guid'])) { |
327
|
|
|
if ($container instanceof ElggGroup) { |
328
|
|
|
$members = elgg_get_entities_from_relationship([ |
329
|
|
|
'types' => 'user', |
330
|
|
|
'relationship' => 'member', |
331
|
|
|
'relationship_guid' => $container->guid, |
|
|
|
|
332
|
|
|
'inverse_relationship' => true, |
333
|
|
|
'limit' => 1, |
334
|
|
|
'metadata_names' => '__faker', |
335
|
|
|
'order_by' => 'RAND()', |
336
|
|
|
]); |
337
|
|
|
$owner = array_shift($members); |
338
|
|
|
} else { |
339
|
|
|
$owner = $container; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
$attributes['owner_guid'] = $owner->guid; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$owner = get_entity($attributes['owner_guid']); |
346
|
|
|
if (!$owner) { |
347
|
|
|
return false; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
$container = get_entity($attributes['container_guid']); |
351
|
|
|
if (!$container) { |
352
|
|
|
return false; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
if (empty($attributes['access_id'])) { |
356
|
|
|
//$attributes['access_id'] = $this->getRandomAccessId($owner, $container); |
357
|
|
|
$attributes['access_id'] = ACCESS_PUBLIC; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
$class = get_subtype_class('object', $attributes['subtype']); |
361
|
|
|
if ($class && class_exists($class)) { |
|
|
|
|
362
|
|
|
$object = new $class(); |
363
|
|
|
} else { |
364
|
|
|
$object = new ElggObject(); |
365
|
|
|
} |
366
|
|
|
foreach ($attributes as $name => $value) { |
367
|
|
|
$object->$name = $value; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
$object = $this->populateMetadata($object, [], $metadata); |
371
|
|
|
|
372
|
|
|
$object->save(); |
373
|
|
|
|
374
|
|
|
$type_str = elgg_echo("item:object:{$object->getSubtype()}"); |
375
|
|
|
|
376
|
|
|
$this->log("Created new item in $type_str $object->title [guid: $object->guid]"); |
377
|
|
|
|
378
|
|
|
return $object; |
379
|
|
|
}; |
380
|
|
|
|
381
|
|
|
$ia = elgg_set_ignore_access(true); |
382
|
|
|
|
383
|
|
|
$object = false; |
384
|
|
|
while (!$object instanceof \ElggObject) { |
385
|
|
|
$object = $create(); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
elgg_set_ignore_access($ia); |
389
|
|
|
|
390
|
|
|
return $object; |
391
|
|
|
|
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Returns random fake user |
396
|
|
|
* |
397
|
|
|
* @param int[] $exclude GUIDs to exclude |
398
|
|
|
* |
399
|
|
|
* @return ElggUser|false |
400
|
|
|
*/ |
401
|
|
View Code Duplication |
public function getRandomUser(array $exclude = []) { |
|
|
|
|
402
|
|
|
|
403
|
|
|
$exclude[] = 0; |
404
|
|
|
$exclude_in = implode(',', array_map(function ($e) { |
405
|
|
|
return (int) $e; |
406
|
|
|
}, $exclude)); |
407
|
|
|
|
408
|
|
|
$users = elgg_get_entities_from_metadata([ |
409
|
|
|
'types' => 'user', |
410
|
|
|
'metadata_names' => ['__faker'], |
411
|
|
|
'limit' => 1, |
412
|
|
|
'wheres' => [ |
413
|
|
|
"e.guid NOT IN ($exclude_in)", |
414
|
|
|
], |
415
|
|
|
'order_by' => 'RAND()', |
416
|
|
|
]); |
417
|
|
|
|
418
|
|
|
return $users ? $users[0] : false; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Returns random fake group |
423
|
|
|
* |
424
|
|
|
* @param int[] $exclude GUIDs to exclude |
425
|
|
|
* |
426
|
|
|
* @return ElggGroup|false |
427
|
|
|
*/ |
428
|
|
View Code Duplication |
public function getRandomGroup(array $exclude = []) { |
|
|
|
|
429
|
|
|
|
430
|
|
|
$exclude[] = 0; |
431
|
|
|
$exclude_in = implode(',', array_map(function ($e) { |
432
|
|
|
return (int) $e; |
433
|
|
|
}, $exclude)); |
434
|
|
|
|
435
|
|
|
$groups = elgg_get_entities_from_metadata([ |
436
|
|
|
'types' => 'group', |
437
|
|
|
'metadata_names' => ['__faker'], |
438
|
|
|
'limit' => 1, |
439
|
|
|
'wheres' => [ |
440
|
|
|
"e.guid NOT IN ($exclude_in)", |
441
|
|
|
], |
442
|
|
|
'order_by' => 'RAND()', |
443
|
|
|
]); |
444
|
|
|
|
445
|
|
|
return $groups ? $groups[0] : false; |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Get random access id |
450
|
|
|
* |
451
|
|
|
* @param ElggUser $user User |
452
|
|
|
* @param ElggEntity $container Container |
453
|
|
|
* |
454
|
|
|
* @return int |
455
|
|
|
*/ |
456
|
|
|
public function getRandomAccessId(\ElggUser $user = null, ElggEntity $container = null) { |
457
|
|
|
|
458
|
|
|
$params = [ |
459
|
|
|
'container_guid' => $container->guid, |
460
|
|
|
]; |
461
|
|
|
|
462
|
|
|
$access_array = get_write_access_array($user->guid, null, null, $params); |
|
|
|
|
463
|
|
|
|
464
|
|
|
$access_key = array_rand($access_array, 1); |
465
|
|
|
|
466
|
|
|
return $access_array[$access_key]; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* Generates a unique available and valid username |
471
|
|
|
* |
472
|
|
|
* @param string $base_name Display name, email or other prefix to use as basis |
473
|
|
|
* |
474
|
|
|
* @return string |
475
|
|
|
*/ |
476
|
|
|
public function getRandomUsername($base_name = 'user') { |
477
|
|
|
|
478
|
|
|
$available = false; |
479
|
|
|
|
480
|
|
|
$base_name = iconv('UTF-8', 'ASCII//TRANSLIT', $base_name); |
481
|
|
|
$blacklist = '/[\x{0080}-\x{009f}\x{00a0}\x{2000}-\x{200f}\x{2028}-\x{202f}\x{3000}\x{e000}-\x{f8ff}]/u'; |
482
|
|
|
$blacklist2 = [ |
483
|
|
|
' ', |
484
|
|
|
'\'', |
485
|
|
|
'/', |
486
|
|
|
'\\', |
487
|
|
|
'"', |
488
|
|
|
'*', |
489
|
|
|
'&', |
490
|
|
|
'?', |
491
|
|
|
'#', |
492
|
|
|
'%', |
493
|
|
|
'^', |
494
|
|
|
'(', |
495
|
|
|
')', |
496
|
|
|
'{', |
497
|
|
|
'}', |
498
|
|
|
'[', |
499
|
|
|
']', |
500
|
|
|
'~', |
501
|
|
|
'?', |
502
|
|
|
'<', |
503
|
|
|
'>', |
504
|
|
|
';', |
505
|
|
|
'|', |
506
|
|
|
'¬', |
507
|
|
|
'`', |
508
|
|
|
'@', |
509
|
|
|
'-', |
510
|
|
|
'+', |
511
|
|
|
'=' |
512
|
|
|
]; |
513
|
|
|
|
514
|
|
|
$base_name = preg_replace($blacklist, '', $base_name); |
515
|
|
|
$base_name = str_replace($blacklist2, '', $base_name); |
516
|
|
|
$base_name = str_replace('.', '_', $base_name); |
517
|
|
|
|
518
|
|
|
$ia = elgg_set_ignore_access(true); |
519
|
|
|
|
520
|
|
|
$ha = access_get_show_hidden_status(); |
521
|
|
|
access_show_hidden_entities(true); |
522
|
|
|
|
523
|
|
|
$minlength = elgg_get_config('minusername') ? : 4; |
524
|
|
|
if ($base_name) { |
525
|
|
|
$fill = $minlength - strlen($base_name); |
526
|
|
|
} else { |
527
|
|
|
$fill = 8; |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
$separator = ''; |
531
|
|
|
|
532
|
|
|
if ($fill > 0) { |
533
|
|
|
$suffix = (new ElggCrypto())->getRandomString($fill); |
534
|
|
|
$base_name = "$base_name$separator$suffix"; |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
$iterator = 0; |
538
|
|
|
while (!$available) { |
539
|
|
|
if ($iterator > 0) { |
540
|
|
|
$base_name = "$base_name$separator$iterator"; |
541
|
|
|
} |
542
|
|
|
$user = get_user_by_username($base_name); |
543
|
|
|
$available = !$user; |
544
|
|
|
try { |
545
|
|
|
if ($available) { |
546
|
|
|
validate_username($base_name); |
547
|
|
|
} |
548
|
|
|
} catch (\Exception $e) { |
549
|
|
|
if ($iterator >= 10) { |
550
|
|
|
// too many failed attempts |
551
|
|
|
$base_name = (new ElggCrypto())->getRandomString(8); |
552
|
|
|
} |
553
|
|
|
} |
554
|
|
|
|
555
|
|
|
$iterator++; |
556
|
|
|
} |
557
|
|
|
|
558
|
|
|
access_show_hidden_entities($ha); |
559
|
|
|
elgg_set_ignore_access($ia); |
560
|
|
|
|
561
|
|
|
return strtolower($base_name); |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* Set random metadata |
566
|
|
|
* |
567
|
|
|
* @param ElggEntity $entity Entity |
568
|
|
|
* @param array $fields An array of profile fields in $name => $input_type format |
569
|
|
|
* @param array $metadata Other metadata $name => $value pairs to set |
570
|
|
|
* |
571
|
|
|
* @return ElggEntity |
572
|
|
|
*/ |
573
|
|
|
public function populateMetadata(ElggEntity $entity, array $fields = [], array $metadata = []) { |
574
|
|
|
|
575
|
|
|
foreach ($fields as $name => $type) { |
576
|
|
|
if (isset($metadata[$name])) { |
577
|
|
|
continue; |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
switch ($name) { |
581
|
|
|
case 'phone' : |
582
|
|
|
case 'mobile' : |
583
|
|
|
$metadata[$name] = $this->faker()->phoneNumber; |
584
|
|
|
break; |
585
|
|
|
|
586
|
|
|
default : |
587
|
|
|
switch ($type) { |
588
|
|
|
case 'plaintext' : |
589
|
|
|
case 'longtext' : |
590
|
|
|
$metadata[$name] = $this->faker()->text($this->faker()->numberBetween(500, 1000)); |
591
|
|
|
break; |
592
|
|
|
|
593
|
|
|
case 'text' : |
594
|
|
|
$metadata[$name] = $this->faker()->sentence; |
595
|
|
|
break; |
596
|
|
|
|
597
|
|
|
case 'tags' : |
598
|
|
|
$metadata[$name] = $this->faker()->words(10); |
599
|
|
|
break; |
600
|
|
|
|
601
|
|
|
case 'url' : |
602
|
|
|
$metadata[$name] = $this->faker()->url; |
603
|
|
|
|
604
|
|
|
case 'email' : |
605
|
|
|
$metadata[$name] = $this->faker()->email; |
606
|
|
|
break; |
607
|
|
|
|
608
|
|
|
case 'number' : |
609
|
|
|
$metadata[$name] = $this->faker()->randomNumber(); |
610
|
|
|
break; |
611
|
|
|
|
612
|
|
|
case 'date' : |
613
|
|
|
$metadata[$name] = $this->faker()->unixTime; |
614
|
|
|
break; |
615
|
|
|
|
616
|
|
|
case 'password' : |
617
|
|
|
$metadata[$name] = generate_random_cleartext_password(); |
618
|
|
|
break; |
619
|
|
|
|
620
|
|
View Code Duplication |
case 'location' : |
|
|
|
|
621
|
|
|
$metadata[$name] = $this->faker()->address; |
622
|
|
|
$metadata['geo:lat'] = $this->faker()->latitude; |
623
|
|
|
$metadata['geo:long'] = $this->faker()->longitude; |
624
|
|
|
break; |
625
|
|
|
|
626
|
|
View Code Duplication |
case 'email' : |
|
|
|
|
627
|
|
|
$metadata[$name] = $this->faker()->address; |
628
|
|
|
$metadata['geo:lat'] = $this->faker()->latitude; |
629
|
|
|
$metadata['geo:long'] = $this->faker()->longitude; |
630
|
|
|
break; |
631
|
|
|
|
632
|
|
|
default : |
633
|
|
|
$metadata[$name] = ''; |
634
|
|
|
break; |
635
|
|
|
} |
636
|
|
|
|
637
|
|
|
break; |
638
|
|
|
} |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
foreach ($metadata as $key => $value) { |
642
|
|
|
$entity->$key = $value; |
643
|
|
|
} |
644
|
|
|
|
645
|
|
|
return $entity; |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Create an icon for an entity |
650
|
|
|
* |
651
|
|
|
* @param ElggEntity $entity Entity |
652
|
|
|
* |
653
|
|
|
* @return bool |
654
|
|
|
*/ |
655
|
|
|
public function createIcon(ElggEntity $entity) { |
656
|
|
|
|
657
|
|
|
$icon_url = $this->faker()->imageURL(); |
658
|
|
|
|
659
|
|
|
$file_contents = file_get_contents($icon_url); |
660
|
|
|
|
661
|
|
|
$tmp = new \ElggFile(); |
662
|
|
|
$tmp->owner_guid = $entity->guid; |
663
|
|
|
$tmp->setFilename("tmp/icon_src.jpg"); |
664
|
|
|
$tmp->open('write'); |
665
|
|
|
$tmp->write($file_contents); |
666
|
|
|
$tmp->close(); |
667
|
|
|
|
668
|
|
|
$result = $entity->saveIconFromElggFile($tmp); |
669
|
|
|
|
670
|
|
|
$tmp->delete(); |
671
|
|
|
|
672
|
|
View Code Duplication |
if ($result && $entity instanceof ElggUser) { |
|
|
|
|
673
|
|
|
elgg_create_river_item([ |
674
|
|
|
'view' => 'river/user/default/profileiconupdate', |
675
|
|
|
'action_type' => 'update', |
676
|
|
|
'subject_guid' => $entity->guid, |
677
|
|
|
'object_guid' => $entity->guid, |
678
|
|
|
]); |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
return $result; |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Create comments/replies |
686
|
|
|
* |
687
|
|
|
* @param ElggEntity $entity Entity to comment on |
688
|
|
|
* @param int $limit Number of comments to create |
689
|
|
|
* |
690
|
|
|
* @return int Number of generated comments |
691
|
|
|
*/ |
692
|
|
|
public function createComments(ElggEntity $entity, $limit = null) { |
693
|
|
|
|
694
|
|
|
$ia = elgg_set_ignore_access(true); |
695
|
|
|
|
696
|
|
|
$tries = 0; |
697
|
|
|
$success = 0; |
698
|
|
|
|
699
|
|
|
if (!$limit) { |
|
|
|
|
700
|
|
|
$limit = $this->faker()->numberBetween(1, 20); |
701
|
|
|
} |
702
|
|
|
|
703
|
|
|
while ($tries < $limit) { |
704
|
|
|
$comment = new \ElggComment(); |
705
|
|
|
$comment->subtype = $entity->getSubtype() == 'discussion' ? 'discussion_reply' : 'comment'; |
706
|
|
|
$comment->owner_guid = $this->getRandomUser()->guid ? : $entity->owner_guid; |
707
|
|
|
$comment->container_guid = $entity->guid; |
708
|
|
|
$comment->description = $this->faker()->paragraph; |
709
|
|
|
|
710
|
|
|
$tries++; |
711
|
|
|
if ($comment->save()) { |
712
|
|
|
$success++; |
713
|
|
|
} |
714
|
|
|
} |
715
|
|
|
|
716
|
|
|
elgg_set_ignore_access($ia); |
717
|
|
|
|
718
|
|
|
return $success; |
719
|
|
|
|
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* Create likes |
724
|
|
|
* |
725
|
|
|
* @param ElggEntity $entity Entity to like |
726
|
|
|
* @param int $limit Number of likes to create |
727
|
|
|
* |
728
|
|
|
* @return int |
729
|
|
|
*/ |
730
|
|
|
public function createLikes(ElggEntity $entity, $limit = null) { |
731
|
|
|
|
732
|
|
|
$ia = elgg_set_ignore_access(true); |
733
|
|
|
|
734
|
|
|
$success = 0; |
735
|
|
|
|
736
|
|
|
if (!$limit) { |
|
|
|
|
737
|
|
|
$limit = $this->faker()->numberBetween(1, 20); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
while ($success < $limit) { |
741
|
|
|
if ($entity->annotate('likes', true, $entity->access_id, $this->getRandomUser()->guid)) { |
742
|
|
|
$success++; |
743
|
|
|
} |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
elgg_set_ignore_access($ia); |
747
|
|
|
|
748
|
|
|
return $success; |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
/** |
752
|
|
|
* Log a message |
753
|
|
|
* |
754
|
|
|
* @param string $msg Message to log |
755
|
|
|
* @param string $level Message level |
756
|
|
|
* Note that 'ERROR' will terminate further code execution |
757
|
|
|
* |
758
|
|
|
* @return void |
759
|
|
|
*/ |
760
|
|
|
public function log($msg, $level = 'NOTICE') { |
761
|
|
|
elgg_log($msg, $level); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.