Skip to content
This repository was archived by the owner on Mar 2, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ Pull requests should be made to the **develop** branch.
- Source code: https://github.com/ixis/codeception-module-drupal-user-registry


## Creating or deleting users using the command line

Once this module is installed in a Codeception test suite the following commands can be used to create and delete test users (from the root of the test suite):

# Create test users for all defined roles.
vendor/bin/drupal-user-registry users:create

# Delete test users for all defined roles.
vendor/bin/drupal-user-registry users:delete

These commands will default to using the **acceptance** suite to determine the alias on which Drush will run from the suite's configuration. To run the commands using a suite other than **acceptance**, pass the suite name as an argument:

# Create test users for all defined roles in the front-end suite configuration.
vendor/bin/drupal-user-registry users:create front-end

# Delete test users for all defined roles in the front-end suite configuration.
vendor/bin/drupal-user-registry users:delete front-end


## Acknowledgements

Thanks to [Andy Rigby](https://github.com/ixisandyr) for the storage code and inspiration.
Expand Down
13 changes: 13 additions & 0 deletions bin/drupal-user-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php

require_once dirname(__FILE__) . '/../vendor/autoload.php';

use Codeception\Module\Drupal\UserRegistry\Command\CreateTestUsersCommand;
use Codeception\Module\Drupal\UserRegistry\Command\DeleteTestUsersCommand;
use Symfony\Component\Console\Application;

$app = new Application();
$app->add(new CreateTestUsersCommand());
$app->add(new DeleteTestUsersCommand());
$app->run();
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@
"psr-4": {
"Codeception\\Module\\": "src"
}
}
},

"bin": ["bin/drupal-user-registry"]
}
61 changes: 61 additions & 0 deletions src/Drupal/UserRegistry/Command/CreateTestUsersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Codeception\Module\Drupal\UserRegistry\Command;

use Codeception\Exception\Module as ModuleException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class CreateTestUsersCommand.
*
* @package Codeception\Module\DrupalUserRegistry\Command\TestUsersCommand
*/
class CreateTestUsersCommand extends Command
{
use TestUsersCommandTrait;

/**
* Configuration for this command.
*/
protected function configure()
{
$this
->setName('users:create')
->setDescription('Create test users.')
->addArgument(
'suite',
InputArgument::OPTIONAL,
"Which suite configuration to use. Defaults to 'acceptance'."
);
}

/**
* Execute command: create any defined test users on the configured Drush alias.
*
* @todo Codeception debug calls won't work here.
*
* @param InputInterface $input
* The command input.
*
* @param OutputInterface $output
* The command output.
*
* @return int|null|void
*
* @throws ModuleException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// The suite name can be passed as an argument. Without it, the command defaults to 'acceptance'.
$suiteName = $input->getArgument('suite');
if (!$suiteName) {
$suiteName = 'acceptance';
}

$this->getTestUsers($suiteName);
$this->testUserManager->createUsers($this->users);
}
}
61 changes: 61 additions & 0 deletions src/Drupal/UserRegistry/Command/DeleteTestUsersCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Codeception\Module\Drupal\UserRegistry\Command;

use Codeception\Exception\Module as ModuleException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class DeleteTestUsersCommand.
*
* @package Codeception\Module\Drupal\UserRegistry\Command
*/
class DeleteTestUsersCommand extends Command
{
use TestUsersCommandTrait;

/**
* Configuration for this command.
*/
protected function configure()
{
$this
->setName('users:delete')
->setDescription('Delete test users.')
->addArgument(
'suite',
InputArgument::OPTIONAL,
"Which suite configuration to use. Defaults to 'acceptance'."
);
}

/**
* Execute command: create any defined test users on the configured Drush alias.
*
* @todo Codeception debug calls won't work here.
*
* @param InputInterface $input
* The command input.
*
* @param OutputInterface $output
* The command output.
*
* @return int|null|void
*
* @throws ModuleException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
// The suite name can be passed as an argument. Without it, the command defaults to 'acceptance'.
$suiteName = $input->getArgument('suite');
if (!$suiteName) {
$suiteName = 'acceptance';
}

$this->getTestUsers($suiteName);
$this->testUserManager->deleteUsers($this->users);
}
}
56 changes: 56 additions & 0 deletions src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Codeception\Module\Drupal\UserRegistry\Command;

use Codeception\Configuration;
use Codeception\Exception\Module as ModuleException;
use Codeception\Module\Drupal\UserRegistry\DrushTestUserManager;
use Codeception\Module\Drupal\UserRegistry\Storage\ModuleConfigStorage;

/**
* Class TestUsersCommandTrait.
*
* @package Codeception\Module\Drupal\UserRegistry\Command
*/
trait TestUsersCommandTrait
{
/**
* @var \Codeception\Module\Drupal\UserRegistry\DrupalTestUser[]
* Store an array of test users to be created or deleted.
*/
protected $users;

/**
* @var \Codeception\Module\Drupal\UserRegistry\TestUserManagerInterface
* Store the test user manager being used to create or delete users.
*/
protected $testUserManager;

/**
* @param string $suiteName
* @param array $suiteSettings
*
* @throws ModuleException
* @throws \Codeception\Exception\Configuration
* @throws \Exception
*/
protected function getTestUsers($suiteName, $suiteSettings = null)
{
if (!$suiteSettings) {
$suiteSettings = Configuration::suiteSettings($suiteName, Configuration::config());
}

if (!isset($suiteSettings['modules']['config']['DrupalUserRegistry'])) {
throw new ModuleException(
__CLASS__,
sprintf("Drupal User Registry is not configured correctly in suite '%s'.", $suiteName)
);
}

$config = $suiteSettings['modules']['config']['DrupalUserRegistry'];

$moduleConfigStorage = new ModuleConfigStorage($config);
$this->users = $moduleConfigStorage->load();
$this->testUserManager = new DrushTestUserManager($config, $moduleConfigStorage);
}
}
19 changes: 19 additions & 0 deletions tests/_support/FunctionalHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@

namespace Codeception\Module;

use Codeception\Module\Drupal\UserRegistry\Storage\ModuleConfigStorage;

/**
* Define custom actions for functional tests.
*
* All non-static public methods declared in helper class will be available in $I.
*/
class FunctionalHelper extends \Codeception\Module
{
/**
* Helper to translate role names to test usernames.
*
* @todo This code is copied from ModuleConfigStorage::load(), where it's a bit buried. Needs refactoring.
*
* @see ModuleConfigStorage::load()
*
* @param string $role
* The name of the role to translate into a test username.
*
* @return string
*/
public function getTestUsername($role)
{
$roleNameSuffix = preg_replace(ModuleConfigStorage::DRUPAL_ROLE_TO_USERNAME_PATTERN, ".", $role);
return ModuleConfigStorage::DRUPAL_USERNAME_PREFIX . "." . $roleNameSuffix;
}
}
27 changes: 26 additions & 1 deletion tests/functional.suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,32 @@
#
class_name: FunctionalTester
modules:
enabled: [Db, FunctionalHelper]
enabled: [Asserts, Db, FunctionalHelper]
config:
# Used for command line tests.
DrupalUserRegistry:
defaultPass: "foobar"
users:
administrator:
name: test.administrator
email: test.administrator@example.com
pass: "foo"
roles: [ administrator, editor ]
root: true
editor:
name: test.editor
email: editor@example.com
roles: [ editor, moderator ]
moderator:
name: "test.moderator"
email: "sub.editor@example.com"
roles: [ moderator ]
authenticated:
name: authenticated
email: authenticated@example.com
roles: [ "authenticated user" ]
drush-alias: "@d7.local"

env:
# Override Db module configuration to run on local.
local:
Expand Down
89 changes: 89 additions & 0 deletions tests/functional/CommandCest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

use \FunctionalTester;

use Symfony\Component\Console\Application;
use Codeception\Module\Drupal\UserRegistry\Command\CreateTestUsersCommand;
use Codeception\Module\Drupal\UserRegistry\Command\DeleteTestUsersCommand;
use Codeception\Util\Fixtures;

/**
* Class CommandCest
*
* @group cli
*/
class CommandCest
{
/**
* @var Application
*/
protected $app;

/**
* Set up the console application.
*
* @param FunctionalTester $I
*/
public function _before(FunctionalTester $I)
{
$this->app = new Application();
$this->app->add(new CreateTestUsersCommand());
$this->app->add(new DeleteTestUsersCommand());
}

/**
* Tear down any created console application.
*
* @param FunctionalTester $I
*/
public function _after(FunctionalTester $I)
{
// unset($this->app);
}

/**
* @param FunctionalTester $I
*/
public function testCreateUsersCommand(FunctionalTester $I)
{
$command = $this->app->find("users:create");
$tester = new \Symfony\Component\Console\Tester\CommandTester($command);

$tester->execute(
array(
"command" => $command->getName(),
"suite" => "unit",
)
);

// @todo Configs are a bit muxed ip...
$config = Fixtures::get("validModuleConfig");
foreach ($config["users"] as $user => $userDetails) {
$I->seeInDatabase("users", array("name" => $userDetails["name"]));
}
}

/**
* @before testCreateUsersCommand
*
* @param FunctionalTester $I
*/
public function testCreateAndDeleteUsersCommand(FunctionalTester $I)
{
$command = $this->app->find("users:delete");
$tester = new \Symfony\Component\Console\Tester\CommandTester($command);

$tester->execute(
array(
"command" => $command->getName(),
"suite" => "unit",
)
);

// @todo Configs are a bit muxed ip...
$config = Fixtures::get("validModuleConfig");
foreach ($config["users"] as $user => $userDetails) {
$I->dontSeeInDatabase("users", array("name" => $userDetails["name"]));
}
}
}
Loading