Skip to content

Commit 4eb7d2d

Browse files
committed
Added tests
1 parent ab3eb82 commit 4eb7d2d

File tree

10 files changed

+126
-31
lines changed

10 files changed

+126
-31
lines changed

composer.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@
1212
"require": {
1313
"php": ">=8.1",
1414
"drupal/autologout": "^1 || ^2",
15-
"drupal/core": "^10 || ^11",
15+
"drupal/core": "^10.3 || ^11",
1616
"drupal/r4032login": "^2",
1717
"drupal/samlauth": "^3"
1818
},
19+
"require-dev": {
20+
"drupal/field_validation": "^3.0"
21+
},
1922
"prefer-stable": true,
2023
"config": {
2124
"sort-packages": true
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Drupal\\stanford_samlauth\\": "src"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Drupal\\Tests\\stanford_samlauth\\": "tests/src"
34+
}
2235
}
2336
}

src/Drush/Commands/StanfordSamlAuthCommands.php renamed to src/Drush/Commands/StanfordSamlAuthDrushCommands.php

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
use Drupal\stanford_samlauth\Form\SamlAuthCreateUserForm;
1212
use Drupal\user\RoleInterface;
1313
use Drush\Boot\DrupalBootLevels;
14+
use Drush\Commands\AutowireTrait;
1415
use Drush\Commands\DrushCommands;
1516
use Drush\Attributes as CLI;
16-
use Symfony\Component\DependencyInjection\ContainerInterface;
1717

1818
/**
1919
* Samlauth drush comamnds.
2020
*/
2121
#[CLI\Bootstrap(DrupalBootLevels::FULL)]
22-
class StanfordSamlAuthCommands extends DrushCommands {
22+
class StanfordSamlAuthDrushCommands extends DrushCommands {
23+
24+
use AutowireTrait;
2325

2426
/**
2527
* Config object of SAML settings.
@@ -29,29 +31,12 @@ class StanfordSamlAuthCommands extends DrushCommands {
2931
protected $samlConfig;
3032

3133
/**
32-
* A config object with stanford_ssp settings.
34+
* A config object with stanford_samlauth settings.
3335
*
3436
* @var \Drupal\Core\Config\ImmutableConfig
3537
*/
3638
protected $stanfordConfig;
3739

38-
/**
39-
* Instantiates a new instance of the implementing class using autowiring.
40-
*
41-
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
42-
* The service container this instance should use.
43-
*
44-
* @return static
45-
*/
46-
public static function create(ContainerInterface $container) {
47-
return new static(
48-
$container->get('externalauth.authmap'),
49-
$container->get('form_builder'),
50-
$container->get('config.factory'),
51-
$container->get('entity_type.manager')
52-
);
53-
}
54-
5540
/**
5641
* Drush command constructor.
5742
*

src/Form/SamlAuthCreateUserForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function validateSunetId(array &$element, FormStateInterface $form
106106
}
107107

108108
// Use the workgroup api to check for valid sunet.
109-
/** @var \Drupal\stanford_ssp\Service\StanfordSSPWorkgroupApiInterface $workgroup_api */
109+
/** @var \Drupal\stanford_samlauth\Service\WorkgroupApiInterface $workgroup_api */
110110
$workgroup_api = \Drupal::service('stanford_samlauth.workgroup_api');
111111
if ($workgroup_api->connectionSuccessful() && !$workgroup_api->isSunetValid($value)) {
112112
$form_state->setError($element, t('Invalid SunetID'));
@@ -186,7 +186,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
186186
$this->authmap->save($new_user, 'samlauth', $form_state->getValue('sunetid'));
187187
$this->messenger()
188188
->addStatus($this->t('Successfully created SSO account for %user', ['%user' => $new_user->getAccountName()]));
189-
$this->logger('stanford_ssp')
189+
$this->logger('stanford_samlauth')
190190
->info('Created User %name', ['%name' => $new_user->getAccountName()]);
191191

192192
// Was the "notify" checkbox checked?

src/Plugin/FieldValidationRule/ValidSunetIDFieldValidationRule.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public function defaultConfiguration() {
3737
return ['message' => 'This is not a valid SunetID.'] + parent::defaultConfiguration();
3838
}
3939

40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getConfiguration() {
44+
return $this->configuration + parent::getConfiguration();
45+
}
46+
4047
/**
4148
* {@inheritdoc}
4249
*/

src/Service/WorkgroupApiInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Interface StanfordSSPWorkgroupApiInterface.
77
*
8-
* @package Drupal\stanford_ssp\Service
8+
* @package Drupal\stanford_samlauth\Service
99
*/
1010
interface WorkgroupApiInterface {
1111

stanford_samlauth.install

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* @file
5-
* Migrate data from stanford_ssp module.
5+
* Migrate data from stanford_samlauth module.
66
*/
77

88
use Drupal\user\Entity\Role;

tests/src/Kernel/Drush/Commands/StanfordSspCommandsTest.php renamed to tests/src/Kernel/Drush/Commands/StanfordSamlAuthDrushCommandsTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Drupal\Tests\stanford_samlauth\Kernel\Drush\Commands;
44

5-
use Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthCommands;
5+
use Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthDrushCommands;
66
use Drupal\stanford_samlauth\Service\WorkgroupApiInterface;
77
use Drupal\Tests\stanford_samlauth\Kernel\StanfordSamlAuthTestBase;
88
use Symfony\Component\Console\Output\OutputInterface;
@@ -11,14 +11,14 @@
1111
* Class StanfordSspCommandsTest
1212
*
1313
* @package Drupal\Tests\stanford_samlauth\Kernel\Commands
14-
* @coversDefaultClass \Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthCommands
14+
* @coversDefaultClass \Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthDrushCommands
1515
*/
16-
class StanfordSspCommandsTest extends StanfordSamlAuthTestBase {
16+
class StanfordSamlAuthDrushCommandsTest extends StanfordSamlAuthTestBase {
1717

1818
/**
1919
* Drush command service.
2020
*
21-
* @var \Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthCommands
21+
* @var \Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthDrushCommands
2222
*/
2323
protected $commandObject;
2424

@@ -40,7 +40,7 @@ public function setup(): void {
4040
$config_factory = \Drupal::configFactory();
4141
$entity_type_manager = \Drupal::entityTypeManager();
4242

43-
$this->commandObject = new StanfordSamlAuthCommands($authmap, $form_builder, $config_factory, $entity_type_manager);
43+
$this->commandObject = new StanfordSamlAuthDrushCommands($authmap, $form_builder, $config_factory, $entity_type_manager);
4444
$this->commandObject->setLogger(\Drupal::logger('stanford_samlauth'));
4545
$this->commandObject->setOutput($this->createMock(OutputInterface::class));
4646

tests/src/Kernel/StanfordSamlAuthTestBase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Drupal\Tests\stanford_samlauth\Kernel;
44

55
use Drupal\KernelTests\KernelTestBase;
6-
use Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthCommands;
6+
use Drupal\stanford_samlauth\Drush\Commands\StanfordSamlAuthDrushCommands;
77
use Drupal\user\Entity\Role;
88
use Symfony\Component\Console\Output\OutputInterface;
99

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Drupal\Tests\stanford_samlauth\Unit\Plugin\Validation\Constraint;
4+
5+
use Drupal\Core\DependencyInjection\ContainerBuilder;
6+
use Drupal\Core\Form\FormState;
7+
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
8+
use Drupal\Core\Logger\LoggerChannelInterface;
9+
use Drupal\Core\Utility\Token;
10+
use Drupal\stanford_samlauth\Plugin\FieldValidationRule\ValidSunetIDFieldValidationRule;
11+
use Drupal\Tests\UnitTestCase;
12+
13+
/**
14+
* @coversDefaultClass \Drupal\stanford_samlauth\Plugin\FieldValidationRule\ValidSunetIDFieldValidationRule
15+
*/
16+
class ValidSunetIDFieldValidationRuleTest extends UnitTestCase {
17+
18+
protected $validationRule;
19+
20+
protected function setUp(): void {
21+
parent::setUp();
22+
23+
$logger_channel = $this->createMock(LoggerChannelInterface::class);
24+
25+
$logger = $this->createMock(LoggerChannelFactoryInterface::class);
26+
$logger->method('get')->willReturn($logger_channel);
27+
28+
$container = new ContainerBuilder();
29+
$container->set('logger.factory', $logger);
30+
$container->set('token', $this->createMock(Token::class));
31+
$container->set('string_translation', $this->getStringTranslationStub());
32+
\Drupal::setContainer($container);
33+
34+
$this->validationRule = ValidSunetIDFieldValidationRule::create($container, [], 'foo', []);
35+
}
36+
37+
public function testFieldValidation() {
38+
$this->assertEquals('ValidSunetID', $this->validationRule->getConstraintName());
39+
$this->assertTrue($this->validationRule->isPropertyConstraint());
40+
41+
$form = [];
42+
$form_state = new FormState();
43+
$this->assertArrayHasKey('message', $this->validationRule->buildConfigurationForm($form, $form_state));
44+
45+
$form_state->setValue('message', 'foobarbaz');
46+
$this->validationRule->submitConfigurationForm($form, $form_state);
47+
48+
$this->assertEquals('foobarbaz', $this->validationRule->getConfiguration()['message']);
49+
}
50+
51+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Drupal\Tests\stanford_samlauth\Unit\Plugin\Validation\Constraint;
4+
5+
use Drupal\Core\DependencyInjection\ContainerBuilder;
6+
use Drupal\stanford_samlauth\Plugin\Validation\Constraint\ValidSunetIDConstraint;
7+
use Drupal\stanford_samlauth\Plugin\Validation\Constraint\ValidSunetIDConstraintValidator;
8+
use Drupal\stanford_samlauth\Service\WorkgroupApiInterface;
9+
use Drupal\Tests\UnitTestCase;
10+
use Symfony\Component\Validator\Context\ExecutionContextInterface;
11+
12+
/**
13+
* @coversDefaultClass \Drupal\stanford_samlauth\Plugin\Validation\Constraint\ValidSunetIDConstraintValidator
14+
*/
15+
class ValidSunetIDConstraintValidatorTest extends UnitTestCase {
16+
17+
protected $plugin;
18+
19+
protected function setUp(): void {
20+
parent::setUp();
21+
22+
$workgroup_api = $this->createMock(WorkgroupApiInterface::class);
23+
$workgroup_api->method('connectionSuccessful')->willReturn(TRUE);
24+
$workgroup_api->method('isSunetValid')->willReturn(FALSE);
25+
26+
$container = new ContainerBuilder();
27+
$container->set('stanford_samlauth.workgroup_api', $workgroup_api);
28+
$this->plugin = ValidSunetIDConstraintValidator::create($container);
29+
30+
$context = $this->createMock(ExecutionContextInterface::class);
31+
$this->plugin->initialize($context);
32+
}
33+
34+
public function testValidation() {
35+
$constraint = new ValidSunetIDConstraint();
36+
$this->assertNull($this->plugin->validate('foobar', $constraint));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)