From 9152fa1a7218a43f6ed5f2c8646d3b7b38e2a973 Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Tue, 28 Oct 2014 20:27:08 +0000 Subject: [PATCH 01/11] First attempt at CLI for creating/deleting test users. --- README.md | 9 +++ .../Command/CreateTestUsersCommand.php | 61 +++++++++++++++++++ .../Command/DeleteTestUsersCommand.php | 61 +++++++++++++++++++ .../Command/TestUsersCommandTrait.php | 52 ++++++++++++++++ test-users | 13 ++++ 5 files changed, 196 insertions(+) create mode 100644 src/Drupal/UserRegistry/Command/CreateTestUsersCommand.php create mode 100644 src/Drupal/UserRegistry/Command/DeleteTestUsersCommand.php create mode 100644 src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php create mode 100755 test-users diff --git a/README.md b/README.md index 8ec7057..55010f7 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,15 @@ Drupal User Registry minimally requires Codeception 2.0 and PHP 5.4 drush-alias: '@mysite.local' # The Drush alias to use when managing users via DrushTestUserManager. +## Creating or deleting users using the command line + + vendor/pfaocle/codeception-module-drupal-user-registry/test-users users:create + +or + + vendor/pfaocle/codeception-module-drupal-user-registry/test-users users:delete + + ## Acknowledgements Props to [Andy Rigby](https://github.com/ixisandyr) for the storage code and inspiration. diff --git a/src/Drupal/UserRegistry/Command/CreateTestUsersCommand.php b/src/Drupal/UserRegistry/Command/CreateTestUsersCommand.php new file mode 100644 index 0000000..f39aafe --- /dev/null +++ b/src/Drupal/UserRegistry/Command/CreateTestUsersCommand.php @@ -0,0 +1,61 @@ +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); + } +} diff --git a/src/Drupal/UserRegistry/Command/DeleteTestUsersCommand.php b/src/Drupal/UserRegistry/Command/DeleteTestUsersCommand.php new file mode 100644 index 0000000..076b9d3 --- /dev/null +++ b/src/Drupal/UserRegistry/Command/DeleteTestUsersCommand.php @@ -0,0 +1,61 @@ +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); + } +} diff --git a/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php b/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php new file mode 100644 index 0000000..70c1c1a --- /dev/null +++ b/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php @@ -0,0 +1,52 @@ +users = $moduleConfigStorage->load(); + $this->testUserManager = new DrushTestUserManager($config); + } +} diff --git a/test-users b/test-users new file mode 100755 index 0000000..58f5498 --- /dev/null +++ b/test-users @@ -0,0 +1,13 @@ +#!/usr/bin/env php +add(new CreateTestUsersCommand()); +$app->add(new DeleteTestUsersCommand()); +$app->run(); From 39bb89de557fdcb4121c7157dc7d3fe2120e8e6f Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Thu, 4 Dec 2014 22:24:26 +0000 Subject: [PATCH 02/11] Move the command-line script to bin/ directory; install to vendor/bin with Composer. --- test-users => bin/drupal-user-registry | 2 +- composer.json | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) rename test-users => bin/drupal-user-registry (85%) diff --git a/test-users b/bin/drupal-user-registry similarity index 85% rename from test-users rename to bin/drupal-user-registry index 58f5498..fb1a464 100755 --- a/test-users +++ b/bin/drupal-user-registry @@ -1,7 +1,7 @@ #!/usr/bin/env php Date: Thu, 4 Dec 2014 22:31:02 +0000 Subject: [PATCH 03/11] Update README with new path/command. --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 55010f7..b5add0f 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,13 @@ Drupal User Registry minimally requires Codeception 2.0 and PHP 5.4 ## Creating or deleting users using the command line - vendor/pfaocle/codeception-module-drupal-user-registry/test-users users:create +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): -or + # Create test users for all defined roles. + vendor/bin/drupal-user-registry users:create - vendor/pfaocle/codeception-module-drupal-user-registry/test-users users:delete + # Delete test users for all defined roles. + vendor/bin/drupal-user-registry users:delete ## Acknowledgements From ba958197d29fa7adf02801a5292d095b4d46fbdd Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Thu, 4 Dec 2014 22:41:59 +0000 Subject: [PATCH 04/11] Document using commands against non-default suite (acceptance). --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index b5add0f..544ff0c 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,14 @@ Once this module is installed in a Codeception test suite the following commands # 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 From 11b34a3089334c7e06389bd6daf55e7f83b5d59b Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Mon, 5 Jan 2015 20:47:36 +0000 Subject: [PATCH 05/11] Update path to vendor/bin directory. --- bin/drupal-user-registry | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/drupal-user-registry b/bin/drupal-user-registry index fb1a464..3465603 100755 --- a/bin/drupal-user-registry +++ b/bin/drupal-user-registry @@ -1,7 +1,7 @@ #!/usr/bin/env php Date: Mon, 5 Jan 2015 20:48:59 +0000 Subject: [PATCH 06/11] Tests for CLI (WIP). --- .../Command/TestUsersCommandTrait.php | 10 ++- tests/_support/FunctionalHelper.php | 19 ++++ tests/functional.suite.yml | 2 +- tests/functional/CommandCest.php | 89 +++++++++++++++++++ tests/functional/CreateDeleteUsersCest.php | 19 ---- tests/unit/CommandTest.php | 46 ++++++++++ tests/unit/TestUsersCommandTraitTest.php | 27 ++++++ 7 files changed, 189 insertions(+), 23 deletions(-) create mode 100644 tests/functional/CommandCest.php create mode 100644 tests/unit/CommandTest.php create mode 100644 tests/unit/TestUsersCommandTraitTest.php diff --git a/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php b/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php index 70c1c1a..e4e8022 100644 --- a/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php +++ b/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php @@ -27,14 +27,18 @@ trait TestUsersCommandTrait protected $testUserManager; /** - * @param $suiteName + * @param string $suiteName + * @param array $suiteSettings + * * @throws ModuleException * @throws \Codeception\Exception\Configuration * @throws \Exception */ - protected function getTestUsers($suiteName) + protected function getTestUsers($suiteName, $suiteSettings = null) { - $suiteSettings = Configuration::suiteSettings($suiteName, Configuration::config()); + if (!$suiteSettings) { + $suiteSettings = Configuration::suiteSettings($suiteName, Configuration::config()); + } if (!isset($suiteSettings['modules']['config']['DrupalUserRegistry'])) { throw new ModuleException( diff --git a/tests/_support/FunctionalHelper.php b/tests/_support/FunctionalHelper.php index a69bccb..9cab964 100644 --- a/tests/_support/FunctionalHelper.php +++ b/tests/_support/FunctionalHelper.php @@ -2,6 +2,8 @@ namespace Codeception\Module; +use Codeception\Module\Drupal\UserRegistry\Storage\ModuleConfigStorage; + /** * Define custom actions for functional tests. * @@ -9,4 +11,21 @@ */ 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; + } } diff --git a/tests/functional.suite.yml b/tests/functional.suite.yml index fe34ac5..d94e335 100644 --- a/tests/functional.suite.yml +++ b/tests/functional.suite.yml @@ -3,7 +3,7 @@ # class_name: FunctionalTester modules: - enabled: [Db, FunctionalHelper] + enabled: [Asserts, Db, FunctionalHelper] env: # Override Db module configuration to run on local. local: diff --git a/tests/functional/CommandCest.php b/tests/functional/CommandCest.php new file mode 100644 index 0000000..6dc242c --- /dev/null +++ b/tests/functional/CommandCest.php @@ -0,0 +1,89 @@ +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["roles"] as $role) { + $I->seeInDatabase("users", array("name" => $I->getTestUsername($role))); + } + } + + /** + * @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["roles"] as $role) { + $I->dontSeeInDatabase("users", array("name" => $I->getTestUsername($role))); + } + } +} diff --git a/tests/functional/CreateDeleteUsersCest.php b/tests/functional/CreateDeleteUsersCest.php index 6d7c229..b527560 100644 --- a/tests/functional/CreateDeleteUsersCest.php +++ b/tests/functional/CreateDeleteUsersCest.php @@ -2,7 +2,6 @@ use \FunctionalTester; -use Codeception\Module\Drupal\UserRegistry\Storage\ModuleConfigStorage; use Codeception\Util\Fixtures; /** @@ -136,22 +135,4 @@ public function testCreatedUsersHaveCorrectRoles(FunctionalTester $I) } } } - - /** - * 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 - */ - protected function getTestUsername($role) - { - $roleNameSuffix = preg_replace(ModuleConfigStorage::DRUPAL_ROLE_TO_USERNAME_PATTERN, ".", $role); - return ModuleConfigStorage::DRUPAL_USERNAME_PREFIX . "." . $roleNameSuffix; - } } diff --git a/tests/unit/CommandTest.php b/tests/unit/CommandTest.php new file mode 100644 index 0000000..40e30bd --- /dev/null +++ b/tests/unit/CommandTest.php @@ -0,0 +1,46 @@ +add(new CreateTestUsersCommand()); + $command = $app->find("users:create"); + $tester = new \Symfony\Component\Console\Tester\CommandTester($command); + + $tester->execute( + array( + "command" => $command->getName(), + "suite" => "unit", + ) + ); + + // @todo Verify...? + } +} diff --git a/tests/unit/TestUsersCommandTraitTest.php b/tests/unit/TestUsersCommandTraitTest.php new file mode 100644 index 0000000..4210ed7 --- /dev/null +++ b/tests/unit/TestUsersCommandTraitTest.php @@ -0,0 +1,27 @@ +setExpectedException('\Codeception\Exception\Module'); + $this->getTestUsers("unit", array("invalid" => "config")); + } +} From 2b59e5b8eea7c08fdea7f4e9c3c088cc6e359319 Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Wed, 7 Oct 2015 16:35:00 +0100 Subject: [PATCH 07/11] Pass storage into DrushTestUserManager in tests --- src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php b/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php index e4e8022..d3e4f67 100644 --- a/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php +++ b/src/Drupal/UserRegistry/Command/TestUsersCommandTrait.php @@ -51,6 +51,6 @@ protected function getTestUsers($suiteName, $suiteSettings = null) $moduleConfigStorage = new ModuleConfigStorage($config); $this->users = $moduleConfigStorage->load(); - $this->testUserManager = new DrushTestUserManager($config); + $this->testUserManager = new DrushTestUserManager($config, $moduleConfigStorage); } } From 2deb6a4d2831664a4d55d681bcd58d5b3ed29695 Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Fri, 26 Feb 2016 16:29:23 +0000 Subject: [PATCH 08/11] Add an example of using getUserByRole with multiple roles --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 8f33adb..e6de4f5 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ It also allows the use of the following statements in tests: // this role. $user = $I->getUserByRole($roleName); +// Returns a DrupalTestUser object representing the test user available for +// exactly these roles. +$user = $I->getUserByRole([$roleName1, $roleName2]); + // Returns a DrupalTestUser object representing the user, or false if no users // were found. Note this will only return a user defined and managed by this // module, it will not return information about arbitrary accounts on the site From f4d97c580efad5d16c0d8ed801c617e1ecb4e43c Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Fri, 26 Feb 2016 16:49:59 +0000 Subject: [PATCH 09/11] Add module config to suite config for use in CL tests... --- tests/functional.suite.yml | 25 +++++++++++++++++++++++++ tests/unit.suite.yml | 24 ++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/tests/functional.suite.yml b/tests/functional.suite.yml index d94e335..820037e 100644 --- a/tests/functional.suite.yml +++ b/tests/functional.suite.yml @@ -4,6 +4,31 @@ class_name: FunctionalTester modules: 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: diff --git a/tests/unit.suite.yml b/tests/unit.suite.yml index 9314168..aa898c9 100644 --- a/tests/unit.suite.yml +++ b/tests/unit.suite.yml @@ -4,3 +4,27 @@ class_name: UnitTester modules: enabled: [UnitHelper] + 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" From 99774cb63562bda9c86e3d05ec14cf262d604508 Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Fri, 26 Feb 2016 16:50:18 +0000 Subject: [PATCH 10/11] Use the new config correctly in testing --- tests/functional/CommandCest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/functional/CommandCest.php b/tests/functional/CommandCest.php index 6dc242c..c8b49db 100644 --- a/tests/functional/CommandCest.php +++ b/tests/functional/CommandCest.php @@ -58,8 +58,8 @@ public function testCreateUsersCommand(FunctionalTester $I) // @todo Configs are a bit muxed ip... $config = Fixtures::get("validModuleConfig"); - foreach ($config["roles"] as $role) { - $I->seeInDatabase("users", array("name" => $I->getTestUsername($role))); + foreach ($config["users"] as $user => $userDetails) { + $I->seeInDatabase("users", array("name" => $I->getTestUsername($userDetails["name"]))); } } @@ -82,8 +82,8 @@ public function testCreateAndDeleteUsersCommand(FunctionalTester $I) // @todo Configs are a bit muxed ip... $config = Fixtures::get("validModuleConfig"); - foreach ($config["roles"] as $role) { - $I->dontSeeInDatabase("users", array("name" => $I->getTestUsername($role))); + foreach ($config["users"] as $user => $userDetails) { + $I->dontSeeInDatabase("users", array("name" => $I->getTestUsername($userDetails["name"]))); } } } From ef97304708d1808bb390e01b7e342209faa6e52f Mon Sep 17 00:00:00 2001 From: Paul Byrne Date: Fri, 26 Feb 2016 17:09:38 +0000 Subject: [PATCH 11/11] Don't need to translate a 'test username' any more --- tests/functional/CommandCest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/functional/CommandCest.php b/tests/functional/CommandCest.php index c8b49db..0989c36 100644 --- a/tests/functional/CommandCest.php +++ b/tests/functional/CommandCest.php @@ -59,7 +59,7 @@ public function testCreateUsersCommand(FunctionalTester $I) // @todo Configs are a bit muxed ip... $config = Fixtures::get("validModuleConfig"); foreach ($config["users"] as $user => $userDetails) { - $I->seeInDatabase("users", array("name" => $I->getTestUsername($userDetails["name"]))); + $I->seeInDatabase("users", array("name" => $userDetails["name"])); } } @@ -83,7 +83,7 @@ public function testCreateAndDeleteUsersCommand(FunctionalTester $I) // @todo Configs are a bit muxed ip... $config = Fixtures::get("validModuleConfig"); foreach ($config["users"] as $user => $userDetails) { - $I->dontSeeInDatabase("users", array("name" => $I->getTestUsername($userDetails["name"]))); + $I->dontSeeInDatabase("users", array("name" => $userDetails["name"])); } } }