Skip to content

Commit

Permalink
additional flexibility for generate users command in order to help test
Browse files Browse the repository at this point in the history
  • Loading branch information
craigh committed Mar 25, 2020
1 parent 8f06910 commit 0a826fc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 22 deletions.
19 changes: 12 additions & 7 deletions docs/AccessControl/Users/GenerateUsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ You can generate any number of users by using the CLI command below:

### Options

- `--active=[0|1|2]`
- `0` all users inactive
- `1` all users active
- `2` random assignment per user (`0|1`)
- `--active=[A|I|P|M|R]`
- `A` all users active (default)
- `I` all users inactive
- `P` all users pending
- `M` all users marked for deletion
- `R` random assignment per user (`A|I|P|M`)
- `--verified=[0|1|2]`
- `0` all user emails unverified
- `1` all user emails verified
- `1` all user emails verified (default)
- `2` random assignment per user (`0|1`)
- `--regdate='(>)YYYYMMDD'`
- setting just a date like `20000101` will set that as regdate for all created users.
- adding `>` before the date will make each regdate a random date between the provided date and now.



bin/console zikula:users:generate --active=0 --verified=2
bin/console zikula:users:generate --active=I --verified=2 --regdate='>20000101'
72 changes: 61 additions & 11 deletions src/Zikula/CoreBundle/Command/GenerateTestUsersCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@ class GenerateTestUsersCommand extends Command
private $startUTC;

/**
* @var int
* @var \DateTime
*/
private $regDate;

/**
* @var bool
*/
private $range = false;

/**
* @var string
*/
private $active;

Expand Down Expand Up @@ -73,8 +83,8 @@ protected function configure()
'active',
null,
InputOption::VALUE_REQUIRED,
'All the users are 1=active, 0=inactive, 2=random choice 0|1',
UsersConstant::ACTIVATED_ACTIVE
'All the users are A=active, I=inactive, P=all users pending, M=all users marked for deletion, R=random choice A|I|P|M',
'A'
)
->addOption(
'verified',
Expand All @@ -83,6 +93,12 @@ protected function configure()
'All the user emails marked as 1=verified, 0=unverified, 2=random choice 0|1',
1
)
->addOption(
'regdate',
null,
InputOption::VALUE_REQUIRED,
'Registration date in format YYYYMMDD or >YYYYMMDD if random dates between provided date and now'
)
->setDescription('Generates users for testing purposes')
->setHelp(
<<<'EOT'
Expand All @@ -94,11 +110,13 @@ protected function configure()
This will generate 1000 randomly named users using all the default values.
Options:
<info>--active (-a)</info> 0|1|2 (default: 1) 1=all users active, 0=all users inactive, 2=random assignment 0|1
<info>--active</info> A|I|P|M|R (default: A) A=all users active, I=all users inactive, P=all users pending, M=all users marked for deletion R=random assignment A|I|P|M
<info>--verified (-v)</info> 0|1|2 (default: 1) 1=all user emails verified, 0=all user emails unverified, 2=random assignment 0|1
<info>--verified</info> 0|1|2 (default: 1) 1=all user emails verified, 0=all user emails unverified, 2=random assignment 0|1
<info>php %command.full_name% 1000 --active=0 --verified=2</info>
<info>--regdate</info> YYYYMMDD or >YYYYMMDD if random dates between provided date and now
<info>php %command.full_name% 1000 --active=I --verified=2 --regdate='>20200101'</info>

EOT
);
Expand All @@ -111,8 +129,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$key = bin2hex(random_bytes(3));
$groupId = $this->createGroup($key);
$divisor = (int) ceil($amount / 100);
$this->active = in_array((int) $input->getOption('active'), [0, 1, 2]) ? (int) $input->getOption('active') : UsersConstant::ACTIVATED_ACTIVE;
$this->active = (string) $input->getOption('active');
$this->verified = in_array((int) $input->getOption('verified'), [0, 1, 2]) ? (int) $input->getOption('verified') : 1;
$regDate = $input->getOption('regdate') ?? $this->nowUTC;
$this->range = '>' === $regDate[0];
$this->regDate = $this->range ? mb_substr($regDate, 1) : $regDate;

$io->title('User generation utility');
$io->text('Generating users...');
Expand All @@ -137,17 +158,46 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

private function configureActivatedStatus(string $value): int {
$statuses = [
'A' => UsersConstant::ACTIVATED_ACTIVE,
'I' => UsersConstant::ACTIVATED_INACTIVE,
'P' => UsersConstant::ACTIVATED_PENDING_REG,
'M' => UsersConstant::ACTIVATED_PENDING_DELETE
];
if ('R' === $value) {
return array_rand(array_flip($statuses));
}

return array_key_exists($value, $statuses) ? $statuses[$value] : UsersConstant::ACTIVATED_ACTIVE;
}

private function configureRegDate(): \DateTime
{
if ($this->regDate instanceof \DateTime) {
return $this->regDate;
}
$utcTz = new \DateTimeZone('UTC');
$regDate = \DateTime::createFromFormat('Ymd', $this->regDate, $utcTz);
if (!$this->range) {
return $regDate;
}
$randTimeStamp = mt_rand($regDate->getTimestamp(), $this->nowUTC->getTimestamp());

return \DateTime::createFromFormat("U", "$randTimeStamp", $utcTz);
}

private function insertUser(string $uname): void
{
$types = [\PDO::PARAM_STR, \PDO::PARAM_STR, \PDO::PARAM_INT, 'datetime', \PDO::PARAM_INT, 'datetime', 'datetime', \PDO::PARAM_STR, \PDO::PARAM_STR];
try {
$this->conn->insert('users', [
'uname' => $uname,
'email' => $uname . '@example.com',
'activated' => 2 === $this->active ? random_int(0, 1) : $this->active,
'approved_date' => $this->nowUTC,
'approved_by' => 2,
'user_regdate' => $this->nowUTC,
'activated' => $activated = $this->configureActivatedStatus($this->active),
'approved_date' => UsersConstant::ACTIVATED_ACTIVE === $activated ? $this->nowUTC : $this->startUTC,
'approved_by' => UsersConstant::ACTIVATED_ACTIVE === $activated ? 2 : 0,
'user_regdate' => $this->configureRegDate(),
'lastlogin' => $this->startUTC,
'tz' => '',
'locale' => ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ public function searchAction(
$form->handleRequest($request);
if ($form->isSubmitted()) {
$resultsForm = $this->createForm(DeleteType::class, [], [
'choices' => $userRepository->queryBySearchForm($form->getData()),
'choices' => $userRepository->queryBySearchForm($form->getData(), 250),
'action' => $this->generateUrl('zikulausersmodule_useradministration_delete')
]);

Expand Down
4 changes: 2 additions & 2 deletions src/system/UsersModule/Entity/Repository/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function setApproved(UserEntity $user, DateTime $approvedOn, int $approve
$this->_em->flush();
}

public function queryBySearchForm(array $formData = [])
public function queryBySearchForm(array $formData = [], $limit = 0)
{
$filter = [];
foreach ($formData as $k => $v) {
Expand All @@ -101,7 +101,7 @@ public function queryBySearchForm(array $formData = [])
}
}

return $this->query($filter);
return $this->query($filter, [], $limit);
}

public function getSearchResults(array $words = [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
</h3>

<div>
<div class="alert alert-warning" role="alert">
{% trans %}Be advised: If your search resulted in more than 250 records, that display has been limited to 250.{% endtrans %}
</div>
{{ form_start(resultsForm) }}
{{ form_errors(resultsForm) }}
<table class="table table-bordered table-striped">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<span class="badge badge-dark">{% trans %}Inactive{% endtrans %}</span>
{% break %}
{% case constant('Zikula\\UsersModule\\Constant::ACTIVATED_PENDING_REG') %}
<span class="badge badge-warning">{% trans %}Pending{% endtrans %}</span>
<span class="badge badge-primary">{% trans %}Pending{% endtrans %}</span>
{% break %}
{% case constant('Zikula\\UsersModule\\Constant::ACTIVATED_PENDING_DELETE') %}
<span class="badge badge-warning">{% trans %}Marked for deletion{% endtrans %}</span>
Expand Down

0 comments on commit 0a826fc

Please sign in to comment.