Skip to content
Snippets Groups Projects
Commit ce37d8bb authored by Christian Foidl's avatar Christian Foidl
Browse files

Issue #3417954: Update drush commands to create() method

parent 7433fbf7
No related branches found
No related tags found
1 merge request!2Issue #3417954: Update drush commands to create() method
Pipeline #84700 passed
......@@ -30,7 +30,9 @@ lint-test:
- nix develop ".#$DEVSHELL" -c composer drupal-check
- echo "Run PHPUnit"
- nix develop ".#$DEVSHELL" -c composer unit
- |
nix develop ".#$DEVSHELL" -c composer webserver &
nix develop ".#$DEVSHELL" -c composer unit
parallel:
matrix:
- DEVSHELL: [php81_drupal10, php82_drupal10]
......
......@@ -8,6 +8,7 @@
"drupal/verification": "^1.0@RC"
},
"require-dev": {
"drush/drush": "^11",
"mglaman/drupal-check": "^1.4"
},
"scripts": {
......
services:
Drupal\magic_code\Commands\MagicCodeCommands:
arguments:
- '@magic_code.manager'
- '@entity_type.manager'
tags:
- { name: drush.command }
......@@ -51,7 +51,7 @@
export NONINTERACTIVE="1"
export COMPOSER_NO_INTERACTION="1"
export WEB_PORT="9000"
export SIMPLETEST_BASE_URL="http://localhost"
export SIMPLETEST_BASE_URL="http://localhost:$WEB_PORT"
export SIMPLETEST_DB="sqlite://localhost/sites/default/files/.sqlite"
export PATH="$PATH:$(pwd)/vendor/bin"
......
......@@ -2,12 +2,14 @@
declare(strict_types=1);
namespace Drupal\magic_code\Commands;
namespace Drupal\magic_code\Drush\Commands;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\magic_code\MagicCodeManagerInterface;
use Drupal\user\Entity\User;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Drush commands for the magic code module.
......@@ -27,14 +29,24 @@ class MagicCodeCommands extends DrushCommands {
protected EntityTypeManagerInterface $entityTypeManager,
) {}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container): self {
return new static(
$container->get('magic_code.manager'),
$container->get('entity_type.manager'),
);
}
/**
* Generate a magic code.
*
* @command magic-code:generate
* @aliases mcg
* @option uid
* The user ID to generate the magic code for.
*/
#[CLI\Command(name: 'magic-code:generate', aliases: ['mcg'])]
#[CLI\Argument(name: 'operation', description: 'Operation for which the magic code should be generated.')]
#[CLI\Option(name: 'uid', description: 'UID of the user to create magic code for.')]
#[CLI\Option(name: 'client-id', description: 'Explicitly set which client ID to use. Otherwise default client is used.')]
#[CLI\Option(name: 'email', description: 'E-Mail to set on magic code.')]
public function generateMagicCode(string $operation, array $options = [
'uid' => NULL,
'client-id' => NULL,
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\magic_code\Functional;
use Drupal\consumers\Entity\Consumer;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\magic_code\MagicCodeManagerInterface;
use Drupal\Tests\BrowserTestBase;
use Drush\TestTraits\DrushTestTrait;
/**
* Test MagicCode Drush Commands.
*/
class MagicCodeCommandsTest extends BrowserTestBase {
use DrushTestTrait;
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected static $modules = [
'consumers',
'image',
'file',
'magic_code',
];
/**
* The client.
*/
protected Consumer $client;
/**
* The manager service.
*/
protected MagicCodeManagerInterface $manager;
/**
* The magic code entity storage.
*/
protected EntityStorageInterface $magicCodeStorage;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Delete default consumer.
Consumer::load('1')->delete();
$this->client = Consumer::create([
'client_id' => 'test',
'label' => 'test',
'is_default' => TRUE,
]);
$this->client->save();
$this->manager = $this->container->get('magic_code.manager');
$this->magicCodeStorage = $this->container->get('entity_type.manager')->getStorage('magic_code');
}
/**
* Test magic code generate command.
*/
public function testGenerateMagicCode() {
$operation = 'demo-operation';
$user = $this->drupalCreateUser(
values: [
'mail' => 'user@example.com',
]
);
//
// Error path.
//
// Missing operation.
$this->drush(
command: 'magic-code:generate',
expected_return: 1,
);
$this->assertStringContainsString('missing: "operation"', $this->getErrorOutput());
// Missing user id.
$this->drush(
command: 'magic-code:generate',
args: [$operation],
);
$this->assertStringContainsString('No user ID specified', $this->getOutput());
// Wrong user id.
$this->drush(
command: 'magic-code:generate',
args: [$operation],
options: [
'uid' => '99',
],
);
$this->assertStringContainsString('No user found', $this->getOutput());
// Wrong client-id.
$this->drush(
command: 'magic-code:generate',
args: [$operation],
options: [
'uid' => $user->id(),
'client-id' => '99',
],
);
$this->assertStringContainsString('No client found', $this->getOutput());
//
// Success path.
//
// With default consumer.
$this->drush(
command: 'magic-code:generate',
args: [$operation],
options: [
'uid' => $user->id(),
],
);
$output = $this->getOutput();
$code = strlen($output) > 7 ? substr($this->getOutput(), 5, 7) : $output;
$result = $this->magicCodeStorage->loadByProperties(['value' => $code]);
/** @var \Drupal\magic_code\Entity\MagicCodeInterface $codeEntity */
$codeEntity = reset($result);
$this->assertNotNull($codeEntity);
$this->assertEquals($this->client->id(), $codeEntity->get('client')->entity->id());
// With dedicated consumer.
$consumer = Consumer::create([
'client_id' => 'new',
'label' => 'new',
]);
$consumer->save();
$this->drush(
command: 'magic-code:generate',
args: [$operation],
options: [
'uid' => $user->id(),
'client-id' => 'new',
],
);
$output = $this->getOutput();
$code = strlen($output) > 7 ? substr($this->getOutput(), 5, 7) : $output;
$result = $this->magicCodeStorage->loadByProperties(['value' => $code]);
/** @var \Drupal\magic_code\Entity\MagicCodeInterface $codeEntity */
$codeEntity = reset($result);
$this->assertNotNull($codeEntity);
$this->assertEquals($consumer->id(), $codeEntity->get('client')->entity->id());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment